Ticket #14796: 14796-move_keyword_check_with_tests.diff

File 14796-move_keyword_check_with_tests.diff, 3.9 KB (added by mmcnickle, 13 years ago)

Improved the test by using the pre-existing stdout capture method, updated inspectdb.py to take advantage of it.

  • django/core/management/commands/inspectdb.py

     
    2020    def handle_noargs(self, **options):
    2121        try:
    2222            for line in self.handle_inspection(options):
    23                 print line
     23                self.stdout.write("%s\n" % line)
    2424        except NotImplementedError:
    2525            raise CommandError("Database inspection isn't supported for the currently selected database backend.")
    2626
     
    6666                if ' ' in att_name:
    6767                    att_name = att_name.replace(' ', '_')
    6868                    comment_notes.append('Field renamed to remove spaces.')
     69                   
    6970                if '-' in att_name:
    7071                    att_name = att_name.replace('-', '_')
    7172                    comment_notes.append('Field renamed to remove dashes.')
    72                 if keyword.iskeyword(att_name):
    73                     att_name += '_field'
    74                     comment_notes.append('Field renamed because it was a Python reserved word.')
     73                   
    7574                if column_name != att_name:
    7675                    comment_notes.append('Field name made lowercase.')
    7776
     
    9796                            extra_params['unique'] = True
    9897
    9998                    field_type += '('
     99                   
     100                if keyword.iskeyword(att_name):
     101                    att_name += '_field'
     102                    comment_notes.append('Field renamed because it was a Python reserved word.')
    100103
    101104                # Don't output 'id = meta.AutoField(primary_key=True)', because
    102105                # that's assumed if it doesn't exist.
  • tests/regressiontests/inspectdb/tests.py

     
     1import os
     2import sys
     3from StringIO import StringIO
     4
     5from django.conf import settings
     6from django.core.management import call_command
     7from django.db.models.loading import load_app
     8from django.test import TestCase
     9
     10class InspectDBTestCase(TestCase):
     11   
     12    def setUp(self):
     13        self.old_sys_path = sys.path[:]
     14        sys.path.append(os.path.dirname(os.path.abspath(__file__)))
     15        self.old_installed_apps = settings.INSTALLED_APPS
     16        settings.INSTALLED_APPS = ('bug',)
     17        map(load_app, settings.INSTALLED_APPS)
     18        call_command('syncdb', verbosity=0)
     19       
     20    def test_attribute_name_not_python_keyword(self):
     21        out = StringIO()
     22        call_command('inspectdb', stdout=out)
     23        error_message = "inspectdb generated an attribute name which is a python keyword"
     24        self.assertNotIn("from = models.ForeignKey(BugPeople)", out.getvalue(), msg=error_message)
     25        out.close()
     26       
     27    def tearDown(self):
     28        settings.INSTALLED_APPS = self.old_installed_apps
     29        sys.path = self.old_sys_path
  • tests/regressiontests/inspectdb/bug/models.py

     
     1from django.db import models
     2
     3class People(models.Model):
     4    name = models.CharField(max_length=255)
     5
     6class Message(models.Model):
     7    from_field = models.ForeignKey(People, db_column='from_id')
Back to Top