Skip to content

Commit 9587e75

Browse files
committed
minor tweaks
1 parent f2dccd1 commit 9587e75

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

gino/crud.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def __init__(self, instance: 'CRUDModel'):
8888
try:
8989
self._locator = instance.lookup()
9090
except LookupError:
91-
# apply() will fail anyway, but still allow updates()
91+
# apply() will fail anyway, but still allow update()
9292
pass
9393

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

149151
opts = dict(return_model=False)
150152
if timeout is not DEFAULT:

gino/declarative.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77

88

99
class ColumnAttribute:
10-
def __init__(self, name, column):
11-
self.name = name
10+
def __init__(self, prop_name, column):
11+
self.prop_name = prop_name
1212
self.column = column
1313

1414
def __get__(self, instance, owner):
1515
if instance is None:
1616
return self.column
1717
else:
18-
return instance.__values__.get(self.name)
18+
return instance.__values__.get(self.prop_name)
1919

2020
def __set__(self, instance, value):
21-
instance.__values__[self.name] = value
21+
instance.__values__[self.prop_name] = value
2222

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

3838
def __setitem__(self, key, value):
39-
if value in self._inverted_dict:
39+
if value in self._inverted_dict and self._inverted_dict[value] != key:
4040
raise GinoException(
4141
'Column name {} already maps to {}'.format(
4242
value, self._inverted_dict[value]))

tests/test_declarative.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ async def test_invert_dict():
229229
with pytest.raises(gino.GinoException,
230230
match=r'Column name c1 already maps to \w+'):
231231
InvertDict({'col1': 'c1', 'col2': 'c1'})
232+
232233
with pytest.raises(gino.GinoException,
233234
match=r'Column name c1 already maps to \w+'):
234235
d = InvertDict()
@@ -237,6 +238,8 @@ async def test_invert_dict():
237238

238239
d = InvertDict()
239240
d['col1'] = 'c1'
241+
# it works for same key/value pair
242+
d['col1'] = 'c1'
240243
d['col2'] = 'c2'
241244
assert d.invert_get('c1') == 'col1'
242245
assert d.invert_get('c2') == 'col2'

tests/test_json.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ class News(db.Model):
102102
try:
103103
news = await News.create()
104104
assert news.visits == 0
105-
with pytest.raises(TypeError, match='JSON is not supported.'):
105+
with pytest.raises(TypeError, match='JSON is not supported'):
106106
await news.update(visits=News.visits + 10).apply()
107107
assert news.visits == 0
108-
with pytest.raises(TypeError, match='JSON is not supported.'):
108+
with pytest.raises(TypeError, match='JSON is not supported'):
109109
await news.update(visits=10).apply()
110110
assert news.visits == 10
111111
assert await news.select('visits').gino.scalar() == 0
@@ -185,7 +185,7 @@ class PropsTest(db.Model):
185185
profile1 = db.Column(JSON(), nullable=False, server_default='{}')
186186

187187
bool = db.BooleanProperty()
188-
bool1 = db.BooleanProperty(column_name='profile1')
188+
bool1 = db.BooleanProperty(prop_name='profile1')
189189

190190
await PropsTest.gino.create()
191191
try:

0 commit comments

Comments
 (0)