Skip to content

Commit

Permalink
Clean in mqtt tests before creating more devices (#858)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelboulton committed Mar 13, 2023
1 parent dd037db commit 1674c70
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion example/mqtt/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def on_message_callback(client, userdata, message):
else:
logging.warning("Got unexpected MQTT topic '%s'", message.topic)
except Exception as e:
logging.exception(e)
logging.exception("error handling message: {}".format(e))


def wait_for_messages():
Expand Down
16 changes: 12 additions & 4 deletions example/mqtt/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,19 @@ def create_device():
)

try:
next(row)
except StopIteration:
pass
r["clean"]
except (TypeError):
return jsonify({"error": "checking for clean key"}), 500
except KeyError:
try:
next(row)
except StopIteration:
pass
else:
return jsonify({"error": "device already exists"}), 400
else:
return jsonify({"error": "device already exists"}), 400
with db:
db.execute("DELETE FROM devices_table")

new_device = dict(lights_on=False, **r)

Expand Down
1 change: 1 addition & 0 deletions example/mqtt/test_mqtt.tavern.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ stages:
method: PUT
json:
device_id: "{random_device_id}"
clean: True
response:
status_code: 201

Expand Down
6 changes: 4 additions & 2 deletions example/mqtt/test_mqtt_failures.tavern.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ stages:
method: PUT
json:
device_id: "{random_device_id}"
clean: True
response:
status_code: 201

Expand Down Expand Up @@ -164,7 +165,8 @@ stages:
payload: !anything
timeout: 2
qos: 1
- topic: /device/123/status/response
unexpected: true
- topic: /device/{random_device_id}/status/response
payload: !anything
timeout: 2
qos: 1
Expand All @@ -188,7 +190,7 @@ stages:
mqtt_publish:
topic: /devices/status
mqtt_response:
- topic: /device/123/status/response
- topic: /device/88466412/status/response
payload: !anything
timeout: 3
qos: 1
Expand Down
9 changes: 5 additions & 4 deletions tavern/_plugins/mqtt/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import logging
import time
from dataclasses import dataclass
from typing import List, Optional, Tuple, Union
from typing import Dict, List, Mapping, Optional, Tuple, Union

from paho.mqtt.client import MQTTMessage

Expand Down Expand Up @@ -135,13 +135,14 @@ def _await_response(self) -> dict:
return saved

def _await_messages_on_topic(
self, topic: str, expected
self, topic: str, expected: List[Dict]
) -> Tuple[List["_ReturnedMessage"], List[str]]:
"""
Waits for the specific message
Args:
expected (list): expected response for this block
topic: topic to listen on
expected: expected response for this block
Returns:
tuple(msg, list): The correct message (if any) and warnings from processing the message
Expand Down Expand Up @@ -317,7 +318,7 @@ def addwarning(w, *args, **kwargs):
return False

@staticmethod
def _get_payload_vals(expected) -> Tuple[Optional[Union[str, dict]], bool]:
def _get_payload_vals(expected: Mapping) -> Tuple[Optional[Union[str, dict]], bool]:
"""Gets the payload from the 'expected' block
Returns:
Expand Down

0 comments on commit 1674c70

Please sign in to comment.