~ubuntuone-pqm-team/django/stable

« back to all changes in this revision

Viewing changes to tests/regressiontests/inspectdb/tests.py

  • Committer: Natalia
  • Date: 2014-12-05 15:21:13 UTC
  • Revision ID: natalia.bidart@ubuntu.com-20141205152113-cchtmygjia45gb87
Tags: 1.6.8
- Imported Django 1.6.8 from released tarball.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from __future__ import unicode_literals
2
 
 
3
 
from django.core.management import call_command
4
 
from django.db import connection
5
 
from django.test import TestCase, skipUnlessDBFeature
6
 
from django.utils.six import StringIO
7
 
 
8
 
 
9
 
class InspectDBTestCase(TestCase):
10
 
 
11
 
    def test_stealth_table_name_filter_option(self):
12
 
        out = StringIO()
13
 
        # Lets limit the introspection to tables created for models of this
14
 
        # application
15
 
        call_command('inspectdb',
16
 
                     table_name_filter=lambda tn:tn.startswith('inspectdb_'),
17
 
                     stdout=out)
18
 
        error_message = "inspectdb has examined a table that should have been filtered out."
19
 
        # contrib.contenttypes is one of the apps always installed when running
20
 
        # the Django test suite, check that one of its tables hasn't been
21
 
        # inspected
22
 
        self.assertNotIn("class DjangoContentType(models.Model):", out.getvalue(), msg=error_message)
23
 
 
24
 
    @skipUnlessDBFeature('can_introspect_foreign_keys')
25
 
    def test_attribute_name_not_python_keyword(self):
26
 
        out = StringIO()
27
 
        # Lets limit the introspection to tables created for models of this
28
 
        # application
29
 
        call_command('inspectdb',
30
 
                     table_name_filter=lambda tn:tn.startswith('inspectdb_'),
31
 
                     stdout=out)
32
 
        output = out.getvalue()
33
 
        error_message = "inspectdb generated an attribute name which is a python keyword"
34
 
        self.assertNotIn("from = models.ForeignKey(InspectdbPeople)", output, msg=error_message)
35
 
        # As InspectdbPeople model is defined after InspectdbMessage, it should be quoted
36
 
        self.assertIn("from_field = models.ForeignKey('InspectdbPeople', db_column='from_id')",
37
 
            output)
38
 
        self.assertIn("people_pk = models.ForeignKey(InspectdbPeople, primary_key=True)",
39
 
            output)
40
 
        self.assertIn("people_unique = models.ForeignKey(InspectdbPeople, unique=True)",
41
 
            output)
42
 
 
43
 
    def test_digits_column_name_introspection(self):
44
 
        """Introspection of column names consist/start with digits (#16536/#17676)"""
45
 
        out = StringIO()
46
 
        # Lets limit the introspection to tables created for models of this
47
 
        # application
48
 
        call_command('inspectdb',
49
 
                     table_name_filter=lambda tn:tn.startswith('inspectdb_'),
50
 
                     stdout=out)
51
 
        output = out.getvalue()
52
 
        error_message = "inspectdb generated a model field name which is a number"
53
 
        self.assertNotIn("    123 = models.CharField", output, msg=error_message)
54
 
        self.assertIn("number_123 = models.CharField", output)
55
 
 
56
 
        error_message = "inspectdb generated a model field name which starts with a digit"
57
 
        self.assertNotIn("    4extra = models.CharField", output, msg=error_message)
58
 
        self.assertIn("number_4extra = models.CharField", output)
59
 
 
60
 
        self.assertNotIn("    45extra = models.CharField", output, msg=error_message)
61
 
        self.assertIn("number_45extra = models.CharField", output)
62
 
 
63
 
    def test_special_column_name_introspection(self):
64
 
        """
65
 
        Introspection of column names containing special characters,
66
 
        unsuitable for Python identifiers
67
 
        """
68
 
        out = StringIO()
69
 
        call_command('inspectdb', stdout=out)
70
 
        output = out.getvalue()
71
 
        base_name = 'Field' if connection.vendor != 'oracle' else 'field'
72
 
        self.assertIn("field = models.IntegerField()", output)
73
 
        self.assertIn("field_field = models.IntegerField(db_column='%s_')" % base_name, output)
74
 
        self.assertIn("field_field_0 = models.IntegerField(db_column='%s__')" % base_name, output)
75
 
        self.assertIn("field_field_1 = models.IntegerField(db_column='__field')", output)
76
 
        self.assertIn("prc_x = models.IntegerField(db_column='prc(%) x')", output)