Ticket #14796: 14796-move_keyword_check_with_tests.diff
File 14796-move_keyword_check_with_tests.diff, 3.9 KB (added by , 14 years ago) |
---|
-
django/core/management/commands/inspectdb.py
20 20 def handle_noargs(self, **options): 21 21 try: 22 22 for line in self.handle_inspection(options): 23 print line23 self.stdout.write("%s\n" % line) 24 24 except NotImplementedError: 25 25 raise CommandError("Database inspection isn't supported for the currently selected database backend.") 26 26 … … 66 66 if ' ' in att_name: 67 67 att_name = att_name.replace(' ', '_') 68 68 comment_notes.append('Field renamed to remove spaces.') 69 69 70 if '-' in att_name: 70 71 att_name = att_name.replace('-', '_') 71 72 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 75 74 if column_name != att_name: 76 75 comment_notes.append('Field name made lowercase.') 77 76 … … 97 96 extra_params['unique'] = True 98 97 99 98 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.') 100 103 101 104 # Don't output 'id = meta.AutoField(primary_key=True)', because 102 105 # that's assumed if it doesn't exist. -
tests/regressiontests/inspectdb/tests.py
1 import os 2 import sys 3 from StringIO import StringIO 4 5 from django.conf import settings 6 from django.core.management import call_command 7 from django.db.models.loading import load_app 8 from django.test import TestCase 9 10 class 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
1 from django.db import models 2 3 class People(models.Model): 4 name = models.CharField(max_length=255) 5 6 class Message(models.Model): 7 from_field = models.ForeignKey(People, db_column='from_id')