spec/seam/worker_spec.rb in seam-mongodb-0.0.4 vs spec/seam/worker_spec.rb in seam-mongodb-0.0.5

- old
+ new

@@ -2,10 +2,11 @@ describe "worker" do before do Seam::Persistence.destroy + @stamp_data_history = true end after do Timecop.return end @@ -17,30 +18,86 @@ worker.step.must_equal "darren" end end describe "move_to_next_step" do - it "should work" do - flow = Seam::Flow.new - flow.apple - flow.orange - effort = flow.start( { first_name: 'John' } ) - effort = Seam::Effort.find(effort.id) + [:date].to_objects {[ + ['1/1/2011'], + ['3/4/2015'] + ]}.each do |test| + + describe "move immediately" do - effort.next_step.must_equal "apple" + before { Timecop.freeze Time.parse(test.date) } + after { Timecop.return } - apple_worker = Seam::Worker.new - apple_worker.handles(:apple) - def apple_worker.process - move_to_next_step + it "should move to the next step and set the date to now" do + flow = Seam::Flow.new + flow.apple + flow.orange + + effort = flow.start( { first_name: 'John' } ) + effort = Seam::Effort.find(effort.id) + + effort.next_step.must_equal "apple" + + apple_worker = Seam::Worker.new + apple_worker.handles(:apple) + def apple_worker.process + move_to_next_step + end + + apple_worker.execute effort + + effort = Seam::Effort.find(effort.id) + effort.next_step.must_equal "orange" + effort.next_execute_at.must_equal Time.parse(test.date) + end + end - apple_worker.execute effort + end - effort = Seam::Effort.find(effort.id) - effort.next_step.must_equal "orange" + [:date, :next_date].to_objects {[ + ['1/1/2011', '2/1/2011'], + ['1/1/2011', '2/2/2011'], + ['3/4/2015', '4/5/2016'] + ]}.each do |test| + + describe "move to some point in the future" do + + before { Timecop.freeze Time.parse(test.date) } + after { Timecop.return } + + it "should move to the next step and set the date to now" do + flow = Seam::Flow.new + flow.apple + flow.orange + + effort = flow.start( { first_name: 'John' } ) + effort = Seam::Effort.find(effort.id) + + effort.next_step.must_equal "apple" + + apple_worker = Seam::Worker.new + apple_worker.handles(:apple) + eval(" + def apple_worker.process + move_to_next_step( { on: Time.parse('#{test.next_date}') } ) + end + ") + + apple_worker.execute effort + + effort = Seam::Effort.find(effort.id) + effort.next_step.must_equal "orange" + effort.next_execute_at.must_equal Time.parse(test.next_date) + end + + end + end end describe "move_to_next_step as a default" do it "should go to move_to_next_step by default" do @@ -258,10 +315,11 @@ end let(:effort_creator) do ->() do e = flow.start + flow.stamp_data_history = @stamp_data_history Seam::Effort.find(e.id) end end let(:wait_for_attempting_contact_stage_worker) do @@ -545,15 +603,14 @@ effort = Seam::Effort.find effort.id effort.next_step.must_equal "wait_for_attempting_contact_stage" effort.history.count.must_equal 1 effort.history[0].contrast_with!( { + "step_id" => effort.flow['steps'][0]['id'], "started_at"=> Time.now, "step"=>"wait_for_attempting_contact_stage", "stopped_at" => Time.now, - "data_before" => { "first_name" => "DARREN" } , - "data_after" => { "first_name" => "DARREN", "hit 1" => 1 } } ) send_postcard_if_necessary_worker.execute_all determine_if_postcard_should_be_sent_worker.execute_all wait_for_attempting_contact_stage_worker.execute_all @@ -563,11 +620,18 @@ effort = Seam::Effort.find effort.id effort.next_step.must_equal "wait_for_attempting_contact_stage" effort.history.count.must_equal 1 - effort.history[0].contrast_with!({"started_at"=> Time.now, "step"=>"wait_for_attempting_contact_stage", "stopped_at" => Time.now, "result" => "try_again_in", "try_again_on" => Time.now + 1.day } ) + effort.history[0].contrast_with!( { + "step_id" => effort.flow['steps'][0]['id'], + "started_at"=> Time.now, + "step"=>"wait_for_attempting_contact_stage", + "stopped_at" => Time.now, + "result" => "try_again_in", + "try_again_on" => Time.now + 1.day + } ) # THE NEXT DAY Timecop.freeze Time.parse('27/12/2013') send_postcard_if_necessary_worker.execute_all @@ -576,11 +640,17 @@ effort = Seam::Effort.find effort.id effort.next_step.must_equal "wait_for_attempting_contact_stage" effort.history.count.must_equal 2 - effort.history[1].contrast_with!({"started_at"=> Time.now, "step"=>"wait_for_attempting_contact_stage", "stopped_at" => Time.now, "result" => "try_again_in" } ) + effort.history[1].contrast_with!( { + "step_id" => effort.flow['steps'][0]['id'], + "started_at"=> Time.now, + "step"=>"wait_for_attempting_contact_stage", + "stopped_at" => Time.now, + "result" => "try_again_in" + } ) # THE NEXT DAY Timecop.freeze Time.parse('28/12/2013') send_postcard_if_necessary_worker.execute_all @@ -589,31 +659,49 @@ effort = Seam::Effort.find effort.id effort.next_step.must_equal "determine_if_postcard_should_be_sent" effort.history.count.must_equal 3 - effort.history[2].contrast_with!({"started_at"=> Time.now, "step"=>"wait_for_attempting_contact_stage", "stopped_at" => Time.now, "result" => "move_to_next_step" } ) + effort.history[2].contrast_with!( { + "step_id" => effort.flow['steps'][0]['id'], + "started_at"=> Time.now, + "step"=>"wait_for_attempting_contact_stage", + "stopped_at" => Time.now, + "result" => "move_to_next_step" + } ) # KEEP GOING send_postcard_if_necessary_worker.execute_all determine_if_postcard_should_be_sent_worker.execute_all wait_for_attempting_contact_stage_worker.execute_all effort = Seam::Effort.find effort.id effort.next_step.must_equal "send_postcard_if_necessary" effort.history.count.must_equal 4 - effort.history[3].contrast_with!({"started_at"=> Time.now, "step"=>"determine_if_postcard_should_be_sent", "stopped_at" => Time.now, "result" => "move_to_next_step" } ) + effort.history[3].contrast_with!( { + "step_id" => effort.flow['steps'][1]['id'], + "started_at"=> Time.now, + "step"=>"determine_if_postcard_should_be_sent", + "stopped_at" => Time.now, + "result" => "move_to_next_step" + } ) # KEEP GOING send_postcard_if_necessary_worker.execute_all determine_if_postcard_should_be_sent_worker.execute_all wait_for_attempting_contact_stage_worker.execute_all effort = Seam::Effort.find effort.id effort.next_step.must_equal nil effort.history.count.must_equal 5 - effort.history[4].contrast_with!({"started_at"=> Time.now, "step"=>"send_postcard_if_necessary", "stopped_at" => Time.now, "result" => "move_to_next_step" } ) + effort.history[4].contrast_with!( { + "step_id" => effort.flow['steps'][2]['id'], + "started_at"=> Time.now, + "step"=>"send_postcard_if_necessary", + "stopped_at" => Time.now, + "result" => "move_to_next_step" + } ) end end describe "eject" do @@ -731,9 +819,88 @@ current_step["arguments"].must_equal ["another test"] end apple_worker.execute_all orange_worker.execute_all + end + end + + describe "data history" do + describe "stamping the history" do + let(:effort) do + flow = Seam::Flow.new + flow.stamp_data_history = true + flow.apple + + e = flow.start( { first_name: 'John' } ) + Seam::Effort.find(e.id) + end + + before do + Timecop.freeze Time.parse('3/4/2013') + effort.next_step.must_equal "apple" + + apple_worker = Seam::Worker.new + apple_worker.handles(:apple) + def apple_worker.process + effort.data['something'] = 'else' + end + + apple_worker.execute effort + end + + it "should not update the next step" do + fresh_effort = Seam::Effort.find(effort.id) + fresh_effort.history.count.must_equal 1 + end + + it "should set the data_before history" do + fresh_effort = Seam::Effort.find(effort.id) + fresh_effort.history.first["data_before"].must_equal( { "first_name" => 'John' } ) + end + + it "should set the data_after history" do + fresh_effort = Seam::Effort.find(effort.id) + fresh_effort.history.first["data_after"].must_equal( { "first_name" => 'John', "something" => 'else' } ) + end + end + + describe "not stamping the history" do + let(:effort) do + flow = Seam::Flow.new + flow.stamp_data_history = false + flow.apple + + e = flow.start( { first_name: 'John' } ) + Seam::Effort.find(e.id) + end + + before do + Timecop.freeze Time.parse('3/4/2013') + + apple_worker = Seam::Worker.new + apple_worker.handles(:apple) + def apple_worker.process + effort.data['something'] = 'else' + end + + apple_worker.execute effort + end + + it "should not update the next step" do + fresh_effort = Seam::Effort.find(effort.id) + fresh_effort.history.count.must_equal 1 + end + + it "should set the data_before history" do + fresh_effort = Seam::Effort.find(effort.id) + fresh_effort.history.first["data_before"].nil?.must_equal true + end + + it "should set the data_after history" do + fresh_effort = Seam::Effort.find(effort.id) + fresh_effort.history.first["data_after"].nil?.must_equal true + end end end end class IWillNotCallHandlesWorker < Seam::Worker