-
Notifications
You must be signed in to change notification settings - Fork 22
/
test_xenon.py
192 lines (162 loc) · 4.64 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
183
184
185
186
187
188
189
190
191
192
# coding=utf-8
# import mock # can't use mock because patches influences each other
import os
import sys
import unittest
import collections
# Monkey-patch paramunittest for Python 3.10+
if sys.version_info[:2] >= (3, 10):
import collections.abc
collections.Mapping = collections.abc.Mapping
import httpretty
from paramunittest import parametrized
from xenon import core, api, main
Args = collections.namedtuple(
'Args', 'absolute average modules averagenum paths_in_front'
)
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[0], '.xenon.yml')
exclude = None
ignore = None
no_assert = False
average = None
absolute = None
modules = None
averagenum = None
paths_in_front = False
@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, averagenum
# infractions
(
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
('C', 'B', 'B', None, False),
0
),
(
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
('B', 'B', 'B', None, False),
1
),
(
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
('C', 'A', 'B', None, True),
1
),
(
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
('C', 'B', 'A', None, False),
1
),
(
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
(None, 'B', 'B', None, True),
0
),
(
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
('C', None, 'B', None, False),
0
),
(
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
('C', 'B', None, None, True),
0
),
(
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
(None, None, None, 0, True),
1
),
)
class InfractionsTestCase(unittest.TestCase):
def setParameters(self, results, args, infractions):
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.infractions = infractions
def test_run(self):
infr = core.find_infractions(self.args, self.logger, self.r)
self.assertEqual(infr, self.infractions)
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()