Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-k committed Aug 31, 2018
1 parent 6e987e6 commit dc71e72
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 129 deletions.
13 changes: 5 additions & 8 deletions rohrpost/tests.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import json


class ReplyChannel:
class Consumer:
def __init__(self):
self.data = []
self.closed = False

def send(self, message_dict):
def send(self, message: str):
if not self.closed:
self.data.append(json.loads(message_dict.get("text")))
self.closed = message_dict.get("close", False)
self.data.append(json.loads(message))


class Message:
def __init__(self):
self.reply_channel = ReplyChannel()
def close(self):
self.closed = True
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import pytest

from rohrpost.mixins import NotifyOnChange
from rohrpost.tests import Message
from rohrpost.tests import Consumer


@pytest.fixture
def message():
return Message()
def consumer():
return Consumer()


class MockModel:
Expand Down
76 changes: 35 additions & 41 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,57 @@
from rohrpost.main import handle_rohrpost_message


def test_handle_empty_message(message):
message.content = {"text": None}
handle_rohrpost_message(message)
def test_handle_empty_message(consumer):
handle_rohrpost_message(consumer=consumer, text_data=None)

assert len(message.reply_channel.data) == 1
assert message.reply_channel.data[-1]["error"].startswith("Received empty message")
assert len(consumer.data) == 1
print("DEBUG", consumer.data)
assert consumer.data[-1]["error"].startswith("Received empty message")


def test_handle_non_json(message):
message.content = {"text": [1, 2, 3]}
handle_rohrpost_message(message)
def test_handle_non_json(consumer):
message = [1, 2, 3]
handle_rohrpost_message(consumer=consumer, text_data=message)

assert len(message.reply_channel.data) == 1
assert message.reply_channel.data[-1]["error"].startswith(
"Could not decode JSON message"
)
assert len(consumer.data) == 1
assert consumer.data[-1]["error"].startswith("Could not decode JSON message")


def test_handle_nondict_message(message):
message.content = {"text": json.dumps([1, 2, 3])}
handle_rohrpost_message(message)
def test_handle_nondict_message(consumer):
message = json.dumps([1, 2, 3])
handle_rohrpost_message(consumer=consumer, text_data=message)

assert len(message.reply_channel.data) == 1
assert message.reply_channel.data[-1]["error"].startswith(
"Expected a JSON object as message."
)
assert len(consumer.data) == 1
assert consumer.data[-1]["error"].startswith("Expected a JSON object as message.")


def test_handle_missing_fields(message):
message.content = {"text": json.dumps({})}
handle_rohrpost_message(message)
def test_handle_missing_fields(consumer):
message = json.dumps({})
handle_rohrpost_message(consumer=consumer, text_data=message)

assert len(message.reply_channel.data) == 1
assert message.reply_channel.data[-1]["error"].startswith(
"Missing required field 'type'."
)
assert len(consumer.data) == 1
assert consumer.data[-1]["error"].startswith("Missing required field 'type'.")


def test_handle_unknown_type(message):
message.content = {"text": json.dumps({"id": 123, "type": "handler_not_known"})}
handle_rohrpost_message(message)
def test_handle_unknown_type(consumer):
message = json.dumps({"id": 123, "type": "handler_not_known"})
handle_rohrpost_message(consumer=consumer, text_data=message)

assert len(message.reply_channel.data) == 1
assert message.reply_channel.data[-1]["error"].startswith("Unknown message type")
assert len(consumer.data) == 1
assert consumer.data[-1]["error"].startswith("Unknown message type")


def test_successful_handle(message):
message.content = {"text": json.dumps({"id": 123, "type": "ping"})}
handle_rohrpost_message(message)
def test_successful_handle(consumer):
message = json.dumps({"id": 123, "type": "ping"})
handle_rohrpost_message(consumer=consumer, text_data=message)

assert len(message.reply_channel.data) == 1
assert message.reply_channel.data[-1]["type"] == "pong"
assert len(consumer.data) == 1
assert consumer.data[-1]["type"] == "pong"


def test_successful_handle_zero_id(message):
message.content = {"text": json.dumps({"id": 0, "type": "ping"})}
handle_rohrpost_message(message)
def test_successful_handle_zero_id(consumer):
message = json.dumps({"id": 0, "type": "ping"})
handle_rohrpost_message(consumer=consumer, text_data=message)

assert len(message.reply_channel.data) == 1
assert message.reply_channel.data[-1]["type"] == "pong"
assert len(consumer.data) == 1
assert consumer.data[-1]["type"] == "pong"
97 changes: 50 additions & 47 deletions tests/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,195 +3,198 @@
from rohrpost.message import send_error, send_message, send_success


def test_send_error(message):
def test_send_error(consumer):
send_error(
message=message, message_id=1, handler="some_handler", error="error_message"
consumer=consumer, message_id=1, handler="some_handler", error="error_message"
)
data = message.reply_channel.data
data = consumer.data
assert len(data) == 1
assert data[0]["id"] == 1
assert data[0]["type"] == "some_handler"
assert data[0]["error"] == "error_message"


def test_send_error_with_additional_data(message):
def test_send_error_with_additional_data(consumer):
send_error(
message=message,
consumer=consumer,
message_id=1,
handler="some_handler",
error="error_message",
data={"some_field": "additional_info"},
)
data = message.reply_channel.data
data = consumer.data
assert len(data) == 1
assert data[0]["id"] == 1
assert data[0]["type"] == "some_handler"
assert data[0]["error"] == "error_message"
assert data[0]["data"]["some_field"] == "additional_info"


