11import itertools
22
3- from django .db import connection
4- from django .test import TransactionTestCase
3+ from django .db import connection , models
4+ from django .test import TransactionTestCase , ignore_warnings
5+ from django .test .utils import isolate_apps
6+ from django .utils .deprecation import RemovedInDjango51Warning
7+
8+ from django_mongodb_backend .fields import EmbeddedModelField
59
610from .models import Address , Author , Book , new_apps
711
@@ -199,6 +203,54 @@ def test_unique(self):
199203 editor .delete_model (Book )
200204 self .assertTableNotExists (Author )
201205
206+ @ignore_warnings (category = RemovedInDjango51Warning )
207+ @isolate_apps ("schema_" )
208+ def test_index_together (self ):
209+ """Meta.index_together on an embedded model."""
210+
211+ class Address (models .Model ):
212+ index_together_one = models .CharField (max_length = 10 )
213+ index_together_two = models .CharField (max_length = 10 )
214+
215+ class Meta :
216+ app_label = "schema"
217+ index_together = [("index_together_one" , "index_together_two" )]
218+
219+ class Author (models .Model ):
220+ address = EmbeddedModelField (Address )
221+ index_together_three = models .CharField (max_length = 10 )
222+ index_together_four = models .CharField (max_length = 10 )
223+
224+ class Meta :
225+ app_label = "schema"
226+ index_together = [("index_together_three" , "index_together_four" )]
227+
228+ class Book (models .Model ):
229+ author = EmbeddedModelField (Author )
230+
231+ class Meta :
232+ app_label = "schema"
233+
234+ with connection .schema_editor () as editor :
235+ editor .create_model (Book )
236+ self .assertTableExists (Book )
237+ # Embedded uniques are created.
238+ self .assertEqual (
239+ self .get_constraints_for_columns (
240+ Book , ["author.address.index_together_one" , "author.address.index_together_two" ]
241+ ),
242+ ["schema_addr_index_t_a0305a_idx" ],
243+ )
244+ self .assertEqual (
245+ self .get_constraints_for_columns (
246+ Book ,
247+ ["author.index_together_three" , "author.index_together_four" ],
248+ ),
249+ ["schema_auth_index_t_65817b_idx" ],
250+ )
251+ editor .delete_model (Author )
252+ self .assertTableNotExists (Author )
253+
202254 def test_unique_together (self ):
203255 """Meta.unique_together on an embedded model."""
204256 with connection .schema_editor () as editor :
@@ -225,7 +277,7 @@ def test_unique_together(self):
225277 editor .delete_model (Author )
226278 self .assertTableNotExists (Author )
227279
228- def test_index (self ):
280+ def test_indexes (self ):
229281 """Meta.indexes on an embedded model."""
230282 with connection .schema_editor () as editor :
231283 editor .create_model (Book )
@@ -244,3 +296,23 @@ def test_index(self):
244296 )
245297 editor .delete_model (Author )
246298 self .assertTableNotExists (Author )
299+
300+ def test_constraints (self ):
301+ """Meta.constraints on an embedded model."""
302+ with connection .schema_editor () as editor :
303+ editor .create_model (Book )
304+ self .assertTableExists (Book )
305+ # Embedded uniques are created.
306+ self .assertEqual (
307+ self .get_constraints_for_columns (Book , ["author.unique_constraint_two" ]),
308+ ["unique_two" ],
309+ )
310+ self .assertEqual (
311+ self .get_constraints_for_columns (
312+ Book ,
313+ ["author.address.unique_constraint_one" ],
314+ ),
315+ ["unique_one" ],
316+ )
317+ editor .delete_model (Author )
318+ self .assertTableNotExists (Author )
0 commit comments