spec/gush/workflow_spec.rb in gush-3.0.0 vs spec/gush/workflow_spec.rb in gush-4.0.0

- old
+ new

@@ -13,23 +13,75 @@ end expect_any_instance_of(klass).to receive(:configure).with("arg1", "arg2") klass.new("arg1", "arg2") end - end - describe "#status" do - context "when failed" do - it "returns :failed" do - flow = TestWorkflow.create - flow.find_job("Prepare").fail! - flow.persist! - expect(flow.reload.status).to eq(:failed) + it "passes constructor keyword arguments to the method" do + klass = Class.new(Gush::Workflow) do + def configure(*args, **kwargs) + run FetchFirstJob + run PersistFirstJob, after: FetchFirstJob + end end + + expect_any_instance_of(klass).to receive(:configure).with("arg1", "arg2", arg3: 123) + klass.new("arg1", "arg2", arg3: 123) end + + it "accepts globals" do + flow = TestWorkflow.new(globals: { global1: 'foo' }) + expect(flow.globals[:global1]).to eq('foo') + end + + it "accepts internal_state" do + flow = TestWorkflow.new + + internal_state = { + id: flow.id, + jobs: flow.jobs, + dependencies: flow.dependencies, + persisted: true, + stopped: true + } + + flow_copy = TestWorkflow.new(internal_state: internal_state) + + expect(flow_copy.id).to eq(flow.id) + expect(flow_copy.jobs).to eq(flow.jobs) + expect(flow_copy.dependencies).to eq(flow.dependencies) + expect(flow_copy.persisted).to eq(true) + expect(flow_copy.stopped).to eq(true) + end + + it "does not call #configure if needs_setup is false" do + INTERNAL_SETUP_SPY = double('configure spy') + klass = Class.new(Gush::Workflow) do + def configure(*args) + INTERNAL_SETUP_SPY.some_method + end + end + + expect(INTERNAL_SETUP_SPY).not_to receive(:some_method) + + flow = TestWorkflow.new(internal_state: { needs_setup: false }) + end end + describe "#find" do + it "fiends a workflow by id" do + expect(Gush::Workflow.find(subject.id).id).to eq(subject.id) + end + end + + describe "#page" do + it "returns a page of registered workflows" do + flow = TestWorkflow.create + expect(Gush::Workflow.page.map(&:id)).to eq([flow.id]) + end + end + describe "#save" do context "workflow not persisted" do it "sets persisted to true" do flow = TestWorkflow.new flow.save @@ -79,31 +131,73 @@ subject.stopped = true expect{ subject.mark_as_started }.to change{subject.stopped?}.from(true).to(false) end end + describe "#status" do + context "when failed" do + it "returns :failed" do + flow = TestWorkflow.create + flow.find_job("Prepare").fail! + flow.persist! + expect(flow.reload.status).to eq(:failed) + end + end + + it "returns failed" do + subject.find_job('Prepare').fail! + expect(subject.status).to eq(:failed) + end + + it "returns running" do + subject.find_job('Prepare').start! + expect(subject.status).to eq(:running) + end + + it "returns finished" do + subject.jobs.each {|n| n.finish! } + expect(subject.status).to eq(:finished) + end + + it "returns stopped" do + subject.stopped = true + expect(subject.status).to eq(:stopped) + end + + it "returns pending" do + expect(subject.status).to eq(:pending) + end + end + describe "#to_json" do it "returns correct hash" do klass = Class.new(Gush::Workflow) do def configure(*args) run FetchFirstJob run PersistFirstJob, after: FetchFirstJob end end - result = JSON.parse(klass.create("arg1", "arg2").to_json) + result = JSON.parse(klass.create("arg1", "arg2", arg3: 123).to_json) expected = { "id" => an_instance_of(String), "name" => klass.to_s, "klass" => klass.to_s, - "status" => "running", + "job_klasses" => ["FetchFirstJob", "PersistFirstJob"], + "status" => "pending", "total" => 2, "finished" => 0, "started_at" => nil, "finished_at" => nil, "stopped" => false, - "arguments" => ["arg1", "arg2"] + "dependencies" => [{ + "from" => "FetchFirstJob", + "to" => job_with_id("PersistFirstJob") + }], + "arguments" => ["arg1", "arg2"], + "kwargs" => {"arg3" => 123}, + "globals" => {} } expect(result).to match(expected) end end @@ -116,17 +210,24 @@ 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 }) + expect(flow.jobs.first.params).to eq({ something: 1 }) end + it "merges globals with params and passes them to the job, with job param taking precedence" do + flow = Gush::Workflow.new(globals: { something: 2, global1: 123 }) + flow.run(Gush::Job, params: { something: 1 }) + flow.save + expect(flow.jobs.first.params).to eq({ something: 1, global1: 123 }) + end + it "allows passing wait param to the job" do flow = Gush::Workflow.new flow.run(Gush::Job, wait: 5.seconds) flow.save - expect(flow.jobs.first.wait).to eq (5.seconds) + expect(flow.jobs.first.wait).to eq(5.seconds) end context "when graph is empty" do it "adds new job with the given class as a node" do flow = Gush::Workflow.new