|
8 | 8 |
|
9 | 9 | import random
|
10 | 10 | import json
|
| 11 | +import inspect |
| 12 | +from dateutil.parser import parse as parse_date |
| 13 | + |
| 14 | + |
| 15 | +try: |
| 16 | + strtypes = (str, unicode) |
| 17 | + inttypes = (int, long) |
| 18 | +except NameError: |
| 19 | + strtypes = str |
| 20 | + inttypes = int |
11 | 21 |
|
12 | 22 |
|
13 | 23 | @pytest.fixture
|
@@ -57,8 +67,7 @@ def rand_hash(n):
|
57 | 67 | 'source_file_version': 'file_versions'},
|
58 | 68 | 'instances': {'file_version': 'file_versions'},
|
59 | 69 | 'vectors': {'instance': 'instances',
|
60 |
| - 'file_version': 'file_versions', |
61 |
| - 'file': 'files'}} |
| 70 | + 'file_version': 'file_versions'}} |
62 | 71 |
|
63 | 72 |
|
64 | 73 | def resolve_reqs(model_name, user):
|
@@ -103,31 +112,60 @@ def setup_model(model_name, user):
|
103 | 112 | return model_dict
|
104 | 113 |
|
105 | 114 |
|
| 115 | +def simplify_object(obj): |
| 116 | + try: |
| 117 | + obj = parse_date(obj) |
| 118 | + except (AttributeError, ValueError, TypeError): |
| 119 | + pass |
| 120 | + try: |
| 121 | + obj = obj.replace(microsecond=0, tzinfo=None).isoformat() |
| 122 | + except (AttributeError, TypeError): |
| 123 | + pass |
| 124 | + return obj |
| 125 | + |
| 126 | + |
106 | 127 | def assert_eq(a, b):
|
| 128 | + a, b = simplify_object(a), simplify_object(b) |
| 129 | + print(a, b) |
107 | 130 | if isinstance(a, list) and isinstance(b, list):
|
108 | 131 | assert len(a) == len(b)
|
109 | 132 | for a_item, b_item in zip(a, b):
|
110 | 133 | assert_eq(a_item, b_item)
|
111 |
| - if isinstance(a, models.Model) and isinstance(b, dict): |
| 134 | + elif isinstance(a, dict) and isinstance(b, dict): |
112 | 135 | for k in b:
|
113 |
| - d_value = b.__getitem__(k) |
114 |
| - o_value = a.__getattribute__(k) |
115 |
| - d_type = type(d_value) |
116 |
| - o_type = type(o_value) |
117 |
| - if d_type == o_type: |
118 |
| - assert d_value == o_value |
119 |
| - else: |
120 |
| - print("Skipped matching {k}: {d_value}({d_type}) ?? " |
121 |
| - "{o_value}({o_type})".format(k=k, d_value=d_value, |
122 |
| - d_type=d_type, o_value=o_value, |
123 |
| - o_type=o_type)) |
| 136 | + assert_eq(a[k], b[k]) |
| 137 | + elif isinstance(b, dict) and (isinstance(a, models.Model) or |
| 138 | + inspect.isclass(a)): |
| 139 | + assert_eq(b, a) |
| 140 | + elif isinstance(a, dict) and (isinstance(b, models.Model) or |
| 141 | + inspect.isclass(b)): |
| 142 | + for k in a: |
| 143 | + # TODO: serializer-added values cannot be validated, so we'll have to |
| 144 | + # ignore any attribute that does not exist in Model object |
| 145 | + if not hasattr(b, k): |
| 146 | + print("Ignoring missing model parameter: {} in {}".format(k, b)) |
| 147 | + continue |
| 148 | + a_value = a.__getitem__(k) |
| 149 | + b_value = getattr(b, k) |
| 150 | + assert_eq(a_value, b_value) |
| 151 | + elif isinstance(a, inttypes) and isinstance(b, models.Model): |
| 152 | + assert_eq(a, b.id) |
| 153 | + elif isinstance(a, strtypes) and isinstance(b, models.Model): |
| 154 | + assert_eq(a, b.username) |
| 155 | + elif b.__class__.__name__ == 'RelatedManager': |
| 156 | + assert_eq(a, list(b.all())) |
| 157 | + else: |
| 158 | + assert a == b |
124 | 159 |
|
125 | 160 |
|
126 | 161 | def assert_response(response, status, data=None):
|
127 | 162 | print(response.content)
|
128 | 163 | assert response.status_code == status
|
129 | 164 | if isinstance(data, (list, dict)):
|
130 |
| - assert_eq(response.json(), data) |
| 165 | + if 'results' in response.data: |
| 166 | + assert_eq(response.data['results'], data) |
| 167 | + else: |
| 168 | + assert_eq(response.data, data) |
131 | 169 | elif data:
|
132 | 170 | assert_eq(response.content, data)
|
133 | 171 |
|
@@ -202,13 +240,15 @@ def test_file_fileversion(admin_client, admin_user):
|
202 | 240 | assert_response(response, status.HTTP_200_OK, obj)
|
203 | 241 |
|
204 | 242 |
|
| 243 | +@pytest.mark.parametrize('limit', [None, 10]) |
205 | 244 | @pytest.mark.parametrize('resource', ['locals', 'remotes', 'matches'])
|
206 |
| -def test_task_resource_empty(resource, admin_client, admin_user): |
| 245 | +def test_task_resource_empty(resource, limit, admin_client, admin_user): |
207 | 246 | task = create_model('tasks', admin_user)
|
208 | 247 | task.save()
|
209 | 248 |
|
| 249 | + data = {'limit': limit} if limit else {} |
210 | 250 | response = admin_client.get('/collab/tasks/{}/{}/'.format(task.id, resource),
|
211 |
| - content_type="application/json") |
| 251 | + data=data, content_type="application/json") |
212 | 252 | assert_response(response, status.HTTP_200_OK, [])
|
213 | 253 |
|
214 | 254 |
|
|
0 commit comments