Skip to content

Commit 4248d84

Browse files
michaelwoodR2ZER0
authored andcommitted
tests: Add new test test_aggregate_data
This should help maintain stability of the aggregate data in both the test data and to detect any changes/regressions in manage_entities_data output.
1 parent 6e85d3f commit 4248d84

1 file changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
from django.test import TestCase
2+
from django.forms.models import model_to_dict
3+
4+
from db.management.commands.manage_entities_data import update_entities
5+
import db.models as db
6+
7+
8+
class AggregateDataTest(TestCase):
9+
maxDiff = None
10+
fixtures = ["test_data.json"]
11+
12+
def test_aggregate_data_doesnt_change(self):
13+
"""Test that there haven't been other changes in the test data which
14+
require the aggregate data to be updated.
15+
"""
16+
current_funders = list(
17+
db.Funder.objects.values_list("org_id", "non_primary_org_ids", "aggregate")
18+
)
19+
current_recipients = list(
20+
db.Recipient.objects.values_list(
21+
"org_id", "non_primary_org_ids", "aggregate"
22+
)
23+
)
24+
25+
update_entities()
26+
27+
post_update_funders = list(
28+
db.Funder.objects.all().values_list(
29+
"org_id", "non_primary_org_ids", "aggregate"
30+
)
31+
)
32+
post_update_recipients = list(
33+
db.Recipient.objects.all().values_list(
34+
"org_id", "non_primary_org_ids", "aggregate"
35+
)
36+
)
37+
38+
self.assertEqual(
39+
current_funders,
40+
post_update_funders,
41+
"update_entities caused funder data to change in the test data, consider updating test_data?",
42+
)
43+
self.assertEqual(
44+
current_recipients,
45+
post_update_recipients,
46+
"update_entities caused recipient data to change in the test data, consider updating test data?",
47+
)
48+
49+
def test_funder_aggregate_data(self):
50+
expected = {
51+
"additional_data": {"alternative_names": []},
52+
"aggregate": {
53+
"currencies": {
54+
"GBP": {
55+
"recipient_org": {
56+
"avg": 500.94,
57+
"grants": 50,
58+
"max": 990,
59+
"min": 46,
60+
"total": 25047,
61+
}
62+
}
63+
},
64+
"grants": 50,
65+
"grants_ind": 0,
66+
"grants_org": 50,
67+
"maxAwardDate": "2019-10-03",
68+
"minAwardDate": "2019-10-03",
69+
},
70+
"name": "Funding for examples",
71+
"non_primary_org_ids": [],
72+
"org_id": "GB-example-b",
73+
"source": "GRANT",
74+
}
75+
76+
found_data = model_to_dict(db.Funder.objects.get(org_id="GB-example-b"))
77+
# We don't care if the pk doesn't match
78+
del found_data["id"]
79+
80+
self.assertEqual(found_data, expected)
81+
update_entities()
82+
self.assertEqual(found_data, expected)
83+
84+
def test_recipient_aggregate_data(self):
85+
expected = {
86+
"additional_data": {"alternative_names": []},
87+
"aggregate": {
88+
"currencies": {
89+
"GBP": {
90+
"recipient_org": {
91+
"avg": 504.0217391304348,
92+
"grants": 46,
93+
"max": 990,
94+
"min": 46,
95+
"total": 23185,
96+
}
97+
}
98+
},
99+
"grants": 46,
100+
"grants_ind": 0,
101+
"grants_org": 46,
102+
"maxAwardDate": "2019-10-03",
103+
"minAwardDate": "2019-10-03",
104+
},
105+
"name": "Receive an example grant",
106+
"non_primary_org_ids": ["360G-example-nonprimary"],
107+
"org_id": "360G-example-a",
108+
"source": "GRANT",
109+
}
110+
111+
found_data = model_to_dict(db.Recipient.objects.get(org_id="360G-example-a"))
112+
# We don't care if the pk doesn't match
113+
del found_data["id"]
114+
115+
self.assertEqual(found_data, expected)
116+
update_entities()
117+
self.assertEqual(found_data, expected)

0 commit comments

Comments
 (0)