Esempio n. 1
0
    def test_multiple_same_objects(self):
        """
        Tests initial data when registering the same object for deletion twice.
        """
        class AccountInitialData1(BaseInitialData):
            """
            Initial data code that registers the same object many times for deletion
            """
            def update_initial_data(self):
                # Return the object from update_initial_data, thus registering it for deletion
                account = Account.objects.get_or_create()[0]
                return [account, account, account]

        # Verify no account objects exist
        self.assertEquals(Account.objects.count(), 0)

        with patch.object(InitialDataUpdater,
                          'load_app',
                          return_value=AccountInitialData1):
            InitialDataUpdater().update_all_apps()
            InitialDataUpdater().update_all_apps()

        # Verify an account object was created and is managed by a deletion receipt
        self.assertEquals(Account.objects.count(), 1)
        self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 1)
 def test_update_all_apps(self, update_app_patch):
     """
     Verifies that update_app is called with all installed apps
     """
     initial_data_manager = InitialDataUpdater()
     initial_data_manager.update_all_apps()
     self.assertEqual(2, update_app_patch.call_count)
Esempio n. 3
0
 def test_update_all_apps(self, update_app_patch):
     """
     Verifies that update_app is called with all installed apps
     """
     initial_data_manager = InitialDataUpdater()
     initial_data_manager.update_all_apps()
     self.assertEqual(2, update_app_patch.call_count)
 def test_update_all_apps(self):
     """
     Verifies that update_app is called with all installed apps
     """
     num_apps = len(settings.INSTALLED_APPS)
     with patch('dynamic_initial_data.base.InitialDataUpdater.update_app', spec_set=True) as update_app_patch:
         initial_data_manager = InitialDataUpdater()
         initial_data_manager.update_all_apps()
         self.assertEqual(num_apps, update_app_patch.call_count)
 def test_verbose_option(self):
     """
     Verifies that the verbose option gets set from the manage options
     """
     options = {'verbose': True}
     initial_data_manager = InitialDataUpdater(options)
     self.assertTrue(initial_data_manager.verbose)
     # cover the branch that prints if verbose is true
     initial_data_manager.log('test')
 def test_load_app_cached(self, import_patch):
     """
     Tests that the cache is hit since import is only called once.
     """
     initial_data_updater = InitialDataUpdater()
     initial_data_updater.load_app('fake')
     initial_data_updater.load_app('fake')
     initial_data_updater.load_app('fake')
     self.assertEquals(import_patch.call_count, 1)
Esempio n. 7
0
 def test_verbose_option(self):
     """
     Verifies that the verbose option gets set from the manage options
     """
     options = {'verbose': True}
     initial_data_manager = InitialDataUpdater(options)
     self.assertTrue(initial_data_manager.verbose)
     # cover the branch that prints if verbose is true
     initial_data_manager.log('test')
 def test_update_all_apps(self):
     """
     Verifies that update_app is called with all installed apps
     """
     num_apps = len(settings.INSTALLED_APPS)
     with patch('dynamic_initial_data.base.InitialDataUpdater.update_app',
                spec_set=True) as update_app_patch:
         initial_data_manager = InitialDataUpdater()
         initial_data_manager.update_all_apps()
         self.assertEqual(num_apps, update_app_patch.call_count)
    def test_load_app(self):
        """
        Tests the load_app method
        """
        with patch('dynamic_initial_data.base.import_string') as import_patch:
            import_patch.return_value = MockInitialData
            initial_data_manager = InitialDataUpdater()
            self.assertEqual(MockInitialData, initial_data_manager.load_app('fake'))

            # try to load an app that doesn't exist
            initial_data_manager = InitialDataUpdater()
            import_patch.return_value = MockClass
            self.assertIsNone(initial_data_manager.load_app('fake'))
    def test_update_app_cached_updated_apps(self, update_initial_data_patch, mock_load_app):
        """
        Tests that the app gets added to updated apps and it is cached
        """
        initial_data_manager = InitialDataUpdater()
        initial_data_manager.update_app('dynamic_initial_data')
        self.assertEqual(1, update_initial_data_patch.call_count)

        # make sure it doesn't call update static again
        initial_data_manager.update_app('dynamic_initial_data')
        self.assertEqual(1, update_initial_data_patch.call_count)

        # make sure the app is in the updated_apps list
        self.assertIn('dynamic_initial_data', initial_data_manager.updated_apps)
