2015-01-28 20:35:27 +08:00
|
|
|
from django.db import connection, models
|
2014-12-01 21:21:03 +08:00
|
|
|
from django.db.backends.utils import truncate_name
|
2010-10-15 15:34:23 +08:00
|
|
|
from django.test import TestCase
|
|
|
|
|
2014-12-24 01:09:55 +08:00
|
|
|
from .models.article import Article, Site
|
2015-01-28 20:35:27 +08:00
|
|
|
from .models.publication import Publication
|
2010-10-15 15:34:23 +08:00
|
|
|
|
2009-11-20 08:59:38 +08:00
|
|
|
|
2014-05-29 08:39:14 +08:00
|
|
|
class Advertisement(models.Model):
|
2009-11-20 08:59:38 +08:00
|
|
|
customer = models.CharField(max_length=100)
|
2014-07-09 04:42:40 +08:00
|
|
|
publications = models.ManyToManyField("model_package.Publication", blank=True)
|
2009-11-20 08:59:38 +08:00
|
|
|
|
2009-11-05 19:57:28 +08:00
|
|
|
|
2010-10-15 15:34:23 +08:00
|
|
|
class ModelPackageTests(TestCase):
|
2014-12-10 03:50:45 +08:00
|
|
|
|
2014-12-01 21:21:03 +08:00
|
|
|
def test_m2m_tables_in_subpackage_models(self):
|
|
|
|
"""
|
|
|
|
Regression for #12168: models split into subpackages still get M2M
|
|
|
|
tables.
|
|
|
|
"""
|
2010-10-15 15:34:23 +08:00
|
|
|
p = Publication.objects.create(title="FooBar")
|
|
|
|
|
2014-12-24 01:09:55 +08:00
|
|
|
site = Site.objects.create(name="example.com")
|
2010-10-15 15:34:23 +08:00
|
|
|
|
|
|
|
a = Article.objects.create(headline="a foo headline")
|
|
|
|
a.publications.add(p)
|
2014-12-24 01:09:55 +08:00
|
|
|
a.sites.add(site)
|
2010-10-15 15:34:23 +08:00
|
|
|
|
|
|
|
a = Article.objects.get(id=a.pk)
|
|
|
|
self.assertEqual(a.id, a.pk)
|
|
|
|
self.assertEqual(a.sites.count(), 1)
|
|
|
|
|
2014-12-01 21:21:03 +08:00
|
|
|
def test_models_in_the_test_package(self):
|
|
|
|
"""
|
|
|
|
Regression for #12245 - Models can exist in the test package, too.
|
|
|
|
"""
|
|
|
|
p = Publication.objects.create(title="FooBar")
|
2014-05-29 08:39:14 +08:00
|
|
|
ad = Advertisement.objects.create(customer="Lawrence Journal-World")
|
2010-10-15 15:34:23 +08:00
|
|
|
ad.publications.add(p)
|
|
|
|
|
2014-05-29 08:39:14 +08:00
|
|
|
ad = Advertisement.objects.get(id=ad.pk)
|
2010-10-15 15:34:23 +08:00
|
|
|
self.assertEqual(ad.publications.count(), 1)
|
|
|
|
|
2014-12-01 21:21:03 +08:00
|
|
|
def test_automatic_m2m_column_names(self):
|
|
|
|
"""
|
|
|
|
Regression for #12386 - field names on the autogenerated intermediate
|
|
|
|
class that are specified as dotted strings don't retain any path
|
|
|
|
component for the field or column name.
|
|
|
|
"""
|
2010-10-15 15:34:23 +08:00
|
|
|
self.assertEqual(
|
|
|
|
Article.publications.through._meta.fields[1].name, 'article'
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
Article.publications.through._meta.fields[1].get_attname_column(),
|
|
|
|
('article_id', 'article_id')
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
Article.publications.through._meta.fields[2].name, 'publication'
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
Article.publications.through._meta.fields[2].get_attname_column(),
|
|
|
|
('publication_id', 'publication_id')
|
|
|
|
)
|
|
|
|
|
2014-12-01 21:21:03 +08:00
|
|
|
self.assertEqual(
|
|
|
|
Article._meta.get_field('publications').m2m_db_table(),
|
|
|
|
truncate_name('model_package_article_publications', connection.ops.max_name_length()),
|
2010-10-15 15:34:23 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
Article._meta.get_field('publications').m2m_column_name(), 'article_id'
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
Article._meta.get_field('publications').m2m_reverse_name(),
|
|
|
|
'publication_id'
|
|
|
|
)
|