forked from rubik/xenon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_xenon.py
182 lines (153 loc) · 4.27 KB
/
test_xenon.py
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# coding=utf-8
# import mock # can't use mock because patches influences each other
import os
import unittest
import collections
import httpretty
from paramunittest import parametrized
from xenon import core, api, main
Args = collections.namedtuple('Args', 'absolute average modules')
class CatchAll(object):
def __getattr__(self, attr):
return lambda *a, **kw: True
class Arguments(object):
path = 'xenon'
url = 'http://api.barium.cc/jobs'
repo_token = 'abcdef1234569abdcef'
service_job_id = '4699301'
service_name = 'travis-ci',
config = os.path.join(path, '.xenon.yml')
exclude = None
ignore = None
no_assert = False
average = None
absolute = None
modules = None
@parametrized(
(20, 10, 2),
(0, 10, 0),
(0, 0, 0),
)
class AvTestCase(unittest.TestCase):
def setParameters(self, m, n, r):
self.m = m
self.n = n
self.r = r
def testAv(self):
self.assertEqual(core.av(self.m, self.n), self.r)
@parametrized(
('A', 'A', False),
('B', 'A', True),
('C', None, False),
('A', None, False),
('A', 'a', False),
('B', 'a', True),
)
class CheckTestCase(unittest.TestCase):
def setParameters(self, a, b, r):
self.a = a
self.b = b
self.r = r
def testCheck(self):
self.assertEqual(core.check(self.a, self.b), self.r)
@parametrized(
# results
# absolute, average, modules
# exit code
(
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
('C', 'B', 'B'),
0
),
(
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
('B', 'B', 'B'),
1
),
(
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
('C', 'A', 'B'),
1
),
(
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
('C', 'B', 'A'),
1
),
(
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
(None, 'B', 'B'),
0
),
(
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
('C', None, 'B'),
0
),
(
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
('C', 'B', None),
0
),
(
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
(None, None, None),
0
),
)
class RunTestCase(unittest.TestCase):
def setParameters(self, results, args, exit_code):
r = {}
for k, v in results.items():
r[k] = [dict(name='block', complexity=cc, lineno=1) for cc in v]
self.r = r
self.args = Args(*args)
self.logger = CatchAll()
self.exit_code = exit_code
def test_run(self):
code = core.find_infractions(self.args, self.logger, self.r)
self.assertEqual(code != 0, self.exit_code)
class APITestCase(unittest.TestCase):
def _exit_code(self):
try:
main(Arguments)
except SystemExit as e:
return e.code
@httpretty.activate
def test_main_ok(self):
httpretty.register_uri(
httpretty.POST,
'http://api.barium.cc/jobs',
body='{"message":"Job #5.1","url":"http://barium.cc/jobs/5722"}'
)
self.assertEqual(self._exit_code(), 0)
@httpretty.activate
def test_main_not_ok(self):
httpretty.register_uri(
httpretty.POST,
'http://api.barium.cc/jobs',
body='{"message":"Build processing error.","error":true,"url":""}',
status=500,
)
self.assertEqual(self._exit_code(), 3)
@httpretty.activate
def test_api(self):
httpretty.register_uri(
httpretty.POST,
'http://api.barium.cc/jobs',
body='{"message":"Resource creation started",'
'"url":"http://barium.cc/jobs/5722"}'
)
response = api.post(
url=Arguments.url,
repo_token=Arguments.repo_token,
service_job_id=Arguments.service_job_id,
service_name=Arguments.service_name,
git={},
cc_data={}
)
self.assertEqual(response.json(),
{'url': 'http://barium.cc/jobs/5722',
'message': 'Resource creation started'})
if __name__ == '__main__':
unittest.main()