Skip to content

Commit

Permalink
add system check to prohibit AutoField
Browse files Browse the repository at this point in the history
  • Loading branch information
timgraham committed Nov 6, 2024
1 parent 245e487 commit ba4c521
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
2 changes: 2 additions & 0 deletions django_mongodb/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .query_utils import regex_match
from .schema import DatabaseSchemaEditor
from .utils import OperationDebugWrapper
from .validation import DatabaseValidation


class Cursor:
Expand Down Expand Up @@ -128,6 +129,7 @@ def _isnull_operator(a, b):
features_class = DatabaseFeatures
introspection_class = DatabaseIntrospection
ops_class = DatabaseOperations
validation_class = DatabaseValidation

def get_collection(self, name, **kwargs):
collection = Collection(self.database, name, **kwargs)
Expand Down
20 changes: 20 additions & 0 deletions django_mongodb/validation.py
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.
63 changes: 63 additions & 0 deletions tests/invalid_models_tests_/test_autofield.py
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",
)
],
)

0 comments on commit ba4c521

Please sign in to comment.