9684: add workflow resource to api server
[arvados.git] / services / api / test / unit / workflow_test.rb
1 require 'test_helper'
2
3 class WorkflowTest < ActiveSupport::TestCase
4   test "create workflow with no workflow yaml" do
5     set_user_from_auth :active
6
7     wf = {
8       name: "test name",
9     }
10
11     w = Workflow.create!(wf)
12     assert_not_nil w.uuid
13   end
14
15   test "create workflow with valid workflow yaml" do
16     set_user_from_auth :active
17
18     wf = {
19       name: "test name",
20       workflow: "k1:\n v1: x\n v2: y"
21     }
22
23     w = Workflow.create!(wf)
24     assert_not_nil w.uuid
25   end
26
27   test "create workflow with invalid workflow yaml" do
28     set_user_from_auth :active
29
30     wf = {
31       name: "test name",
32       workflow: "k1:\n v1: x\n  v2: y"
33     }
34
35     assert_raises(ActiveRecord::RecordInvalid) do
36       Workflow.create! wf
37     end
38   end
39
40   test "update workflow with invalid workflow yaml" do
41     set_user_from_auth :active
42
43     w = Workflow.find_by_uuid(workflows(:workflow_with_workflow_yml).uuid)
44     wf = "k1:\n v1: x\n  v2: y"
45
46     assert_raises(ActiveRecord::RecordInvalid) do
47       w.update_attributes!(workflow: wf)
48     end
49   end
50
51   test "update workflow and verify name and description" do
52     set_user_from_auth :active
53
54     # Workflow name and desc should be set with values from workflow yaml
55     # when it does not already have custom values for these fields
56     w = Workflow.find_by_uuid(workflows(:workflow_with_no_name_and_desc).uuid)
57     wf = "name: test name 1\ndescription: test desc 1\nother: some more"
58     w.update_attributes!(workflow: wf)
59     assert_equal "test name 1", w.name
60     assert_equal "test desc 1", w.description
61
62     # Workflow name and desc should be set with values from workflow yaml
63     # when it does not already have custom values for these fields
64     wf = "name: test name 2\ndescription: test desc 2\nother: some more"
65     w.update_attributes!(workflow: wf)
66     assert_equal "test name 2", w.name
67     assert_equal "test desc 2", w.description
68
69     # Workflow name and desc should be set with values from workflow yaml
70     # even if it means emptying them out
71     wf = "more: etc"
72     w.update_attributes!(workflow: wf)
73     assert_equal nil, w.name
74     assert_equal nil, w.description
75
76     # Workflow name and desc should be set to provided custom values
77     wf = "name: test name 3\ndescription: test desc 3\nother: some more"
78     w.update_attributes!(name: "remains", description: "remains", workflow: wf)
79     assert_equal "remains", w.name
80     assert_equal "remains", w.description
81
82     # Workflow name and desc should retain provided custom values
83     # and should not be overwritten by values from yaml
84     wf = "name: test name 4\ndescription: test desc 4\nother: some more"
85     w.update_attributes!(workflow: wf)
86     assert_equal "remains", w.name
87     assert_equal "remains", w.description
88   end
89 end