def test_sync_all_accounts_teams_inactive_entity_kind(self): """ Tests syncing of all accounts when they have super entities and the entiity kind is inactive """ # 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() team_ek = EntityKind.objects.get(name='tests.team') team_ek.delete() # 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 run(): print("importing player data now") with open('./../resources/NFL Salary Visualization - nfl_salaries.csv', newline='') as csvfile: reader = csv.DictReader(csvfile) for row in reader: first_name = row['player'].split(' ')[0] last_name = row['player'].split(' ')[1] salary = row['salary'] team_name = teams.teams[row['team']] position_name = positions.positions[row['position']] team = Team.objects.filter(name=team_name).first() if not team: team = Team(name=team_name, name_abbreviated=row['team']) team.save() position = Position.objects.filter(name=position_name).first() if not position: position = Position(name=position_name, name_abbreviated=row['position']) position.save() player = Player( email='{}.{}@{}.net'.format(first_name, last_name, team.name_abbreviated), first_name=first_name, password='******', last_name=last_name, position=position, team=team, salary=int(salary)) player.save() print('syncing now') sync_entities()
def test_sync_all_accounts_teams_inactive_entity_kind(self): """ Tests syncing of all accounts when they have super entities and the entiity kind is inactive """ # 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() team_ek = EntityKind.objects.get(name='tests.team') team_ek.delete() # 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_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(AccountConfig) new_registry.register_entity(TeamConfig) with patch('entity.sync.entity_registry') as mock_entity_registry: mock_entity_registry.entity_registry = new_registry.entity_registry ContentType.objects.clear_cache() with self.assertNumQueries(19): 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 save_entity_signal_handler(sender, instance, **kwargs): """ Defines a signal handler for saving an entity. Syncs the entity to the entity mirror table. """ if instance.__class__ in entity_registry.entity_registry: sync_entities(instance) if instance.__class__ in entity_registry.entity_watching: sync_entities_watching(instance)
def bulk_operation_signal_handler(sender, *args, **kwargs): """ When a bulk operation has happened on a model, sync all the entities again. NOTE - bulk syncing isn't turned on by default because of the consequences of it. For example, a user may issue a simple update to a single model, which would trigger syncing of all entities. It is up to the user to explicitly enable syncing on bulk operations with turn_on_syncing(bulk=True) """ if sender in entity_registry.entity_registry: sync_entities()
def test_method(test, count=5, sync_all=False): # Create some entities for i in range(count): Account.objects.create() if sync_all: sync_entities() # Assert that we do not have any entities test.assertEquals(Entity.objects.all().count(), 0)
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_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_get_all_entities_active(self): """ Makes sure only active entity ids are returned """ turn_off_syncing() account_type = ContentType.objects.get_for_model(Account) # Set up teams teams = Team.objects.bulk_create([ Team() for i in range(0, 3) ]) # Set up accounts accounts = Account.objects.bulk_create([ Account(team=teams[i % 3]) for i in range(0, 20) ]) accounts[0].is_active = False accounts[0].save() # Turn on syncing and do a sync turn_on_syncing() sync_entities() account_entities = list(Entity.all_objects.filter(entity_type=account_type).order_by('entity_id')) # Create groups EntityGroup.objects.bulk_create([ EntityGroup() for i in range(0, 6) ]) # Refresh for django 1.9 because bulk create does not return ids entity_groups = list(EntityGroup.objects.order_by('id')) # Set up individual entity groups entity_groups[0].bulk_add_entities([ [account_entities[0], None], [account_entities[1], None], [account_entities[2], None], [account_entities[3], None], ]) self.assertEqual(len(entity_groups[0].get_all_entities()), 3) self.assertEqual(len(entity_groups[0].get_all_entities(is_active=False)), 1) self.assertEqual(len(entity_groups[0].get_all_entities(is_active=None)), 4)
def test_get_all_entities_active(self): """ Makes sure only active entity ids are returned """ turn_off_syncing() account_type = ContentType.objects.get_for_model(Account) # Set up teams teams = Team.objects.bulk_create([Team() for i in range(0, 3)]) # Set up accounts accounts = Account.objects.bulk_create( [Account(team=teams[i % 3]) for i in range(0, 20)]) accounts[0].is_active = False accounts[0].save() # Turn on syncing and do a sync turn_on_syncing() sync_entities() account_entities = list( Entity.all_objects.filter( entity_type=account_type).order_by('entity_id')) # Create groups EntityGroup.objects.bulk_create([EntityGroup() for i in range(0, 6)]) # Refresh for django 1.9 because bulk create does not return ids entity_groups = list(EntityGroup.objects.order_by('id')) # Set up individual entity groups entity_groups[0].bulk_add_entities([ [account_entities[0], None], [account_entities[1], None], [account_entities[2], None], [account_entities[3], None], ]) self.assertEqual(len(entity_groups[0].get_all_entities()), 3) self.assertEqual( len(entity_groups[0].get_all_entities(is_active=False)), 1) self.assertEqual( len(entity_groups[0].get_all_entities(is_active=None)), 4)
def test_sync_all_account_no_teams(self): """ Tests syncing all accounts with no super entities. """ # Create five test accounts accounts = [Account.objects.create() for i in range(5)] # 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 accounts[0].delete() self.assertEquals(Entity.objects.all().count(), 5) sync_entities() self.assertEquals(Entity.objects.all().count(), 4)
def test_sync_optimal_queries(self): """ Tests optimal queries of syncing. """ # 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() with self.assertNumQueries(56): sync_entities()
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() with self.assertNumQueries(20): 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(self, mock_model_activations_changed): """ Tests that when we sync all we fire the correct signals """ # Create five test accounts turn_off_syncing() initial_accounts = [] for i in range(5): initial_accounts.append(Account.objects.create()) turn_on_syncing() # Test that the management command syncs all five entities self.assertEquals(Entity.objects.all().count(), 0) sync_entities() self.assertEquals(Entity.objects.all().count(), 5) initial_entity_ids = list(Entity.objects.all().values_list('id', flat=True)) mock_model_activations_changed.send.assert_called_once_with( sender=Entity, instance_ids=sorted(initial_entity_ids), is_active=True ) # Create five new test accounts, and deactivate our initial accounts mock_model_activations_changed.reset_mock() turn_off_syncing() new_accounts = [] for i in range(5): new_accounts.append(Account.objects.create()) for account in initial_accounts: account.delete() turn_on_syncing() # Sync entities sync_entities() # Assert that the correct signals were called self.assertEqual( mock_model_activations_changed.send.mock_calls, [ call( sender=Entity, instance_ids=sorted(list(Entity.objects.filter( entity_id__in=[account.id for account in new_accounts] ).values_list('id', flat=True))), is_active=True ), call( sender=Entity, instance_ids=sorted(initial_entity_ids), is_active=False ) ] ) # Test syncing all when nothing should have changed mock_model_activations_changed.reset_mock() sync_entities() self.assertFalse(mock_model_activations_changed.send.called)
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)
def test_sync_all(self, mock_model_activations_changed): """ Tests that when we sync all we fire the correct signals """ # Create five test accounts turn_off_syncing() initial_accounts = [] for i in range(5): initial_accounts.append(Account.objects.create()) turn_on_syncing() # Test that the management command syncs all five entities self.assertEquals(Entity.objects.all().count(), 0) sync_entities() self.assertEquals(Entity.objects.all().count(), 5) initial_entity_ids = list(Entity.objects.all().values_list('id', flat=True)) mock_model_activations_changed.send.assert_called_once_with( sender=Entity, instance_ids=sorted(initial_entity_ids), is_active=True) # Create five new test accounts, and deactivate our initial accounts mock_model_activations_changed.reset_mock() turn_off_syncing() new_accounts = [] for i in range(5): new_accounts.append(Account.objects.create()) for account in initial_accounts: account.delete() turn_on_syncing() # Sync entities sync_entities() # Assert that the correct signals were called self.assertEqual(mock_model_activations_changed.send.mock_calls, [ call(sender=Entity, instance_ids=sorted( list( Entity.objects.filter(entity_id__in=[ account.id for account in new_accounts ]).values_list('id', flat=True))), is_active=True), call(sender=Entity, instance_ids=sorted(initial_entity_ids), is_active=False) ]) # Test syncing all when nothing should have changed mock_model_activations_changed.reset_mock() sync_entities() self.assertFalse(mock_model_activations_changed.send.called)
def sync_ambition_entity(): sync_entities()
def run(self, *args, **kwargs): # Check if there are selected model objs to update if 'model_obj_class' and 'model_obj_ids' in kwargs: sync_entities(*kwargs['model_obj_class'].objects.filter(id__in=kwargs['model_obj_ids'])) else: sync_entities()
def run(self, *args, **kwargs): sync_entities()
def handle(self, *args, **options): """ Runs sync entities """ sync_entities()
def test_get_all_entities(self): turn_off_syncing() account_type = ContentType.objects.get_for_model(Account) team_type = ContentType.objects.get_for_model(Team) # Set up teams teams = Team.objects.bulk_create([Team() for i in range(0, 3)]) # Set up accounts Account.objects.bulk_create( [Account(team=teams[i % 3]) for i in range(0, 20)]) # Turn on syncing and do a sync turn_on_syncing() sync_entities() account_entities = list( Entity.all_objects.filter( entity_type=account_type).order_by('entity_id')) account_kind = account_entities[0].entity_kind team_entities = list( Entity.all_objects.filter( entity_type=team_type).order_by('entity_id')) team_kind = team_entities[0].entity_kind # Create groups EntityGroup.objects.bulk_create([EntityGroup() for i in range(0, 6)]) # Refresh for django 1.9 because bulk create does not return ids entity_groups = list(EntityGroup.objects.order_by('id')) # Set up individual entity groups entity_groups[0].bulk_add_entities([ [account_entities[0], None], [account_entities[1], None], [account_entities[2], None], [account_entities[3], None], ]) entity_groups[1].bulk_add_entities([ [account_entities[2], None], [account_entities[3], None], [account_entities[4], None], [account_entities[5], None], ]) # Set up sub entity kind of super entity groups entity_groups[2].bulk_add_entities([ [team_entities[0], account_kind], [team_entities[1], account_kind], ]) entity_groups[3].bulk_add_entities([ [team_entities[0], account_kind], ]) # Set up sub entity kind groups # This group has two copies of the same set of all accounts as well as all teams entity_groups[4].bulk_add_entities([ [None, account_kind], [None, account_kind], [None, team_kind], ]) # This group has the same copy of all accounts entity_groups[5].bulk_add_entities([ [None, account_kind], ]) with self.assertNumQueries(3): membership_cache = EntityGroup.objects.get_membership_cache() entities_by_kind = get_entities_by_kind( membership_cache=membership_cache) for entity_group in entity_groups: entity_group.get_all_entities(membership_cache, entities_by_kind) with self.assertNumQueries(3): get_entities_by_kind() # Make sure to hit the no group cache case self.assertEqual( entity_groups[0].get_all_entities(membership_cache={1000: []}), set())
def test_get_all_entities(self): turn_off_syncing() account_type = ContentType.objects.get_for_model(Account) team_type = ContentType.objects.get_for_model(Team) # Set up teams teams = Team.objects.bulk_create([ Team() for i in range(0, 3) ]) # Set up accounts Account.objects.bulk_create([ Account(team=teams[i % 3]) for i in range(0, 20) ]) # Turn on syncing and do a sync turn_on_syncing() sync_entities() account_entities = list(Entity.all_objects.filter(entity_type=account_type).order_by('entity_id')) account_kind = account_entities[0].entity_kind team_entities = list(Entity.all_objects.filter(entity_type=team_type).order_by('entity_id')) team_kind = team_entities[0].entity_kind # Create groups EntityGroup.objects.bulk_create([ EntityGroup() for i in range(0, 6) ]) # Refresh for django 1.9 because bulk create does not return ids entity_groups = list(EntityGroup.objects.order_by('id')) # Set up individual entity groups entity_groups[0].bulk_add_entities([ [account_entities[0], None], [account_entities[1], None], [account_entities[2], None], [account_entities[3], None], ]) entity_groups[1].bulk_add_entities([ [account_entities[2], None], [account_entities[3], None], [account_entities[4], None], [account_entities[5], None], ]) # Set up sub entity kind of super entity groups entity_groups[2].bulk_add_entities([ [team_entities[0], account_kind], [team_entities[1], account_kind], ]) entity_groups[3].bulk_add_entities([ [team_entities[0], account_kind], ]) # Set up sub entity kind groups # This group has two copies of the same set of all accounts as well as all teams entity_groups[4].bulk_add_entities([ [None, account_kind], [None, account_kind], [None, team_kind], ]) # This group has the same copy of all accounts entity_groups[5].bulk_add_entities([ [None, account_kind], ]) with self.assertNumQueries(3): membership_cache = EntityGroup.objects.get_membership_cache() entities_by_kind = get_entities_by_kind(membership_cache=membership_cache) for entity_group in entity_groups: entity_group.get_all_entities(membership_cache, entities_by_kind) with self.assertNumQueries(3): get_entities_by_kind() # Make sure to hit the no group cache case self.assertEqual(entity_groups[0].get_all_entities(membership_cache={1000: []}), set())