Skip to content

Commit

Permalink
add payload tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vladak committed Feb 13, 2024
1 parent 74edd44 commit 24f1065
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/python-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,3 +41,6 @@ jobs:
run: |
python3 -m pip install isort
isort *.py --check --diff
- name: Run pytest
run: |
pytest -vvv
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/mq2anno.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions test_payload.py
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 24f1065

Please sign in to comment.