From 60d7d9edb2fe286fd6c1cf47b6df04cdadcc8a7c Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Fri, 10 May 2019 22:46:17 +0200 Subject: Statistics - manage queries --- archaeological_operations/tests.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'archaeological_operations/tests.py') diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py index 10afc61f2..42ceea292 100644 --- a/archaeological_operations/tests.py +++ b/archaeological_operations/tests.py @@ -2006,6 +2006,35 @@ class OperationSearchTest(TestCase, OperationInitTest): self.assertEqual(json.loads(response.content.decode())['recordsTotal'], 1) + def test_statistics(self): + c = Client() + c.login(username=self.username, password=self.password) + q = {"stats_modality_1": "year", + "stats_modality_2": "operation_type__label"} + response = c.get(reverse('get-operation', args=['json-stats']), q) + self.assertEqual(response.status_code, 200) + + expected_result = [] + for ope in models.Operation.objects.all(): + years = [y for y, res in expected_result] + if ope.year in years: + year_idx = years.index(ope.year) + else: + expected_result.append([ope.year, []]) + year_idx = len(expected_result) - 1 + current_values = expected_result[year_idx][1] + values = [v for v, cnt in current_values] + val = ope.operation_type.label + if val in values: + val_idx = values.index(val) + else: + current_values.append([val, 0]) + val_idx = len(current_values) - 1 + current_values[val_idx][1] += 1 + + values = json.loads(response.content.decode()) + self.assertEqual(values['data'], expected_result) + class OperationPermissionTest(TestCase, OperationInitTest): fixtures = FILE_FIXTURES -- cgit v1.2.3