diff --git a/aiosqs/tests/cases.py b/aiosqs/tests/cases.py new file mode 100644 index 0000000..2026843 --- /dev/null +++ b/aiosqs/tests/cases.py @@ -0,0 +1,17 @@ +import unittest + +from aiosqs.parser import parse_xml_result_response +from aiosqs.tests.fixtures import load_fixture + + +class ActionTestCase(unittest.TestCase): + maxDiff = None + action: str = None + + def parseXMLResponse(self, fixture_name: str): + self.assertIsNotNone(self.action) + res = parse_xml_result_response( + action=self.action, + body=load_fixture(fixture_name), + ) + return res diff --git a/aiosqs/tests/utils.py b/aiosqs/tests/fixtures.py similarity index 100% rename from aiosqs/tests/utils.py rename to aiosqs/tests/fixtures.py diff --git a/aiosqs/tests/test_client.py b/aiosqs/tests/test_client.py index b77bf8a..791ec8c 100644 --- a/aiosqs/tests/test_client.py +++ b/aiosqs/tests/test_client.py @@ -9,7 +9,7 @@ from aiosqs.exceptions import SQSErrorResponse from aiosqs.client import SQSClient -from aiosqs.tests.utils import load_fixture +from aiosqs.tests.fixtures import load_fixture @ddt.ddt(testNameFormat=ddt.TestNameFormat.INDEX_ONLY) diff --git a/aiosqs/tests/test_delete_message.py b/aiosqs/tests/test_delete_message.py index 7021866..bf0a747 100644 --- a/aiosqs/tests/test_delete_message.py +++ b/aiosqs/tests/test_delete_message.py @@ -1,15 +1,25 @@ -import unittest +from aiosqs.exceptions import SQSErrorResponse +from aiosqs.tests.cases import ActionTestCase -from aiosqs.parser import parse_xml_result_response -from aiosqs.tests.utils import load_fixture - -class DeleteMessageTestCase(unittest.TestCase): +class DeleteMessageTestCase(ActionTestCase): action = "DeleteMessage" def test_delete_message_ok(self): - res = parse_xml_result_response( - action=self.action, - body=load_fixture("delete_message.xml"), - ) + res = self.parseXMLResponse("delete_message.xml") self.assertIsNone(res) + + def test_delete_message_error(self): + with self.assertRaises(SQSErrorResponse) as e: + self.parseXMLResponse("error.xml") + exception = e.exception + self.assertEqual(exception.request_id, "42d59b56-7407-4c4a-be0f-4c88daeea257") + self.assertEqual(exception.error.type, "Sender") + self.assertEqual(exception.error.code, "InvalidParameterValue") + self.assertEqual( + exception.error.message, + ( + "Value (quename_nonalpha) for parameter QueueName is invalid.\n " + "Must be an alphanumeric String of 1 to 80 in length." + ), + ) diff --git a/aiosqs/tests/test_errors.py b/aiosqs/tests/test_errors.py deleted file mode 100644 index 76ab8ca..0000000 --- a/aiosqs/tests/test_errors.py +++ /dev/null @@ -1,51 +0,0 @@ -import unittest - -from aiosqs.exceptions import SQSErrorResponse -from aiosqs.parser import parse_xml_result_response -from aiosqs.tests.utils import load_fixture - - -class ErrorsTestCase(unittest.TestCase): - def test_delete_message_error(self): - with self.assertRaises(SQSErrorResponse) as e: - parse_xml_result_response( - action="DeleteMessage", - body=load_fixture("error.xml"), - ) - exception = e.exception - self.assertEqual(exception.request_id, "42d59b56-7407-4c4a-be0f-4c88daeea257") - self.assertEqual(exception.error.type, "Sender") - self.assertEqual(exception.error.code, "InvalidParameterValue") - self.assertEqual( - exception.error.message, - ( - "Value (quename_nonalpha) for parameter QueueName is invalid.\n " - "Must be an alphanumeric String of 1 to 80 in length." - ), - ) - - def test_unknown_error(self): - with self.assertRaises(SQSErrorResponse) as e: - parse_xml_result_response( - action="ReceiveMessage", - body=load_fixture("error_500.xml"), - ) - exception = e.exception - self.assertEqual(exception.request_id, "9ae1448a-3d1c-4b7f-b699-f6407e9dc5f2") - self.assertEqual(exception.error.type, "Sender") - self.assertEqual(exception.error.code, "InternalFailure") - self.assertEqual( - exception.error.message, - "The request processing has failed because of an unknown error, exception or failure.", - ) - - def test_signature_does_not_match(self): - with self.assertRaises(SQSErrorResponse) as e: - parse_xml_result_response( - action="SendMessage", - body=load_fixture("error_signature.xml"), - ) - exception = e.exception - self.assertEqual(exception.request_id, "f679cf26-effe-5e59-81ca-92cf5c4c713b") - self.assertEqual(exception.error.type, "Sender") - self.assertEqual(exception.error.code, "SignatureDoesNotMatch") diff --git a/aiosqs/tests/test_get_queue_url.py b/aiosqs/tests/test_get_queue_url.py index dc0ef38..6cb610e 100644 --- a/aiosqs/tests/test_get_queue_url.py +++ b/aiosqs/tests/test_get_queue_url.py @@ -1,17 +1,11 @@ -import unittest +from aiosqs.tests.cases import ActionTestCase -from aiosqs.parser import parse_xml_result_response -from aiosqs.tests.utils import load_fixture - -class GetQueueUrlTestCase(unittest.TestCase): +class GetQueueUrlTestCase(ActionTestCase): action = "GetQueueUrl" def test_get_queue_url(self): - res = parse_xml_result_response( - action=self.action, - body=load_fixture("get_queue_url.xml"), - ) + res = self.parseXMLResponse("get_queue_url.xml") self.assertEqual( res, { @@ -20,10 +14,7 @@ def test_get_queue_url(self): ) def test_response_with_namespace(self): - res = parse_xml_result_response( - action=self.action, - body=load_fixture("get_queue_url_namespace.xml"), - ) + res = self.parseXMLResponse("get_queue_url_namespace.xml") self.assertEqual( res, { diff --git a/aiosqs/tests/test_receive_message.py b/aiosqs/tests/test_receive_message.py index 61a0347..44b32c6 100644 --- a/aiosqs/tests/test_receive_message.py +++ b/aiosqs/tests/test_receive_message.py @@ -1,25 +1,16 @@ -import unittest +from aiosqs.exceptions import SQSErrorResponse +from aiosqs.tests.cases import ActionTestCase -from aiosqs.parser import parse_xml_result_response -from aiosqs.tests.utils import load_fixture - -class ReceiveMessageTestCase(unittest.TestCase): - maxDiff = None +class ReceiveMessageTestCase(ActionTestCase): action = "ReceiveMessage" def test_no_messages(self): - res = parse_xml_result_response( - action=self.action, - body=load_fixture("receive_message_empty.xml"), - ) + res = self.parseXMLResponse("receive_message_empty.xml") self.assertIsNone(res) def test_one_message(self): - res = parse_xml_result_response( - action=self.action, - body=load_fixture("receive_message.xml"), - ) + res = self.parseXMLResponse("receive_message.xml") self.assertEqual( res, [ @@ -33,10 +24,7 @@ def test_one_message(self): ) def test_parse_oneline_xml(self): - res = parse_xml_result_response( - action=self.action, - body=load_fixture("receive_message_ugly.xml"), - ) + res = self.parseXMLResponse("receive_message_ugly.xml") self.assertEqual( res, [ @@ -50,10 +38,7 @@ def test_parse_oneline_xml(self): ) def test_many_messages(self): - res = parse_xml_result_response( - action=self.action, - body=load_fixture("receive_messages.xml"), - ) + res = self.parseXMLResponse("receive_messages.xml") self.assertEqual( res, [ @@ -90,3 +75,15 @@ def test_many_messages(self): ], ) self.assertEqual(len(res), 5) + + def test_unknown_error(self): + with self.assertRaises(SQSErrorResponse) as e: + self.parseXMLResponse("error_500.xml") + exception = e.exception + self.assertEqual(exception.request_id, "9ae1448a-3d1c-4b7f-b699-f6407e9dc5f2") + self.assertEqual(exception.error.type, "Sender") + self.assertEqual(exception.error.code, "InternalFailure") + self.assertEqual( + exception.error.message, + "The request processing has failed because of an unknown error, exception or failure.", + ) diff --git a/aiosqs/tests/test_send_message.py b/aiosqs/tests/test_send_message.py index db4cf92..8015aec 100644 --- a/aiosqs/tests/test_send_message.py +++ b/aiosqs/tests/test_send_message.py @@ -1,17 +1,12 @@ -import unittest +from aiosqs.exceptions import SQSErrorResponse +from aiosqs.tests.cases import ActionTestCase -from aiosqs.parser import parse_xml_result_response -from aiosqs.tests.utils import load_fixture - -class SendMessageTestCase(unittest.TestCase): +class SendMessageTestCase(ActionTestCase): action = "SendMessage" def test_send_message_ok(self): - res = parse_xml_result_response( - action=self.action, - body=load_fixture("send_message.xml"), - ) + res = self.parseXMLResponse("send_message.xml") self.assertEqual( res, { @@ -19,3 +14,11 @@ def test_send_message_ok(self): "MD5OfMessageBody": "a88e5d79dc2948e662b90dc2857ba05c", }, ) + + def test_signature_does_not_match(self): + with self.assertRaises(SQSErrorResponse) as e: + self.parseXMLResponse("error_signature.xml") + exception = e.exception + self.assertEqual(exception.request_id, "f679cf26-effe-5e59-81ca-92cf5c4c713b") + self.assertEqual(exception.error.type, "Sender") + self.assertEqual(exception.error.code, "SignatureDoesNotMatch")