|
7 | 7 | from django_dynamic_fixture import G
|
8 | 8 | from entity.config import EntityRegistry
|
9 | 9 | from entity.models import Entity, EntityRelationship, EntityKind
|
10 |
| -from entity.sync import sync_entities, defer_entity_syncing, transaction_atomic_with_retry |
| 10 | +from entity.sync import sync_entities, defer_entity_syncing, transaction_atomic_with_retry, _get_super_entities_by_ctype |
11 | 11 | from entity.signal_handlers import turn_on_syncing, turn_off_syncing
|
12 | 12 | from mock import patch, MagicMock, call
|
13 | 13 |
|
@@ -250,9 +250,86 @@ def test_sync_all_accounts_teams(self):
|
250 | 250 | # There should be four entity relationships since four accounts have teams
|
251 | 251 | self.assertEquals(EntityRelationship.objects.all().count(), 4)
|
252 | 252 |
|
| 253 | + def test_sync_all_accounts_teams_new_account_during_sync(self): |
| 254 | + """ |
| 255 | + Tests the scenario of a new account being created after account ids are fetched but before the super |
| 256 | + entities are fetched |
| 257 | + """ |
| 258 | + # Create five test accounts |
| 259 | + accounts = [Account.objects.create() for i in range(5)] |
| 260 | + # Create two teams to assign to some of the accounts |
| 261 | + teams = [Team.objects.create() for i in range(2)] |
| 262 | + accounts[0].team = teams[0] |
| 263 | + accounts[0].save() |
| 264 | + accounts[1].team = teams[0] |
| 265 | + accounts[1].save() |
| 266 | + accounts[2].team = teams[1] |
| 267 | + accounts[2].save() |
| 268 | + accounts[3].team = teams[1] |
| 269 | + accounts[3].save() |
| 270 | + |
| 271 | + def wrapped_super_entities(*args, **kwargs): |
| 272 | + if not Account. objects. filter( email='[email protected]'). exists(): |
| 273 | + Account.objects.create( |
| 274 | + |
| 275 | + team=Team.objects.order_by('id')[0], |
| 276 | + team2=Team.objects.order_by('id')[1], |
| 277 | + ) |
| 278 | + |
| 279 | + return _get_super_entities_by_ctype(*args, **kwargs) |
| 280 | + |
| 281 | + # Sync all the entities. There should be 8 (6 accounts 2 teams) |
| 282 | + with patch('entity.sync._get_super_entities_by_ctype', wraps=wrapped_super_entities): |
| 283 | + sync_entities() |
| 284 | + |
| 285 | + self.assertEquals(Entity.objects.filter(entity_type=ContentType.objects.get_for_model(Account)).count(), 6) |
| 286 | + self.assertEquals(Entity.objects.filter(entity_type=ContentType.objects.get_for_model(Team)).count(), 2) |
| 287 | + self.assertEquals(Entity.objects.all().count(), 8) |
| 288 | + |
| 289 | + # There should be six entity relationships |
| 290 | + self.assertEquals(EntityRelationship.objects.all().count(), 6) |
| 291 | + |
| 292 | + def test_sync_all_accounts_teams_deleted_account_during_sync(self): |
| 293 | + """ |
| 294 | + Tests the scenario of an account being deleted after account ids are fetched but before the super |
| 295 | + entities are fetched |
| 296 | + """ |
| 297 | + # Create five test accounts |
| 298 | + accounts = [Account.objects.create() for i in range(5)] |
| 299 | + # Create two teams to assign to some of the accounts |
| 300 | + teams = [Team.objects.create() for i in range(2)] |
| 301 | + accounts[0].team = teams[0] |
| 302 | + accounts[ 0]. email = '[email protected]' |
| 303 | + accounts[0].save() |
| 304 | + accounts[1].team = teams[0] |
| 305 | + accounts[1].save() |
| 306 | + accounts[2].team = teams[1] |
| 307 | + accounts[2].save() |
| 308 | + accounts[3].team = teams[1] |
| 309 | + accounts[3].save() |
| 310 | + |
| 311 | + def wrapped_super_entities(*args, **kwargs): |
| 312 | + if Account. objects. filter( email='[email protected]'). exists(): |
| 313 | + Account. objects. filter( email='[email protected]'). delete() |
| 314 | + |
| 315 | + return _get_super_entities_by_ctype(*args, **kwargs) |
| 316 | + |
| 317 | + # Sync all the entities. There should be 6 (4 accounts 2 teams) |
| 318 | + with patch('entity.sync._get_super_entities_by_ctype', wraps=wrapped_super_entities): |
| 319 | + sync_entities(*accounts) |
| 320 | + # Sync again to hit other wrapped function branch and make sure it doesn't error |
| 321 | + sync_entities(*accounts) |
| 322 | + |
| 323 | + self.assertEquals(Entity.objects.filter(entity_type=ContentType.objects.get_for_model(Account)).count(), 4) |
| 324 | + self.assertEquals(Entity.objects.filter(entity_type=ContentType.objects.get_for_model(Team)).count(), 2) |
| 325 | + self.assertEquals(Entity.objects.all().count(), 6) |
| 326 | + |
| 327 | + # There should be six entity relationships |
| 328 | + self.assertEquals(EntityRelationship.objects.all().count(), 3) |
| 329 | + |
253 | 330 | def test_sync_all_accounts_teams_inactive_entity_kind(self):
|
254 | 331 | """
|
255 |
| - Tests syncing of all accounts when they have super entities and the entiity kind is inactive |
| 332 | + Tests syncing of all accounts when they have super entities and the entity kind is inactive |
256 | 333 | """
|
257 | 334 | # Create five test accounts
|
258 | 335 | accounts = [Account.objects.create() for i in range(5)]
|
@@ -905,7 +982,7 @@ def test_sync_all_optimal_queries(self):
|
905 | 982 | with patch('entity.sync.entity_registry') as mock_entity_registry:
|
906 | 983 | mock_entity_registry.entity_registry = new_registry.entity_registry
|
907 | 984 | ContentType.objects.clear_cache()
|
908 |
| - with self.assertNumQueries(19): |
| 985 | + with self.assertNumQueries(20): |
909 | 986 | sync_entities()
|
910 | 987 |
|
911 | 988 | self.assertEquals(Entity.objects.filter(entity_type=ContentType.objects.get_for_model(Account)).count(), 5)
|
|
0 commit comments