Esempio n. 11
0
 def test_get_dependency_call_list_circular_dependency(
         self, load_app_patch):
     """
     Tests when a circular dependency is found
     """
     with self.assertRaises(InitialDataCircularDependency):
         InitialDataUpdater().update_app('MockThree')
Esempio n. 12
0
    def test_handle_deletions_updates_registered_from_update_initial_data(
            self):
        """
        Tests handling of deletions and updates when they are registered from the update_initial_data function.
        """
        class AccountInitialData1(BaseInitialData):
            """
            The initial data code the first time it is called. It registers two accounts for deletion
            by returning it from the update_initial_data function.
            """
            def update_initial_data(self):
                # Register two account objects for deletion
                self.register_for_deletion(
                    Account.objects.get_or_create(name='hi')[0],
                    Account.objects.get_or_create(name='hi2')[0])

        class AccountInitialData2(BaseInitialData):
            """
            The initial data code the second time it is called. It only manages one of the previous accounts
            """
            def update_initial_data(self):
                self.register_for_deletion(
                    Account.objects.get_or_create(name='hi')[0])

        # Verify no account objects exist
        self.assertEquals(Account.objects.count(), 0)

        with patch.object(InitialDataUpdater,
                          'load_app',
                          return_value=AccountInitialData1):
            InitialDataUpdater().update_all_apps()

        # Verify two account objects were created and are managed by deletion receipts
        self.assertEquals(Account.objects.count(), 2)
        self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 2)

        # Run the initial data process again, this time deleting the account named 'hi2'
        with patch.object(InitialDataUpdater,
                          'load_app',
                          return_value=AccountInitialData2):
            InitialDataUpdater().update_all_apps()

        # Verify only the 'hi' account exists
        self.assertEquals(Account.objects.count(), 1)
        self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 1)
        self.assertEquals(
            RegisteredForDeletionReceipt.objects.get().model_obj.name, 'hi')
    def test_cant_delete_obj_in_receipt(self):
        """
        Tests when the object in the receipt cant be deleted such as a deleted content type
        or another model that cant be cascaded.
        """
        initial_data_updater = InitialDataUpdater()
        rel_model = G(RelModel)
        G(CantCascadeModel, rel_model=rel_model)
        RegisteredForDeletionReceipt.objects.create(model_obj=rel_model, register_time=datetime(2013, 4, 5))

        account = G(Account)
        RegisteredForDeletionReceipt.objects.create(model_obj=account, register_time=datetime(2013, 4, 5))
        initial_data_updater.model_objs_registered_for_deletion = []

        self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 2)
        with transaction.atomic():
            initial_data_updater.handle_deletions()
        self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 0)
Esempio n. 14
0
    def test_handle_deletions_returned_from_update_initial_data(self):
        """
        Tests handling of deletions when they are returned from the update_initial_data function.
        """
        class AccountInitialData1(BaseInitialData):
            """
            The initial data code the first time it is called. It registers an account for deletion
            by returning it from the update_initial_data function.
            """
            def update_initial_data(self):
                # Return the object from update_initial_data, thus registering it for deletion
                return [Account.objects.get_or_create()[0]]

        class AccountInitialData2(BaseInitialData):
            """
            The initial data code the second time it is called. It no longer creates the account object, so the
            previously created account object should be deleted.
            """
            def update_initial_data(self):
                pass

        # Verify no account objects exist
        self.assertEquals(Account.objects.count(), 0)

        with patch.object(InitialDataUpdater,
                          'load_app',
                          return_value=AccountInitialData1):
            InitialDataUpdater().update_all_apps()

        # Verify an account object was created and is managed by a deletion receipt
        self.assertEquals(Account.objects.count(), 1)
        self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 1)

        # Run the initial data process again, this time not registering the account for
        # deletion. It should be deleted.
        with patch.object(InitialDataUpdater,
                          'load_app',
                          return_value=AccountInitialData2):
            InitialDataUpdater().update_all_apps()

        # Verify there are no accounts or receipts
        self.assertEquals(Account.objects.count(), 0)
        self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 0)
