Skip to content

Commit

Permalink
Fix string addition to use concat. Fixes #1241.
Browse files Browse the repository at this point in the history
  • Loading branch information
coleifer committed Apr 7, 2017
1 parent 6d14a5a commit 1740cc1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
27 changes: 18 additions & 9 deletions peewee.py
Expand Up @@ -597,7 +597,7 @@ def between(self, low, high):
def regexp(self, expression):
return Expression(self, OP.REGEXP, expression)
def concat(self, rhs):
return Expression(self, OP.CONCAT, rhs)
return StringExpression(self, OP.CONCAT, rhs)

class SQL(Node):
"""An unescaped SQL string, with optional parameters."""
Expand Down Expand Up @@ -685,6 +685,12 @@ def __init__(self, lhs, op, rhs, flat=False):
def clone_base(self):
return Expression(self.lhs, self.op, self.rhs, self.flat)

class StringExpression(Expression):
def __add__(self, other):
return self.concat(other)
def __radd__(self, other):
return other.concat(self)

class Param(Node):
"""
Arbitrary parameter passed into a query. Instructs the query compiler to
Expand Down Expand Up @@ -1131,7 +1137,16 @@ def coerce_to_unicode(s, encoding='utf-8'):
return s
return unicode_type(s)

class CharField(Field):
class _StringField(Field):
def coerce(self, value):
return coerce_to_unicode(value or '')

def __add__(self, other):
return self.concat(other)
def __radd__(self, other):
return other.concat(self)

class CharField(_StringField):
db_field = 'string'

def __init__(self, max_length=255, *args, **kwargs):
Expand All @@ -1146,9 +1161,6 @@ def clone_base(self, **kwargs):
def get_modifiers(self):
return self.max_length and [self.max_length] or None

def coerce(self, value):
return coerce_to_unicode(value or '')

class FixedCharField(CharField):
db_field = 'fixed_char'

Expand All @@ -1158,12 +1170,9 @@ def python_value(self, value):
value = value.strip()
return value

class TextField(Field):
class TextField(_StringField):
db_field = 'text'

def coerce(self, value):
return coerce_to_unicode(value or '')

class BlobField(Field):
db_field = 'blob'
_constructor = binary_construct
Expand Down
26 changes: 25 additions & 1 deletion playhouse/tests/test_hybrid.py
Expand Up @@ -32,8 +32,17 @@ def radius(cls):
return fn.abs(cls.length) / 2


class Person(BaseModel):
first = CharField()
last = CharField()

@hybrid_property
def full_name(self):
return self.first + ' ' + self.last


class TestHybrid(ModelTestCase):
requires = [Interval]
requires = [Interval, Person]

def setUp(self):
super(TestHybrid, self).setUp()
Expand Down Expand Up @@ -101,3 +110,18 @@ def test_separate_expr(self):
query = Interval.select().order_by(Interval.id)
actuals = [interval.radius for interval in query]
self.assertEqual(actuals, radii)

def test_string_fields(self):
huey = Person.create(first='huey', last='cat')
zaizee = Person.create(first='zaizee', last='kitty')

self.assertEqual(huey.full_name, 'huey cat')
self.assertEqual(zaizee.full_name, 'zaizee kitty')

query = Person.select().where(Person.full_name == 'zaizee kitty')
zaizee_db = query.get()
self.assertEqual(zaizee_db, zaizee)

query = Person.select().where(Person.full_name.startswith('huey c'))
huey_db = query.get()
self.assertEqual(huey_db, huey)

0 comments on commit 1740cc1

Please sign in to comment.