diff --git a/.github/workflows/python-checks.yml b/.github/workflows/python-checks.yml index ad5ad68..6ad912f 100644 --- a/.github/workflows/python-checks.yml +++ b/.github/workflows/python-checks.yml @@ -24,6 +24,7 @@ jobs: run: | python3 -m pip install --upgrade pip python3 -m pip install -r requirements.txt + python3 -m pip install pytest - name: Run flake8 run: | python3 -m pip install flake8 @@ -40,3 +41,6 @@ jobs: run: | python3 -m pip install isort isort *.py --check --diff + - name: Run pytest + run: | + pytest -vvv diff --git a/.idea/misc.xml b/.idea/misc.xml index daee374..3502fd0 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,5 +3,5 @@ - + \ No newline at end of file diff --git a/.idea/mq2anno.iml b/.idea/mq2anno.iml index 7ee3eda..74d515a 100644 --- a/.idea/mq2anno.iml +++ b/.idea/mq2anno.iml @@ -4,7 +4,7 @@ - + \ No newline at end of file diff --git a/test_payload.py b/test_payload.py index e69de29..a966113 100644 --- a/test_payload.py +++ b/test_payload.py @@ -0,0 +1,41 @@ +""" +MQTT payload handling tests +""" +import json + +import unittest +from unittest.mock import MagicMock, Mock, patch +from mq2anno import on_message, Userdata + + +class TestPayloadHandling(unittest.TestCase): + """payload handling tests""" + + # pylint: disable=no-self-use + @patch("mq2anno.requests") + def test_payload_processing(self, mock_requests): + """ + Verify that payload handling in on_message() works correctly for the positive case. + """ + payload = json.dumps({"annotation": True, "tags": ["foo", "bar"]}) + msg = Mock() + msg.payload = payload + msg.topic = "foo" + topic_cfg = {msg.topic: {"aaa": "bbb"}} + url = "http://localhost:8080" + userdata = Userdata(topic_cfg, url, {}) + + mock_response = MagicMock() + mock_response.status_code = 200 + mock_response.json.return_value = { + "userId": 1, + "id": 1, + "title": "hello", + } + mock_requests.post.return_value = mock_response + + on_message(None, userdata, msg) + + mock_requests.post.assert_called_once() + assert len(mock_requests.post.call_args) >= 1 + assert mock_requests.post.call_args[0][0] == url + "/api/annotations"