From b80addbb13c5385d0d29ae9ceb0264cb7465d35a Mon Sep 17 00:00:00 2001 From: Andrew Brookins Date: Wed, 29 Sep 2021 20:32:44 -0700 Subject: [PATCH] Test embedded Model.DoesNotExist error --- redis_developer/orm/model.py | 23 +++++++++-------------- tests/test_hash_model.py | 6 ++++++ 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/redis_developer/orm/model.py b/redis_developer/orm/model.py index 29a2d5e..5c40949 100644 --- a/redis_developer/orm/model.py +++ b/redis_developer/orm/model.py @@ -92,11 +92,7 @@ class RedisModelError(Exception): class NotFoundError(Exception): - """ - A query found no results. - - TODO: embed in Model class? - """ + """A query found no results.""" class Operators(Enum): @@ -671,16 +667,15 @@ class ModelMeta(ModelMetaclass): key = f"{new_class.__module__}.{new_class.__name__}" model_registry[key] = new_class - if not hasattr(new_class, 'NotFoundError'): - new_class.add_to_class( + new_class.add_to_class( + 'NotFoundError', + subclass_exception( 'NotFoundError', - subclass_exception( - 'NotFoundError', - tuple( - x.NotFoundError for x in bases if hasattr(x, '_meta') and not issubclass(x, abc.ABC) - ), - attrs['__module__'], - attached_to=new_class)) + tuple( + x.NotFoundError for x in bases if hasattr(x, '_meta') and not issubclass(x, abc.ABC) + ) or (NotFoundError,), + attrs['__module__'], + attached_to=new_class)) # Create proxies for each model field so that we can use the field # in queries, like Model.get(Model.field_name == 1) diff --git a/tests/test_hash_model.py b/tests/test_hash_model.py index 02371cb..d26ebfe 100644 --- a/tests/test_hash_model.py +++ b/tests/test_hash_model.py @@ -310,6 +310,12 @@ def test_sorting(members): Member.find().sort_by('join_date').all() +def test_not_found(): + with pytest.raises(Member.NotFoundError): + # This ID does not exist. + Member.get(1000) + + def test_schema(): class Address(BaseHashModel): a_string: str = Field(index=True)