def test_sync_all_accounts_teams(self): """ Tests syncing of all accounts when they have syper entities. """ # Create five test accounts accounts = [Account.objects.create() for i in range(5)] # Create two teams to assign to some of the accounts teams = [Team.objects.create() for i in range(2)] accounts[0].team = teams[0] accounts[0].save() accounts[1].team = teams[0] accounts[1].save() accounts[2].team = teams[1] accounts[2].save() accounts[3].team = teams[1] accounts[3].save() # Sync all the entities. There should be 7 (5 accounts 2 teams) sync_entities() self.assertEquals(Entity.objects.filter(entity_type=ContentType.objects.get_for_model(Account)).count(), 5) self.assertEquals(Entity.objects.filter(entity_type=ContentType.objects.get_for_model(Team)).count(), 2) self.assertEquals(Entity.objects.all().count(), 7) # There should be four entity relationships since four accounts have teams self.assertEquals(EntityRelationship.objects.all().count(), 4)
def test_sync_multi_inherited_data(self): """ Test when models are synced that don't directly inherit EntityModelMixin. """ # Create an entity that does not directly inherit EntityModelMixin MultiInheritEntity.objects.create() # Sync all entities and verify that one was created sync_entities() self.assertEquals(Entity.objects.all().count(), 1)
def test_sync_two_accounts_one_team_group(self): turn_off_syncing() team = G(Team) account1 = G(Account, team=team) account2 = G(Account, team=team) team_group = G(TeamGroup) sync_entities(account1, account2, team_group) self.assertEquals(Entity.objects.count(), 4) self.assertEquals(EntityRelationship.objects.count(), 2)
def test_sync_dummy_data(self): """ Tests that dummy data (i.e data that does not inherit EntityModelMixin) doesn't get synced. """ # Create dummy data DummyModel.objects.create() # Sync all entities and verify that none were created sync_entities() self.assertEquals(Entity.objects.all().count(), 0)
def test_sync_all_optimal_queries(self): """ Tests optimal queries of syncing all entities. """ # Create five test accounts accounts = [Account.objects.create() for i in range(5)] # Create two teams to assign to some of the accounts teams = [Team.objects.create() for i in range(2)] accounts[0].team = teams[0] accounts[0].save() accounts[1].team = teams[0] accounts[1].save() accounts[2].team = teams[1] accounts[2].save() accounts[3].team = teams[1] accounts[3].save() # Use an entity registry that only has accounts and teams. This ensures that other registered # entity models dont pollute the test case new_registry = EntityRegistry() new_registry.register_entity( Account.objects.select_related('team', 'team2', 'team_group', 'competitor'), AccountConfig) new_registry.register_entity( Team.objects.select_related('team_group'), TeamConfig) with patch('entity.sync.entity_registry') as mock_entity_registry: mock_entity_registry.entity_registry = new_registry.entity_registry ContentType.objects.clear_cache() # TODO this was 14 queries before removing shallow and deep caching in sync_entities. Get it back down with self.assertNumQueries(16): sync_entities() self.assertEquals(Entity.objects.filter(entity_type=ContentType.objects.get_for_model(Account)).count(), 5) self.assertEquals(Entity.objects.filter(entity_type=ContentType.objects.get_for_model(Team)).count(), 2) self.assertEquals(Entity.objects.all().count(), 7) # There should be four entity relationships since four accounts have teams self.assertEquals(EntityRelationship.objects.all().count(), 4)
def test_sync_all_account_no_teams(self): """ Tests syncing all accounts with no super entities. """ turn_off_syncing() # Create five test accounts accounts = [Account.objects.create() for i in range(5)] turn_on_syncing() # Sync all of the entities and verify that five Entity models were created for the Account model self.assertEquals(Entity.objects.all().count(), 0) sync_entities() self.assertEquals(Entity.objects.all().count(), 5) # Delete an account. When all entities are synced again, # there should only be four accounts turn_off_syncing() accounts[0].delete() turn_on_syncing() self.assertEquals(Entity.objects.all().count(), 5) sync_entities() self.assertEquals(Entity.objects.all().count(), 4)