From 6f44d292371430c397125df4eb383ad806dd7fad Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Fri, 22 Nov 2024 17:31:05 -0500 Subject: [PATCH] refactor tests --- tests/schema_/models.py | 10 --- tests/schema_/test_embedded_model.py | 101 +++++++++++++++++++++++---- 2 files changed, 88 insertions(+), 23 deletions(-) diff --git a/tests/schema_/models.py b/tests/schema_/models.py index 6447c118..af012ceb 100644 --- a/tests/schema_/models.py +++ b/tests/schema_/models.py @@ -15,16 +15,11 @@ class Address(models.Model): state = models.CharField(max_length=2) zip_code = models.IntegerField(db_index=True) uid = models.IntegerField(unique=True) - unique_together_one = models.CharField(max_length=10) - unique_together_two = models.CharField(max_length=10) indexed_by_index_one = models.CharField(max_length=10) - unique_constraint_one = models.CharField(max_length=10) class Meta: apps = new_apps - constraints = [models.UniqueConstraint(fields=["unique_constraint_one"], name="unique_one")] indexes = [models.Index(fields=["indexed_by_index_one"])] - unique_together = [("unique_together_one", "unique_together_two")] class Author(models.Model): @@ -32,16 +27,11 @@ class Author(models.Model): age = models.IntegerField(db_index=True) address = EmbeddedModelField(Address) employee_id = models.IntegerField(unique=True) - unique_together_three = models.CharField(max_length=10) - unique_together_four = models.CharField(max_length=10) indexed_by_index_two = models.CharField(max_length=10) - unique_constraint_two = models.CharField(max_length=10) class Meta: apps = new_apps - constraints = [models.UniqueConstraint(fields=["unique_constraint_two"], name="unique_two")] indexes = [models.Index(fields=["indexed_by_index_two"])] - unique_together = [("unique_together_three", "unique_together_four")] class Book(models.Model): diff --git a/tests/schema_/test_embedded_model.py b/tests/schema_/test_embedded_model.py index 6189e9d2..ba9b9788 100644 --- a/tests/schema_/test_embedded_model.py +++ b/tests/schema_/test_embedded_model.py @@ -213,7 +213,7 @@ class Address(models.Model): index_together_two = models.CharField(max_length=10) class Meta: - app_label = "schema" + app_label = "schema_" index_together = [("index_together_one", "index_together_two")] class Author(models.Model): @@ -222,14 +222,14 @@ class Author(models.Model): index_together_four = models.CharField(max_length=10) class Meta: - app_label = "schema" + app_label = "schema_" index_together = [("index_together_three", "index_together_four")] class Book(models.Model): author = EmbeddedModelField(Author) class Meta: - app_label = "schema" + app_label = "schema_" with connection.schema_editor() as editor: editor.create_model(Book) @@ -239,20 +239,45 @@ class Meta: self.get_constraints_for_columns( Book, ["author.address.index_together_one", "author.address.index_together_two"] ), - ["schema_addr_index_t_a0305a_idx"], + ["schema__add_index_t_efa93e_idx"], ) self.assertEqual( self.get_constraints_for_columns( Book, ["author.index_together_three", "author.index_together_four"], ), - ["schema_auth_index_t_65817b_idx"], + ["schema__aut_index_t_df32aa_idx"], ) - editor.delete_model(Author) - self.assertTableNotExists(Author) + editor.delete_model(Book) + self.assertTableNotExists(Book) + @isolate_apps("schema_") def test_unique_together(self): """Meta.unique_together on an embedded model.""" + + class Address(models.Model): + unique_together_one = models.CharField(max_length=10) + unique_together_two = models.CharField(max_length=10) + + class Meta: + app_label = "schema_" + unique_together = [("unique_together_one", "unique_together_two")] + + class Author(models.Model): + address = EmbeddedModelField(Address) + unique_together_three = models.CharField(max_length=10) + unique_together_four = models.CharField(max_length=10) + + class Meta: + app_label = "schema_" + unique_together = [("unique_together_three", "unique_together_four")] + + class Book(models.Model): + author = EmbeddedModelField(Author) + + class Meta: + app_label = "schema_" + with connection.schema_editor() as editor: editor.create_model(Book) self.assertTableExists(Book) @@ -274,31 +299,81 @@ def test_unique_together(self): "schema__address_author.address.unique_together_one_author.address.unique_together_two_de682e30_uniq" ], ) - editor.delete_model(Author) - self.assertTableNotExists(Author) + editor.delete_model(Book) + self.assertTableNotExists(Book) + @isolate_apps("schema_") def test_indexes(self): """Meta.indexes on an embedded model.""" + + class Address(models.Model): + indexed_one = models.CharField(max_length=10) + + class Meta: + app_label = "schema_" + indexes = [models.Index(fields=["indexed_one"])] + + class Author(models.Model): + address = EmbeddedModelField(Address) + indexed_two = models.CharField(max_length=10) + + class Meta: + app_label = "schema_" + indexes = [models.Index(fields=["indexed_two"])] + + class Book(models.Model): + author = EmbeddedModelField(Author) + + class Meta: + app_label = "schema_" + with connection.schema_editor() as editor: editor.create_model(Book) self.assertTableExists(Book) # Embedded uniques are created. self.assertEqual( - self.get_constraints_for_columns(Book, ["author.indexed_by_index_two"]), - ["schema__aut_indexed_7e3a5c_idx"], + self.get_constraints_for_columns(Book, ["author.indexed_two"]), + ["schema__aut_indexed_b19137_idx"], ) self.assertEqual( self.get_constraints_for_columns( Book, - ["author.address.indexed_by_index_one"], + ["author.address.indexed_one"], ), - ["schema__add_indexed_ef5dd6_idx"], + ["schema__add_indexed_b64972_idx"], ) editor.delete_model(Author) self.assertTableNotExists(Author) + @isolate_apps("schema_") def test_constraints(self): """Meta.constraints on an embedded model.""" + + class Address(models.Model): + unique_constraint_one = models.CharField(max_length=10) + + class Meta: + app_label = "schema_" + constraints = [ + models.UniqueConstraint(fields=["unique_constraint_one"], name="unique_one") + ] + + class Author(models.Model): + address = EmbeddedModelField(Address) + unique_constraint_two = models.CharField(max_length=10) + + class Meta: + app_label = "schema_" + constraints = [ + models.UniqueConstraint(fields=["unique_constraint_two"], name="unique_two") + ] + + class Book(models.Model): + author = EmbeddedModelField(Author) + + class Meta: + app_label = "schema_" + with connection.schema_editor() as editor: editor.create_model(Book) self.assertTableExists(Book)