-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add system check to prohibit AutoField
- Loading branch information
Showing
4 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from django.core import checks | ||
from django.db.backends.base.validation import BaseDatabaseValidation | ||
|
||
|
||
class DatabaseValidation(BaseDatabaseValidation): | ||
prohibited_fields = {"AutoField", "BigAutoField", "SmallAutoField"} | ||
|
||
def check_field_type(self, field, field_type): | ||
"""Prohibit AutoField on MongoDB.""" | ||
errors = [] | ||
if field.get_internal_type() in self.prohibited_fields: | ||
errors.append( | ||
checks.Error( | ||
f"{self.connection.display_name} does not support {field.__class__.__name__}.", | ||
obj=field, | ||
hint="Use django_mongodb.fields.ObjectIdAutoField instead.", | ||
id="mongodb.E001", | ||
) | ||
) | ||
return errors |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from django.core.checks import Error | ||
from django.db import connection, models | ||
from django.test import SimpleTestCase | ||
from django.test.utils import isolate_apps | ||
|
||
from django_mongodb.validation import DatabaseValidation | ||
|
||
|
||
@isolate_apps("invalid_models_tests") | ||
class ProhibitedFieldTests(SimpleTestCase): | ||
def test_autofield(self): | ||
class Model(models.Model): | ||
id = models.AutoField(primary_key=True) | ||
|
||
field = Model._meta.get_field("id") | ||
validator = DatabaseValidation(connection=connection) | ||
self.assertEqual( | ||
validator.check_field(field), | ||
[ | ||
Error( | ||
"MongoDB does not support AutoField.", | ||
hint="Use django_mongodb.fields.ObjectIdAutoField instead.", | ||
obj=field, | ||
id="mongodb.E001", | ||
) | ||
], | ||
) | ||
|
||
def test_bigautofield(self): | ||
class Model(models.Model): | ||
id = models.BigAutoField(primary_key=True) | ||
|
||
field = Model._meta.get_field("id") | ||
validator = DatabaseValidation(connection=connection) | ||
self.assertEqual( | ||
validator.check_field(field), | ||
[ | ||
Error( | ||
"MongoDB does not support BigAutoField.", | ||
hint="Use django_mongodb.fields.ObjectIdAutoField instead.", | ||
obj=field, | ||
id="mongodb.E001", | ||
) | ||
], | ||
) | ||
|
||
def test_smallautofield(self): | ||
class Model(models.Model): | ||
id = models.SmallAutoField(primary_key=True) | ||
|
||
field = Model._meta.get_field("id") | ||
validator = DatabaseValidation(connection=connection) | ||
self.assertEqual( | ||
validator.check_field(field), | ||
[ | ||
Error( | ||
"MongoDB does not support SmallAutoField.", | ||
hint="Use django_mongodb.fields.ObjectIdAutoField instead.", | ||
obj=field, | ||
id="mongodb.E001", | ||
) | ||
], | ||
) |