|
| 1 | +from django.core.checks import Error |
| 2 | +from django.db import connection, models |
| 3 | +from django.test import SimpleTestCase |
| 4 | +from django.test.utils import isolate_apps |
| 5 | + |
| 6 | +from django_mongodb.validation import DatabaseValidation |
| 7 | + |
| 8 | + |
| 9 | +@isolate_apps("invalid_models_tests") |
| 10 | +class AutoFieldTests(SimpleTestCase): |
| 11 | + def test_autofield_prohibited(self): |
| 12 | + class Model(models.Model): |
| 13 | + id = models.AutoField(primary_key=True) |
| 14 | + |
| 15 | + field = Model._meta.get_field("id") |
| 16 | + validator = DatabaseValidation(connection=connection) |
| 17 | + self.assertEqual( |
| 18 | + validator.check_field(field), |
| 19 | + [ |
| 20 | + Error( |
| 21 | + "MongoDB does not support AutoField.", |
| 22 | + hint="Use django_mongodb.fields.ObjectIdAutoField instead.", |
| 23 | + obj=field, |
| 24 | + id="mongodb.E001", |
| 25 | + ) |
| 26 | + ], |
| 27 | + ) |
| 28 | + |
| 29 | + def test_bigautofield_prohibited(self): |
| 30 | + class Model(models.Model): |
| 31 | + id = models.BigAutoField(primary_key=True) |
| 32 | + |
| 33 | + field = Model._meta.get_field("id") |
| 34 | + validator = DatabaseValidation(connection=connection) |
| 35 | + self.assertEqual( |
| 36 | + validator.check_field(field), |
| 37 | + [ |
| 38 | + Error( |
| 39 | + "MongoDB does not support BigAutoField.", |
| 40 | + hint="Use django_mongodb.fields.ObjectIdAutoField instead.", |
| 41 | + obj=field, |
| 42 | + id="mongodb.E001", |
| 43 | + ) |
| 44 | + ], |
| 45 | + ) |
| 46 | + |
| 47 | + def test_smallautofield_prohibited(self): |
| 48 | + class Model(models.Model): |
| 49 | + id = models.SmallAutoField(primary_key=True) |
| 50 | + |
| 51 | + field = Model._meta.get_field("id") |
| 52 | + validator = DatabaseValidation(connection=connection) |
| 53 | + self.assertEqual( |
| 54 | + validator.check_field(field), |
| 55 | + [ |
| 56 | + Error( |
| 57 | + "MongoDB does not support SmallAutoField.", |
| 58 | + hint="Use django_mongodb.fields.ObjectIdAutoField instead.", |
| 59 | + obj=field, |
| 60 | + id="mongodb.E001", |
| 61 | + ) |
| 62 | + ], |
| 63 | + ) |
0 commit comments