PageRenderTime 268ms CodeModel.GetById 259ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/modeltests/model_package/tests.py

https://code.google.com/p/mango-py/
Python | 72 lines | 51 code | 14 blank | 7 comment | 1 complexity | 33a75138bac58093aa50f11bdf94d6bf MD5 | raw file
 1from django.contrib.sites.models import Site
 2from django.db import models
 3from django.test import TestCase
 4
 5from models.publication import Publication
 6from models.article import Article
 7
 8
 9class Advertisment(models.Model):
10    customer = models.CharField(max_length=100)
11    publications = models.ManyToManyField(
12        "model_package.Publication", null=True, blank=True
13    )
14
15    class Meta:
16        app_label = 'model_package'
17
18
19class ModelPackageTests(TestCase):
20    def test_model_packages(self):
21        p = Publication.objects.create(title="FooBar")
22
23        current_site = Site.objects.get_current()
24        self.assertEqual(current_site.domain, "example.com")
25
26        # Regression for #12168: models split into subpackages still get M2M
27        # tables
28        a = Article.objects.create(headline="a foo headline")
29        a.publications.add(p)
30        a.sites.add(current_site)
31
32        a = Article.objects.get(id=a.pk)
33        self.assertEqual(a.id, a.pk)
34        self.assertEqual(a.sites.count(), 1)
35
36        # Regression for #12245 - Models can exist in the test package, too
37        ad = Advertisment.objects.create(customer="Lawrence Journal-World")
38        ad.publications.add(p)
39
40        ad = Advertisment.objects.get(id=ad.pk)
41        self.assertEqual(ad.publications.count(), 1)
42
43        # Regression for #12386 - field names on the autogenerated intermediate
44        # class that are specified as dotted strings don't retain any path
45        # component for the field or column name
46        self.assertEqual(
47            Article.publications.through._meta.fields[1].name, 'article'
48        )
49        self.assertEqual(
50            Article.publications.through._meta.fields[1].get_attname_column(),
51            ('article_id', 'article_id')
52        )
53        self.assertEqual(
54            Article.publications.through._meta.fields[2].name, 'publication'
55        )
56        self.assertEqual(
57            Article.publications.through._meta.fields[2].get_attname_column(),
58            ('publication_id', 'publication_id')
59        )
60
61        # The oracle backend truncates the name to 'model_package_article_publ233f'.
62        self.assertTrue(
63            Article._meta.get_field('publications').m2m_db_table() in ('model_package_article_publications', 'model_package_article_publ233f')
64        )
65
66        self.assertEqual(
67            Article._meta.get_field('publications').m2m_column_name(), 'article_id'
68        )
69        self.assertEqual(
70            Article._meta.get_field('publications').m2m_reverse_name(),
71            'publication_id'
72        )