2352: Add state to pipeline_instance. Db migration and unit testing.
[arvados.git] / services / api / test / unit / pipeline_instance_test.rb
1 require 'test_helper'
2
3 class PipelineInstanceTest < ActiveSupport::TestCase
4
5   test "check active and success for a pipeline in new state" do
6     pi = pipeline_instances :new_pipeline
7
8     assert !pi.active, 'expected active to be false for a new pipeline'
9     assert !pi.success, 'expected success to be false for a new pipeline'
10     assert !pi.state, 'expected state to be nil because the fixture had no state specified'
11
12     # save the pipeline and expect state to be New
13     pi.save
14     pi = PipelineInstance.find_by_uuid 'zzzzz-xxxxx-f4gneyn6br1xize'
15     assert_equal PipelineInstance::Ready, pi.state, 'expected state to be New for new pipeline'
16     assert !pi.active, 'expected active to be false for a new pipeline'
17     assert !pi.success, 'expected success to be false for a new pipeline'
18   end
19
20   test "update attributes for pipeline" do
21     pi = pipeline_instances :new_pipeline
22
23     # add a component with no input and expect state to be New
24     component = {'script_parameters' => {"input_not_provided" => {"required" => "true"}}}
25     pi.components['first'] = component
26     components = pi.components
27     pi.update_attribute 'components', pi.components
28     pi = PipelineInstance.find_by_uuid 'zzzzz-xxxxx-f4gneyn6br1xize'
29     assert_equal PipelineInstance::New, pi.state, 'expected state to be New after adding component with input'
30     assert_equal pi.components.size, 1, 'expected one component'
31     assert !pi.active, 'expected active to be false after update'
32     assert !pi.success, 'expected success to be false for a new pipeline'
33
34     # add a component with input and expect state to become Ready
35     component = {'script_parameters' => {"input" => "yyyad4b39ca5a924e481008009d94e32+210"}}
36     pi.components['first'] = component
37     components = pi.components
38     pi.update_attribute 'components', pi.components
39     pi = PipelineInstance.find_by_uuid 'zzzzz-xxxxx-f4gneyn6br1xize'
40     assert_equal PipelineInstance::Ready, pi.state, 'expected state to be Ready after adding component with input'
41     assert_equal pi.components.size, 1, 'expected one component'
42     assert !pi.active, 'expected active to be false after update'
43     assert !pi.success, 'expected success to be false for a new pipeline'
44    
45     pi.active = true
46     pi.save
47     pi = PipelineInstance.find_by_uuid 'zzzzz-xxxxx-f4gneyn6br1xize'
48     assert_equal PipelineInstance::RunningOnServer, pi.state, 'expected state to be RunningOnServer after updating active to true'
49     assert pi.active, 'expected active to be true after update'
50     assert !pi.success, 'expected success to be false for a new pipeline'
51
52     pi.success = false
53     pi.save
54     pi = PipelineInstance.find_by_uuid 'zzzzz-xxxxx-f4gneyn6br1xize'
55     assert_equal PipelineInstance::Failed, pi.state, 'expected state to be Failed after updating success to false'
56     assert !pi.active, 'expected active to be false after update'
57     assert !pi.success, 'expected success to be false for a new pipeline'
58
59     pi.success = true
60     pi.save
61     pi = PipelineInstance.find_by_uuid 'zzzzz-xxxxx-f4gneyn6br1xize'
62     assert_equal PipelineInstance::Complete, pi.state, 'expected state to be Complete after updating success to true'
63     assert !pi.active, 'expected active to be false after update'
64     assert pi.success, 'expected success to be true after update'
65
66     pi.update_attribute 'active', true
67     pi = PipelineInstance.find_by_uuid 'zzzzz-xxxxx-f4gneyn6br1xize'
68     assert_equal PipelineInstance::RunningOnServer, pi.state, 'expected state to be RunningOnServer after updating active to true'
69     assert pi.active, 'expected active to be true after update'
70
71     pi.update_attribute 'success', false
72     pi = PipelineInstance.find_by_uuid 'zzzzz-xxxxx-f4gneyn6br1xize'
73     assert_equal PipelineInstance::Failed, pi.state, 'expected state to be Failed after updating success to false'
74     assert !pi.active, 'expected active to be false after update'
75     assert !pi.success, 'expected success to be false for a new pipeline'
76
77     pi.update_attribute 'success', true
78     pi = PipelineInstance.find_by_uuid 'zzzzz-xxxxx-f4gneyn6br1xize'
79     assert_equal PipelineInstance::Complete, pi.state, 'expected state to be Complete after updating success to true'
80     assert !pi.active, 'expected active to be false after update'
81     assert pi.success, 'expected success to be true after update'
82   end
83
84   test "update attributes for pipeline with two components" do
85     pi = pipeline_instances :new_pipeline
86
87     # add two components, one with input and one with no input and expect state to be New
88     component1 = {'script_parameters' => {"something" => "xxxad4b39ca5a924e481008009d94e32+210", "input" => "c1bad4b39ca5a924e481008009d94e32+210"}}
89     component2 = {'script_parameters' => {"something_else" => "xxxad4b39ca5a924e481008009d94e32+210", "input_missing" => {"required" => "true"}}}
90     pi.components['first'] = component1
91     pi.components['second'] = component2
92     components = pi.components
93     pi.update_attribute 'components', pi.components
94     pi = PipelineInstance.find_by_uuid 'zzzzz-xxxxx-f4gneyn6br1xize'
95     assert_equal PipelineInstance::New, pi.state, 'expected state to be New after adding component with input'
96     assert_equal pi.components.size, 2, 'expected two components'
97     assert !pi.active, 'expected active to be false after update'
98     assert !pi.success, 'expected success to be false for a new pipeline'
99   end
100
101 end