Make data migration model import check a bit less strict

It now allows:
* get_model(...) calls with one parameter only
* allows the variable name to be not the same as the model name itself (this will become a warning)
This commit is contained in:
David-Wobrock authored and David Wobrock committed 2020-11-26 19:02:57 +01:00
1 parent 5e26d9a910
commit a68ff7c306
3 files changed
+47 -4

No files matched your search

+1
View File
@@ -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
+4 -4
View File
@@ -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
)
+42
View File
@@ -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))