2013-07-29 17:19:04 +00:00
|
|
|
from __future__ import unicode_literals
|
2011-10-13 18:04:12 +00:00
|
|
|
|
2009-04-09 15:03:31 +00:00
|
|
|
from django.db import connection
|
2011-10-13 18:04:12 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
|
2015-01-28 12:35:27 +00:00
|
|
|
from .models import A01, A02, B01, B02, C01, C02, Managed1, Unmanaged2
|
2011-10-13 18:04:12 +00:00
|
|
|
|
2010-09-13 05:28:18 +00:00
|
|
|
|
|
|
|
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
|
2015-12-31 19:29:52 +00:00
|
|
|
lookup tests while we're here to show that the second of models do
|
2010-09-13 05:28:18 +00:00
|
|
|
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)
|
2015-10-08 21:17:10 +00:00
|
|
|
c.mm_a.set([a])
|
2010-09-13 05:28:18 +00:00
|
|
|
|
|
|
|
# ... and pull it out via the other set.
|
|
|
|
a2 = A02.objects.all()[0]
|
2013-05-21 09:42:15 +00:00
|
|
|
self.assertIsInstance(a2, A02)
|
2010-09-13 05:28:18 +00:00
|
|
|
self.assertEqual(a2.f_a, "foo")
|
|
|
|
|
|
|
|
b2 = B02.objects.all()[0]
|
2013-05-21 09:42:15 +00:00
|
|
|
self.assertIsInstance(b2, B02)
|
2010-09-13 05:28:18 +00:00
|
|
|
self.assertEqual(b2.f_a, "fred")
|
|
|
|
|
2013-05-21 09:42:15 +00:00
|
|
|
self.assertIsInstance(b2.fk_a, A02)
|
2010-09-13 05:28:18 +00:00
|
|
|
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)
|
|
|
|
|
2013-05-21 09:42:15 +00:00
|
|
|
self.assertIsInstance(resp[0], C02)
|
2010-09-13 05:28:18 +00:00
|
|
|
self.assertEqual(resp[0].f_a, 'barney')
|
|
|
|
|
2009-04-09 15:03:31 +00:00
|
|
|
|
|
|
|
class ManyToManyUnmanagedTests(TestCase):
|
2010-09-13 05:28:18 +00:00
|
|
|
|
2009-04-09 15:03:31 +00:00
|
|
|
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()
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertNotIn(table, tables, "Table '%s' should not exist, but it does." % table)
|
2010-09-13 05:28:18 +00:00
|
|
|
|
2009-04-09 15:03:31 +00:00
|
|
|
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()
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertIn(table, tables, "Table '%s' does not exist." % table)
|