123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- from django.apps.registry import Apps
- from django.db import models
- from django.db.migrations.state import ProjectState, ModelState, InvalidBasesError
- from django.test import TestCase
- class StateTests(TestCase):
- """
- Tests state construction, rendering and modification by operations.
- """
- def test_create(self):
- """
- Tests making a ProjectState from an Apps
- """
- new_apps = Apps(["migrations"])
- class Author(models.Model):
- name = models.CharField(max_length=255)
- bio = models.TextField()
- age = models.IntegerField(blank=True, null=True)
- class Meta:
- app_label = "migrations"
- apps = new_apps
- unique_together = ["name", "bio"]
- class AuthorProxy(Author):
- class Meta:
- app_label = "migrations"
- apps = new_apps
- proxy = True
- ordering = ["name"]
- class Book(models.Model):
- title = models.CharField(max_length=1000)
- author = models.ForeignKey(Author)
- contributors = models.ManyToManyField(Author)
- class Meta:
- app_label = "migrations"
- apps = new_apps
- verbose_name = "tome"
- db_table = "test_tome"
- project_state = ProjectState.from_apps(new_apps)
- author_state = project_state.models['migrations', 'author']
- author_proxy_state = project_state.models['migrations', 'authorproxy']
- book_state = project_state.models['migrations', 'book']
- self.assertEqual(author_state.app_label, "migrations")
- self.assertEqual(author_state.name, "Author")
- self.assertEqual([x for x, y in author_state.fields], ["id", "name", "bio", "age"])
- self.assertEqual(author_state.fields[1][1].max_length, 255)
- self.assertEqual(author_state.fields[2][1].null, False)
- self.assertEqual(author_state.fields[3][1].null, True)
- self.assertEqual(author_state.options, {"unique_together": {("name", "bio")}})
- self.assertEqual(author_state.bases, (models.Model, ))
- self.assertEqual(book_state.app_label, "migrations")
- self.assertEqual(book_state.name, "Book")
- self.assertEqual([x for x, y in book_state.fields], ["id", "title", "author", "contributors"])
- self.assertEqual(book_state.fields[1][1].max_length, 1000)
- self.assertEqual(book_state.fields[2][1].null, False)
- self.assertEqual(book_state.fields[3][1].__class__.__name__, "ManyToManyField")
- self.assertEqual(book_state.options, {"verbose_name": "tome", "db_table": "test_tome"})
- self.assertEqual(book_state.bases, (models.Model, ))
- self.assertEqual(author_proxy_state.app_label, "migrations")
- self.assertEqual(author_proxy_state.name, "AuthorProxy")
- self.assertEqual(author_proxy_state.fields, [])
- self.assertEqual(author_proxy_state.options, {"proxy": True, "ordering": ["name"]})
- self.assertEqual(author_proxy_state.bases, ("migrations.author", ))
- def test_render(self):
- """
- Tests rendering a ProjectState into an Apps.
- """
- project_state = ProjectState()
- project_state.add_model_state(ModelState(
- "migrations",
- "Tag",
- [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=100)),
- ("hidden", models.BooleanField()),
- ],
- {},
- None,
- ))
- new_apps = project_state.render()
- self.assertEqual(new_apps.get_model("migrations", "Tag")._meta.get_field_by_name("name")[0].max_length, 100)
- self.assertEqual(new_apps.get_model("migrations", "Tag")._meta.get_field_by_name("hidden")[0].null, False)
- def test_render_model_inheritance(self):
- class Book(models.Model):
- title = models.CharField(max_length=1000)
- class Meta:
- app_label = "migrations"
- apps = Apps()
- class Novel(Book):
- class Meta:
- app_label = "migrations"
- apps = Apps()
- # First, test rendering individually
- apps = Apps(["migrations"])
- # We shouldn't be able to render yet
- ms = ModelState.from_model(Novel)
- with self.assertRaises(InvalidBasesError):
- ms.render(apps)
- # Once the parent model is in the app registry, it should be fine
- ModelState.from_model(Book).render(apps)
- ModelState.from_model(Novel).render(apps)
- def test_render_model_with_multiple_inheritance(self):
- class Foo(models.Model):
- class Meta:
- app_label = "migrations"
- apps = Apps()
- class Bar(models.Model):
- class Meta:
- app_label = "migrations"
- apps = Apps()
- class FooBar(Foo, Bar):
- class Meta:
- app_label = "migrations"
- apps = Apps()
- apps = Apps(["migrations"])
- # We shouldn't be able to render yet
- ms = ModelState.from_model(FooBar)
- with self.assertRaises(InvalidBasesError):
- ms.render(apps)
- # Once the parent models are in the app registry, it should be fine
- ModelState.from_model(Foo).render(apps)
- ModelState.from_model(Bar).render(apps)
- ModelState.from_model(FooBar).render(apps)
- def test_render_project_dependencies(self):
- """
- Tests that the ProjectState render method correctly renders models
- to account for inter-model base dependencies.
- """
- new_apps = Apps()
- class A(models.Model):
- class Meta:
- app_label = "migrations"
- apps = new_apps
- class B(A):
- class Meta:
- app_label = "migrations"
- apps = new_apps
- class C(B):
- class Meta:
- app_label = "migrations"
- apps = new_apps
- class D(A):
- class Meta:
- app_label = "migrations"
- apps = new_apps
- class E(B):
- class Meta:
- app_label = "migrations"
- apps = new_apps
- proxy = True
- class F(D):
- class Meta:
- app_label = "migrations"
- apps = new_apps
- proxy = True
- # Make a ProjectState and render it
- project_state = ProjectState()
- project_state.add_model_state(ModelState.from_model(A))
- project_state.add_model_state(ModelState.from_model(B))
- project_state.add_model_state(ModelState.from_model(C))
- project_state.add_model_state(ModelState.from_model(D))
- project_state.add_model_state(ModelState.from_model(E))
- project_state.add_model_state(ModelState.from_model(F))
- final_apps = project_state.render()
- self.assertEqual(len(final_apps.get_models()), 6)
- # Now make an invalid ProjectState and make sure it fails
- project_state = ProjectState()
- project_state.add_model_state(ModelState.from_model(A))
- project_state.add_model_state(ModelState.from_model(B))
- project_state.add_model_state(ModelState.from_model(C))
- project_state.add_model_state(ModelState.from_model(F))
- with self.assertRaises(InvalidBasesError):
- project_state.render()
- def test_equality(self):
- """
- Tests that == and != are implemented correctly.
- """
- # Test two things that should be equal
- project_state = ProjectState()
- project_state.add_model_state(ModelState(
- "migrations",
- "Tag",
- [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=100)),
- ("hidden", models.BooleanField()),
- ],
- {},
- None,
- ))
- other_state = project_state.clone()
- self.assertEqual(project_state, project_state)
- self.assertEqual(project_state, other_state)
- self.assertEqual(project_state != project_state, False)
- self.assertEqual(project_state != other_state, False)
- # Make a very small change (max_len 99) and see if that affects it
- project_state = ProjectState()
- project_state.add_model_state(ModelState(
- "migrations",
- "Tag",
- [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=99)),
- ("hidden", models.BooleanField()),
- ],
- {},
- None,
- ))
- self.assertNotEqual(project_state, other_state)
- self.assertEqual(project_state == other_state, False)
|