This commit is contained in:
Andrew Brookins 2021-10-20 23:31:11 -07:00
parent 2ffd4e6f5a
commit 7155b9af35
3 changed files with 92 additions and 59 deletions

View file

@ -25,6 +25,7 @@ def key_prefix(redis):
yield key_prefix yield key_prefix
_delete_test_keys(key_prefix, redis) _delete_test_keys(key_prefix, redis)
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def delete_test_keys(redis, request, key_prefix): def delete_test_keys(redis, request, key_prefix):
_delete_test_keys(key_prefix, redis) _delete_test_keys(key_prefix, redis)

View file

@ -26,13 +26,11 @@ def m(key_prefix):
class Meta: class Meta:
global_key_prefix = key_prefix global_key_prefix = key_prefix
class Order(BaseHashModel): class Order(BaseHashModel):
total: decimal.Decimal total: decimal.Decimal
currency: str currency: str
created_on: datetime.datetime created_on: datetime.datetime
class Member(BaseHashModel): class Member(BaseHashModel):
first_name: str = Field(index=True) first_name: str = Field(index=True)
last_name: str = Field(index=True) last_name: str = Field(index=True)
@ -46,8 +44,9 @@ def m(key_prefix):
Migrator().run() Migrator().run()
return namedtuple('Models', ['BaseHashModel', 'Order', 'Member'])( return namedtuple("Models", ["BaseHashModel", "Order", "Member"])(
BaseHashModel, Order, Member) BaseHashModel, Order, Member
)
@pytest.fixture @pytest.fixture
@ -130,6 +129,7 @@ def test_raises_error_with_embedded_models(m):
postal_code: str postal_code: str
with pytest.raises(RedisModelError): with pytest.raises(RedisModelError):
class InvalidMember(m.BaseHashModel): class InvalidMember(m.BaseHashModel):
address: Address address: Address
@ -169,7 +169,7 @@ def test_updates_a_model(members, m):
def test_paginate_query(members, m): def test_paginate_query(members, m):
member1, member2, member3 = members member1, member2, member3 = members
actual = m.Member.find().sort_by('age').all(batch_size=1) actual = m.Member.find().sort_by("age").all(batch_size=1)
assert actual == [member2, member1, member3] assert actual == [member2, member1, member3]
@ -202,7 +202,7 @@ def test_access_result_by_index_not_cached(members, m):
def test_exact_match_queries(members, m): def test_exact_match_queries(members, m):
member1, member2, member3 = members member1, member2, member3 = members
actual = m.Member.find(m.Member.last_name == "Brookins").sort_by('age').all() actual = m.Member.find(m.Member.last_name == "Brookins").sort_by("age").all()
assert actual == [member2, member1] assert actual == [member2, member1]
actual = m.Member.find( actual = m.Member.find(
@ -216,10 +216,14 @@ def test_exact_match_queries(members, m):
actual = m.Member.find(m.Member.last_name != "Brookins").all() actual = m.Member.find(m.Member.last_name != "Brookins").all()
assert actual == [member3] assert actual == [member3]
actual = m.Member.find( actual = (
m.Member.find(
(m.Member.last_name == "Brookins") & (m.Member.first_name == "Andrew") (m.Member.last_name == "Brookins") & (m.Member.first_name == "Andrew")
| (m.Member.first_name == "Kim") | (m.Member.first_name == "Kim")
).sort_by('age').all() )
.sort_by("age")
.all()
)
assert actual == [member2, member1] assert actual == [member2, member1]
actual = m.Member.find( actual = m.Member.find(
@ -231,20 +235,28 @@ def test_exact_match_queries(members, m):
def test_recursive_query_resolution(members, m): def test_recursive_query_resolution(members, m):
member1, member2, member3 = members member1, member2, member3 = members
actual = m.Member.find( actual = (
m.Member.find(
(m.Member.last_name == "Brookins") (m.Member.last_name == "Brookins")
| (m.Member.age == 100) & (m.Member.last_name == "Smith") | (m.Member.age == 100) & (m.Member.last_name == "Smith")
).sort_by('age').all() )
.sort_by("age")
.all()
)
assert actual == [member2, member1, member3] assert actual == [member2, member1, member3]
def test_tag_queries_boolean_logic(members, m): def test_tag_queries_boolean_logic(members, m):
member1, member2, member3 = members member1, member2, member3 = members
actual = m.Member.find( actual = (
m.Member.find(
(m.Member.first_name == "Andrew") & (m.Member.last_name == "Brookins") (m.Member.first_name == "Andrew") & (m.Member.last_name == "Brookins")
| (m.Member.last_name == "Smith") | (m.Member.last_name == "Smith")
).sort_by('age').all() )
.sort_by("age")
.all()
)
assert actual == [member1, member3] assert actual == [member1, member3]
@ -267,8 +279,12 @@ def test_tag_queries_punctuation(m):
) )
member2.save() member2.save()
assert m.Member.find(m.Member.first_name == "Andrew, the Michael").first() == member1 assert (
assert m.Member.find(m.Member.last_name == "St. Brookins-on-Pier").first() == member1 m.Member.find(m.Member.first_name == "Andrew, the Michael").first() == member1
)
assert (
m.Member.find(m.Member.last_name == "St. Brookins-on-Pier").first() == member1
)
# Notice that when we index and query multiple values that use the internal # Notice that when we index and query multiple values that use the internal
# TAG separator for single-value exact-match fields, like an indexed string, # TAG separator for single-value exact-match fields, like an indexed string,
@ -341,7 +357,7 @@ def test_tag_queries_negation(members, m):
~(m.Member.first_name == "Andrew") & (m.Member.last_name == "Brookins") ~(m.Member.first_name == "Andrew") & (m.Member.last_name == "Brookins")
| (m.Member.last_name == "Smith") | (m.Member.last_name == "Smith")
) )
assert query.sort_by('age').all() == [member2, member3] assert query.sort_by("age").all() == [member2, member3]
actual = m.Member.find( actual = m.Member.find(
(m.Member.first_name == "Andrew") & ~(m.Member.last_name == "Brookins") (m.Member.first_name == "Andrew") & ~(m.Member.last_name == "Brookins")
@ -355,7 +371,7 @@ def test_numeric_queries(members, m):
actual = m.Member.find(m.Member.age == 34).all() actual = m.Member.find(m.Member.age == 34).all()
assert actual == [member2] assert actual == [member2]
actual = m.Member.find(m.Member.age > 34).sort_by('age').all() actual = m.Member.find(m.Member.age > 34).sort_by("age").all()
assert actual == [member1, member3] assert actual == [member1, member3]
actual = m.Member.find(m.Member.age < 35).all() actual = m.Member.find(m.Member.age < 35).all()
@ -367,15 +383,13 @@ def test_numeric_queries(members, m):
actual = m.Member.find(m.Member.age >= 100).all() actual = m.Member.find(m.Member.age >= 100).all()
assert actual == [member3] assert actual == [member3]
actual = m.Member.find(m.Member.age != 34).sort_by('age').all() actual = m.Member.find(m.Member.age != 34).sort_by("age").all()
assert actual == [member1, member3] assert actual == [member1, member3]
actual = m.Member.find(~(m.Member.age == 100)).sort_by('age').all() actual = m.Member.find(~(m.Member.age == 100)).sort_by("age").all()
assert actual == [member2, member1] assert actual == [member2, member1]
actual = m.Member.find( actual = m.Member.find(m.Member.age > 30, m.Member.age < 40).sort_by("age").all()
m.Member.age > 30, m.Member.age < 40
).sort_by('age').all()
assert actual == [member2, member1] assert actual == [member2, member1]

View file

@ -27,7 +27,6 @@ def m(key_prefix):
class Meta: class Meta:
global_key_prefix = key_prefix global_key_prefix = key_prefix
class Note(EmbeddedJsonModel): class Note(EmbeddedJsonModel):
# TODO: This was going to be a full-text search example, but # TODO: This was going to be a full-text search example, but
# we can't index embedded documents for full-text search in # we can't index embedded documents for full-text search in
@ -35,7 +34,6 @@ def m(key_prefix):
description: str = Field(index=True) description: str = Field(index=True)
created_on: datetime.datetime created_on: datetime.datetime
class Address(EmbeddedJsonModel): class Address(EmbeddedJsonModel):
address_line_1: str address_line_1: str
address_line_2: Optional[str] address_line_2: Optional[str]
@ -45,17 +43,14 @@ def m(key_prefix):
postal_code: str = Field(index=True) postal_code: str = Field(index=True)
note: Optional[Note] note: Optional[Note]
class Item(EmbeddedJsonModel): class Item(EmbeddedJsonModel):
price: decimal.Decimal price: decimal.Decimal
name: str = Field(index=True) name: str = Field(index=True)
class Order(EmbeddedJsonModel): class Order(EmbeddedJsonModel):
items: List[Item] items: List[Item]
created_on: datetime.datetime created_on: datetime.datetime
class Member(BaseJsonModel): class Member(BaseJsonModel):
first_name: str = Field(index=True) first_name: str = Field(index=True)
last_name: str = Field(index=True) last_name: str = Field(index=True)
@ -72,8 +67,9 @@ def m(key_prefix):
Migrator().run() Migrator().run()
return namedtuple('Models', ['BaseJsonModel', 'Note', 'Address', 'Item', 'Order', 'Member'])( return namedtuple(
BaseJsonModel, Note, Address, Item, Order, Member) "Models", ["BaseJsonModel", "Note", "Address", "Item", "Order", "Member"]
)(BaseJsonModel, Note, Address, Item, Order, Member)
@pytest.fixture() @pytest.fixture()
@ -213,12 +209,14 @@ def test_updates_a_model(members, m):
# Or, updating a field in an embedded model: # Or, updating a field in an embedded model:
member2.update(address__city="Happy Valley") member2.update(address__city="Happy Valley")
assert m.Member.find(m.Member.pk == member2.pk).first().address.city == "Happy Valley" assert (
m.Member.find(m.Member.pk == member2.pk).first().address.city == "Happy Valley"
)
def test_paginate_query(members, m): def test_paginate_query(members, m):
member1, member2, member3 = members member1, member2, member3 = members
actual = m.Member.find().sort_by('age').all(batch_size=1) actual = m.Member.find().sort_by("age").all(batch_size=1)
assert actual == [member2, member1, member3] assert actual == [member2, member1, member3]
@ -250,7 +248,11 @@ def test_access_result_by_index_not_cached(members, m):
def test_in_query(members, m): def test_in_query(members, m):
member1, member2, member3 = members member1, member2, member3 = members
actual = m.Member.find(m.Member.pk << [member1.pk, member2.pk, member3.pk]).sort_by('age').all() actual = (
m.Member.find(m.Member.pk << [member1.pk, member2.pk, member3.pk])
.sort_by("age")
.all()
)
assert actual == [member2, member1, member3] assert actual == [member2, member1, member3]
@ -272,7 +274,7 @@ def test_update_query(members, m):
def test_exact_match_queries(members, m): def test_exact_match_queries(members, m):
member1, member2, member3 = members member1, member2, member3 = members
actual = m.Member.find(m.Member.last_name == "Brookins").sort_by('age').all() actual = m.Member.find(m.Member.last_name == "Brookins").sort_by("age").all()
assert actual == [member2, member1] assert actual == [member2, member1]
actual = m.Member.find( actual = m.Member.find(
@ -286,10 +288,14 @@ def test_exact_match_queries(members, m):
actual = m.Member.find(m.Member.last_name != "Brookins").all() actual = m.Member.find(m.Member.last_name != "Brookins").all()
assert actual == [member3] assert actual == [member3]
actual = m.Member.find( actual = (
m.Member.find(
(m.Member.last_name == "Brookins") & (m.Member.first_name == "Andrew") (m.Member.last_name == "Brookins") & (m.Member.first_name == "Andrew")
| (m.Member.first_name == "Kim") | (m.Member.first_name == "Kim")
).sort_by('age').all() )
.sort_by("age")
.all()
)
assert actual == [member2, member1] assert actual == [member2, member1]
actual = m.Member.find( actual = m.Member.find(
@ -297,17 +303,21 @@ def test_exact_match_queries(members, m):
).all() ).all()
assert actual == [member2] assert actual == [member2]
actual = m.Member.find(m.Member.address.city == "Portland").sort_by('age').all() actual = m.Member.find(m.Member.address.city == "Portland").sort_by("age").all()
assert actual == [member2, member1, member3] assert actual == [member2, member1, member3]
def test_recursive_query_expression_resolution(members, m): def test_recursive_query_expression_resolution(members, m):
member1, member2, member3 = members member1, member2, member3 = members
actual = m.Member.find( actual = (
m.Member.find(
(m.Member.last_name == "Brookins") (m.Member.last_name == "Brookins")
| (m.Member.age == 100) & (m.Member.last_name == "Smith") | (m.Member.age == 100) & (m.Member.last_name == "Smith")
).sort_by('age').all() )
.sort_by("age")
.all()
)
assert actual == [member2, member1, member3] assert actual == [member2, member1, member3]
@ -338,7 +348,7 @@ def test_full_text_search(members, m):
member1.update(bio="Hates sunsets, likes beaches") member1.update(bio="Hates sunsets, likes beaches")
member2.update(bio="Hates beaches, likes forests") member2.update(bio="Hates beaches, likes forests")
actual = m.Member.find(m.Member.bio % "beaches").sort_by('age').all() actual = m.Member.find(m.Member.bio % "beaches").sort_by("age").all()
assert actual == [member2, member1] assert actual == [member2, member1]
actual = m.Member.find(m.Member.bio % "forests").all() actual = m.Member.find(m.Member.bio % "forests").all()
@ -348,10 +358,14 @@ def test_full_text_search(members, m):
def test_tag_queries_boolean_logic(members, m): def test_tag_queries_boolean_logic(members, m):
member1, member2, member3 = members member1, member2, member3 = members
actual = m.Member.find( actual = (
m.Member.find(
(m.Member.first_name == "Andrew") & (m.Member.last_name == "Brookins") (m.Member.first_name == "Andrew") & (m.Member.last_name == "Brookins")
| (m.Member.last_name == "Smith") | (m.Member.last_name == "Smith")
).sort_by('age').all() )
.sort_by("age")
.all()
)
assert actual == [member1, member3] assert actual == [member1, member3]
@ -376,8 +390,12 @@ def test_tag_queries_punctuation(address, m):
) )
member2.save() member2.save()
assert m.Member.find(m.Member.first_name == "Andrew, the Michael").first() == member1 assert (
assert m.Member.find(m.Member.last_name == "St. Brookins-on-Pier").first() == member1 m.Member.find(m.Member.first_name == "Andrew, the Michael").first() == member1
)
assert (
m.Member.find(m.Member.last_name == "St. Brookins-on-Pier").first() == member1
)
# Notice that when we index and query multiple values that use the internal # Notice that when we index and query multiple values that use the internal
# TAG separator for single-value exact-match fields, like an indexed string, # TAG separator for single-value exact-match fields, like an indexed string,
@ -450,7 +468,7 @@ def test_tag_queries_negation(members, m):
~(m.Member.first_name == "Andrew") & (m.Member.last_name == "Brookins") ~(m.Member.first_name == "Andrew") & (m.Member.last_name == "Brookins")
| (m.Member.last_name == "Smith") | (m.Member.last_name == "Smith")
) )
assert query.sort_by('age').all() == [member2, member3] assert query.sort_by("age").all() == [member2, member3]
actual = m.Member.find( actual = m.Member.find(
(m.Member.first_name == "Andrew") & ~(m.Member.last_name == "Brookins") (m.Member.first_name == "Andrew") & ~(m.Member.last_name == "Brookins")
@ -476,13 +494,13 @@ def test_numeric_queries(members, m):
actual = m.Member.find(m.Member.age >= 100).all() actual = m.Member.find(m.Member.age >= 100).all()
assert actual == [member3] assert actual == [member3]
actual = m.Member.find(~(m.Member.age == 100)).sort_by('age').all() actual = m.Member.find(~(m.Member.age == 100)).sort_by("age").all()
assert actual == [member2, member1] assert actual == [member2, member1]
actual = m.Member.find(m.Member.age > 30, m.Member.age < 40).sort_by('age').all() actual = m.Member.find(m.Member.age > 30, m.Member.age < 40).sort_by("age").all()
assert actual == [member2, member1] assert actual == [member2, member1]
actual = m.Member.find(m.Member.age != 34).sort_by('age').all() actual = m.Member.find(m.Member.age != 34).sort_by("age").all()
assert actual == [member1, member3] assert actual == [member1, member3]
@ -521,7 +539,7 @@ def test_list_field_limitations(m):
with pytest.raises(RedisModelError): with pytest.raises(RedisModelError):
class SortableFullTextSearchAlchemicalWitch(BaseJsonModel): class SortableFullTextSearchAlchemicalWitch(m.BaseJsonModel):
# We don't support indexing a list of strings for full-text search # We don't support indexing a list of strings for full-text search
# queries. Support for this feature is not planned. # queries. Support for this feature is not planned.
potions: List[str] = Field(index=True, full_text_search=True) potions: List[str] = Field(index=True, full_text_search=True)