diff --git a/CHANGELOG.md b/CHANGELOG.md index 80c4869..ebf18cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## 2.5.0 * Add Python 3.9 support +* Make data migration model import error less strict (issue #121) Switched from Travis CI to GitHub Actions for tests diff --git a/django_migration_linter/migration_linter.py b/django_migration_linter/migration_linter.py index 4e27826..34f1278 100644 --- a/django_migration_linter/migration_linter.py +++ b/django_migration_linter/migration_linter.py @@ -461,15 +461,15 @@ class MigrationLinter(object): model_object_regex = re.compile(r"[^a-zA-Z]?([a-zA-Z0-9]+?)\.objects") function_name = code.__name__ - reverse_code_source = inspect.getsource(code) + source_code = inspect.getsource(code) - called_models = model_object_regex.findall(reverse_code_source) + called_models = model_object_regex.findall(source_code) issues = [] for model in called_models: has_get_model_call = ( re.search( - r"get_model\(\s*?.*?,.*?{}.+?\s*?\)".format(model), - reverse_code_source, + r"{}.*= +\w+\.get_model\(".format(model), + source_code, ) is not None ) diff --git a/tests/functional/test_data_migrations.py b/tests/functional/test_data_migrations.py index 2405091..6ebce7a 100644 --- a/tests/functional/test_data_migrations.py +++ b/tests/functional/test_data_migrations.py @@ -66,6 +66,8 @@ class DataMigrationDetectionTestCase(unittest.TestCase): self.assertEqual(1, self.linter.nb_erroneous) self.assertTrue(self.linter.has_errors) + +class DataMigrationModelImportTestCase(unittest.TestCase): def test_missing_get_model_import(self): def incorrect_importing_model_forward(apps, schema_editor): from tests.test_project.app_data_migrations.models import MyModel @@ -83,11 +85,51 @@ class DataMigrationDetectionTestCase(unittest.TestCase): MyVeryLongLongLongModel = apps.get_model( "app_data_migrations", "MyVeryLongLongLongModel" ) + MultiLineModel = apps.get_model( + "app_data_migrations", + "MultiLineModel", + ) MyModel.objects.filter(id=1).first() MyVeryLongLongLongModel.objects.filter(id=1).first() + MultiLineModel.objects.all() issues = MigrationLinter.get_data_migration_model_import_issues( correct_importing_model_forward ) self.assertEqual(0, len(issues)) + + def test_not_overlapping_model_name(self): + """ + Correct for the import error, but should raise a warning + """ + + def forward_method(apps, schema_editor): + User = apps.get_model("auth", "CustomUserModel") + + User.objects.filter(id=1).first() + + issues = MigrationLinter.get_data_migration_model_import_issues(forward_method) + self.assertEqual(0, len(issues)) + + def test_correct_one_param_get_model_import(self): + def forward_method(apps, schema_editor): + User = apps.get_model("auth.User") + + User.objects.filter(id=1).first() + + issues = MigrationLinter.get_data_migration_model_import_issues(forward_method) + self.assertEqual(0, len(issues)) + + def test_not_overlapping_one_param(self): + """ + Not an error, but should raise a warning + """ + + def forward_method(apps, schema_editor): + User = apps.get_model("auth.CustomUserModel") + + User.objects.filter(id=1).first() + + issues = MigrationLinter.get_data_migration_model_import_issues(forward_method) + self.assertEqual(0, len(issues))