Esempio n. 15
0
    def test_cant_delete_obj_in_receipt(self):
        """
        Tests when the object in the receipt cant be deleted such as a deleted content type
        or another model that cant be cascaded.
        """
        initial_data_updater = InitialDataUpdater()
        rel_model = G(RelModel)
        G(CantCascadeModel, rel_model=rel_model)
        RegisteredForDeletionReceipt.objects.create(model_obj=rel_model,
                                                    register_time=datetime(
                                                        2013, 4, 5))

        account = G(Account)
        RegisteredForDeletionReceipt.objects.create(model_obj=account,
                                                    register_time=datetime(
                                                        2013, 4, 5))
        initial_data_updater.model_objs_registered_for_deletion = []

        self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 2)
        with transaction.atomic():
            initial_data_updater.handle_deletions()
        self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 0)
    def test_get_dependency_call_list(self):
        """
        Makes sure that dependency cycles are found and raises an exception
        """
        initial_data_manager = InitialDataUpdater()
        with patch('dynamic_initial_data.base.InitialDataUpdater.load_app',
                   spec_set=True) as load_app_patch:
            load_app_patch.return_value = MockThree

            with self.assertRaises(InitialDataCircularDependency):
                initial_data_manager.update_app('MockThree')

        initial_data_manager = InitialDataUpdater()
        with self.assertRaises(InitialDataMissingApp):
            initial_data_manager.get_dependency_call_list('fake')
Esempio n. 17
0
 def test_load_app_cached(self, import_patch):
     """
     Tests that the cache is hit since import is only called once.
     """
     initial_data_updater = InitialDataUpdater()
     initial_data_updater.load_app('fake')
     initial_data_updater.load_app('fake')
     initial_data_updater.load_app('fake')
     self.assertEquals(import_patch.call_count, 1)
    def test_create_account(self):
        """
        Tests creating a test account in the initial data process.
        """
        class AccountInitialData(BaseInitialData):
            def update_initial_data(self):
                Account.objects.get_or_create()

        # Verify no account objects exist
        self.assertEquals(Account.objects.count(), 0)

        with patch.object(InitialDataUpdater, 'load_app', return_value=AccountInitialData) as load_app_mock:
            InitialDataUpdater().update_app('test_app')
            # It should be called twice - once for initial loading, and twice for dependency testing
            self.assertEquals(load_app_mock.call_count, 2)

        # Verify an account object was created
        self.assertEquals(Account.objects.count(), 1)
    def test_load_app(self):
        """
        Tests the load_app method
        """
        with patch('dynamic_initial_data.base.import_string') as import_patch:
            import_patch.return_value = MockInitialData
            initial_data_manager = InitialDataUpdater()
            self.assertEqual(MockInitialData,
                             initial_data_manager.load_app('fake'))

            # try to load an app that doesn't exist
            initial_data_manager = InitialDataUpdater()
            import_patch.return_value = MockClass
            self.assertIsNone(initial_data_manager.load_app('fake'))
    def test_get_dependency_call_list(self):
        """
        Makes sure that dependency cycles are found and raises an exception
        """
        initial_data_manager = InitialDataUpdater()
        with patch('dynamic_initial_data.base.InitialDataUpdater.load_app', spec_set=True) as load_app_patch:
            load_app_patch.return_value = MockThree

            with self.assertRaises(InitialDataCircularDependency):
                initial_data_manager.update_app('MockThree')

        initial_data_manager = InitialDataUpdater()
        with self.assertRaises(InitialDataMissingApp):
            initial_data_manager.get_dependency_call_list('fake')
