spec/lib/gush/workflow_spec.rb in gush-0.1.1 vs spec/lib/gush/workflow_spec.rb in gush-0.1.2

- old
+ new

@@ -1,37 +1,37 @@ require 'spec_helper' describe Gush::Workflow do - subject { TestWorkflow.new("test-workflow") } + subject { TestWorkflow.create } describe "#initialize" do - context "when configure option is true" do - it "runs #configure method " do - expect_any_instance_of(TestWorkflow).to receive(:configure) - TestWorkflow.new(true) - end - end + end - context "when configure option is false" do - it "it doesn't run #configure method " do - expect_any_instance_of(TestWorkflow).to_not receive(:configure) - TestWorkflow.new(false) + describe "#save" do + it "passes constructor arguments to the method" do + klass = Class.new(Gush::Workflow) do + def configure(*args) + run FetchFirstJob + run PersistFirstJob, after: FetchFirstJob + end end + + flow = klass.new("arg1", "arg2") + + expect(flow).to receive(:configure).with("arg1", "arg2") + flow.save end - end - describe "#save" do context "workflow not persisted" do it "sets persisted to true" do flow = TestWorkflow.new flow.save expect(flow.persisted).to be(true) end it "assigns new unique id" do flow = TestWorkflow.new - expect(flow.id).to eq(nil) flow.save expect(flow.id).to_not be_nil end end @@ -59,89 +59,130 @@ end end describe "#to_json" do it "returns correct hash" do - klass = Class.new(Gush::Workflow) do - def configure + def configure(*args) run FetchFirstJob run PersistFirstJob, after: FetchFirstJob end end - result = JSON.parse(klass.new("workflow").to_json) + result = JSON.parse(klass.create("arg1", "arg2").to_json) expected = { - "id"=>nil, + "id" => an_instance_of(String), "name" => klass.to_s, "klass" => klass.to_s, "status" => "pending", "total" => 2, "finished" => 0, "started_at" => nil, "finished_at" => nil, "stopped" => false, + "arguments" => ["arg1", "arg2"], "jobs" => [ { "name"=>"FetchFirstJob", "klass"=>"FetchFirstJob", - "finished"=>false, - "enqueued"=>false, - "failed"=>false, "incoming"=>[], "outgoing"=>["PersistFirstJob"], "finished_at"=>nil, "started_at"=>nil, "enqueued_at"=>nil, "failed_at"=>nil, - "running" => false + "params" => {}, + "output_payload" => nil }, { "name"=>"PersistFirstJob", "klass"=>"PersistFirstJob", - "finished"=>false, - "enqueued"=>false, - "failed"=>false, "incoming"=>["FetchFirstJob"], "outgoing"=>[], "finished_at"=>nil, "started_at"=>nil, "enqueued_at"=>nil, "failed_at"=>nil, - "running" => false + "params" => {}, + "output_payload" => nil } ] } - expect(result).to eq(expected) + expect(result).to match(expected) end end describe "#find_job" do it "finds job by its name" do - expect(TestWorkflow.new("test").find_job("PersistFirstJob")).to be_instance_of(PersistFirstJob) + expect(TestWorkflow.create.find_job("PersistFirstJob")).to be_instance_of(PersistFirstJob) end end describe "#run" do + it "allows passing additional params to the job" do + flow = Gush::Workflow.new + flow.run(Gush::Job, params: { something: 1 }) + flow.save + expect(flow.jobs.first.params).to eq ({ something: 1 }) + end + context "when graph is empty" do it "adds new job with the given class as a node" do - flow = Gush::Workflow.new("workflow") + flow = Gush::Workflow.new flow.run(Gush::Job) + flow.save expect(flow.jobs.first).to be_instance_of(Gush::Job) end end - context "when last node is a job" do - it "attaches job as a child of the last inserted job" do - tree = Gush::Workflow.new("workflow") - klass1 = Class.new(Gush::Job) - klass2 = Class.new(Gush::Job) - tree.run(klass1) - tree.run(klass2, after: klass1) - tree.create_dependencies - expect(tree.jobs.first).to be_an_instance_of(klass1) - expect(tree.jobs.first.outgoing.first).to eq(klass2.to_s) - end + it "allows `after` to accept an array of jobs" do + tree = Gush::Workflow.new + klass1 = Class.new(Gush::Job) + klass2 = Class.new(Gush::Job) + klass3 = Class.new(Gush::Job) + tree.run(klass1) + tree.run(klass2, after: [klass1, klass3]) + tree.run(klass3) + + tree.resolve_dependencies + + expect(tree.jobs.first.outgoing).to match_array([klass2.to_s]) + end + + it "allows `before` to accept an array of jobs" do + tree = Gush::Workflow.new + klass1 = Class.new(Gush::Job) + klass2 = Class.new(Gush::Job) + klass3 = Class.new(Gush::Job) + tree.run(klass1) + tree.run(klass2, before: [klass1, klass3]) + tree.run(klass3) + + tree.resolve_dependencies + + expect(tree.jobs.first.incoming).to match_array([klass2.to_s]) + end + + it "attaches job as a child of the job in `after` key" do + tree = Gush::Workflow.new + klass1 = Class.new(Gush::Job) + klass2 = Class.new(Gush::Job) + tree.run(klass1) + tree.run(klass2, after: klass1) + tree.resolve_dependencies + job = tree.jobs.first + expect(job.outgoing).to match_array([klass2.to_s]) + end + + it "attaches job as a parent of the job in `before` key" do + tree = Gush::Workflow.new + klass1 = Class.new(Gush::Job) + klass2 = Class.new(Gush::Job) + tree.run(klass1) + tree.run(klass2, before: klass1) + tree.resolve_dependencies + job = tree.jobs.first + expect(job.incoming).to match_array([klass2.to_s]) end end describe "#failed?" do context "when one of the jobs failed" do