Skip to content
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

Refactor existing unit-tests #23

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions aiosqs/tests/cases.py
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
2 changes: 1 addition & 1 deletion aiosqs/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 19 additions & 9 deletions aiosqs/tests/test_delete_message.py
Original file line number Diff line number Diff line change
@@ -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."
),
)
51 changes: 0 additions & 51 deletions aiosqs/tests/test_errors.py

This file was deleted.

17 changes: 4 additions & 13 deletions aiosqs/tests/test_get_queue_url.py
Original file line number Diff line number Diff line change
@@ -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,
{
Expand All @@ -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,
{
Expand Down
41 changes: 19 additions & 22 deletions aiosqs/tests/test_receive_message.py
Original file line number Diff line number Diff line change
@@ -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,
[
Expand All @@ -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,
[
Expand All @@ -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,
[
Expand Down Expand Up @@ -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.",
)
21 changes: 12 additions & 9 deletions aiosqs/tests/test_send_message.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
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,
{
"MessageId": "acdf9b7f-a639-4a5b-9557-b4de52a56d01",
"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")
Loading