Esempio n. 21
0
    def test_update_app_dependencies(self, update_initial_data_patch2,
                                     update_initial_data_patch1):
        """
        Tests the update_app method when there are dependencies.
        """

        # test dependencies
        def app_loader(app):
            if app == 'MockOne':
                return MockOne
            else:
                return MockTwo

        with patch.object(InitialDataUpdater,
                          'load_app',
                          side_effect=app_loader,
                          spec_set=True):
            InitialDataUpdater().update_app('MockTwo')
            self.assertEqual(1, update_initial_data_patch1.call_count)
            self.assertEqual(1, update_initial_data_patch2.call_count)
Esempio n. 22
0
    def test_update_app_cached_updated_apps(self, update_initial_data_patch,
                                            mock_load_app):
        """
        Tests that the app gets added to updated apps and it is cached
        """
        initial_data_manager = InitialDataUpdater()
        initial_data_manager.update_app('dynamic_initial_data')
        self.assertEqual(1, update_initial_data_patch.call_count)

        # make sure it doesn't call update static again
        initial_data_manager.update_app('dynamic_initial_data')
        self.assertEqual(1, update_initial_data_patch.call_count)

        # make sure the app is in the updated_apps list
        self.assertIn('dynamic_initial_data',
                      initial_data_manager.updated_apps)
