Skip to content

Commit

Permalink
fix JSONPathType bind processor for asyncpg
Browse files Browse the repository at this point in the history
  • Loading branch information
fantix committed Dec 10, 2019
1 parent 3109577 commit 515062f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
18 changes: 17 additions & 1 deletion gino/dialects/asyncpg.py
Expand Up @@ -9,7 +9,8 @@
CreateEnumType,
DropEnumType,
JSON,
JSONB
JSONB,
json,
)
from sqlalchemy.dialects.postgresql.base import (
ENUM,
Expand Down Expand Up @@ -333,6 +334,20 @@ def result_processor(self, dialect, coltype):
return super().result_processor(dialect, coltype)


class AsyncpgJSONPathType(json.JSONPathType):
def bind_processor(self, dialect):
super_proc = self.string_bind_processor(dialect)

def process(value):
assert isinstance(value, util.collections_abc.Sequence)
if super_proc:
return [super_proc(util.text_type(elem)) for elem in value]
else:
return [util.text_type(elem) for elem in value]

return process


# noinspection PyAbstractClass
class AsyncpgDialect(PGDialect, base.AsyncDialectMixin):
driver = 'asyncpg'
Expand All @@ -350,6 +365,7 @@ class AsyncpgDialect(PGDialect, base.AsyncDialectMixin):
ENUM: AsyncEnum,
sqltypes.Enum: AsyncEnum,
sqltypes.NullType: GinoNullType,
sqltypes.JSON.JSONPathType: AsyncpgJSONPathType,
}
)

Expand Down
17 changes: 17 additions & 0 deletions tests/test_json.py
Expand Up @@ -233,3 +233,20 @@ class PropsTest(db.Model):
assert custom_profile2 == 123
finally:
await PropsTest.gino.drop()


async def test_json_path(bind):
from gino.dialects.asyncpg import JSONB

class PathTest(db.Model):
__tablename__ = 'path_test_json_path'
data = db.Column(JSONB())

await PathTest.gino.create()
try:
t1 = await PathTest.create(data=dict(a=dict(b='c')))
t2 = await PathTest.query.where(
PathTest.data[('a', 'b')].astext == 'c').gino.first()
assert t1.data == t2.data
finally:
await PathTest.gino.drop()

0 comments on commit 515062f

Please sign in to comment.