| Conditions | 1 |
| Total Lines | 65 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 30 |
| CRAP Score | 1.042 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | """Test framework for running tests of PPP modules.""" |
||
| 23 | 1 | def PPPTestCase(app): |
|
| 24 | 1 | class _PPPTestCase(TestCase): |
|
| 25 | 1 | config_var = None |
|
| 26 | 1 | config = None |
|
| 27 | 1 | def setUp(self): |
|
| 28 | 1 | super(_PPPTestCase, self).setUp() |
|
| 29 | 1 | self.app = TestApp(app) |
|
| 30 | 1 | if self.config_var or self.config is not None: |
|
| 31 | assert self.config_var and self.config is not None |
||
| 32 | self.config_file = tempfile.NamedTemporaryFile('w+') |
||
| 33 | os.environ[self.config_var] = self.config_file.name |
||
| 34 | self.config_file.write(self.config) |
||
| 35 | self.config_file.seek(0) |
||
| 36 | |||
| 37 | 1 | def tearDown(self): |
|
| 38 | 1 | if self.config_var or self.config is not None: |
|
| 39 | assert self.config_var and self.config is not None |
||
| 40 | self.config_file.close() |
||
| 41 | del os.environ[self.config_var] |
||
| 42 | del self.config_file |
||
| 43 | 1 | super(_PPPTestCase, self).tearDown() |
|
| 44 | |||
| 45 | 1 | @arg_to_dict |
|
| 46 | def request(self, obj): |
||
| 47 | 1 | j = self.app.post_json('/', obj).json |
|
| 48 | """Make a request and return the answers.""" |
||
| 49 | 1 | return list(map(Response.from_dict, j)) |
|
| 50 | |||
| 51 | 1 | @arg_to_dict |
|
| 52 | 1 | def assertResponses(self, request, expected_responses, *, remove_time=True): |
|
| 53 | """Makes a request and asserts the responses are the expected one.""" |
||
| 54 | 1 | responses = self.request(request) |
|
| 55 | 1 | if remove_time: |
|
| 56 | 1 | for response in responses: |
|
| 57 | 1 | for trace_item in response.trace: |
|
| 58 | 1 | trace_item.times.clear() |
|
| 59 | 1 | self.assertEqual(responses, expected_responses) |
|
| 60 | 1 | assertResponse = assertResponses # Alias for old code |
|
| 61 | |||
| 62 | 1 | @arg_to_dict |
|
| 63 | 1 | def assertResponsesIn(self, request, expected, *, remove_time=True): |
|
| 64 | """Makes a request and asserts the response is among a set |
||
| 65 | of expected ones.""" |
||
| 66 | responses = self.request(request) |
||
| 67 | if remove_time: |
||
| 68 | for response in responses: |
||
| 69 | for trace_item in response.trace: |
||
| 70 | trace_item.times.clear() |
||
| 71 | self.assertTrue(all(x in expected for x in responses), |
||
| 72 | 'Not all of:\n%r\n are in:\n%r' % (responses, expected)) |
||
| 73 | |||
| 74 | 1 | @arg_to_dict |
|
| 75 | def assertResponsesCount(self, request, count): |
||
| 76 | """Makes a request and asserts the number of responses is |
||
| 77 | a given number.""" |
||
| 78 | self.assertEqual(len(self.request(request)), count) |
||
| 79 | |||
| 80 | 1 | @arg_to_dict |
|
| 81 | def assertStatusInt(self, obj, status): |
||
| 82 | """Makes a request and asserts the HTTP status code is |
||
| 83 | the one that is expected.""" |
||
| 84 | 1 | res = self.app.post_json('/', obj, status='*') |
|
| 85 | 1 | self.assertEqual(res.status_int, status) |
|
| 86 | |||
| 87 | 1 | return _PPPTestCase |
|
| 88 | |||
| 89 |