2013-05-18 19:49:56 +08:00
|
|
|
from django.test import TestCase
|
|
|
|
from django.db import models
|
|
|
|
from django.db.models.loading import BaseAppCache
|
2013-09-07 01:18:24 +08:00
|
|
|
from django.db.migrations.state import ProjectState, ModelState, InvalidBasesError
|
2013-05-18 19:49:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
class StateTests(TestCase):
|
|
|
|
"""
|
|
|
|
Tests state construction, rendering and modification by operations.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def test_create(self):
|
|
|
|
"""
|
|
|
|
Tests making a ProjectState from an AppCache
|
|
|
|
"""
|
2013-05-19 00:30:34 +08:00
|
|
|
|
2013-05-18 19:49:56 +08:00
|
|
|
new_app_cache = BaseAppCache()
|
|
|
|
|
|
|
|
class Author(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
bio = models.TextField()
|
|
|
|
age = models.IntegerField(blank=True, null=True)
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2013-05-18 19:49:56 +08:00
|
|
|
class Meta:
|
|
|
|
app_label = "migrations"
|
|
|
|
app_cache = new_app_cache
|
2013-05-19 18:35:17 +08:00
|
|
|
unique_together = ["name", "bio"]
|
2013-05-18 19:49:56 +08:00
|
|
|
|
2013-09-07 01:14:09 +08:00
|
|
|
class AuthorProxy(Author):
|
|
|
|
class Meta:
|
|
|
|
app_label = "migrations"
|
|
|
|
app_cache = new_app_cache
|
|
|
|
proxy = True
|
|
|
|
ordering = ["name"]
|
|
|
|
|
2013-05-18 19:49:56 +08:00
|
|
|
class Book(models.Model):
|
|
|
|
title = models.CharField(max_length=1000)
|
|
|
|
author = models.ForeignKey(Author)
|
2013-11-27 23:28:33 +08:00
|
|
|
contributors = models.ManyToManyField(Author)
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2013-05-18 19:49:56 +08:00
|
|
|
class Meta:
|
|
|
|
app_label = "migrations"
|
|
|
|
app_cache = new_app_cache
|
2013-05-19 18:35:17 +08:00
|
|
|
verbose_name = "tome"
|
|
|
|
db_table = "test_tome"
|
2013-05-18 19:49:56 +08:00
|
|
|
|
|
|
|
project_state = ProjectState.from_app_cache(new_app_cache)
|
|
|
|
author_state = project_state.models['migrations', 'author']
|
2013-09-07 01:14:09 +08:00
|
|
|
author_proxy_state = project_state.models['migrations', 'authorproxy']
|
2013-05-18 19:49:56 +08:00
|
|
|
book_state = project_state.models['migrations', 'book']
|
2013-10-11 04:42:30 +08:00
|
|
|
|
2013-05-18 19:49:56 +08:00
|
|
|
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)
|
2013-07-02 18:19:02 +08:00
|
|
|
self.assertEqual(author_state.options, {"unique_together": set(("name", "bio"))})
|
2013-05-18 19:49:56 +08:00
|
|
|
self.assertEqual(author_state.bases, (models.Model, ))
|
2013-10-11 04:42:30 +08:00
|
|
|
|
2013-05-19 00:30:34 +08:00
|
|
|
self.assertEqual(book_state.app_label, "migrations")
|
|
|
|
self.assertEqual(book_state.name, "Book")
|
2013-11-27 23:28:33 +08:00
|
|
|
self.assertEqual([x for x, y in book_state.fields], ["id", "title", "author", "contributors"])
|
2013-05-19 00:30:34 +08:00
|
|
|
self.assertEqual(book_state.fields[1][1].max_length, 1000)
|
|
|
|
self.assertEqual(book_state.fields[2][1].null, False)
|
2013-11-27 23:28:33 +08:00
|
|
|
self.assertEqual(book_state.fields[3][1].__class__.__name__, "ManyToManyField")
|
2013-05-19 18:35:17 +08:00
|
|
|
self.assertEqual(book_state.options, {"verbose_name": "tome", "db_table": "test_tome"})
|
2013-05-19 00:30:34 +08:00
|
|
|
self.assertEqual(book_state.bases, (models.Model, ))
|
2013-10-11 04:42:30 +08:00
|
|
|
|
2013-09-07 01:14:09 +08:00
|
|
|
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", ))
|
2013-05-19 00:30:34 +08:00
|
|
|
|
|
|
|
def test_render(self):
|
|
|
|
"""
|
|
|
|
Tests rendering a ProjectState into an AppCache.
|
|
|
|
"""
|
|
|
|
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_app_cache = project_state.render()
|
|
|
|
self.assertEqual(new_app_cache.get_model("migrations", "Tag")._meta.get_field_by_name("name")[0].max_length, 100)
|
|
|
|
self.assertEqual(new_app_cache.get_model("migrations", "Tag")._meta.get_field_by_name("hidden")[0].null, False)
|
2013-09-01 05:18:44 +08:00
|
|
|
|
2013-12-06 07:55:31 +08:00
|
|
|
def test_render_model_inheritance(self):
|
2013-09-01 05:18:44 +08:00
|
|
|
class Book(models.Model):
|
|
|
|
title = models.CharField(max_length=1000)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
app_label = "migrations"
|
2013-12-06 07:55:31 +08:00
|
|
|
app_cache = BaseAppCache()
|
2013-09-01 05:18:44 +08:00
|
|
|
|
|
|
|
class Novel(Book):
|
|
|
|
class Meta:
|
|
|
|
app_label = "migrations"
|
2013-12-06 07:55:31 +08:00
|
|
|
app_cache = BaseAppCache()
|
2013-09-01 05:18:44 +08:00
|
|
|
|
2013-09-07 01:14:09 +08:00
|
|
|
# First, test rendering individually
|
2013-12-06 07:55:31 +08:00
|
|
|
app_cache = BaseAppCache()
|
2013-09-07 01:14:09 +08:00
|
|
|
|
|
|
|
# We shouldn't be able to render yet
|
2013-12-06 07:55:31 +08:00
|
|
|
ms = ModelState.from_model(Novel)
|
|
|
|
with self.assertRaises(InvalidBasesError):
|
|
|
|
ms.render(app_cache)
|
2013-09-07 01:14:09 +08:00
|
|
|
|
|
|
|
# Once the parent model is in the app cache, it should be fine
|
2013-12-06 07:55:31 +08:00
|
|
|
ModelState.from_model(Book).render(app_cache)
|
|
|
|
ModelState.from_model(Novel).render(app_cache)
|
|
|
|
|
|
|
|
def test_render_model_with_multiple_inheritance(self):
|
|
|
|
class Foo(models.Model):
|
|
|
|
class Meta:
|
|
|
|
app_label = "migrations"
|
|
|
|
app_cache = BaseAppCache()
|
|
|
|
|
|
|
|
class Bar(models.Model):
|
|
|
|
class Meta:
|
|
|
|
app_label = "migrations"
|
|
|
|
app_cache = BaseAppCache()
|
|
|
|
|
|
|
|
class FooBar(Foo, Bar):
|
|
|
|
class Meta:
|
|
|
|
app_label = "migrations"
|
|
|
|
app_cache = BaseAppCache()
|
|
|
|
|
|
|
|
app_cache = BaseAppCache()
|
|
|
|
|
|
|
|
# We shouldn't be able to render yet
|
|
|
|
ms = ModelState.from_model(FooBar)
|
|
|
|
with self.assertRaises(InvalidBasesError):
|
|
|
|
ms.render(app_cache)
|
|
|
|
|
|
|
|
# Once the parent models are in the app cache, it should be fine
|
|
|
|
ModelState.from_model(Foo).render(app_cache)
|
|
|
|
ModelState.from_model(Bar).render(app_cache)
|
|
|
|
ModelState.from_model(FooBar).render(app_cache)
|
2013-09-07 01:14:09 +08:00
|
|
|
|
|
|
|
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)
|
2013-09-07 01:18:24 +08:00
|
|
|
|
|
|
|
# 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()
|
2013-09-25 20:47:46 +08:00
|
|
|
|
|
|
|
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)
|