diff options
| author | Florian Apolloner <florian@apolloner.eu> | 2013-02-26 09:53:47 +0100 |
|---|---|---|
| committer | Florian Apolloner <florian@apolloner.eu> | 2013-02-26 14:36:57 +0100 |
| commit | 89f40e36246100df6a11316c31a76712ebc6c501 (patch) | |
| tree | 6e65639683ddaf2027908d1ecb1739e0e2ff853b /tests/unmanaged_models | |
| parent | b3d2ccb5bfbaf6e7fe1f98843baaa48c35a70950 (diff) | |
Merged regressiontests and modeltests into the test root.
Diffstat (limited to 'tests/unmanaged_models')
| -rw-r--r-- | tests/unmanaged_models/__init__.py | 2 | ||||
| -rw-r--r-- | tests/unmanaged_models/models.py | 132 | ||||
| -rw-r--r-- | tests/unmanaged_models/tests.py | 61 |
3 files changed, 195 insertions, 0 deletions
diff --git a/tests/unmanaged_models/__init__.py b/tests/unmanaged_models/__init__.py new file mode 100644 index 0000000000..139597f9cb --- /dev/null +++ b/tests/unmanaged_models/__init__.py @@ -0,0 +1,2 @@ + + diff --git a/tests/unmanaged_models/models.py b/tests/unmanaged_models/models.py new file mode 100644 index 0000000000..0eef69977c --- /dev/null +++ b/tests/unmanaged_models/models.py @@ -0,0 +1,132 @@ +""" +Models can have a ``managed`` attribute, which specifies whether the SQL code +is generated for the table on various manage.py operations. +""" + +from django.db import models +from django.utils.encoding import python_2_unicode_compatible + +# All of these models are created in the database by Django. + +@python_2_unicode_compatible +class A01(models.Model): + f_a = models.CharField(max_length=10, db_index=True) + f_b = models.IntegerField() + + class Meta: + db_table = 'a01' + + def __str__(self): + return self.f_a + +@python_2_unicode_compatible +class B01(models.Model): + fk_a = models.ForeignKey(A01) + f_a = models.CharField(max_length=10, db_index=True) + f_b = models.IntegerField() + + class Meta: + db_table = 'b01' + # 'managed' is True by default. This tests we can set it explicitly. + managed = True + + def __str__(self): + return self.f_a + +@python_2_unicode_compatible +class C01(models.Model): + mm_a = models.ManyToManyField(A01, db_table='d01') + f_a = models.CharField(max_length=10, db_index=True) + f_b = models.IntegerField() + + class Meta: + db_table = 'c01' + + def __str__(self): + return self.f_a + +# All of these models use the same tables as the previous set (they are shadows +# of possibly a subset of the columns). There should be no creation errors, +# since we have told Django they aren't managed by Django. + +@python_2_unicode_compatible +class A02(models.Model): + f_a = models.CharField(max_length=10, db_index=True) + + class Meta: + db_table = 'a01' + managed = False + + def __str__(self): + return self.f_a + +@python_2_unicode_compatible +class B02(models.Model): + class Meta: + db_table = 'b01' + managed = False + + fk_a = models.ForeignKey(A02) + f_a = models.CharField(max_length=10, db_index=True) + f_b = models.IntegerField() + + def __str__(self): + return self.f_a + +# To re-use the many-to-many intermediate table, we need to manually set up +# things up. +@python_2_unicode_compatible +class C02(models.Model): + mm_a = models.ManyToManyField(A02, through="Intermediate") + f_a = models.CharField(max_length=10, db_index=True) + f_b = models.IntegerField() + + class Meta: + db_table = 'c01' + managed = False + + def __str__(self): + return self.f_a + +class Intermediate(models.Model): + a02 = models.ForeignKey(A02, db_column="a01_id") + c02 = models.ForeignKey(C02, db_column="c01_id") + + class Meta: + db_table = 'd01' + managed = False + +# +# These next models test the creation (or not) of many to many join tables +# between managed and unmanaged models. A join table between two unmanaged +# models shouldn't be automatically created (see #10647). +# + +# Firstly, we need some models that will create the tables, purely so that the +# tables are created. This is a test setup, not a requirement for unmanaged +# models. +class Proxy1(models.Model): + class Meta: + db_table = "unmanaged_models_proxy1" + +class Proxy2(models.Model): + class Meta: + db_table = "unmanaged_models_proxy2" + +class Unmanaged1(models.Model): + class Meta: + managed = False + db_table = "unmanaged_models_proxy1" + +# Unmanged with an m2m to unmanaged: the intermediary table won't be created. +class Unmanaged2(models.Model): + mm = models.ManyToManyField(Unmanaged1) + + class Meta: + managed = False + db_table = "unmanaged_models_proxy2" + +# Here's an unmanaged model with an m2m to a managed one; the intermediary +# table *will* be created (unless given a custom `through` as for C02 above). +class Managed1(models.Model): + mm = models.ManyToManyField(Unmanaged1) diff --git a/tests/unmanaged_models/tests.py b/tests/unmanaged_models/tests.py new file mode 100644 index 0000000000..64e33bbb47 --- /dev/null +++ b/tests/unmanaged_models/tests.py @@ -0,0 +1,61 @@ +from __future__ import absolute_import + +from django.db import connection +from django.test import TestCase + +from .models import A01, A02, B01, B02, C01, C02, Unmanaged2, Managed1 + + +class SimpleTests(TestCase): + + def test_simple(self): + """ + The main test here is that the all the models can be created without + any database errors. We can also do some more simple insertion and + lookup tests whilst we're here to show that the second of models do + refer to the tables from the first set. + """ + # Insert some data into one set of models. + a = A01.objects.create(f_a="foo", f_b=42) + B01.objects.create(fk_a=a, f_a="fred", f_b=1729) + c = C01.objects.create(f_a="barney", f_b=1) + c.mm_a = [a] + + # ... and pull it out via the other set. + a2 = A02.objects.all()[0] + self.assertTrue(isinstance(a2, A02)) + self.assertEqual(a2.f_a, "foo") + + b2 = B02.objects.all()[0] + self.assertTrue(isinstance(b2, B02)) + self.assertEqual(b2.f_a, "fred") + + self.assertTrue(isinstance(b2.fk_a, A02)) + self.assertEqual(b2.fk_a.f_a, "foo") + + self.assertEqual(list(C02.objects.filter(f_a=None)), []) + + resp = list(C02.objects.filter(mm_a=a.id)) + self.assertEqual(len(resp), 1) + + self.assertTrue(isinstance(resp[0], C02)) + self.assertEqual(resp[0].f_a, 'barney') + + +class ManyToManyUnmanagedTests(TestCase): + + def test_many_to_many_between_unmanaged(self): + """ + The intermediary table between two unmanaged models should not be created. + """ + table = Unmanaged2._meta.get_field('mm').m2m_db_table() + tables = connection.introspection.table_names() + self.assertTrue(table not in tables, "Table '%s' should not exist, but it does." % table) + + def test_many_to_many_between_unmanaged_and_managed(self): + """ + An intermediary table between a managed and an unmanaged model should be created. + """ + table = Managed1._meta.get_field('mm').m2m_db_table() + tables = connection.introspection.table_names() + self.assertTrue(table in tables, "Table '%s' does not exist." % table) |
