1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Project/ModelState now correctly serialize multi-model inheritance

This commit is contained in:
Andrew Godwin
2013-09-06 12:14:09 -05:00
parent 6f7977bb63
commit cdeff3acc2
2 changed files with 104 additions and 5 deletions

View File

@@ -25,6 +25,13 @@ class StateTests(TestCase):
app_cache = new_app_cache
unique_together = ["name", "bio"]
class AuthorProxy(Author):
class Meta:
app_label = "migrations"
app_cache = new_app_cache
proxy = True
ordering = ["name"]
class Book(models.Model):
title = models.CharField(max_length=1000)
author = models.ForeignKey(Author)
@@ -36,6 +43,7 @@ class StateTests(TestCase):
project_state = ProjectState.from_app_cache(new_app_cache)
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")
@@ -54,6 +62,12 @@ class StateTests(TestCase):
self.assertEqual(book_state.fields[2][1].null, False)
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):
"""
@@ -92,5 +106,63 @@ class StateTests(TestCase):
app_label = "migrations"
app_cache = new_app_cache
# First, test rendering individually
yet_another_app_cache = BaseAppCache()
# We shouldn't be able to render yet
with self.assertRaises(ValueError):
ModelState.from_model(Novel).render(yet_another_app_cache)
# Once the parent model is in the app cache, it should be fine
ModelState.from_model(Book).render(yet_another_app_cache)
ModelState.from_model(Novel).render(yet_another_app_cache)
def test_render_project_dependencies(self):
"""
Tests that the ProjectState render method correctly renders models
to account for inter-model base dependencies.
"""
new_app_cache = BaseAppCache()
class A(models.Model):
class Meta:
app_label = "migrations"
app_cache = new_app_cache
class B(A):
class Meta:
app_label = "migrations"
app_cache = new_app_cache
class C(B):
class Meta:
app_label = "migrations"
app_cache = new_app_cache
class D(A):
class Meta:
app_label = "migrations"
app_cache = new_app_cache
class E(B):
class Meta:
app_label = "migrations"
app_cache = new_app_cache
proxy = True
class F(D):
class Meta:
app_label = "migrations"
app_cache = new_app_cache
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_app_cache = project_state.render()
self.assertEqual(len(final_app_cache.get_models()), 6)