Exemplo n.º 1
0
 def test_turn_off_syncing_all_handlers_true(
         self, post_bulk_operation_mock, m2m_changed_mock, post_delete_mock, post_save_mock):
     turn_off_syncing(for_post_save=True, for_post_delete=True, for_m2m_changed=True, for_post_bulk_operation=True)
     self.assertTrue(post_save_mock.disconnect.called)
     self.assertTrue(post_delete_mock.disconnect.called)
     self.assertTrue(m2m_changed_mock.disconnect.called)
     self.assertTrue(post_bulk_operation_mock.disconnect.called)
Exemplo n.º 2
0
 def setUp(self):
     """
     Ensure entity syncing is in default state when tests start.
     """
     super(EntityTestCase, self).setUp()
     turn_off_syncing()
     turn_on_syncing()
Exemplo n.º 3
0
 def tearDown(self):
     """
     Make sure syncing is turned back to its original state.
     """
     super(EntityTestCase, self).tearDown()
     turn_off_syncing()
     turn_on_syncing()
Exemplo n.º 4
0
    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)
Exemplo n.º 5
0
 def test_turn_off_save(self):
     """
     Tests turning off syncing for the save signal.
     """
     turn_off_syncing()
     with patch('entity.signal_handlers.sync_entities') as mock_handler:
         Account.objects.create()
         self.assertFalse(mock_handler.called)
Exemplo n.º 6
0
 def test_turn_off_bulk(self):
     """
     Tests turning off syncing for bulk operations.
     """
     turn_off_syncing()
     with patch('entity.signal_handlers.sync_entities') as mock_handler:
         Account.objects.bulk_create([Account() for i in range(5)])
         self.assertFalse(mock_handler.called)
Exemplo n.º 7
0
 def test_turn_off_bulk(self):
     """
     Tests turning off syncing for bulk operations.
     """
     turn_off_syncing()
     with patch('entity.signal_handlers.sync_entities') as mock_handler:
         Account.objects.bulk_create([Account() for i in range(5)])
         self.assertFalse(mock_handler.called)
Exemplo n.º 8
0
 def test_turn_off_save(self):
     """
     Tests turning off syncing for the save signal.
     """
     turn_off_syncing()
     with patch('entity.signal_handlers.sync_entities') as mock_handler:
         Account.objects.create()
         self.assertFalse(mock_handler.called)
Exemplo n.º 9
0
 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)
Exemplo n.º 10
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)
Exemplo n.º 11
0
 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)
Exemplo n.º 12
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)
Exemplo n.º 13
0
 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)
Exemplo n.º 14
0
    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)
Exemplo n.º 15
0
 def test_turn_off_syncing_all_handlers_true(self, post_bulk_operation_mock,
                                             m2m_changed_mock,
                                             post_delete_mock,
                                             post_save_mock):
     turn_off_syncing(for_post_save=True,
                      for_post_delete=True,
                      for_m2m_changed=True,
                      for_post_bulk_operation=True)
     self.assertTrue(post_save_mock.disconnect.called)
     self.assertTrue(post_delete_mock.disconnect.called)
     self.assertTrue(m2m_changed_mock.disconnect.called)
     self.assertTrue(post_bulk_operation_mock.disconnect.called)
Exemplo n.º 16
0
    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)
Exemplo n.º 17
0
    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)
Exemplo n.º 18
0
    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)
Exemplo n.º 19
0
    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)
Exemplo n.º 20
0
    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)
Exemplo n.º 21
0
    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)
Exemplo n.º 22
0
    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)
Exemplo n.º 23
0
    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)
Exemplo n.º 24
0
    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)
Exemplo n.º 25
0
    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)
Exemplo n.º 26
0
    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)
Exemplo n.º 27
0
    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)
Exemplo n.º 28
0
    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())
Exemplo n.º 29
0
    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())