Esempio n. 23
0
class TestHandleDeletions(TestCase):
    """
    Tests the handle_deletions functionality in the InitialDataUpater class.
    """
    def setUp(self):
        super(TestHandleDeletions, self).setUp()
        self.initial_data_updater = InitialDataUpdater()

    def test_handle_deletions_no_objs(self):
        """
        Tests when there are no objs to handle. The function should not raise any exceptions.
        """
        self.initial_data_updater.handle_deletions()

    def test_create_one_obj(self):
        """
        Tests creating one object to handle for deletion.
        """
        account = G(Account)
        self.initial_data_updater.model_objs_registered_for_deletion = [
            account
        ]

        self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 0)
        with freeze_time('2013-04-12'):
            self.initial_data_updater.handle_deletions()
        receipt = RegisteredForDeletionReceipt.objects.get()
        self.assertEquals(receipt.model_obj_type,
                          ContentType.objects.get_for_model(Account))
        self.assertEquals(receipt.model_obj_id, account.id)
        self.assertEquals(receipt.register_time, datetime(2013, 4, 12))

    def test_create_dup_objs(self):
        """
        Tests creating duplicate objects for deletion.
        """
        account = G(Account)
        self.initial_data_updater.model_objs_registered_for_deletion = [
            account, account
        ]

        self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 0)
        with freeze_time('2013-04-12'):
            self.initial_data_updater.handle_deletions()
        receipt = RegisteredForDeletionReceipt.objects.get()
        self.assertEquals(receipt.model_obj_type,
                          ContentType.objects.get_for_model(Account))
        self.assertEquals(receipt.model_obj_id, account.id)
        self.assertEquals(receipt.register_time, datetime(2013, 4, 12))

    def test_create_dup_proxy_objs(self):
        """
        Tests creating duplicate objects for deletion when one is a proxy of another.
        """
        account = G(Account)
        proxy_account = ProxyAccount.objects.get(id=account.id)
        self.initial_data_updater.model_objs_registered_for_deletion = [
            account, account, proxy_account
        ]

        self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 0)
        with freeze_time('2013-04-12'):
            self.initial_data_updater.handle_deletions()
        self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 2)

        receipt = RegisteredForDeletionReceipt.objects.get(
            model_obj_type=ContentType.objects.get_for_model(account))
        self.assertEquals(receipt.model_obj_type,
                          ContentType.objects.get_for_model(Account))
        self.assertEquals(receipt.model_obj_id, account.id)
        self.assertEquals(receipt.register_time, datetime(2013, 4, 12))

        receipt = RegisteredForDeletionReceipt.objects.get(
            model_obj_type=ContentType.objects.get_for_model(
                proxy_account, for_concrete_model=False))
        self.assertEquals(
            receipt.model_obj_type,
            ContentType.objects.get_for_model(ProxyAccount,
                                              for_concrete_model=False))
        self.assertEquals(receipt.model_obj_id, proxy_account.id)
        self.assertEquals(receipt.register_time, datetime(2013, 4, 12))

    def test_create_delete_one_obj(self):
        """
        Tests creating one object to handle for deletion and then deleting it.
        """
        account = G(Account)
        self.initial_data_updater.model_objs_registered_for_deletion = [
            account
        ]

        self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 0)
        with freeze_time('2013-04-12'):
            self.initial_data_updater.handle_deletions()
        receipt = RegisteredForDeletionReceipt.objects.get()
        self.assertEquals(receipt.model_obj_type,
                          ContentType.objects.get_for_model(Account))
        self.assertEquals(receipt.model_obj_id, account.id)
        self.assertEquals(receipt.register_time, datetime(2013, 4, 12))

        # Now, don't register the object for deletion and run it again at a different time
        self.initial_data_updater.model_objs_registered_for_deletion = []
        with freeze_time('2013-04-12 05:00:00'):
            self.initial_data_updater.handle_deletions()
        # The object should be deleted, along with its receipt
        self.assertEquals(Account.objects.count(), 0)
        self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 0)

    def test_create_update_one_obj(self):
        """
        Tests creating one object to handle for deletion and then updating it.
        """
        account = G(Account)
        self.initial_data_updater.model_objs_registered_for_deletion = [
            account
        ]

        self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 0)
        with freeze_time('2013-04-12'):
            self.initial_data_updater.handle_deletions()
        receipt = RegisteredForDeletionReceipt.objects.get()
        self.assertEquals(receipt.model_obj_type,
                          ContentType.objects.get_for_model(Account))
        self.assertEquals(receipt.model_obj_id, account.id)
        self.assertEquals(receipt.register_time, datetime(2013, 4, 12))

        # Run the deletion handler again at a different time. It should not delete the object
        with freeze_time('2013-04-12 05:00:00'):
            self.initial_data_updater.handle_deletions()
        # The object should not be deleted, along with its receipt
        self.assertEquals(Account.objects.count(), 1)
        self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 1)
        self.assertEquals(
            RegisteredForDeletionReceipt.objects.get().register_time,
            datetime(2013, 4, 12, 5))

    def test_delete_already_deleted_obj(self):
        """
        Tests the case when an object that was registered for deletion has already been deleted.
        """
        account = G(Account)
        self.initial_data_updater.model_objs_registered_for_deletion = [
            account
        ]

        self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 0)
        with freeze_time('2013-04-12'):
            self.initial_data_updater.handle_deletions()
        receipt = RegisteredForDeletionReceipt.objects.get()
        self.assertEquals(receipt.model_obj_type,
                          ContentType.objects.get_for_model(Account))
        self.assertEquals(receipt.model_obj_id, account.id)
        self.assertEquals(receipt.register_time, datetime(2013, 4, 12))

        # Delete the model object. The receipt should still exist
        account.delete()
        self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 1)

        # Now, don't register the object for deletion and run it again at a different time
        self.initial_data_updater.model_objs_registered_for_deletion = []
        with freeze_time('2013-04-12 05:00:00'):
            self.initial_data_updater.handle_deletions()
        # The object should be deleted, along with its receipt
        self.assertEquals(Account.objects.count(), 0)
        self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 0)
 def setUp(self):
     super(TestHandleDeletions, self).setUp()
     self.initial_data_updater = InitialDataUpdater()
