test_state.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. from django.apps.registry import Apps
  2. from django.db import models
  3. from django.db.migrations.state import ProjectState, ModelState, InvalidBasesError
  4. from django.test import TestCase
  5. class StateTests(TestCase):
  6. """
  7. Tests state construction, rendering and modification by operations.
  8. """
  9. def test_create(self):
  10. """
  11. Tests making a ProjectState from an Apps
  12. """
  13. new_apps = Apps(["migrations"])
  14. class Author(models.Model):
  15. name = models.CharField(max_length=255)
  16. bio = models.TextField()
  17. age = models.IntegerField(blank=True, null=True)
  18. class Meta:
  19. app_label = "migrations"
  20. apps = new_apps
  21. unique_together = ["name", "bio"]
  22. class AuthorProxy(Author):
  23. class Meta:
  24. app_label = "migrations"
  25. apps = new_apps
  26. proxy = True
  27. ordering = ["name"]
  28. class Book(models.Model):
  29. title = models.CharField(max_length=1000)
  30. author = models.ForeignKey(Author)
  31. contributors = models.ManyToManyField(Author)
  32. class Meta:
  33. app_label = "migrations"
  34. apps = new_apps
  35. verbose_name = "tome"
  36. db_table = "test_tome"
  37. project_state = ProjectState.from_apps(new_apps)
  38. author_state = project_state.models['migrations', 'author']
  39. author_proxy_state = project_state.models['migrations', 'authorproxy']
  40. book_state = project_state.models['migrations', 'book']
  41. self.assertEqual(author_state.app_label, "migrations")
  42. self.assertEqual(author_state.name, "Author")
  43. self.assertEqual([x for x, y in author_state.fields], ["id", "name", "bio", "age"])
  44. self.assertEqual(author_state.fields[1][1].max_length, 255)
  45. self.assertEqual(author_state.fields[2][1].null, False)
  46. self.assertEqual(author_state.fields[3][1].null, True)
  47. self.assertEqual(author_state.options, {"unique_together": {("name", "bio")}})
  48. self.assertEqual(author_state.bases, (models.Model, ))
  49. self.assertEqual(book_state.app_label, "migrations")
  50. self.assertEqual(book_state.name, "Book")
  51. self.assertEqual([x for x, y in book_state.fields], ["id", "title", "author", "contributors"])
  52. self.assertEqual(book_state.fields[1][1].max_length, 1000)
  53. self.assertEqual(book_state.fields[2][1].null, False)
  54. self.assertEqual(book_state.fields[3][1].__class__.__name__, "ManyToManyField")
  55. self.assertEqual(book_state.options, {"verbose_name": "tome", "db_table": "test_tome"})
  56. self.assertEqual(book_state.bases, (models.Model, ))
  57. self.assertEqual(author_proxy_state.app_label, "migrations")
  58. self.assertEqual(author_proxy_state.name, "AuthorProxy")
  59. self.assertEqual(author_proxy_state.fields, [])
  60. self.assertEqual(author_proxy_state.options, {"proxy": True, "ordering": ["name"]})
  61. self.assertEqual(author_proxy_state.bases, ("migrations.author", ))
  62. def test_render(self):
  63. """
  64. Tests rendering a ProjectState into an Apps.
  65. """
  66. project_state = ProjectState()
  67. project_state.add_model_state(ModelState(
  68. "migrations",
  69. "Tag",
  70. [
  71. ("id", models.AutoField(primary_key=True)),
  72. ("name", models.CharField(max_length=100)),
  73. ("hidden", models.BooleanField()),
  74. ],
  75. {},
  76. None,
  77. ))
  78. new_apps = project_state.render()
  79. self.assertEqual(new_apps.get_model("migrations", "Tag")._meta.get_field_by_name("name")[0].max_length, 100)
  80. self.assertEqual(new_apps.get_model("migrations", "Tag")._meta.get_field_by_name("hidden")[0].null, False)
  81. def test_render_model_inheritance(self):
  82. class Book(models.Model):
  83. title = models.CharField(max_length=1000)
  84. class Meta:
  85. app_label = "migrations"
  86. apps = Apps()
  87. class Novel(Book):
  88. class Meta:
  89. app_label = "migrations"
  90. apps = Apps()
  91. # First, test rendering individually
  92. apps = Apps(["migrations"])
  93. # We shouldn't be able to render yet
  94. ms = ModelState.from_model(Novel)
  95. with self.assertRaises(InvalidBasesError):
  96. ms.render(apps)
  97. # Once the parent model is in the app registry, it should be fine
  98. ModelState.from_model(Book).render(apps)
  99. ModelState.from_model(Novel).render(apps)
  100. def test_render_model_with_multiple_inheritance(self):
  101. class Foo(models.Model):
  102. class Meta:
  103. app_label = "migrations"
  104. apps = Apps()
  105. class Bar(models.Model):
  106. class Meta:
  107. app_label = "migrations"
  108. apps = Apps()
  109. class FooBar(Foo, Bar):
  110. class Meta:
  111. app_label = "migrations"
  112. apps = Apps()
  113. apps = Apps(["migrations"])
  114. # We shouldn't be able to render yet
  115. ms = ModelState.from_model(FooBar)
  116. with self.assertRaises(InvalidBasesError):
  117. ms.render(apps)
  118. # Once the parent models are in the app registry, it should be fine
  119. ModelState.from_model(Foo).render(apps)
  120. ModelState.from_model(Bar).render(apps)
  121. ModelState.from_model(FooBar).render(apps)
  122. def test_render_project_dependencies(self):
  123. """
  124. Tests that the ProjectState render method correctly renders models
  125. to account for inter-model base dependencies.
  126. """
  127. new_apps = Apps()
  128. class A(models.Model):
  129. class Meta:
  130. app_label = "migrations"
  131. apps = new_apps
  132. class B(A):
  133. class Meta:
  134. app_label = "migrations"
  135. apps = new_apps
  136. class C(B):
  137. class Meta:
  138. app_label = "migrations"
  139. apps = new_apps
  140. class D(A):
  141. class Meta:
  142. app_label = "migrations"
  143. apps = new_apps
  144. class E(B):
  145. class Meta:
  146. app_label = "migrations"
  147. apps = new_apps
  148. proxy = True
  149. class F(D):
  150. class Meta:
  151. app_label = "migrations"
  152. apps = new_apps
  153. proxy = True
  154. # Make a ProjectState and render it
  155. project_state = ProjectState()
  156. project_state.add_model_state(ModelState.from_model(A))
  157. project_state.add_model_state(ModelState.from_model(B))
  158. project_state.add_model_state(ModelState.from_model(C))
  159. project_state.add_model_state(ModelState.from_model(D))
  160. project_state.add_model_state(ModelState.from_model(E))
  161. project_state.add_model_state(ModelState.from_model(F))
  162. final_apps = project_state.render()
  163. self.assertEqual(len(final_apps.get_models()), 6)
  164. # Now make an invalid ProjectState and make sure it fails
  165. project_state = ProjectState()
  166. project_state.add_model_state(ModelState.from_model(A))
  167. project_state.add_model_state(ModelState.from_model(B))
  168. project_state.add_model_state(ModelState.from_model(C))
  169. project_state.add_model_state(ModelState.from_model(F))
  170. with self.assertRaises(InvalidBasesError):
  171. project_state.render()
  172. def test_equality(self):
  173. """
  174. Tests that == and != are implemented correctly.
  175. """
  176. # Test two things that should be equal
  177. project_state = ProjectState()
  178. project_state.add_model_state(ModelState(
  179. "migrations",
  180. "Tag",
  181. [
  182. ("id", models.AutoField(primary_key=True)),
  183. ("name", models.CharField(max_length=100)),
  184. ("hidden", models.BooleanField()),
  185. ],
  186. {},
  187. None,
  188. ))
  189. other_state = project_state.clone()
  190. self.assertEqual(project_state, project_state)
  191. self.assertEqual(project_state, other_state)
  192. self.assertEqual(project_state != project_state, False)
  193. self.assertEqual(project_state != other_state, False)
  194. # Make a very small change (max_len 99) and see if that affects it
  195. project_state = ProjectState()
  196. project_state.add_model_state(ModelState(
  197. "migrations",
  198. "Tag",
  199. [
  200. ("id", models.AutoField(primary_key=True)),
  201. ("name", models.CharField(max_length=99)),
  202. ("hidden", models.BooleanField()),
  203. ],
  204. {},
  205. None,
  206. ))
  207. self.assertNotEqual(project_state, other_state)
  208. self.assertEqual(project_state == other_state, False)