-
Notifications
You must be signed in to change notification settings - Fork 26
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
Draft
Beeram12
wants to merge
2
commits into
XortexAI:main
Choose a base branch
from
Beeram12:feature/unit-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,7 +53,6 @@ logs/ | |
| # Dev-only folders | ||
| frontend/ | ||
| scripts/ | ||
| tests/ | ||
| benchmarks/ | ||
| LongMemEval/ | ||
| backboard/ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| 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_message_attribute_is_accessible(self): | ||
| err = XMemError("hello") | ||
| assert err.message == "hello" | ||
|
|
||
| def test_operation_is_none_by_default(self): | ||
| err = XMemError("message") | ||
| assert err.operation is None | ||
|
|
||
| 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_vector_store_validation_error_is_caught_as_vector_store_error(self): | ||
| with pytest.raises(VectorStoreError): | ||
| raise VectorStoreValidationError("invalid metadata") | ||
|
|
||
| def test_validation_error_is_not_a_vector_store_error(self): | ||
| err = ValidationError("bad input") | ||
| assert not isinstance(err, VectorStoreError) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.