class TestHandleDeletions(TestCase):
    """
    Tests the handle_deletions functionality in the InitialDataUpater class.
    """
    def setUp(self):
        super(TestHandleDeletions, self).setUp()
        self.initial_data_updater = InitialDataUpdater()

    def test_handle_deletions_no_objs(self):
        """
        Tests when there are no objs to handle. The function should not raise any exceptions.
        """
        self.initial_data_updater.handle_deletions()

    def test_create_one_obj(self):
        """
        Tests creating one object to handle for deletion.
        """
        account = G(Account)
        self.initial_data_updater.model_objs_registered_for_deletion = [account]

        self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 0)
        with freeze_time('2013-04-12'):
            self.initial_data_updater.handle_deletions()
        receipt = RegisteredForDeletionReceipt.objects.get()
        self.assertEquals(receipt.model_obj_type, ContentType.objects.get_for_model(Account))
        self.assertEquals(receipt.model_obj_id, account.id)
        self.assertEquals(receipt.register_time, datetime(2013, 4, 12))

    def test_create_dup_objs(self):
        """
        Tests creating duplicate objects for deletion.
        """
        account = G(Account)
        self.initial_data_updater.model_objs_registered_for_deletion = [account, account]

        self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 0)
        with freeze_time('2013-04-12'):
            self.initial_data_updater.handle_deletions()
        receipt = RegisteredForDeletionReceipt.objects.get()
        self.assertEquals(receipt.model_obj_type, ContentType.objects.get_for_model(Account))
        self.assertEquals(receipt.model_obj_id, account.id)
        self.assertEquals(receipt.register_time, datetime(2013, 4, 12))

    def test_create_dup_proxy_objs(self):
        """
        Tests creating duplicate objects for deletion when one is a proxy of another.
        """
        account = G(Account)
        proxy_account = ProxyAccount.objects.get(id=account.id)
        self.initial_data_updater.model_objs_registered_for_deletion = [account, account, proxy_account]

        self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 0)
        with freeze_time('2013-04-12'):
            self.initial_data_updater.handle_deletions()
        self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 2)

        receipt = RegisteredForDeletionReceipt.objects.get(model_obj_type=ContentType.objects.get_for_model(account))
        self.assertEquals(receipt.model_obj_type, ContentType.objects.get_for_model(Account))
        self.assertEquals(receipt.model_obj_id, account.id)
        self.assertEquals(receipt.register_time, datetime(2013, 4, 12))

        receipt = RegisteredForDeletionReceipt.objects.get(
            model_obj_type=ContentType.objects.get_for_model(proxy_account, for_concrete_model=False))
        self.assertEquals(
            receipt.model_obj_type, ContentType.objects.get_for_model(ProxyAccount, for_concrete_model=False))
        self.assertEquals(receipt.model_obj_id, proxy_account.id)
        self.assertEquals(receipt.register_time, datetime(2013, 4, 12))

    def test_create_delete_one_obj(self):
        """
        Tests creating one object to handle for deletion and then deleting it.
        """
        account = G(Account)
        self.initial_data_updater.model_objs_registered_for_deletion = [account]

        self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 0)
        with freeze_time('2013-04-12'):
            self.initial_data_updater.handle_deletions()
        receipt = RegisteredForDeletionReceipt.objects.get()
        self.assertEquals(receipt.model_obj_type, ContentType.objects.get_for_model(Account))
        self.assertEquals(receipt.model_obj_id, account.id)
        self.assertEquals(receipt.register_time, datetime(2013, 4, 12))

        # Now, don't register the object for deletion and run it again at a different time
        self.initial_data_updater.model_objs_registered_for_deletion = []
        with freeze_time('2013-04-12 05:00:00'):
            self.initial_data_updater.handle_deletions()
        # The object should be deleted, along with its receipt
        self.assertEquals(Account.objects.count(), 0)
        self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 0)

    def test_create_update_one_obj(self):
        """
        Tests creating one object to handle for deletion and then updating it.
        """
        account = G(Account)
        self.initial_data_updater.model_objs_registered_for_deletion = [account]

        self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 0)
        with freeze_time('2013-04-12'):
            self.initial_data_updater.handle_deletions()
        receipt = RegisteredForDeletionReceipt.objects.get()
        self.assertEquals(receipt.model_obj_type, ContentType.objects.get_for_model(Account))
        self.assertEquals(receipt.model_obj_id, account.id)
        self.assertEquals(receipt.register_time, datetime(2013, 4, 12))

        # Run the deletion handler again at a different time. It should not delete the object
        with freeze_time('2013-04-12 05:00:00'):
            self.initial_data_updater.handle_deletions()
        # The object should not be deleted, along with its receipt
        self.assertEquals(Account.objects.count(), 1)
        self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 1)
        self.assertEquals(RegisteredForDeletionReceipt.objects.get().register_time, datetime(2013, 4, 12, 5))

    def test_delete_already_deleted_obj(self):
        """
        Tests the case when an object that was registered for deletion has already been deleted.
        """
        account = G(Account)
        self.initial_data_updater.model_objs_registered_for_deletion = [account]

        self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 0)
        with freeze_time('2013-04-12'):
            self.initial_data_updater.handle_deletions()
        receipt = RegisteredForDeletionReceipt.objects.get()
        self.assertEquals(receipt.model_obj_type, ContentType.objects.get_for_model(Account))
        self.assertEquals(receipt.model_obj_id, account.id)
        self.assertEquals(receipt.register_time, datetime(2013, 4, 12))

        # Delete the model object. The receipt should still exist
        account.delete()
        self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 1)

        # Now, don't register the object for deletion and run it again at a different time
        self.initial_data_updater.model_objs_registered_for_deletion = []
        with freeze_time('2013-04-12 05:00:00'):
            self.initial_data_updater.handle_deletions()
        # The object should be deleted, along with its receipt
        self.assertEquals(Account.objects.count(), 0)
        self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 0)
    def test_update_app(self):
        """
        Tests the update_app method
        """
        # an error should only be raised for missing dependencies and not for directly
        # calling update on an app that doesn't have an initial data file
        initial_data_manager = InitialDataUpdater()
        initial_data_manager.update_app('fake')

        # make sure app gets added to updated apps
        initial_data_manager = InitialDataUpdater()
        with patch(
                'dynamic_initial_data.base.InitialDataUpdater.get_class_path',
                spec_set=True) as get_path_patch:
            get_path_patch.return_value = 'dynamic_initial_data.tests.mocks.MockInitialData'

            # patch the update_initial_data method so we make sure it is called
            update_initial_data_patcher = patch(
                'dynamic_initial_data.tests.mocks.MockInitialData.update_initial_data'
            )
            update_initial_data_patch = update_initial_data_patcher.start()
            initial_data_manager.update_app('dynamic_initial_data')
            self.assertEqual(1, update_initial_data_patch.call_count)

            # make sure it doesn't call update static again
            initial_data_manager.update_app('dynamic_initial_data')
            self.assertEqual(1, update_initial_data_patch.call_count)

            # stop the patcher
            update_initial_data_patcher.stop()

            # make sure the app is in the updated_apps list
            self.assertIn('dynamic_initial_data',
                          initial_data_manager.updated_apps)

        # test dependencies
        def app_loader(app):
            if app == 'MockOne':
                return MockOne
            elif app == 'MockTwo':
                return MockTwo
            return None

        # coverage
        app_loader(None)

        initial_data_manager = InitialDataUpdater()
        with patch('dynamic_initial_data.base.InitialDataUpdater.load_app',
                   spec_set=True) as load_app_patch:
            load_app_patch.side_effect = app_loader

            # patch update_initial_data methods
            update_initial_data_patcher1 = patch(
                'dynamic_initial_data.tests.mocks.MockOne.update_initial_data')
            update_initial_data_patcher2 = patch(
                'dynamic_initial_data.tests.mocks.MockTwo.update_initial_data')
            update_initial_data_patch1 = update_initial_data_patcher1.start()
            update_initial_data_patch2 = update_initial_data_patcher2.start()

            initial_data_manager.update_app('MockTwo')
            self.assertEqual(1, update_initial_data_patch1.call_count)
            self.assertEqual(1, update_initial_data_patch2.call_count)

            update_initial_data_patcher1.stop()
            update_initial_data_patcher2.stop()
