def setUp(self): """ Ensure entity syncing is in default state when tests start. """ super(EntityTestCase, self).setUp() turn_off_syncing() turn_on_syncing()
def tearDown(self): """ Make sure syncing is turned back to its original state. """ super(EntityTestCase, self).tearDown() turn_off_syncing() turn_on_syncing()
def test_turn_on_syncing_all_handlers_true( self, post_bulk_operation_mock, m2m_changed_mock, post_delete_mock, post_save_mock): turn_on_syncing(for_post_save=True, for_post_delete=True, for_m2m_changed=True, for_post_bulk_operation=True) self.assertTrue(post_save_mock.connect.called) self.assertTrue(post_delete_mock.connect.called) self.assertTrue(m2m_changed_mock.connect.called) self.assertTrue(post_bulk_operation_mock.connect.called)
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_turn_on_save(self): """ Tests turning on syncing for the save signal. """ turn_off_syncing() turn_on_syncing() with patch('entity.signal_handlers.sync_entities') as mock_handler: Account.objects.create() self.assertTrue(mock_handler.called)
def test_turn_on_bulk(self): """ Tests turning on syncing for bulk operations. """ turn_off_syncing() turn_on_syncing(for_post_bulk_operation=True) with patch('entity.signal_handlers.sync_entities') as mock_handler: Account.objects.bulk_create([Account() for i in range(5)]) self.assertTrue(mock_handler.called)
def test_turn_on_delete(self): """ Tests turning on syncing for the delete signal. """ turn_off_syncing() turn_on_syncing() with patch('entity.models.Entity.all_objects.delete_for_obj') as mock_handler: a = Account.objects.create() a.delete() self.assertEquals(mock_handler.call_count, 1)
def test_turn_on_delete(self): """ Tests turning on syncing for the delete signal. """ turn_off_syncing() turn_on_syncing() with patch('entity.models.Entity.all_objects.delete_for_obj' ) as mock_handler: a = Account.objects.create() a.delete() self.assertEquals(mock_handler.call_count, 1)
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_save_model_with_m2m(self): """ Verifies that the m2m test entity is synced properly upon save. """ turn_off_syncing() m = G(M2mEntity) m.teams.add(G(Team)) turn_on_syncing() m.save() self.assertEquals(Entity.objects.count(), 2) self.assertEquals(EntityRelationship.objects.count(), 1)
def test_turn_on_syncing_all_handlers_false(self, post_bulk_operation_mock, m2m_changed_mock, post_delete_mock, post_save_mock): turn_on_syncing(for_post_save=False, for_post_delete=False, for_m2m_changed=False, for_post_bulk_operation=False) self.assertFalse(post_save_mock.connect.called) self.assertFalse(post_delete_mock.connect.called) self.assertFalse(m2m_changed_mock.connect.called) self.assertFalse(post_bulk_operation_mock.connect.called)
def test_cascade_sync_super_entities(self): """ Tests that super entities will be synced when a sub entity is synced (even if the super entities werent synced before) """ turn_off_syncing() team = G(Team) turn_on_syncing() self.assertFalse(Entity.objects.exists()) G(Account, team=team) self.assertEquals(Entity.objects.count(), 2) self.assertEquals(EntityRelationship.objects.count(), 1)
def test_sync_entities_management_command(self): """ Tests that the management command for syncing entities works properly. """ # Create five test accounts turn_off_syncing() for i in range(5): Account.objects.create() turn_on_syncing() # Test that the management command syncs all five entities self.assertEquals(Entity.objects.all().count(), 0) call_command('sync_entities') self.assertEquals(Entity.objects.all().count(), 5)
def test_async_sync_entities_management_command(self): """ Tests that the sync_entities command works with the asynchronous option. """ # Create five test accounts without syncing on turn_off_syncing() for i in range(5): Account.objects.create() turn_on_syncing() # Test that the management command syncs all five entities self.assertEquals(Entity.objects.all().count(), 0) call_command('sync_entities', async=True) self.assertEquals(Entity.objects.all().count(), 5)
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. """ 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 setUp(self): super(TestEntityBulkSignalSync, self).setUp() turn_on_syncing(for_post_bulk_operation=True)
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())