require File.dirname(__FILE__) + '/../../spec_helper' def valid_<%=rule_name%>_rule_data '["Rule Title", "1001"]' end describe RulesEngine::Rule::<%=rule_class%> do def valid_attributes { :<%=rule_name%>_title => 'Rule Title' } end it "should be discoverable" do RulesEngine::Discovery.rule_class("RulesEngine::Rule::<%=rule_class%>").should == RulesEngine::Rule::<%=rule_class%> end describe "the expected class options" do it "should be in the 'Twitter' group" do RulesEngine::Rule::<%=rule_class%>.options[:group].should == "Twitter" end it "should have the diplay name of 'Tweet Word Writer'" do RulesEngine::Rule::<%=rule_class%>.options[:display_name].should == "Tweet Word Writer" end it "should have the help template of '/re_rules/<%=rule_name%>/help'" do RulesEngine::Rule::<%=rule_class%>.options[:help_partial].should == '/re_rules/<%=rule_name%>/help' end it "should have the new template of '/re_rules/<%=rule_name%>/new'" do RulesEngine::Rule::<%=rule_class%>.options[:new_partial].should == '/re_rules/<%=rule_name%>/new' end it "should have the edit view partial template of '/re_rules/<%=rule_name%>/edit'" do RulesEngine::Rule::<%=rule_class%>.options[:edit_partial].should == '/re_rules/<%=rule_name%>/edit' end end describe "setting the rule data" do before(:each) do @<%=rule_name%> = RulesEngine::Rule::<%=rule_class%>.new @<%=rule_name%>.data = valid_<%=rule_name%>_rule_data end describe "the data is valid" do it "should be valid" do @<%=rule_name%>.should be_valid end it "should set the title" do @<%=rule_name%>.title.should == "Rule Title" end it "should set the re_<%=rule_name%>_id" do @<%=rule_name%>.re_<%=rule_name%>_id.should == 1001 end end describe "the data is nil" do it "should set the title to nil" do @<%=rule_name%>.title.should_not be_nil @<%=rule_name%>.data = nil @<%=rule_name%>.title.should be_nil end it "should set the 're_<%=rule_name%>_id' to nil" do @<%=rule_name%>.re_<%=rule_name%>_id.should_not be_nil @<%=rule_name%>.data = nil @<%=rule_name%>.re_<%=rule_name%>_id.should be_nil end end end describe "the summary" do it "should not be blank" do <%=rule_name%> = RulesEngine::Rule::<%=rule_class%>.new <%=rule_name%>.summary.should_not be_blank end end describe "the data" do it "should be converted to a json string" do <%=rule_name%> = RulesEngine::Rule::<%=rule_class%>.new <%=rule_name%>.should_receive(:title).and_return("mock title") <%=rule_name%>.should_receive(:re_<%=rule_name%>_id).and_return(1002) <%=rule_name%>.data.should == '["mock title","1002"]' end end describe "the expected_outcomes" do it "should be outcome next" do <%=rule_name%> = RulesEngine::Rule::<%=rule_class%>.new <%=rule_name%>.expected_outcomes[0][:outcome].should == RulesEngine::Rule::Outcome::NEXT end end describe "setting the rule attributes" do before(:each) do @<%=rule_name%> = RulesEngine::Rule::<%=rule_class%>.new end it "should be valid with valid attributes" do @<%=rule_name%>.attributes = valid_attributes @<%=rule_name%>.should be_valid end describe "setting the <%=rule_name%>_title" do it "should set the title" do @<%=rule_name%>.attributes = valid_attributes @<%=rule_name%>.title.should == 'Rule Title' end it "should not be valid if the '<%=rule_name%>_title' attribute is missing" do @<%=rule_name%>.attributes = valid_attributes.except(:<%=rule_name%>_title) @<%=rule_name%>.should_not be_valid @<%=rule_name%>.errors.should include(:<%=rule_name%>_title) end it "should not be valid if the '<%=rule_name%>_title' attribute is blank" do @<%=rule_name%>.attributes = valid_attributes.merge(:<%=rule_name%>_title => "") @<%=rule_name%>.should_not be_valid @<%=rule_name%>.errors.should include(:<%=rule_name%>_title) end end end describe "before a rule is created" do it "should create a Re<%=rule_class%> record" do Re<%=rule_class%>.should_receive(:create).with(:title => "new title").and_return(mock_model(Re<%=rule_class%>, :id => 1004)) <%=rule_name%> = RulesEngine::Rule::<%=rule_class%>.new <%=rule_name%>.stub!(:title).and_return("new title") <%=rule_name%>.before_create end it "should set the re_<%=rule_name%>_id to the created record" do Re<%=rule_class%>.stub!(:create).with(:title => "new title").and_return(mock_model(Re<%=rule_class%>, :id => 1004)) <%=rule_name%> = RulesEngine::Rule::<%=rule_class%>.new <%=rule_name%>.stub!(:title).and_return("new title") <%=rule_name%>.before_create <%=rule_name%>.re_<%=rule_name%>_id.should == 1004 end end describe "before a rule is updated" do describe "Re<%=rule_class%> record does not exist" do before(:each) do Re<%=rule_class%>.stub!(:find_by_id).and_return(nil) end it "should create a Re<%=rule_class%> record" do Re<%=rule_class%>.should_receive(:create).with(:title => "update title").and_return(mock_model(Re<%=rule_class%>, :id => 1004)) <%=rule_name%> = RulesEngine::Rule::<%=rule_class%>.new <%=rule_name%>.stub!(:title).and_return("update title") <%=rule_name%>.before_update end it "should set the re_<%=rule_name%>_id to the created record" do Re<%=rule_class%>.stub!(:create).with(:title => "update title").and_return(mock_model(Re<%=rule_class%>, :id => 1004)) <%=rule_name%> = RulesEngine::Rule::<%=rule_class%>.new <%=rule_name%>.stub!(:title).and_return("update title") <%=rule_name%>.before_update <%=rule_name%>.re_<%=rule_name%>_id.should == 1004 end end describe "Re<%=rule_class%> record exists" do it "should set the update to the created record" do Re<%=rule_class%>.stub!(:find_by_id).with(1008).and_return(re_<%=rule_name%> = mock_model(Re<%=rule_class%>)) re_<%=rule_name%>.should_receive(:update_attributes).with(:title => "update title") <%=rule_name%> = RulesEngine::Rule::<%=rule_class%>.new <%=rule_name%>.stub!(:title).and_return("update title") <%=rule_name%>.stub!(:re_<%=rule_name%>_id).and_return(1008) <%=rule_name%>.before_update end end end describe "before a rule is destroyed" do it "should destroy the found record" do Re<%=rule_class%>.should_receive(:find_by_id).with(1004).and_return(re_<%=rule_name%> = mock_model(Re<%=rule_class%>)) re_<%=rule_name%>.should_receive(:destroy) <%=rule_name%> = RulesEngine::Rule::<%=rule_class%>.new <%=rule_name%>.stub!(:re_<%=rule_name%>_id).and_return(1004) <%=rule_name%>.before_destroy end it "should ignore records that do not exist" do Re<%=rule_class%>.stub!(:find_by_id).and_return(nil) <%=rule_name%> = RulesEngine::Rule::<%=rule_class%>.new <%=rule_name%>.before_destroy end end describe "processing the rule" do it "should return outcome 'NEXT'" do <%=rule_name%> = RulesEngine::Rule::<%=rule_class%>.new <%=rule_name%>.process(1001, {:plan => "plan"}, {:tweet_words => []}).outcome.should == RulesEngine::Rule::Outcome::NEXT end describe "updateing the database" do it "should find_or_create tweet_word" do Re<%=rule_class%>Words.should_receive(:find_or_create).with(1006, "one") Re<%=rule_class%>Words.should_receive(:find_or_create).with(1006, "two") <%=rule_name%> = RulesEngine::Rule::<%=rule_class%>.new <%=rule_name%>.stub!(:re_<%=rule_name%>_id).and_return(1006) <%=rule_name%>.process(1001, {:plan => "plan"}, {:tweet_words => ['one', 'two']}) end it "should update_time_codes for each tweet_word" do Re<%=rule_class%>Count.should_receive(:update_time_codes).with(1001) Re<%=rule_class%>Count.should_receive(:update_time_codes).with(2002) Re<%=rule_class%>Words.stub!(:find_or_create).and_return(1001, 2002) <%=rule_name%> = RulesEngine::Rule::<%=rule_class%>.new <%=rule_name%>.process(1001, {:plan => "plan"}, {:tweet_words => ['one', 'two']}) end it "should write to the database" do now = Time.parse('1 Jan 2000 12:00 pm') Time.stub!(:now).and_return(now) <%=rule_name%> = RulesEngine::Rule::<%=rule_class%>.new <%=rule_name%>.stub!(:re_<%=rule_name%>_id).and_return(1006) <%=rule_name%>.process(1001, {:plan => "plan"}, {:tweet_words => ['one', 'two']}) <%=rule_name%>.process(1002, {:plan => "plan"}, {:tweet_words => ['one', 'three']}) word_writer = Re<%=rule_class%>Words.find(:all, :order => 'word') word_writer.map(&:word).should == ['one', 'three', 'two'] time_codes = [now.strftime("%Y%m%d%H"), now.strftime("%Y%m%d"), now.strftime("%Y%m"), now.strftime("%Y")] word_count = Re<%=rule_class%>Count.find(:all, :order => "time_code DESC", :conditions => ["word_id = ?", word_writer[0].id]) word_count.map(&:time_code).should == time_codes word_count.map(&:word_count).should == [2, 2, 2, 2] word_count = Re<%=rule_class%>Count.find(:all, :order => "time_code DESC", :conditions => ["word_id = ?", word_writer[1].id]) word_count.map(&:time_code).should == time_codes word_count.map(&:word_count).should == [1, 1, 1, 1] word_count = Re<%=rule_class%>Count.find(:all, :order => "time_code DESC", :conditions => ["word_id = ?", word_writer[2].id]) word_count.map(&:time_code).should == time_codes word_count.map(&:word_count).should == [1, 1, 1, 1] # add two hours now = now + 2.hours Time.stub!(:now).and_return(now) <%=rule_name%>.process(1002, {:plan => "plan"}, {:tweet_words => ['one', 'three']}) word_count = Re<%=rule_class%>Count.find(:all, :order => "time_code DESC", :conditions => ["word_id = ?", word_writer[0].id]) word_count.map(&:time_code).should == [now.strftime("%Y%m%d%H")] + time_codes word_count.map(&:word_count).should == [1, 2, 3, 3, 3] end end end end describe ReWorkflowRulesController, :type => :controller do integrate_views describe "RulesEngine::Rule::<%=rule_class%>" do before(:each) do controller.instance_eval { flash.stub!(:sweep) } RulesEngine::Discovery.discover! controller.stub!(:rules_engine_reader_access_required).and_return(true) controller.stub!(:rules_engine_editor_access_required).and_return(true) @re_workflow = ReWorkflow.make ReWorkflow.stub!(:find).and_return(@re_workflow) end describe "help" do it "should assign the <%=rule_name%> rule class" do get :help, :rule_class_name => "RulesEngine::Rule::<%=rule_class%>" assigns[:rule_class].should == RulesEngine::Rule::<%=rule_class%> end end describe "new" do it "show the new form" do get :new, :rule_class_name => "RulesEngine::Rule::<%=rule_class%>" response.should have_tag("form#re_rule_new_form") do with_tag("input#<%=rule_name%>_title") end end end describe "edit" do it "show the edit form" do re_rule = ReRule.make(:re_workflow_id => @re_workflow.id, :rule_class_name => "RulesEngine::Rule::<%=rule_class%>", :data => valid_<%=rule_name%>_rule_data) ReRule.stub!(:find).and_return(re_rule) get :edit, :re_workflow_id => @re_workflow.id, :re_rule_id => 1001, :rule_class_name => "RulesEngine::Rule::<%=rule_class%>" response.should have_tag("form#re_rule_edit_form") do with_tag("input#<%=rule_name%>_title", :value => 'Rule Title') end end end end end