-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_validate.py
More file actions
133 lines (110 loc) · 3.74 KB
/
test_validate.py
File metadata and controls
133 lines (110 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""
Tests the functionality of validate.py with several different recipes which should raise different errors
"""
from __future__ import annotations
import sys
from unittest import mock
import pytest
import ruamel.yaml
import workflows
from workflows.recipe.validate import main, validate_recipe
def test_validate_returns_type_error_when_called_without_parameters():
with pytest.raises(TypeError):
validate_recipe()
def test_no_errors_when_validating_healthy_recipe(tmpdir):
healthy_recipe = """
{
"1": {
"queue": "simpleservice.submission",
"parameters": {
"commands": [
"echo This is a command32"
],
"workingdir": "/dls/tmp/riw56156/zocalo",
"output_file": "out.txt"
}
},
"start": [
[1, []]
]
}
"""
recipe_file = tmpdir.join("recipe.json")
recipe_file.write(healthy_recipe)
validate_recipe(recipe_file.strpath)
def test_value_error_when_validating_bad_json(tmpdir):
bad_json = """
{
"1": {
"queue": "simpleservice.submission"
"parameters": {
"commands": [
"echo This is a command32"
],
"workingdir": "/dls/tmp/riw56156/zocalo",
"output_file": "out.txt"
}
},
"start": [
[1, []]
]
}
"""
recipe_file = tmpdir.join("recipe.json")
recipe_file.write(bad_json)
# Run validate with mock open, expect parsing error
with pytest.raises(ruamel.yaml.parser.ParserError):
validate_recipe(recipe_file.strpath)
def test_workflows_error_when_validating_incorrect_workflows_recipe(tmpdir):
bad_recipe = """
{
"1": {
"queue": "simpleservice.submission",
"parameters": {
"commands": [
"echo This is a command32"
],
"workingdir": "/dls/tmp/riw56156/zocalo",
"output_file": "out.txt"
}
}
}
"""
recipe_file = tmpdir.join("recipe.json")
recipe_file.write(bad_recipe)
# Run validate with mock open, expect JSON error
with pytest.raises(workflows.Error):
validate_recipe(recipe_file.strpath)
# Create a mock of the validate call
@mock.patch("workflows.recipe.validate.validate_recipe")
def test_command_line_validation_one_argument(mock_requests):
# Create fake arguments to test on
test_args = ["validate.py", "file1"]
with mock.patch.object(sys, "argv", test_args):
main()
mock_requests.assert_called_once_with("file1")
def test_exit_when_command_line_validation_given_no_arguments():
# Create fake arguments to test on
test_args = ["validate.py"]
with pytest.raises(SystemExit):
with mock.patch.object(sys, "argv", test_args):
main()
# Create a mock of the validate call
@mock.patch("workflows.recipe.validate.validate_recipe")
def test_command_line_validation_multiple_arguments(mock_requests):
# Create fake arguments to test on
test_args = ["validate.py", "file1", "file2", "file3"]
with mock.patch.object(sys, "argv", test_args):
main()
mock_requests.assert_has_calls(
[mock.call("file1"), mock.call("file2"), mock.call("file3")]
)
assert mock_requests.call_count == 3
@mock.patch("workflows.recipe.validate.validate_recipe")
def test_system_exit_when_error_raised_from_command_line_validation(mock_requests):
mock_requests.get.side_effect = Exception
# Create fake arguments to test on
test_args = ["validate.py"]
with pytest.raises(SystemExit):
with mock.patch.object(sys, "argv", test_args):
main()