Skip to content

adding support for boolean checks #611

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 3 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
adding support for boolean checks
  • Loading branch information
slorello89 committed May 3, 2024
commit 5f05dede3ba26d4a1441c8839673b77755da8ccc
14 changes: 13 additions & 1 deletion aredis_om/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ class Operators(Enum):
STARTSWITH = 14
ENDSWITH = 15
CONTAINS = 16
TRUE = 17
FALSE = 18

def __str__(self):
return str(self.name)
Expand Down Expand Up @@ -582,6 +584,8 @@ def resolve_field_type(
"Only lists and tuples are supported for multi-value fields. "
f"Docs: {ERRORS_URL}#E4"
)
elif field_type is bool:
return RediSearchFieldTypes.TAG
elif any(issubclass(field_type, t) for t in NUMERIC_TYPES):
# Index numeric Python types as NUMERIC fields, so we can support
# range queries.
Expand Down Expand Up @@ -676,7 +680,11 @@ def resolve_value(
separator_char,
)
return ""
if isinstance(value, int):
if isinstance(value, bool):
result = "@{field_name}:{{{value}}}".format(
field_name=field_name, value=value
)
elif isinstance(value, int):
# This if will hit only if the field is a primary key of type int
result = f"@{field_name}:[{value} {value}]"
elif separator_char in value:
Expand Down Expand Up @@ -1814,6 +1822,8 @@ def schema_for_type(cls, name, typ: Any, field_info: PydanticFieldInfo):
return ""
embedded_cls = embedded_cls[0]
schema = cls.schema_for_type(name, embedded_cls, field_info)
elif typ is bool:
schema = f"{name} TAG"
elif any(issubclass(typ, t) for t in NUMERIC_TYPES):
vector_options: Optional[VectorFieldOptions] = getattr(
field_info, "vector_options", None
Expand Down Expand Up @@ -2121,6 +2131,8 @@ def schema_for_type(
raise sortable_tag_error
if case_sensitive is True:
schema += " CASESENSITIVE"
elif typ is bool:
schema = f"{path} AS {index_field_name} TAG"
elif any(issubclass(typ, t) for t in NUMERIC_TYPES):
schema = f"{path} AS {index_field_name} NUMERIC"
elif issubclass(typ, str):
Expand Down
23 changes: 23 additions & 0 deletions tests/test_json_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -971,3 +971,26 @@ async def test_xfix_queries(m):

result = await m.Member.find(m.Member.bio % "*ack*").first()
assert result.first_name == "Steve"


@py_test_mark_asyncio
async def test_boolean():
class Example(JsonModel):
b: bool = Field(index=True)
d: datetime.date = Field(index=True)
name: str = Field(index=True)

await Migrator().run()

ex = Example(b=True, name="steve", d=datetime.date.today())
exFalse = Example(b=False, name="foo", d=datetime.date.today())
await ex.save()
await exFalse.save()
res = await Example.find(Example.b == True).first()
assert res.name == "steve"

res = await Example.find(Example.b == False).first()
assert res.name == "foo"

res = await Example.find(Example.d == ex.d and Example.b == True).first()
assert res.name == ex.name