Skip to content

Commit 80001db

Browse files
committed
add unit tests
Signed-off-by: wiseaidev <business@wiseai.dev>
1 parent 0452bfd commit 80001db

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

tests/test_hash_model.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,3 +681,59 @@ class Address(m.BaseHashModel):
681681
Address.redisearch_schema()
682682
== 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"
683683
)
684+
685+
686+
@py_test_mark_asyncio
687+
async def test_primary_key_model_error(m):
688+
689+
class Customer(m.BaseHashModel):
690+
id: int = Field(primary_key=True, index=True)
691+
first_name: str = Field(primary_key=True, index=True)
692+
last_name: str
693+
bio: Optional[str]
694+
695+
await Migrator().run()
696+
697+
with pytest.raises(RedisModelError, match="You must define only one primary key for a model"):
698+
_ = Customer(
699+
id=0,
700+
first_name="Mahmoud",
701+
last_name="Harmouch",
702+
bio="Python developer, wanna work at Redis, Inc."
703+
)
704+
705+
706+
@py_test_mark_asyncio
707+
async def test_primary_pk_exists(m):
708+
709+
class Customer1(m.BaseHashModel):
710+
id: int
711+
first_name: str
712+
last_name: str
713+
bio: Optional[str]
714+
715+
class Customer2(m.BaseHashModel):
716+
id: int = Field(primary_key=True, index=True)
717+
first_name: str
718+
last_name: str
719+
bio: Optional[str]
720+
721+
await Migrator().run()
722+
723+
customer = Customer1(
724+
id=0,
725+
first_name="Mahmoud",
726+
last_name="Harmouch",
727+
bio="Python developer, wanna work at Redis, Inc."
728+
)
729+
730+
assert 'pk' in customer.__fields__
731+
732+
customer = Customer2(
733+
id=1,
734+
first_name="Kim",
735+
last_name="Brookins",
736+
bio="This is member 2 who can be quite anxious until you get to know them.",
737+
)
738+
739+
assert 'pk' not in customer.__fields__

0 commit comments

Comments
 (0)