Merge branch '8784-dir-listings'
[arvados.git] / services / api / test / unit / workflow_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'test_helper'
6
7 class WorkflowTest < ActiveSupport::TestCase
8   test "create workflow with no definition yaml" do
9     set_user_from_auth :active
10
11     wf = {
12       name: "test name",
13     }
14
15     w = Workflow.create!(wf)
16     assert_not_nil w.uuid
17   end
18
19   test "create workflow with valid definition yaml" do
20     set_user_from_auth :active
21
22     wf = {
23       name: "test name",
24       definition: "k1:\n v1: x\n v2: y"
25     }
26
27     w = Workflow.create!(wf)
28     assert_not_nil w.uuid
29   end
30
31   test "create workflow with simple string as definition" do
32     set_user_from_auth :active
33
34     wf = {
35       name: "test name",
36       definition: "this is valid yaml"
37     }
38
39     w = Workflow.create!(wf)
40     assert_not_nil w.uuid
41   end
42
43   test "create workflow with invalid definition yaml" do
44     set_user_from_auth :active
45
46     wf = {
47       name: "test name",
48       definition: "k1:\n v1: x\n  v2: y"
49     }
50
51     assert_raises(ActiveRecord::RecordInvalid) do
52       Workflow.create! wf
53     end
54   end
55
56   test "update workflow with invalid definition yaml" do
57     set_user_from_auth :active
58
59     w = Workflow.find_by_uuid(workflows(:workflow_with_definition_yml).uuid)
60     definition = "k1:\n v1: x\n  v2: y"
61
62     assert_raises(ActiveRecord::RecordInvalid) do
63       w.update_attributes!(definition: definition)
64     end
65   end
66
67   test "update workflow and verify name and description" do
68     set_user_from_auth :active
69
70     # Workflow name and desc should be set with values from definition yaml
71     # when it does not already have custom values for these fields
72     w = Workflow.find_by_uuid(workflows(:workflow_with_no_name_and_desc).uuid)
73     definition = "name: test name 1\ndescription: test desc 1\nother: some more"
74     w.update_attributes!(definition: definition)
75     w.reload
76     assert_equal "test name 1", w.name
77     assert_equal "test desc 1", w.description
78
79     # Workflow name and desc should be set with values from definition yaml
80     # when it does not already have custom values for these fields
81     definition = "name: test name 2\ndescription: test desc 2\nother: some more"
82     w.update_attributes!(definition: definition)
83     w.reload
84     assert_equal "test name 2", w.name
85     assert_equal "test desc 2", w.description
86
87     # Workflow name and desc should be set with values from definition yaml
88     # even if it means emptying them out
89     definition = "more: etc"
90     w.update_attributes!(definition: definition)
91     w.reload
92     assert_nil w.name
93     assert_nil w.description
94
95     # Workflow name and desc set using definition yaml should be cleared
96     # if definition yaml is cleared
97     definition = "name: test name 2\ndescription: test desc 2\nother: some more"
98     w.update_attributes!(definition: definition)
99     w.reload
100     definition = nil
101     w.update_attributes!(definition: definition)
102     w.reload
103     assert_nil w.name
104     assert_nil w.description
105
106     # Workflow name and desc should be set to provided custom values
107     definition = "name: test name 3\ndescription: test desc 3\nother: some more"
108     w.update_attributes!(name: "remains", description: "remains", definition: definition)
109     w.reload
110     assert_equal "remains", w.name
111     assert_equal "remains", w.description
112
113     # Workflow name and desc should retain provided custom values
114     # and should not be overwritten by values from yaml
115     definition = "name: test name 4\ndescription: test desc 4\nother: some more"
116     w.update_attributes!(definition: definition)
117     w.reload
118     assert_equal "remains", w.name
119     assert_equal "remains", w.description
120
121     # Workflow name and desc should retain provided custom values
122     # and not be affected by the clearing of the definition yaml
123     definition = nil
124     w.update_attributes!(definition: definition)
125     w.reload
126     assert_equal "remains", w.name
127     assert_equal "remains", w.description
128   end
129 end