-
Notifications
You must be signed in to change notification settings - Fork 12
Add unit tests for exception handling #151
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,7 +53,6 @@ logs/ | |
| # Dev-only folders | ||
| frontend/ | ||
| scripts/ | ||
| tests/ | ||
| benchmarks/ | ||
| LongMemEval/ | ||
| backboard/ | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,149 @@ | ||||||||||||||||||||||
| import pytest | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| from src.utils.exceptions import ( | ||||||||||||||||||||||
| XMemError, | ||||||||||||||||||||||
| ConfigurationError, | ||||||||||||||||||||||
| ValidationError, | ||||||||||||||||||||||
| VectorStoreError, | ||||||||||||||||||||||
| VectorStoreConnectionError, | ||||||||||||||||||||||
| VectorStoreValidationError, | ||||||||||||||||||||||
| VectorNotFoundError, | ||||||||||||||||||||||
| DatabaseError, | ||||||||||||||||||||||
| DatabaseConnectionError, | ||||||||||||||||||||||
| LLMError, | ||||||||||||||||||||||
| LLMRateLimitError, | ||||||||||||||||||||||
| LLMContextLengthError, | ||||||||||||||||||||||
| EmbeddingError, | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| class TestXMemError: | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_str_includes_operation_when_provided(self): | ||||||||||||||||||||||
| err = XMemError("something broke", operation="save") | ||||||||||||||||||||||
| assert str(err) == "[save] something broke" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_str_returns_message_only_when_no_operation(self): | ||||||||||||||||||||||
| err = XMemError("something broke") | ||||||||||||||||||||||
| assert str(err) == "something broke" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_mssg_attribute_is_accessible(self): | ||||||||||||||||||||||
| err = XMemError("hello") | ||||||||||||||||||||||
| assert str(err) == "hello" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_operation_is_none_by_default(self): | ||||||||||||||||||||||
| err = XMemError("message") | ||||||||||||||||||||||
| assert err.operation == None | ||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to PEP 8, comparisons to singletons like
Suggested change
References
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_operation_attribute_is_accessible(self): | ||||||||||||||||||||||
| err = XMemError("msg", operation="delete") | ||||||||||||||||||||||
| assert err.operation == "delete" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_details_defaults_to_empty_dict_when_not_provided(self): | ||||||||||||||||||||||
| err = XMemError("msg") | ||||||||||||||||||||||
| assert err.details == {} | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_details_defaults_to_empty_dict_when_none_passed(self): | ||||||||||||||||||||||
| err = XMemError("msg", details=None) | ||||||||||||||||||||||
| assert err.details == {} | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_details_stores_provided_dict(self): | ||||||||||||||||||||||
| err = XMemError("msg", details={"user_id": "123", "count": 5}) | ||||||||||||||||||||||
| assert err.details["user_id"] == "123" | ||||||||||||||||||||||
| assert err.details["count"] == 5 | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_repr_contains_class_name(self): | ||||||||||||||||||||||
| err = XMemError("msg", operation="op") | ||||||||||||||||||||||
| assert "XMemError" in repr(err) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_repr_contains_message_and_operation(self): | ||||||||||||||||||||||
| err = XMemError("msg", operation="op") | ||||||||||||||||||||||
| assert "msg" in repr(err) | ||||||||||||||||||||||
| assert "op" in repr(err) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| class TestToDict: | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_to_dict_has_required_keys(self): | ||||||||||||||||||||||
| err = XMemError("msg") | ||||||||||||||||||||||
| result = err.to_dict() | ||||||||||||||||||||||
| assert "error" in result | ||||||||||||||||||||||
| assert "message" in result | ||||||||||||||||||||||
| assert "operation" in result | ||||||||||||||||||||||
| assert "details" in result | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_to_dict_error_key_is_class_name(self): | ||||||||||||||||||||||
| err = XMemError("msg") | ||||||||||||||||||||||
| assert err.to_dict()["error"] == "XMemError" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_to_dict_subclass_uses_subclass_name(self): | ||||||||||||||||||||||
| err = ValidationError("bad input") | ||||||||||||||||||||||
| assert err.to_dict()["error"] == "ValidationError" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_to_dict_message_matches(self): | ||||||||||||||||||||||
| err = XMemError("something went wrong", operation="fetch") | ||||||||||||||||||||||
| assert err.to_dict()["message"] == "something went wrong" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_to_dict_operation_matches(self): | ||||||||||||||||||||||
| err = XMemError("msg", operation="fetch") | ||||||||||||||||||||||
| assert err.to_dict()["operation"] == "fetch" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_to_dict_details_matches(self): | ||||||||||||||||||||||
| err = XMemError("msg", details={"key": "value"}) | ||||||||||||||||||||||
| assert err.to_dict()["details"] == {"key": "value"} | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| class TestExceptionHierarchy: | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_configuration_error_is_caught_as_xmem_error(self): | ||||||||||||||||||||||
| with pytest.raises(XMemError): | ||||||||||||||||||||||
| raise ConfigurationError("missing key") | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_validation_error_is_caught_as_xmem_error(self): | ||||||||||||||||||||||
| with pytest.raises(XMemError): | ||||||||||||||||||||||
| raise ValidationError("bad input") | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_vector_store_error_is_caught_as_xmem_error(self): | ||||||||||||||||||||||
| with pytest.raises(XMemError): | ||||||||||||||||||||||
| raise VectorStoreError("store failed") | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_vector_store_connection_error_is_caught_as_vector_store_error( | ||||||||||||||||||||||
| self): | ||||||||||||||||||||||
| with pytest.raises(VectorStoreError): | ||||||||||||||||||||||
| raise VectorStoreConnectionError("cannot connect") | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_vector_store_connection_error_is_caught_as_xmem_error(self): | ||||||||||||||||||||||
| with pytest.raises(XMemError): | ||||||||||||||||||||||
| raise VectorStoreConnectionError("cannot connect") | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_vector_not_found_error_is_caught_as_vector_store_error(self): | ||||||||||||||||||||||
| with pytest.raises(VectorStoreError): | ||||||||||||||||||||||
| raise VectorNotFoundError("id not found") | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_database_error_is_caught_as_xmem_error(self): | ||||||||||||||||||||||
| with pytest.raises(XMemError): | ||||||||||||||||||||||
| raise DatabaseError("db failed") | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_database_connection_error_is_caught_as_database_error(self): | ||||||||||||||||||||||
| with pytest.raises(DatabaseError): | ||||||||||||||||||||||
| raise DatabaseConnectionError("cannot connect to mongo") | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_llm_error_is_caught_as_xmem_error(self): | ||||||||||||||||||||||
| with pytest.raises(XMemError): | ||||||||||||||||||||||
| raise LLMError("llm failed") | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_llm_rate_limit_error_is_caught_as_llm_error(self): | ||||||||||||||||||||||
| with pytest.raises(LLMError): | ||||||||||||||||||||||
| raise LLMRateLimitError("rate limited") | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_llm_context_length_error_is_caught_as_llm_error(self): | ||||||||||||||||||||||
| with pytest.raises(LLMError): | ||||||||||||||||||||||
| raise LLMContextLengthError("too long") | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_embedding_error_is_caught_as_xmem_error(self): | ||||||||||||||||||||||
| with pytest.raises(XMemError): | ||||||||||||||||||||||
| raise EmbeddingError("embed failed") | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_validation_error_is_not_a_vector_store_error(self): | ||||||||||||||||||||||
| err = ValidationError("bad input") | ||||||||||||||||||||||
| assert not isinstance(err, VectorStoreError) | ||||||||||||||||||||||
|
Comment on lines
+147
to
+149
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test name
test_mssg_attribute_is_accessiblecontains a typo (mssg) and the implementation redundantly checks the string representation instead of the actualmessageattribute. It should be updated to verify the attribute directly.