Skip to content
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

fixed a potential bug #337

Merged
merged 11 commits into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from all 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.pop("$"))
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
24 changes: 22 additions & 2 deletions 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 @@ -176,6 +183,7 @@ async def test_tag_queries_boolean_logic(members, m):
@py_test_mark_asyncio
async def test_tag_queries_punctuation(m):
member1 = m.Member(
id=0,
first_name="Andrew, the Michael",
last_name="St. Brookins-on-Pier",
email="a|[email protected]", # NOTE: This string uses the TAG field separator.
Expand All @@ -186,6 +194,7 @@ async def test_tag_queries_punctuation(m):
await member1.save()

member2 = m.Member(
id=1,
first_name="Bob",
last_name="the Villain",
email="a|[email protected]", # NOTE: This string uses the TAG field separator.
Expand Down Expand Up @@ -337,18 +346,19 @@ def test_validates_required_fields(m):
# Raises ValidationError: last_name is required
# TODO: Test the error value
with pytest.raises(ValidationError):
m.Member(first_name="Andrew", zipcode="97086", join_date=today)
m.Member(id=0, first_name="Andrew", zipcode="97086", join_date=today)


def test_validates_field(m):
# Raises ValidationError: join_date is not a date
# TODO: Test the error value
with pytest.raises(ValidationError):
m.Member(first_name="Andrew", last_name="Brookins", join_date="yesterday")
m.Member(id=0, first_name="Andrew", last_name="Brookins", join_date="yesterday")


def test_validation_passes(m):
member = m.Member(
id=0,
first_name="Andrew",
last_name="Brookins",
email="[email protected]",
Expand All @@ -362,6 +372,7 @@ def test_validation_passes(m):
@py_test_mark_asyncio
async def test_retrieve_first(m):
member = m.Member(
id=0,
first_name="Simon",
last_name="Prickett",
email="[email protected]",
Expand All @@ -373,6 +384,7 @@ async def test_retrieve_first(m):
await member.save()

member2 = m.Member(
id=1,
first_name="Another",
last_name="Member",
email="[email protected]",
Expand All @@ -384,6 +396,7 @@ async def test_retrieve_first(m):
await member2.save()

member3 = m.Member(
id=2,
first_name="Third",
last_name="Member",
email="[email protected]",
Expand All @@ -401,6 +414,7 @@ async def test_retrieve_first(m):
@py_test_mark_asyncio
async def test_saves_model_and_creates_pk(m):
member = m.Member(
id=0,
first_name="Andrew",
last_name="Brookins",
email="[email protected]",
Expand All @@ -418,6 +432,7 @@ async def test_saves_model_and_creates_pk(m):
@py_test_mark_asyncio
async def test_all_pks(m):
member = m.Member(
id=0,
first_name="Simon",
last_name="Prickett",
email="[email protected]",
Expand All @@ -429,6 +444,7 @@ async def test_all_pks(m):
await member.save()

member1 = m.Member(
id=1,
first_name="Andrew",
last_name="Brookins",
email="[email protected]",
Expand All @@ -449,6 +465,7 @@ async def test_all_pks(m):
@py_test_mark_asyncio
async def test_delete(m):
member = m.Member(
id=0,
first_name="Simon",
last_name="Prickett",
email="[email protected]",
Expand All @@ -465,6 +482,7 @@ async def test_delete(m):
@py_test_mark_asyncio
async def test_expire(m):
member = m.Member(
id=0,
first_name="Expire",
last_name="Test",
email="[email protected]",
Expand Down Expand Up @@ -529,6 +547,7 @@ class InvalidMember(m.BaseHashModel):
@py_test_mark_asyncio
async def test_saves_many(m):
member1 = m.Member(
id=0,
first_name="Andrew",
last_name="Brookins",
email="[email protected]",
Expand All @@ -537,6 +556,7 @@ async def test_saves_many(m):
bio="This is the user bio.",
)
member2 = m.Member(
id=1,
first_name="Kim",
last_name="Brookins",
email="[email protected]",
Expand Down