|
7 | 7 | Unit tests for seed/views/labels.py |
8 | 8 | """ |
9 | 9 |
|
10 | | -import json |
11 | 10 | from collections import defaultdict |
12 | 11 | from datetime import datetime |
13 | 12 |
|
@@ -149,35 +148,55 @@ def test_labels_inventory_specific_filter_endpoint_provides_IDs_for_records_wher |
149 | 148 |
|
150 | 149 | # Ensures that at least a single label exists to ensure that we are not |
151 | 150 | # relying on auto-creation of labels for this test to pass. |
152 | | - new_label = Label.objects.create( |
| 151 | + new_label_1 = Label.objects.create( |
153 | 152 | color="red", |
154 | 153 | name="test_label-a", |
155 | 154 | super_organization=organization_a, |
156 | 155 | ) |
| 156 | + new_label_2 = Label.objects.create( |
| 157 | + color="blue", |
| 158 | + name="test_label-b", |
| 159 | + super_organization=organization_a, |
| 160 | + ) |
157 | 161 |
|
158 | 162 | # Create 2 properties and 2 tax lots. Then, apply that label to one of each |
159 | 163 | property_view_factory = FakePropertyViewFactory(organization=organization_a, user=user) |
160 | 164 | p_view_1 = property_view_factory.get_property_view() |
161 | | - p_view_1.labels.add(new_label) |
| 165 | + p_view_1.labels.add(new_label_1) |
| 166 | + p_view_2 = property_view_factory.get_property_view() |
| 167 | + p_view_2.labels.add(new_label_1) |
| 168 | + p_view_2.labels.add(new_label_2) |
| 169 | + |
| 170 | + # create more random properties |
| 171 | + property_view_factory.get_property_view() |
162 | 172 | property_view_factory.get_property_view() |
163 | 173 |
|
164 | 174 | taxlot_view_factory = FakeTaxLotViewFactory(organization=organization_a, user=user) |
165 | 175 | tl_view_1 = taxlot_view_factory.get_taxlot_view() |
166 | | - tl_view_1.labels.add(new_label) |
| 176 | + tl_view_1.labels.add(new_label_1) |
| 177 | + # more random tax lotx |
167 | 178 | taxlot_view_factory.get_taxlot_view() |
168 | 179 |
|
169 | 180 | client = APIClient() |
170 | 181 | client.login(username=user.username, password='secret') |
171 | 182 |
|
172 | 183 | url = reverse('api:v3:properties-labels') |
173 | | - response_a = client.post(url + '?organization_id={}'.format(organization_a.pk)) |
174 | | - data = json.loads(response_a.content) |
175 | | - |
| 184 | + response_a = client.post(url + f'?organization_id={organization_a.pk}') |
| 185 | + data = response_a.json() |
176 | 186 | for label in data: |
177 | | - if label.get('name') != 'test_label-a': |
178 | | - self.assertCountEqual(label.get('is_applied'), []) |
| 187 | + if label.get('name') == 'test_label-a': |
| 188 | + self.assertListEqual(label.get('is_applied'), [p_view_1.id, p_view_2.id]) |
| 189 | + elif label.get('name') == 'test_label-b': |
| 190 | + self.assertCountEqual(label.get('is_applied'), [p_view_2.id]) |
179 | 191 | else: |
180 | | - self.assertCountEqual(label.get('is_applied'), [p_view_1.id]) |
| 192 | + self.assertCountEqual(label.get('is_applied'), []) |
| 193 | + |
| 194 | + # check if we can filter to only label_2 and on p_view 2 |
| 195 | + response_b = client.post(url + f'?organization_id={organization_a.pk}', data={'selected': [p_view_2.id], 'label_names': [new_label_1.name]}) |
| 196 | + data = response_b.json() |
| 197 | + self.assertEqual(len(data), 1) |
| 198 | + self.assertEqual(data[0].get('name'), new_label_1.name) |
| 199 | + self.assertListEqual(data[0].get('is_applied'), [p_view_2.id]) |
181 | 200 |
|
182 | 201 |
|
183 | 202 | class TestUpdateInventoryLabelsAPIView(DeleteModelsTestCase): |
|
0 commit comments