def test_foreign_key_swapped(self): with isolate_lru_cache(apps.get_swappable_settings_name): # It doesn't matter that we swapped out user for permission; # there's no validation. We just want to check the setting stuff works. field = models.ForeignKey("auth.Permission", models.CASCADE) name, path, args, kwargs = field.deconstruct() self.assertEqual(path, "django.db.models.ForeignKey") self.assertEqual(args, []) self.assertEqual(kwargs, { "to": "auth.permission", "on_delete": models.CASCADE }) self.assertEqual(kwargs['to'].setting_name, "AUTH_USER_MODEL") # Model names are case-insensitive. with isolate_lru_cache(apps.get_swappable_settings_name): # It doesn't matter that we swapped out user for permission; # there's no validation. We just want to check the setting stuff # works. field = models.ForeignKey('auth.permission', models.CASCADE) name, path, args, kwargs = field.deconstruct() self.assertEqual(path, 'django.db.models.ForeignKey') self.assertEqual(args, []) self.assertEqual(kwargs, { 'to': 'auth.permission', 'on_delete': models.CASCADE }) self.assertEqual(kwargs['to'].setting_name, 'AUTH_USER_MODEL')
def test_model_serializer(self): custom_serializer = 'tests.test_app.models.CustomSerializer' with override_settings(SIMPLE_LOG_MODEL_SERIALIZER=custom_serializer): with isolate_lru_cache(get_serializer): self.assertEqual(get_serializer(TestModel), CustomSerializer) with override_settings(SIMPLE_LOG_MODEL_SERIALIZER=CustomSerializer): with isolate_lru_cache(get_serializer): self.assertEqual(get_serializer(TestModel), CustomSerializer)
def test_concrete_model_exclude_fields_add(self, mocked): other_model = OtherModel.objects.create(char_field='other') with isolate_lru_cache(get_fields): initial_count = SimpleLog.objects.count() TestModel.objects.create( char_field='test', fk_field=other_model ) sl = SimpleLog.objects.latest('pk') self.assertEqual(SimpleLog.objects.count(), initial_count + 1) self.assertDictEqual( sl.new, { 'fk_field': { 'label': 'Fk field', 'value': { 'db': other_model.pk, 'repr': force_text(other_model) } }, 'm2m_field': { 'label': 'M2m field', 'value': [] } } )
def test_log_all_models(self): all_models = [ x for x in apps.get_models() if not issubclass(x, SimpleLogAbstract) ] with isolate_lru_cache(get_model_list): self.assertListEqual(get_model_list(), all_models)
def test_model_exclude_list_add(self): model_list = [ x for x in apps.get_models() if not issubclass(x, SimpleLogAbstract) and x is not OtherModel ] with isolate_lru_cache(get_model_list): self.assertListEqual(get_model_list(), model_list)
def test_log_model(self): with isolate_lru_cache(get_log_model): self.assertIs(get_log_model(), SwappableLogModel) other_model = OtherModel.objects.create(char_field='other') initial_count = SwappableLogModel.objects.count() TestModel.objects.create(char_field='test', fk_field=other_model) sl = SwappableLogModel.objects.latest('pk') self.assertEqual(SwappableLogModel.objects.count(), initial_count + 1) self.assertDictEqual( sl.new, { 'char_field': { 'label': 'Char field', 'value': 'test' }, 'fk_field': { 'label': 'Fk field', 'value': { 'db': other_model.pk, 'repr': force_text(other_model), } }, 'm2m_field': { 'label': 'M2m field', 'value': [] }, 'choice_field': { 'label': 'Choice field', 'value': { 'db': TestModel.ONE, 'repr': 'One' } } })
def test_many_to_many_field_swapped(self): with isolate_lru_cache(apps.get_swappable_settings_name): # It doesn't matter that we swapped out user for permission; # there's no validation. We just want to check the setting stuff works. field = models.ManyToManyField("auth.Permission") name, path, args, kwargs = field.deconstruct() self.assertEqual(path, "django.db.models.ManyToManyField") self.assertEqual(args, []) self.assertEqual(kwargs, {"to": "auth.Permission"}) self.assertEqual(kwargs["to"].setting_name, "AUTH_USER_MODEL")
def test_foreign_key_swapped(self): with isolate_lru_cache(apps.get_swappable_settings_name): # It doesn't matter that we swapped out user for permission; # there's no validation. We just want to check the setting stuff works. field = models.ForeignKey("auth.Permission", models.CASCADE) name, path, args, kwargs = field.deconstruct() self.assertEqual(path, "django.db.models.ForeignKey") self.assertEqual(args, []) self.assertEqual(kwargs, {"to": "auth.Permission", "on_delete": models.CASCADE}) self.assertEqual(kwargs["to"].setting_name, "AUTH_USER_MODEL")
def test_many_to_many_field_swapped(self): with isolate_lru_cache(apps.get_swappable_settings_name): # It doesn't matter that we swapped out user for permission; # there's no validation. We just want to check the setting stuff works. field = models.ManyToManyField("auth.Permission") name, path, args, kwargs = field.deconstruct() self.assertEqual(path, "django.db.models.ManyToManyField") self.assertEqual(args, []) self.assertEqual(kwargs, {"to": "auth.Permission"}) self.assertEqual(kwargs['to'].setting_name, "AUTH_USER_MODEL")
def test_concrete_model_fields_add(self, mocked): other_model = OtherModel.objects.create(char_field='other') with isolate_lru_cache(get_fields): initial_count = SimpleLog.objects.count() self.add_object( TestModel, {'char_field': 'test', 'fk_field': other_model} ) sl = SimpleLog.objects.latest('pk') self.assertEqual(SimpleLog.objects.count(), initial_count + 1) self.assertDictEqual( sl.new, {'char_field': {'label': 'Char field', 'value': 'test'}}, )
def test_custom_user(self): """ Regression test for #22325 - references to a custom user model defined in the same app are not resolved correctly. """ with isolate_lru_cache(global_apps.get_swappable_settings_name): executor = MigrationExecutor(connection) self.assertTableNotExists("migrations_author") self.assertTableNotExists("migrations_tribble") # Migrate forwards executor.migrate([("migrations", "0001_initial")]) self.assertTableExists("migrations_author") self.assertTableExists("migrations_tribble") # The soft-application detection works. # Change table_names to not return auth_user during this as it # wouldn't be there in a normal run, and ensure migrations.Author # exists in the global app registry temporarily. old_table_names = connection.introspection.table_names connection.introspection.table_names = lambda c: [ x for x in old_table_names(c) if x != "auth_user" ] migrations_apps = executor.loader.project_state( ("migrations", "0001_initial"), ).apps global_apps.get_app_config( "migrations").models["author"] = migrations_apps.get_model( "migrations", "author") try: migration = executor.loader.get_migration( "auth", "0001_initial") self.assertIs( executor.detect_soft_applied(None, migration)[0], True) finally: connection.introspection.table_names = old_table_names del global_apps.get_app_config("migrations").models["author"] # Migrate back to clean up the database. executor.loader.build_graph() executor.migrate([("migrations", None)]) self.assertTableNotExists("migrations_author") self.assertTableNotExists("migrations_tribble")
def test_concrete_model_serializer(self, mocked): with isolate_lru_cache(get_serializer): self.assertEqual(get_serializer(TestModel), CustomSerializer)
def test_model_list_add(self): with isolate_lru_cache(get_model_list): self.assertListEqual(get_model_list(), [OtherModel])
def test_log_model_not_subclass_simplelog(self): with isolate_lru_cache(get_log_model): msg = 'Log model should be subclass of SimpleLogAbstractBase.' with self.assertRaisesMessage(ImproperlyConfigured, msg): get_log_model()
def test_log_model_not_exist(self): with isolate_lru_cache(get_log_model): msg = ("SIMPLE_LOG_MODEL refers to model 'not_exist.Model' " "that has not been installed") with self.assertRaisesMessage(ImproperlyConfigured, msg): get_log_model()
def test_log_model_wrong_value(self): with isolate_lru_cache(get_log_model): msg = "SIMPLE_LOG_MODEL must be of the form 'app_label.model_name'" with self.assertRaisesMessage(ImproperlyConfigured, msg): get_log_model()