def test_send_error_without_id(message):
def test_send_error_without_id(consumer):
send_error(
message=message, message_id=None, handler="some_handler", error="error_message"
consumer=consumer,
message_id=None,
handler="some_handler",
error="error_message",
)
data = message.reply_channel.data
data = consumer.data
assert len(data) == 1
assert "id" not in data[0]
assert data[0]["type"] == "some_handler"
assert data[0]["error"] == "error_message"


def test_send_error_without_id_with_additional_data(message):
def test_send_error_without_id_with_additional_data(consumer):
send_error(
message=message,
consumer=consumer,
message_id=None,
handler="some_handler",
error="error_message",
data={"some_field": "additional_info"},
)
data = message.reply_channel.data
data = consumer.data
assert len(data) == 1
assert "id" not in data[0]
assert data[0]["type"] == "some_handler"
assert data[0]["error"] == "error_message"
assert data[0]["data"]["some_field"] == "additional_info"


def test_send_error_without_id_and_type(message):
def test_send_error_without_id_and_type(consumer):
send_error(
message=message,
consumer=consumer,
message_id=None,
handler=None,
error="error_message",
data={"some_field": "additional_info"},
)
data = message.reply_channel.data
data = consumer.data
assert len(data) == 1
assert "id" not in data[0]
assert "type" not in data[0]
assert data[0]["error"] == "error_message"
assert data[0]["data"]["some_field"] == "additional_info"


def test_send_error_without_anything(message):
send_error(message=message, message_id=None, handler=None, error="error_message")
data = message.reply_channel.data
def test_send_error_without_anything(consumer):
send_error(consumer=consumer, message_id=None, handler=None, error="error_message")
data = consumer.data
assert len(data) == 1
assert "id" not in data[0]
assert "type" not in data[0]
assert data[0]["error"] == "error_message"


def test_send_success(message):
send_success(message=message, message_id=1, handler="some_handler")
data = message.reply_channel.data
def test_send_success(consumer):
send_success(consumer=consumer, message_id=1, handler="some_handler")
data = consumer.data
assert len(data) == 1
assert data[0]["id"] == 1
assert data[0]["type"] == "some_handler"


def test_send_success_with_additional_data(message):
def test_send_success_with_additional_data(consumer):
send_success(
message=message,
consumer=consumer,
message_id=1,
handler="some_handler",
data={"some_field": "additional_info"},
)
data = message.reply_channel.data
data = consumer.data
assert len(data) == 1
assert data[0]["id"] == 1
assert data[0]["type"] == "some_handler"
assert data[0]["data"]["some_field"] == "additional_info"
assert "some_field" not in data[0]


def test_send_success_without_id(message):
def test_send_success_without_id(consumer):
with pytest.raises(Exception) as exc:
send_success(message=message, message_id=None, handler="some_handler")
send_success(consumer=consumer, message_id=None, handler="some_handler")
assert "ID and handler" in str(exc.value)


def test_send_success_without_id_with_additional_data(message):
def test_send_success_without_id_with_additional_data(consumer):
with pytest.raises(Exception) as exc:
send_success(
message=message,
consumer=consumer,
message_id=None,
handler="some_handler",
data={"some_field": "additional_info"},
)
assert "ID and handler" in str(exc.value)


def test_send_success_without_id_and_type(message):
def test_send_success_without_id_and_type(consumer):
with pytest.raises(Exception) as exc:
send_success(
message=message,
consumer=consumer,
message_id=None,
handler=None,
data={"some_field": "additional_info"},
)
assert "ID and handler" in str(exc.value)


def test_send_message(message):
send_message(message=message, message_id=1, handler="some_handler")
data = message.reply_channel.data
def test_send_message(consumer):
send_message(consumer=consumer, message_id=1, handler="some_handler")
data = consumer.data
assert len(data) == 1
assert data[0]["id"] == 1
assert data[0]["type"] == "some_handler"


def test_send_message_with_additional_data(message):
def test_send_message_with_additional_data(consumer):
send_message(
message=message,
consumer=consumer,
message_id=1,
handler="some_handler",
data={"some_field": "additional_info"},
)
data = message.reply_channel.data
data = consumer.data
assert len(data) == 1
assert data[0]["id"] == 1
assert data[0]["type"] == "some_handler"
assert data[0]["data"]["some_field"] == "additional_info"


def test_send_message_without_id(message):
send_message(message=message, message_id=None, handler="some_handler")
data = message.reply_channel.data
def test_send_message_without_id(consumer):
send_message(consumer=consumer, message_id=None, handler="some_handler")
data = consumer.data
assert len(data) == 1
assert "id" not in data[0]
assert data[0]["type"] == "some_handler"


def test_send_message_without_id_with_additional_data(message):
def test_send_message_without_id_with_additional_data(consumer):
send_message(
message=message,
consumer=consumer,
message_id=None,
handler="some_handler",
data={"some_field": "additional_info"},
)
data = message.reply_channel.data
data = consumer.data
assert len(data) == 1
assert "id" not in data[0]
assert data[0]["type"] == "some_handler"
assert data[0]["data"]["some_field"] == "additional_info"


def test_send_message_without_id_and_type(message):
def test_send_message_without_id_and_type(consumer):
send_message(
message=message,
consumer=consumer,
message_id=None,
handler=None,
data={"some_field": "additional_info"},
)
data = message.reply_channel.data
data = consumer.data
assert len(data) == 1
assert "id" not in data[0]
assert "type" not in data[0]
assert data[0]["data"]["some_field"] == "additional_info"


def test_send_message_without_anything(message):
def test_send_message_without_anything(consumer):
with pytest.raises(Exception) as exc:
send_message(message=message, message_id=None, handler=None)
send_message(consumer=consumer, message_id=None, handler=None)
assert "empty message" in str(exc.value)
Loading

0 comments on commit dc71e72

Please sign in to comment.