1
|
|
|
"""Unit tests for pydecider.step |
2
|
|
|
""" |
3
|
|
|
|
4
|
|
|
import os |
5
|
|
|
import sys |
6
|
|
|
import unittest |
7
|
|
|
|
8
|
|
|
sys.path.append( |
9
|
|
|
os.path.realpath( |
10
|
|
|
os.path.join( |
11
|
|
|
os.path.dirname(__file__), |
12
|
|
|
'..' |
13
|
|
|
) |
14
|
|
|
) |
15
|
|
|
) |
16
|
|
|
|
17
|
|
|
import mock |
18
|
|
|
|
19
|
|
|
from pydecider.step import Step, StepDefinitionError |
20
|
|
|
from pydecider.state import StepStateStatus |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
class StepTest(unittest.TestCase): |
|
|
|
|
24
|
|
|
def setUp(self): |
25
|
|
|
self.activities = { |
26
|
|
|
'test1': mock.Mock(), |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
def test_step_create_bad_data(self): |
|
|
|
|
30
|
|
|
step_data = { |
31
|
|
|
"name": "bad", |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
self.assertRaises( |
35
|
|
|
ValueError, |
36
|
|
|
Step.from_data, |
37
|
|
|
step_data, |
38
|
|
|
self.activities |
39
|
|
|
) |
40
|
|
|
|
41
|
|
|
def test_step_create_requires(self): |
|
|
|
|
42
|
|
|
step_data = { |
43
|
|
|
"name": "test", |
44
|
|
|
"activity": "test1", # Note: Not checking activity here |
45
|
|
|
"requires": ["foo", ("bar", "succeeded"), ("baz", "failed")], |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
step = Step.from_data(step_data, self.activities) |
49
|
|
|
|
50
|
|
|
self.assertEquals( |
51
|
|
|
step.requires, |
52
|
|
|
{ |
53
|
|
|
'foo': StepStateStatus.completed, |
54
|
|
|
'bar': StepStateStatus.succeeded, |
55
|
|
|
'baz': StepStateStatus.failed, |
56
|
|
|
} |
57
|
|
|
) |
58
|
|
|
|
59
|
|
|
def test_activity_step_create(self): |
|
|
|
|
60
|
|
|
step_data = { |
61
|
|
|
"name": "test", |
62
|
|
|
"activity": "test1", |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
step = Step.from_data(step_data, self.activities) |
66
|
|
|
|
67
|
|
|
self.assertEquals(step.name, 'test') |
68
|
|
|
self.assertTrue(step.activity is self.activities['test1']) |
69
|
|
|
self.assertTrue(step.input_template is None) |
70
|
|
|
|
71
|
|
|
def test_activity_step_create_no_activity(self): |
|
|
|
|
72
|
|
|
step_data = { |
73
|
|
|
"name": "test", |
74
|
|
|
"activity": "test2", |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
self.assertRaises( |
78
|
|
|
KeyError, |
79
|
|
|
Step.from_data, |
80
|
|
|
step_data, |
81
|
|
|
self.activities |
82
|
|
|
) |
83
|
|
|
|
84
|
|
|
def test_activity_step_create_bad_status(self): |
|
|
|
|
85
|
|
|
step_data = { |
86
|
|
|
"name": "test", |
87
|
|
|
"activity": "test1", |
88
|
|
|
"requires": [("foo", "blah")], |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
self.assertRaises( |
92
|
|
|
StepDefinitionError, |
93
|
|
|
Step.from_data, |
94
|
|
|
step_data, |
95
|
|
|
self.activities |
96
|
|
|
) |
97
|
|
|
|
98
|
|
|
def test_activity_step_input_template(self): |
|
|
|
|
99
|
|
|
step_data = { |
100
|
|
|
"name": "test", |
101
|
|
|
"activity": "test1", |
102
|
|
|
"requires": ["foo"], |
103
|
|
|
"input": ('{' |
104
|
|
|
'"a": {{foo}},' |
105
|
|
|
'"b": {{__input__}},' |
106
|
|
|
'"c": {{__input__.who}}' |
107
|
|
|
'}') |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
step = Step.from_data(step_data, self.activities) |
111
|
|
|
|
112
|
|
|
self.assertEquals( |
113
|
|
|
step.prepare({ |
114
|
|
|
"foo": "hello", |
115
|
|
|
"__input__": { |
116
|
|
|
"who": "world" |
117
|
|
|
} |
118
|
|
|
}), |
119
|
|
|
{ |
120
|
|
|
'a': 'hello', |
121
|
|
|
'b': { |
122
|
|
|
'who': 'world' |
123
|
|
|
}, |
124
|
|
|
'c': 'world' |
125
|
|
|
} |
126
|
|
|
) |
127
|
|
|
|
128
|
|
|
if __name__ == '__main__': |
129
|
|
|
import logging |
130
|
|
|
logging.basicConfig(level=logging.DEBUG) |
131
|
|
|
unittest.main() |
132
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.