|
2 | 2 |
|
3 | 3 | import db.models as db
|
4 | 4 |
|
| 5 | +import unittest.mock |
| 6 | + |
5 | 7 |
|
6 | 8 | class GetterRunTest(TransactionTestCase):
|
7 | 9 | fixtures = ["test_data.json"]
|
@@ -91,3 +93,137 @@ def test_convenience_fields_from_data(self):
|
91 | 93 | self.assertSetEqual(set(grant.funding_org_ids), {"GB-CHC-12345"})
|
92 | 94 |
|
93 | 95 | self.assertEqual(grant.publisher_org_id, "XI-EXAMPLE-EXAMPLE")
|
| 96 | + |
| 97 | + |
| 98 | +def mock_non_primary_org_ids_lookup_maps(): |
| 99 | + return {"GB-SECONDARY-12345": "GB-PRIMARY-12345"}, {} |
| 100 | + |
| 101 | + |
| 102 | +@unittest.mock.patch( |
| 103 | + "db.models.non_primary_org_ids_lookup_maps", mock_non_primary_org_ids_lookup_maps |
| 104 | +) |
| 105 | +class RecipientUpdateAggregateTest(TestCase): |
| 106 | + def test_single_grant(self): |
| 107 | + recipient = db.Recipient() |
| 108 | + recipient.update_aggregate( |
| 109 | + { |
| 110 | + "currency": "GBP", |
| 111 | + "amountAwarded": 100, |
| 112 | + "awardDate": "2019-10-03T00:00:00+00:00", |
| 113 | + "fundingOrganization": [{"id": "GB-CHC-12345"}], |
| 114 | + "recipientOrganization": [ |
| 115 | + {"id": "GB-COH-12345"}, |
| 116 | + ], |
| 117 | + } |
| 118 | + ) |
| 119 | + self.assertEqual(recipient.aggregate["funders"], 1) |
| 120 | + self.assertEqual(recipient.aggregate["currencies"]["GBP"]["funders"], 1) |
| 121 | + |
| 122 | + def test_two_grants_from_same_funder(self): |
| 123 | + recipient = db.Recipient() |
| 124 | + recipient.update_aggregate( |
| 125 | + { |
| 126 | + "currency": "GBP", |
| 127 | + "amountAwarded": 100, |
| 128 | + "awardDate": "2019-10-03T00:00:00+00:00", |
| 129 | + "fundingOrganization": [{"id": "GB-CHC-12345"}], |
| 130 | + "recipientOrganization": [ |
| 131 | + {"id": "GB-COH-12345"}, |
| 132 | + ], |
| 133 | + } |
| 134 | + ) |
| 135 | + recipient.update_aggregate( |
| 136 | + { |
| 137 | + "currency": "GBP", |
| 138 | + "amountAwarded": 10000, |
| 139 | + "awardDate": "2020-10-03T00:00:00+00:00", |
| 140 | + "fundingOrganization": [{"id": "GB-CHC-12345"}], |
| 141 | + "recipientOrganization": [ |
| 142 | + {"id": "GB-COH-12345"}, |
| 143 | + ], |
| 144 | + } |
| 145 | + ) |
| 146 | + self.assertEqual(recipient.aggregate["funders"], 1) |
| 147 | + self.assertEqual(recipient.aggregate["currencies"]["GBP"]["funders"], 1) |
| 148 | + |
| 149 | + def test_two_grants_from_different_funders(self): |
| 150 | + recipient = db.Recipient() |
| 151 | + recipient.update_aggregate( |
| 152 | + { |
| 153 | + "currency": "GBP", |
| 154 | + "amountAwarded": 100, |
| 155 | + "awardDate": "2019-10-03T00:00:00+00:00", |
| 156 | + "fundingOrganization": [{"id": "GB-CHC-12345"}], |
| 157 | + "recipientOrganization": [ |
| 158 | + {"id": "GB-COH-12345"}, |
| 159 | + ], |
| 160 | + } |
| 161 | + ) |
| 162 | + recipient.update_aggregate( |
| 163 | + { |
| 164 | + "currency": "GBP", |
| 165 | + "amountAwarded": 10000, |
| 166 | + "awardDate": "2020-10-03T00:00:00+00:00", |
| 167 | + "fundingOrganization": [{"id": "GB-CHC-67890"}], |
| 168 | + "recipientOrganization": [ |
| 169 | + {"id": "GB-COH-12345"}, |
| 170 | + ], |
| 171 | + } |
| 172 | + ) |
| 173 | + self.assertEqual(recipient.aggregate["funders"], 2) |
| 174 | + self.assertEqual(recipient.aggregate["currencies"]["GBP"]["funders"], 2) |
| 175 | + |
| 176 | + def test_two_grants_from_different_funders_in_different_currencies(self): |
| 177 | + recipient = db.Recipient() |
| 178 | + recipient.update_aggregate( |
| 179 | + { |
| 180 | + "currency": "GBP", |
| 181 | + "amountAwarded": 100, |
| 182 | + "awardDate": "2019-10-03T00:00:00+00:00", |
| 183 | + "fundingOrganization": [{"id": "GB-CHC-12345"}], |
| 184 | + "recipientOrganization": [ |
| 185 | + {"id": "GB-COH-12345"}, |
| 186 | + ], |
| 187 | + } |
| 188 | + ) |
| 189 | + recipient.update_aggregate( |
| 190 | + { |
| 191 | + "currency": "EUR", |
| 192 | + "amountAwarded": 10000, |
| 193 | + "awardDate": "2020-10-03T00:00:00+00:00", |
| 194 | + "fundingOrganization": [{"id": "GB-CHC-67890"}], |
| 195 | + "recipientOrganization": [ |
| 196 | + {"id": "GB-COH-12345"}, |
| 197 | + ], |
| 198 | + } |
| 199 | + ) |
| 200 | + self.assertEqual(recipient.aggregate["funders"], 2) |
| 201 | + self.assertEqual(recipient.aggregate["currencies"]["GBP"]["funders"], 1) |
| 202 | + self.assertEqual(recipient.aggregate["currencies"]["EUR"]["funders"], 1) |
| 203 | + |
| 204 | + def test_two_grants_from_same_funder_but_different_funder_ids_used(self): |
| 205 | + recipient = db.Recipient() |
| 206 | + recipient.update_aggregate( |
| 207 | + { |
| 208 | + "currency": "GBP", |
| 209 | + "amountAwarded": 100, |
| 210 | + "awardDate": "2019-10-03T00:00:00+00:00", |
| 211 | + "fundingOrganization": [{"id": "GB-PRIMARY-12345"}], |
| 212 | + "recipientOrganization": [ |
| 213 | + {"id": "GB-COH-12345"}, |
| 214 | + ], |
| 215 | + } |
| 216 | + ) |
| 217 | + recipient.update_aggregate( |
| 218 | + { |
| 219 | + "currency": "GBP", |
| 220 | + "amountAwarded": 10000, |
| 221 | + "awardDate": "2020-10-03T00:00:00+00:00", |
| 222 | + "fundingOrganization": [{"id": "GB-SECONDARY-12345"}], |
| 223 | + "recipientOrganization": [ |
| 224 | + {"id": "GB-COH-12345"}, |
| 225 | + ], |
| 226 | + } |
| 227 | + ) |
| 228 | + self.assertEqual(recipient.aggregate["funders"], 1) |
| 229 | + self.assertEqual(recipient.aggregate["currencies"]["GBP"]["funders"], 1) |
0 commit comments