Skip to content

fixed a potential bug #337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 11 additions & 29 deletions aredis_om/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1179,50 +1179,32 @@ def find(cls, *expressions: Union[Any, Expression]) -> FindQuery:
@classmethod
def from_redis(cls, res: Any):
# TODO: Parsing logic copied from redisearch-py. Evaluate.
import six
from six.moves import xrange
from six.moves import zip as izip

def to_string(s):
if isinstance(s, six.string_types):
if isinstance(s, (str,)):
return s
elif isinstance(s, six.binary_type):
return s.decode("utf-8", "ignore")
elif isinstance(s, bytes):
return s.decode(errors="ignore")
else:
return s # Not a string we care about

docs = []
step = 2 # Because the result has content
offset = 1 # The first item is the count of total matches.

for i in xrange(1, len(res), step):
fields_offset = offset

for i in range(1, len(res), step):
fields = dict(
dict(
izip(
map(to_string, res[i + fields_offset][::2]),
map(to_string, res[i + fields_offset][1::2]),
zip(
map(to_string, res[i + offset][::2]),
map(to_string, res[i + offset][1::2]),
)
)
)

try:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!

del fields["id"]
except KeyError:
pass

try:
fields["json"] = fields["$"]
del fields["$"]
except KeyError:
pass

if "json" in fields:
json_fields = json.loads(fields["json"])
# $ means a json entry
if fields.get("$"):
json_fields = json.loads(fields.get("$"))
doc = cls(**json_fields)
else:
doc = cls(**fields)

docs.append(doc)
return docs

Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ redis = ">=3.5.3,<5.0.0"
aioredis = "^2.0.0"
pydantic = "^1.8.2"
click = "^8.0.1"
six = "^1.16.0"
pptree = "^3.1"
types-redis = ">=3.5.9,<5.0.0"
types-six = "^1.16.1"
Expand Down
9 changes: 8 additions & 1 deletion tests/test_hash_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Order(BaseHashModel):
created_on: datetime.datetime

class Member(BaseHashModel):
id: int = Field(index=True)
first_name: str = Field(index=True)
last_name: str = Field(index=True)
email: str = Field(index=True)
Expand All @@ -64,6 +65,7 @@ class Meta:
@pytest_asyncio.fixture
async def members(m):
member1 = m.Member(
id=0,
first_name="Andrew",
last_name="Brookins",
email="[email protected]",
Expand All @@ -73,6 +75,7 @@ async def members(m):
)

member2 = m.Member(
id=1,
first_name="Kim",
last_name="Brookins",
email="[email protected]",
Expand All @@ -82,6 +85,7 @@ async def members(m):
)

member3 = m.Member(
id=2,
first_name="Andrew",
last_name="Smith",
email="[email protected]",
Expand Down Expand Up @@ -129,6 +133,9 @@ async def test_exact_match_queries(members, m):
).all()
assert actual == [member2]

actual = await m.Member.find(m.Member.id == 0).all()
assert actual == [member1]


@py_test_mark_asyncio
async def test_full_text_search_queries(members, m):
Expand Down Expand Up @@ -611,4 +618,4 @@ class Address(m.BaseHashModel):
assert (
Address.redisearch_schema()
== f"ON HASH PREFIX 1 {key_prefix} SCHEMA pk TAG SEPARATOR | a_string TAG SEPARATOR | a_full_text_string TAG SEPARATOR | a_full_text_string AS a_full_text_string_fts TEXT an_integer NUMERIC SORTABLE a_float NUMERIC"
)
)