1 | import multiprocessing |
||
2 | import unittest |
||
3 | from os.path import abspath |
||
4 | |||
5 | from coalib.bears.Bear import Bear |
||
6 | from coalib.output.printers.LOG_LEVEL import LOG_LEVEL |
||
7 | from coalib.processes.communication.LogMessage import LogMessage |
||
8 | from coalib.settings.Section import Section |
||
9 | from coalib.settings.Setting import Setting |
||
10 | |||
11 | |||
12 | class TestBear(Bear): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
13 | |||
14 | def __init__(self, section, queue): |
||
15 | Bear.__init__(self, section, queue) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
16 | |||
17 | def run(self): |
||
18 | self.print("set", "up", delimiter="=") |
||
19 | self.err("teardown") |
||
20 | self.err() |
||
21 | |||
22 | @staticmethod |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
23 | def get_dependencies(): |
||
24 | return [BadTestBear] |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
25 | |||
26 | |||
27 | class BadTestBear(Bear): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
28 | |||
29 | def __init__(self, section, queue): |
||
30 | Bear.__init__(self, section, queue) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
31 | |||
32 | def run(self): |
||
33 | raise NotImplementedError |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
34 | |||
35 | |||
36 | class TypedTestBear(Bear): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
37 | |||
38 | def __init__(self, section, queue): |
||
39 | Bear.__init__(self, section, queue) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
40 | self.was_executed = False |
||
41 | |||
42 | def run(self, something: int): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
43 | self.was_executed = True |
||
44 | return [] |
||
45 | |||
46 | |||
47 | class BearWithPrerequisites(Bear): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
48 | prerequisites_fulfilled = True |
||
49 | |||
50 | def __init__(self, section, queue, prerequisites_fulfilled): |
||
51 | BearWithPrerequisites.prerequisites_fulfilled = prerequisites_fulfilled |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
52 | Bear.__init__(self, section, queue) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
53 | self.was_executed = False |
||
54 | |||
55 | def run(self): |
||
56 | self.was_executed = True |
||
57 | return [] |
||
58 | |||
59 | @classmethod |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
60 | def check_prerequisites(cls): |
||
61 | return cls.prerequisites_fulfilled |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
62 | |||
63 | |||
64 | class BearTest(unittest.TestCase): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
65 | |||
66 | def setUp(self): |
||
67 | self.queue = multiprocessing.Queue() |
||
68 | self.settings = Section("test_settings") |
||
69 | self.uut = TestBear(self.settings, self.queue) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
70 | |||
71 | def test_simple_api(self): |
||
72 | self.assertRaises(TypeError, TestBear, self.settings, 2) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
73 | self.assertRaises(TypeError, TestBear, None, self.queue) |
||
74 | self.assertRaises(NotImplementedError, self.uut.kind) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
75 | |||
76 | base = Bear(self.settings, None) |
||
77 | self.assertRaises(NotImplementedError, base.run) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
78 | self.assertEqual(base.get_non_optional_settings(), {}) |
||
79 | |||
80 | def test_message_queue(self): |
||
81 | self.uut.execute() |
||
82 | self.check_message(LOG_LEVEL.DEBUG, |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
83 | "Running bear {}...".format("TestBear")) |
||
84 | self.check_message(LOG_LEVEL.DEBUG, "set=up") |
||
85 | self.check_message(LOG_LEVEL.ERROR, "teardown") |
||
86 | |||
87 | def test_bad_bear(self): |
||
88 | self.uut = BadTestBear(self.settings, self.queue) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
89 | self.uut.execute() |
||
90 | self.check_message(LOG_LEVEL.DEBUG) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
91 | self.check_message(LOG_LEVEL.WARNING, |
||
92 | "Bear BadTestBear failed to run. Take a look at " |
||
93 | "debug messages for further information.") |
||
94 | # debug message contains custom content, dont test this here |
||
95 | self.queue.get() |
||
96 | |||
97 | def test_inconvertible(self): |
||
98 | self.uut = TypedTestBear(self.settings, self.queue) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
99 | self.settings.append(Setting("something", "5")) |
||
100 | self.uut.execute() |
||
101 | self.check_message(LOG_LEVEL.DEBUG) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
102 | self.assertTrue(self.uut.was_executed) |
||
103 | |||
104 | self.settings.append(Setting("something", "nonsense")) |
||
105 | self.uut.was_executed = False |
||
106 | self.uut.execute() |
||
107 | self.check_message(LOG_LEVEL.DEBUG) |
||
108 | self.check_message(LOG_LEVEL.WARNING) |
||
109 | self.assertTrue(self.queue.empty()) |
||
110 | self.assertFalse(self.uut.was_executed) |
||
111 | |||
112 | def check_message(self, log_level, message=None): |
||
113 | msg = self.queue.get() |
||
114 | self.assertIsInstance(msg, LogMessage) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
115 | if message: |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
116 | self.assertEqual(msg.message, message) |
||
117 | |||
118 | self.assertEqual(msg.log_level, log_level, msg) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
119 | |||
120 | def test_no_queue(self): |
||
121 | uut = TestBear(self.settings, None) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
122 | uut.execute() # No exceptions |
||
123 | |||
124 | def test_dependencies(self): |
||
125 | self.assertEqual(Bear.get_dependencies(), []) |
||
126 | self.assertEqual(Bear.missing_dependencies([]), []) |
||
127 | self.assertEqual(Bear.missing_dependencies([BadTestBear]), []) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
128 | |||
129 | self.assertEqual(TestBear.missing_dependencies([]), [BadTestBear]) |
||
130 | self.assertEqual(TestBear.missing_dependencies([BadTestBear]), []) |
||
131 | self.assertEqual(TestBear.missing_dependencies([TestBear]), |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
132 | [BadTestBear]) |
||
133 | self.assertEqual(TestBear.missing_dependencies([TestBear, |
||
134 | BadTestBear]), |
||
135 | []) |
||
136 | |||
137 | def test_check_prerequisites(self): |
||
138 | uut = BearWithPrerequisites(self.settings, self.queue, True) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
139 | uut.execute() |
||
140 | self.check_message(LOG_LEVEL.DEBUG) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
141 | self.assertTrue(self.queue.empty()) |
||
142 | self.assertTrue(uut.was_executed) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
143 | |||
144 | self.assertRaisesRegex(RuntimeError, |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
145 | "The bear BearWithPrerequisites does not " |
||
146 | "fulfill all requirements\\.", |
||
147 | BearWithPrerequisites, |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
148 | self.settings, |
||
149 | self.queue, |
||
150 | False) |
||
151 | |||
152 | self.check_message(LOG_LEVEL.WARNING, |
||
153 | "The bear BearWithPrerequisites does not fulfill " |
||
154 | "all requirements.") |
||
155 | self.assertTrue(self.queue.empty()) |
||
156 | |||
157 | self.assertRaisesRegex(RuntimeError, |
||
158 | "The bear BearWithPrerequisites does not " |
||
159 | "fulfill all requirements\\. Just because " |
||
160 | "I want to\\.", |
||
161 | BearWithPrerequisites, |
||
162 | self.settings, |
||
163 | self.queue, |
||
164 | "Just because I want to.") |
||
165 | |||
166 | self.check_message(LOG_LEVEL.WARNING, |
||
167 | "The bear BearWithPrerequisites does not fulfill " |
||
168 | "all requirements. Just because I want to.") |
||
169 | self.assertTrue(self.queue.empty()) |
||
170 | |||
171 | def test_get_config_dir(self): |
||
172 | section = Section("default") |
||
173 | section.append(Setting("files", "**", "/path/to/dir/config")) |
||
174 | uut = TestBear(section, None) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
175 | self.assertEqual(uut.get_config_dir(), abspath("/path/to/dir")) |
||
176 |