Skip to content

Commit

Permalink
minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwjfy committed Aug 19, 2018
1 parent f2dccd1 commit 9587e75
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
6 changes: 4 additions & 2 deletions gino/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def __init__(self, instance: 'CRUDModel'):
try:
self._locator = instance.lookup()
except LookupError:
# apply() will fail anyway, but still allow updates()
# apply() will fail anyway, but still allow update()
pass

def _set(self, key, value):
Expand Down Expand Up @@ -144,7 +144,9 @@ async def apply(self, bind=None, timeout=DEFAULT):
sa.func.jsonb_build_object(
*itertools.chain(*updates.items())))
else:
raise TypeError('{} is not supported.'.format(prop.type))
raise TypeError('{} is not supported to update json '
'properties in Gino. Please consider using '
'JSONB.'.format(prop.type))

opts = dict(return_model=False)
if timeout is not DEFAULT:
Expand Down
10 changes: 5 additions & 5 deletions gino/declarative.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@


class ColumnAttribute:
def __init__(self, name, column):
self.name = name
def __init__(self, prop_name, column):
self.prop_name = prop_name
self.column = column

def __get__(self, instance, owner):
if instance is None:
return self.column
else:
return instance.__values__.get(self.name)
return instance.__values__.get(self.prop_name)

def __set__(self, instance, value):
instance.__values__[self.name] = value
instance.__values__[self.prop_name] = value

def __delete__(self, instance):
raise AttributeError('Cannot delete value.')
Expand All @@ -36,7 +36,7 @@ def __init__(self, *args, **kwargs):
self._inverted_dict[v] = k

def __setitem__(self, key, value):
if value in self._inverted_dict:
if value in self._inverted_dict and self._inverted_dict[value] != key:
raise GinoException(
'Column name {} already maps to {}'.format(
value, self._inverted_dict[value]))
Expand Down
3 changes: 3 additions & 0 deletions tests/test_declarative.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ async def test_invert_dict():
with pytest.raises(gino.GinoException,
match=r'Column name c1 already maps to \w+'):
InvertDict({'col1': 'c1', 'col2': 'c1'})

with pytest.raises(gino.GinoException,
match=r'Column name c1 already maps to \w+'):
d = InvertDict()
Expand All @@ -237,6 +238,8 @@ async def test_invert_dict():

d = InvertDict()
d['col1'] = 'c1'
# it works for same key/value pair
d['col1'] = 'c1'
d['col2'] = 'c2'
assert d.invert_get('c1') == 'col1'
assert d.invert_get('c2') == 'col2'
6 changes: 3 additions & 3 deletions tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ class News(db.Model):
try:
news = await News.create()
assert news.visits == 0
with pytest.raises(TypeError, match='JSON is not supported.'):
with pytest.raises(TypeError, match='JSON is not supported'):
await news.update(visits=News.visits + 10).apply()
assert news.visits == 0
with pytest.raises(TypeError, match='JSON is not supported.'):
with pytest.raises(TypeError, match='JSON is not supported'):
await news.update(visits=10).apply()
assert news.visits == 10
assert await news.select('visits').gino.scalar() == 0
Expand Down Expand Up @@ -185,7 +185,7 @@ class PropsTest(db.Model):
profile1 = db.Column(JSON(), nullable=False, server_default='{}')

bool = db.BooleanProperty()
bool1 = db.BooleanProperty(column_name='profile1')
bool1 = db.BooleanProperty(prop_name='profile1')

await PropsTest.gino.create()
try:
Expand Down

0 comments on commit 9587e75

Please sign in to comment.