|
| 1 | +from copy import copy |
| 2 | +from operator import attrgetter |
| 3 | +import os |
| 4 | + |
| 5 | +from .mocks import AbeMock |
| 6 | +from .utils import to_unicode |
| 7 | + |
| 8 | + |
| 9 | +class AbeTestMixin(object): |
| 10 | + """ |
| 11 | + Mixin for unittest.TestCase to check against API samples. |
| 12 | +
|
| 13 | + Example usage: |
| 14 | +
|
| 15 | + class TestCase(AbeTestMixin, unittest.TestCase) |
| 16 | + ... |
| 17 | +
|
| 18 | + """ |
| 19 | + # Root directory to load samples from. |
| 20 | + samples_root = '.' |
| 21 | + |
| 22 | + def load_sample(self, sample_path): |
| 23 | + """ |
| 24 | + Load a sample file into an AbeMock object. |
| 25 | + """ |
| 26 | + sample_file = os.path.join(self.samples_root, sample_path) |
| 27 | + return AbeMock(sample_file) |
| 28 | + |
| 29 | + def get_sample_request(self, path, label): |
| 30 | + """ |
| 31 | + Get the request body to send for a specific sample label. |
| 32 | +
|
| 33 | + """ |
| 34 | + sample = self.load_sample(path) |
| 35 | + sample_request = sample.examples[label].request |
| 36 | + return sample_request.body |
| 37 | + |
| 38 | + def assert_data_equal(self, data1, data2): |
| 39 | + """ |
| 40 | + Two elements are recursively equal without taking order into account |
| 41 | + """ |
| 42 | + try: |
| 43 | + if isinstance(data1, list): |
| 44 | + self.assertIsInstance(data2, list) |
| 45 | + self.assert_data_list_equal(data1, data2) |
| 46 | + elif isinstance(data1, dict): |
| 47 | + self.assertIsInstance(data2, dict) |
| 48 | + self.assert_data_dict_equal(data1, data2) |
| 49 | + else: |
| 50 | + data1 = to_unicode(data1) |
| 51 | + data2 = to_unicode(data2) |
| 52 | + self.assertIsInstance(data2, data1.__class__) |
| 53 | + self.assertEqual(data1, data2) |
| 54 | + except AssertionError as exc: |
| 55 | + message = str(exc) + '\n{}\n{}\n\n'.format(data1, data2) |
| 56 | + raise type(exc)(message) |
| 57 | + |
| 58 | + def assert_data_dict_equal(self, data1, data2): |
| 59 | + """ |
| 60 | + Two dicts are recursively equal without taking order into account |
| 61 | + """ |
| 62 | + self.assertEqual( |
| 63 | + len(data1), len(data2), |
| 64 | + msg='Number of elements mismatch: {} != {}\n'.format( |
| 65 | + data1.keys(), data2.keys()) |
| 66 | + ) |
| 67 | + for key in data1: |
| 68 | + self.assertIn(key, data2) |
| 69 | + self.assert_data_equal(data1[key], data2[key]) |
| 70 | + |
| 71 | + def assert_data_list_equal(self, data1, data2): |
| 72 | + """ |
| 73 | + Two lists are recursively equal without taking order into account |
| 74 | + """ |
| 75 | + self.assertEqual( |
| 76 | + len(data1), len(data2), |
| 77 | + msg='Number of elements mismatch: {} {}'.format( |
| 78 | + data1, data2) |
| 79 | + ) |
| 80 | + |
| 81 | + data1_elements = copy(data1) |
| 82 | + for element2 in data2: |
| 83 | + fails, exceptions, found = [], [], False |
| 84 | + while data1_elements and not found: |
| 85 | + element = data1_elements.pop() |
| 86 | + try: |
| 87 | + self.assert_data_equal(element, element2) |
| 88 | + found = True |
| 89 | + except AssertionError as exc: |
| 90 | + exceptions.append(exc) |
| 91 | + fails.append(element) |
| 92 | + if not data1_elements: |
| 93 | + message = '\n*\n'.join( |
| 94 | + map(attrgetter('message'), exceptions) |
| 95 | + ) |
| 96 | + raise type(exceptions[0])(message) |
| 97 | + data1_elements.extend(fails) |
| 98 | + |
| 99 | + def assert_matches_sample(self, path, label, url, response): |
| 100 | + """ |
| 101 | + Check a URL and response against a sample. |
| 102 | +
|
| 103 | + :param sample_name: |
| 104 | + The name of the sample file, e.g. 'query.json' |
| 105 | + :param label: |
| 106 | + The label for a specific sample request/response, e.g. 'OK' |
| 107 | + :param url: |
| 108 | + The actual URL we want to compare with the sample |
| 109 | + :param response: |
| 110 | + The actual API response we want to match with the sample |
| 111 | + """ |
| 112 | + sample = self.load_sample(path) |
| 113 | + sample_request = sample.examples[label].request |
| 114 | + sample_response = sample.examples[label].response |
| 115 | + |
| 116 | + self.assertEqual(url, sample_request.url) |
| 117 | + self.assertEqual(response.status_code, sample_response.status) |
| 118 | + self.assert_data_equal(response.data, sample_response.body) |
0 commit comments