Permalink
Browse files

Add unit tests

  • Loading branch information...
osteele committed Jan 31, 2018
1 parent 09eacfa commit d393031b9988f18cf2050f7c2da1e4d629b5f4e0
Showing with 44 additions and 4 deletions.
  1. +7 −1 README.md
  2. +4 −3 requirements.txt
  3. +33 −0 tests/mqtt_json_test.py
@@ -57,7 +57,7 @@ Send a test message to the bear:

`python3 mqtt_json/send_mqtt_message.py "forget about your worries"`

## Running the Server
## Run the Server

Provision a RabbitMQ server. Or, use the same server as the Twilio ⟶ MQTT
Gateway.
@@ -69,6 +69,12 @@ On macOS, the server uses the built-in `say` command, which has better quality.

`python tts_worker.py`.

## Test

`pytest` runs the unit tests (currently just of the `mqtt_json` package).

`pytest-watch` runs the tests in watch mode.

## Architecture

The SMS ⟶ Bear gateway example depends on the [Twilio ⟶ MQTT Gateway](https://github.com/olin-build/twilio-mqtt-gateway).
@@ -1,9 +1,10 @@
click~=6.7
paho-mqtt~=1.3.1

# Twilio gateway
Flask~=0.12.2
twilio~=6.10.1

# Testing
pytest~=3.2.2
pytest-watch

# Example gateway
profanityfilter~=2.0.3
@@ -0,0 +1,33 @@
import os
import sys

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from mqtt_json import Client
from unittest.mock import MagicMock, patch


@patch('paho.mqtt.publish.single')
def test_config(publish):
client = Client()
client.publish('topic', k1=1, k2=2)
assert publish.calleds


@patch('paho.mqtt.client.Client')
def test_create_subscription_queue(mqtt_factory):
# Configure the mock object to send a message to the subscriber.
def mock_on_loop_start():
mqtt_client.on_connect(mqtt_client, None, None, None)
msg = MagicMock()
msg.payload = '{"key":1}'.encode()
mqtt_client.on_message(mqtt_client, None, msg)
mqtt_client = mqtt_factory()
mqtt_client.loop_start = MagicMock(side_effect=mock_on_loop_start)

client = Client()
queue = client.create_subscription_queue('topic')
msg = next(queue, None)
assert mqtt_client.connect.called
assert mqtt_client.subscribe.called
assert mqtt_client.loop_start.called
assert msg == {'key': 1}

0 comments on commit d393031

Please sign in to comment.