This repository has been archived by the owner on Aug 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored exception handling, and added top level integration tests (#…
…40)
- Loading branch information
Showing
9 changed files
with
162 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,24 @@ | ||
|
||
|
||
def test_create_user(test_client): | ||
resp = test_client.post("/cp/create_user", json={"namespace_pathname": "create_user_1"}) | ||
assert resp.status_code == 200 | ||
|
||
|
||
def test_create_user_existing(test_client): | ||
test_client.post("/cp/create_user", json={"namespace_pathname": "create_user_2"}) | ||
resp = test_client.post("/cp/create_user", json={"namespace_pathname": "create_user_2"}) | ||
assert resp.status_code == 400 | ||
assert resp.json["error_code"] == "namespace_exists" | ||
|
||
|
||
def test_create_corpus(test_client): | ||
resp = test_client.post("/cp/create_corpus", json={"corpus_pathname": "create_user_1:create_corpus_1"}) | ||
assert resp.status_code == 200 | ||
|
||
|
||
def test_create_corpus_existing(test_client): | ||
test_client.post("/cp/create_corpus", json={"corpus_pathname": "create_user_2:create_corpus_2"}) | ||
resp = test_client.post("/cp/create_corpus", json={"corpus_pathname": "create_user_2:create_corpus_2"}) | ||
assert resp.status_code == 400 | ||
assert resp.json["error_code"] == "namespace_exists" |
This file contains 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,22 @@ | ||
import time | ||
|
||
|
||
def test_memorize_then_recall(test_client): | ||
namespace_pathname = "memorize" | ||
corpus_pathname = namespace_pathname + ":memorize_1" | ||
resp1 = test_client.post("/cp/create_user", json={"namespace_pathname": namespace_pathname}) | ||
print(resp1) | ||
resp2 = test_client.post("/cp/create_corpus", json={"corpus_pathname": corpus_pathname}) | ||
print(resp2) | ||
|
||
resp3 = test_client.post( | ||
"/dp/memorize", json={"corpus_pathname": corpus_pathname, "document": "What's MeMaS", "citation": {}}) | ||
assert resp3.status_code == 200 | ||
assert resp3.json["success"] | ||
|
||
time.sleep(1) | ||
|
||
resp4 = test_client.post("/dp/recall", json={"namespace_pathname": namespace_pathname, "clue": "What's MeMaS"}) | ||
assert resp4.status_code == 200 | ||
assert len(resp4.json) == 1 | ||
assert resp4.json[0]["document"] == "What's MeMaS" |
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,25 +1,68 @@ | ||
from enum import Enum | ||
|
||
|
||
class BadArgumentException(Exception): | ||
class MemasInternalException(Exception): | ||
def __init__(self, *args: object) -> None: | ||
super().__init__(*args) | ||
|
||
|
||
class IllegalStateException(MemasInternalException): | ||
def __init__(self, msg: str) -> None: | ||
super().__init__(msg) | ||
|
||
|
||
class IllegalNameException(BadArgumentException): | ||
def __init__(self, pathname: str) -> None: | ||
super().__init__(f"\"{pathname}\" is not a valid pathname") | ||
class StatusCode(Enum): | ||
# The request could not be understood by the server due to incorrect syntax. The client SHOULD NOT repeat the request without modifications. | ||
BAD_REQUEST = 400 | ||
# Indicates that the request requires user authentication information. The client MAY repeat the request with a suitable Authorization header field | ||
UNAUTHORIZED = 401 | ||
# Unauthorized request. The client does not have access rights to the content. Unlike 401, the client’s identity is known to the server. | ||
FORBIDDEN = 403 | ||
# The server can not find the requested resource. | ||
NOT_FOUND = 404 | ||
# The request HTTP method is known by the server but has been disabled and cannot be used for that resource. | ||
METHOD_NOT_ALLOWED = 405 | ||
|
||
|
||
class NamespaceExistsException(Exception): | ||
def __init__(self, pathname: str) -> None: | ||
super().__init__(f"\"{pathname}\" already exists") | ||
# Each memas error needs to have an unique error code | ||
class ErrorCode(Enum): | ||
NamespaceExists = "namespace_exists" | ||
NamespaceDoesNotExist = "namespace_does_not_exist" | ||
NamespaceIllegalName = "namespace_illegal_name" | ||
|
||
|
||
class NotProperlyInitializedException(Exception): | ||
def __init__(self, msg: str) -> None: | ||
class MemasException(Exception): | ||
def __init__(self, error_code: ErrorCode, msg: str, additional_details: str = None) -> None: | ||
super().__init__(msg) | ||
self.status_code: StatusCode = StatusCode.BAD_REQUEST | ||
self.error_code: ErrorCode = error_code | ||
self.msg: str = msg | ||
self.additional_details: str = additional_details | ||
|
||
def return_obj(self): | ||
resp = {"error_code": self.error_code.value, "msg": self.msg} | ||
if self.additional_details: | ||
resp["details"] = self.additional_details | ||
return resp | ||
|
||
|
||
class IllegalNameException(MemasException): | ||
def __init__(self, pathname: str) -> None: | ||
super().__init__(ErrorCode.NamespaceIllegalName, f"\"{pathname}\" is not a valid pathname") | ||
|
||
|
||
class NamespaceExistsException(MemasException): | ||
def __init__(self, pathname: str, additional_details: str = None) -> None: | ||
super().__init__(ErrorCode.NamespaceExists, f"\"{pathname}\" already exists", additional_details) | ||
|
||
|
||
class NamespaceDoesNotExistException(MemasException): | ||
def __init__(self, pathname: str, additional_details: str = None) -> None: | ||
super().__init__(ErrorCode.NamespaceDoesNotExist, | ||
f"\"{pathname}\" does not exists, you need to create the resource first", additional_details) | ||
|
||
|
||
# TODO: properly specify this exception type | ||
class SentenceLengthOverflowException(Exception): | ||
def __init__(self, sentence_len: int) -> None: | ||
super().__init__("Sentence length is {len} which exceededs limit".format(len=sentence_len)) |
This file contains 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
This file contains 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,15 @@ | ||
import pytest | ||
from memas.interface.exceptions import ErrorCode, MemasException | ||
|
||
|
||
def test_memas_exception_output_object(): | ||
e = MemasException(ErrorCode.NamespaceExists, "test message", additional_details="test details") | ||
|
||
assert e.return_obj() == {"error_code": ErrorCode.NamespaceExists.value, | ||
"msg": "test message", "details": "test details"} | ||
|
||
|
||
def test_memas_exception_details(): | ||
e = MemasException(ErrorCode.NamespaceExists, "test message") | ||
|
||
assert "details" not in e.return_obj() |
This file contains 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