Permalink
Browse files

Clean up invalid column names, fixes #1022

  • Loading branch information...
Charles Leifer
Charles Leifer committed Jul 26, 2016
1 parent b84ca0c commit f565972f9912d84a1a476148511c869733a608ac
Showing with 27 additions and 1 deletion.
  1. +2 −1 playhouse/reflection.py
  2. +25 −0 playhouse/tests/test_pwiz.py
View
@@ -423,7 +423,8 @@ def make_model_name(self, table):
return model_name
def make_column_name(self, column):
column = re.sub('_id$', '', column.lower()) or column.lower()
column = re.sub('_id$', '', column.lower().strip()) or column.lower()
column = re.sub('[^\w]+', '_', column)
if column in RESERVED_WORDS:
column += '_'
return column
@@ -4,6 +4,7 @@
from StringIO import StringIO
except ImportError:
from io import StringIO
import textwrap
import sys
from peewee import *
@@ -40,6 +41,10 @@ class Category(BaseModel):
name = CharField(unique=True)
parent = ForeignKeyField('self', null=True)
class OddColumnNames(BaseModel):
spaces = CharField(db_column='s p aces')
symbols = CharField(db_column='w/-nug!')
class capture_output(object):
def __enter__(self):
self._stdout = sys.stdout
@@ -176,3 +181,23 @@ def test_ordered_columns(self):
print_models(self.introspector, preserve_order=True)
self.assertEqual(output.data.strip(), EXPECTED_ORDERED)
class TestPwizInvalidColumns(BasePwizTestCase):
models = [OddColumnNames]
def test_invalid_columns(self):
with capture_output() as output:
print_models(self.introspector)
result = output.data.strip()
expected = textwrap.dedent("""
class Oddcolumnnames(BaseModel):
s_p_aces = CharField(db_column='s p aces')
w_nug_ = CharField(db_column='w/-nug!')
class Meta:
db_table = 'oddcolumnnames'""").strip()
actual = result[-len(expected):]
self.assertEqual(actual, expected)

0 comments on commit f565972

Please sign in to comment.