Esempio n. 27
0
 def test_update_app_cant_load_initial_data(self):
     """
     Tests when the initial data class can't be loaded. It should execute without error
     """
     InitialDataUpdater().update_app('bad_app_path')
Esempio n. 28
0
 def test_update_app_no_errors_raised(self, mock_load_app):
     """
     Tests the update_app method. No errors should be raised since it has all of the required
     components
     """
     InitialDataUpdater().update_app('fake')
Esempio n. 29
0
 def test_load_app_exists(self, import_patch):
     """
     Tests the load_app method on an app that exists
     """
     self.assertEqual(MockInitialData,
                      InitialDataUpdater().load_app('fake'))
 def handle(self, *args, **options):
     updater = InitialDataUpdater(options)
     if options['app']:
         updater.update_app(options['app'])
     else:
         updater.update_all_apps()
Esempio n. 31
0
 def test_get_dependency_call_list_initial_data_missing(self):
     """
     Tests when the initial data is missing.
     """
     with self.assertRaises(InitialDataMissingApp):
         InitialDataUpdater().get_dependency_call_list('fake')
Esempio n. 32
0
 def handle(self, *args, **options):
     updater = InitialDataUpdater(options)
     if options['app']:
         updater.update_app(options['app'])
     else:
         updater.update_all_apps()
    def test_update_app(self):
        """
        Tests the update_app method
        """
        # an error should only be raised for missing dependencies and not for directly
        # calling update on an app that doesn't have an initial data file
        initial_data_manager = InitialDataUpdater()
        initial_data_manager.update_app('fake')

        # make sure app gets added to updated apps
        initial_data_manager = InitialDataUpdater()
        with patch('dynamic_initial_data.base.InitialDataUpdater.get_class_path', spec_set=True) as get_path_patch:
            get_path_patch.return_value = 'dynamic_initial_data.tests.mocks.MockInitialData'

            # patch the update_initial_data method so we make sure it is called
            update_initial_data_patcher = patch('dynamic_initial_data.tests.mocks.MockInitialData.update_initial_data')
            update_initial_data_patch = update_initial_data_patcher.start()
            initial_data_manager.update_app('dynamic_initial_data')
            self.assertEqual(1, update_initial_data_patch.call_count)

            # make sure it doesn't call update static again
            initial_data_manager.update_app('dynamic_initial_data')
            self.assertEqual(1, update_initial_data_patch.call_count)

            # stop the patcher
            update_initial_data_patcher.stop()

            # make sure the app is in the updated_apps list
            self.assertIn('dynamic_initial_data', initial_data_manager.updated_apps)

        # test dependencies
        def app_loader(app):
            if app == 'MockOne':
                return MockOne
            elif app == 'MockTwo':
                return MockTwo
            return None

        # coverage
        app_loader(None)

        initial_data_manager = InitialDataUpdater()
        with patch('dynamic_initial_data.base.InitialDataUpdater.load_app', spec_set=True) as load_app_patch:
            load_app_patch.side_effect = app_loader

            # patch update_initial_data methods
            update_initial_data_patcher1 = patch('dynamic_initial_data.tests.mocks.MockOne.update_initial_data')
            update_initial_data_patcher2 = patch('dynamic_initial_data.tests.mocks.MockTwo.update_initial_data')
            update_initial_data_patch1 = update_initial_data_patcher1.start()
            update_initial_data_patch2 = update_initial_data_patcher2.start()

            initial_data_manager.update_app('MockTwo')
            self.assertEqual(1, update_initial_data_patch1.call_count)
            self.assertEqual(1, update_initial_data_patch2.call_count)

            update_initial_data_patcher1.stop()
            update_initial_data_patcher2.stop()
Esempio n. 34
0
 def test_load_app_doesnt_exist(self, import_patch):
     """
     Tests the load_app method on an app that doesnt exist
     """
     self.assertIsNone(InitialDataUpdater().load_app('fake'))
Esempio n. 35
0
 def setUp(self):
     super(TestHandleDeletions, self).setUp()
     self.initial_data_updater = InitialDataUpdater()
Esempio n. 36
0
 def handle(self, *args, **options):
     manager = InitialDataUpdater(options)
     manager.update_all_apps()