Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Made inspectdb tests deal with a smaller generated models.py file.

Implemented this by adding a stealth table_name_filter option for the
command.
  • Loading branch information...
commit 72130385bfe503bd440e52605c44a10a0480a25b 1 parent 939af5a
Ramiro Morales authored June 01, 2012
5  django/core/management/commands/inspectdb.py
@@ -26,6 +26,8 @@ def handle_noargs(self, **options):
26 26
 
27 27
     def handle_inspection(self, options):
28 28
         connection = connections[options.get('database')]
  29
+        # 'table_name_filter' is a stealth option
  30
+        table_name_filter = options.get('table_name_filter')
29 31
 
30 32
         table2model = lambda table_name: table_name.title().replace('_', '').replace(' ', '').replace('-', '')
31 33
 
@@ -43,6 +45,9 @@ def handle_inspection(self, options):
43 45
         yield ''
44 46
         known_models = []
45 47
         for table_name in connection.introspection.table_names(cursor):
  48
+            if table_name_filter is not None and callable(table_name_filter):
  49
+                if not table_name_filter(table_name):
  50
+                    continue
46 51
             yield 'class %s(models.Model):' % table2model(table_name)
47 52
             known_models.append(table2model(table_name))
48 53
             try:
29  tests/regressiontests/inspectdb/tests.py
@@ -6,10 +6,31 @@
6 6
 
7 7
 class InspectDBTestCase(TestCase):
8 8
 
  9
+    def test_stealth_table_name_filter_option(self):
  10
+        out = StringIO()
  11
+        # Lets limit the introspection to tables created for models of this
  12
+        # application
  13
+        call_command('inspectdb',
  14
+                     table_name_filter=lambda tn:tn.startswith('inspectdb_'),
  15
+                     stdout=out)
  16
+        error_message = "inspectdb has examined a table that should have been filtered out."
  17
+        # contrib.contenttypes is one of the apps always installed when running
  18
+        # the Django test suite, check that one of its tables hasn't been
  19
+        # inspected
  20
+        self.assertNotIn("class DjangoContentType(models.Model):", out.getvalue(), msg=error_message)
  21
+        out.close()
  22
+
9 23
     @skipUnlessDBFeature('can_introspect_foreign_keys')
10 24
     def test_attribute_name_not_python_keyword(self):
11 25
         out = StringIO()
12  
-        call_command('inspectdb', stdout=out)
  26
+        # Lets limit the introspection to tables created for models of this
  27
+        # application
  28
+        call_command('inspectdb',
  29
+                     table_name_filter=lambda tn:tn.startswith('inspectdb_'),
  30
+                     stdout=out)
  31
+        f = open('/home/ramiro/models2.py', 'w')
  32
+        f.write(out.getvalue())
  33
+        f.close()
13 34
         error_message = "inspectdb generated an attribute name which is a python keyword"
14 35
         self.assertNotIn("from = models.ForeignKey(InspectdbPeople)", out.getvalue(), msg=error_message)
15 36
         # As InspectdbPeople model is defined after InspectdbMessage, it should be quoted
@@ -23,7 +44,11 @@ def test_attribute_name_not_python_keyword(self):
23 44
     def test_digits_column_name_introspection(self):
24 45
         """Introspection of column names consist/start with digits (#16536/#17676)"""
25 46
         out = StringIO()
26  
-        call_command('inspectdb', stdout=out)
  47
+        # Lets limit the introspection to tables created for models of this
  48
+        # application
  49
+        call_command('inspectdb',
  50
+                     table_name_filter=lambda tn:tn.startswith('inspectdb_'),
  51
+                     stdout=out)
27 52
         error_message = "inspectdb generated a model field name which is a number"
28 53
         self.assertNotIn("    123 = models.CharField", out.getvalue(), msg=error_message)
29 54
         self.assertIn("number_123 = models.CharField", out.getvalue())

0 notes on commit 7213038

Please sign in to comment.
Something went wrong with that request. Please try again.