Skip to content

Commit

Permalink
Add missing test cases
Browse files Browse the repository at this point in the history
Signed-off-by: Simone Orru <simone.orru@secomind.com>
  • Loading branch information
sorru94 committed Sep 21, 2023
1 parent 6c1b1b0 commit bbff359
Showing 1 changed file with 188 additions and 33 deletions.
221 changes: 188 additions & 33 deletions tests/test_device_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ def test_send(self, mock_get_interface, mock_bson_dumps, mock_db_store, mock_mqt
@mock.patch.object(AstarteDatabaseSQLite, "store_prop")
@mock.patch("astarte.device.device_mqtt.bson.dumps")
@mock.patch.object(Introspection, "get_interface")
def test_send_zero(self, mock_get_interface, mock_bson_dumps, mock_db_store, mock_mqtt_publish):
def test_send_zero_is_ok(self, mock_get_interface, mock_bson_dumps, mock_db_store, mock_mqtt_publish):
device = self.helper_initialize_device(loop=None)

mock_interface = mock.MagicMock()
Expand Down Expand Up @@ -542,6 +542,47 @@ def test_send_zero(self, mock_get_interface, mock_bson_dumps, mock_db_store, moc
qos=mock_interface.get_reliability.return_value,
)

@mock.patch.object(Client, "publish")
@mock.patch.object(AstarteDatabaseSQLite, "store_prop")
@mock.patch("astarte.device.device_mqtt.bson.dumps")
@mock.patch.object(Introspection, "get_interface")
def test_send_a_property_is_ok(
self, mock_get_interface, mock_bson_dumps, mock_db_store, mock_mqtt_publish
):
device = self.helper_initialize_device(loop=None)

mock_interface = mock.MagicMock()
mock_interface.name = "interface name"
mock_interface.is_server_owned.return_value = False
mock_interface.is_aggregation_object.return_value = False
mock_interface.validate.return_value = None
mock_interface.is_type_properties.return_value = True
mock_get_interface.return_value = mock_interface

mock_bson_dumps.return_value = bytes("bson content", "utf-8")

interface_name = "interface name"
interface_path = "interface path"
payload = 12
timestamp = datetime.now()
device.send(interface_name, interface_path, payload, timestamp)

mock_get_interface.assert_called_once_with(interface_name)
mock_interface.is_server_owned.assert_called_once()
mock_interface.is_aggregation_object.assert_called_once()
mock_interface.validate.assert_called_once_with(interface_path, payload, timestamp)
mock_bson_dumps.assert_called_once_with({"v": payload, "t": timestamp})
mock_interface.is_type_properties.assert_called_once_with()
mock_db_store.assert_called_once_with(
interface_name, mock_get_interface.return_value.version_major, interface_path, payload
)
mock_interface.get_reliability.assert_called_once_with(interface_path)
mock_mqtt_publish.assert_called_once_with(
"realm_name/device_id/" + interface_name + interface_path,
bytes("bson content", "utf-8"),
qos=mock_interface.get_reliability.return_value,
)

@mock.patch.object(Client, "publish")
@mock.patch.object(AstarteDatabaseSQLite, "store_prop")
@mock.patch("astarte.device.device_mqtt.bson.dumps")
Expand Down Expand Up @@ -571,48 +612,35 @@ def test_send_non_existing_interface_raises_interface_not_found(
@mock.patch.object(AstarteDatabaseSQLite, "store_prop")
@mock.patch("astarte.device.device_mqtt.bson.dumps")
@mock.patch.object(Introspection, "get_interface")
def test_send_on_a_property(
def test_send_to_a_server_owned_interface_raises(
self, mock_get_interface, mock_bson_dumps, mock_db_store, mock_mqtt_publish
):
device = self.helper_initialize_device(loop=None)

mock_interface = mock.MagicMock()
mock_interface.name = "interface name"
mock_interface.is_server_owned.return_value = False
mock_interface.is_aggregation_object.return_value = False
mock_interface.validate.return_value = None
mock_interface.is_type_properties.return_value = True
mock_interface.is_server_owned.return_value = True
mock_get_interface.return_value = mock_interface

mock_bson_dumps.return_value = bytes("bson content", "utf-8")

interface_name = "interface name"
interface_path = "interface path"
payload = 12
timestamp = datetime.now()
device.send(interface_name, interface_path, payload, timestamp)

mock_get_interface.assert_called_once_with(interface_name)
mock_interface.is_server_owned.assert_called_once()
mock_interface.is_aggregation_object.assert_called_once()
mock_interface.validate.assert_called_once_with(interface_path, payload, timestamp)
mock_bson_dumps.assert_called_once_with({"v": payload, "t": timestamp})
mock_interface.is_type_properties.assert_called_once_with()
mock_db_store.assert_called_once_with(
interface_name, mock_get_interface.return_value.version_major, interface_path, payload
)
mock_interface.get_reliability.assert_called_once_with(interface_path)
mock_mqtt_publish.assert_called_once_with(
"realm_name/device_id/" + interface_name + interface_path,
bytes("bson content", "utf-8"),
qos=mock_interface.get_reliability.return_value,
self.assertRaises(
ValidationError, lambda: device.send(interface_name, interface_path, payload, timestamp)
)

mock_get_interface.assert_called_once_with("interface name")
mock_get_interface.return_value.is_server_owned.assert_called_once()
mock_get_interface.return_value.is_aggregation_object.assert_not_called()
mock_bson_dumps.assert_not_called()
mock_db_store.assert_not_called()
mock_mqtt_publish.assert_not_called()

@mock.patch.object(Client, "publish")
@mock.patch.object(AstarteDatabaseSQLite, "store_prop")
@mock.patch("astarte.device.device_mqtt.bson.dumps")
@mock.patch.object(Introspection, "get_interface")
def test_send_is_an_aggregate_raises_validation_err(
def test_send_an_aggregate_raises_validation_err(
self, mock_get_interface, mock_bson_dumps, mock_db_store, mock_mqtt_publish
):
device = self.helper_initialize_device(loop=None)
Expand Down Expand Up @@ -641,7 +669,7 @@ def test_send_is_an_aggregate_raises_validation_err(
@mock.patch.object(AstarteDatabaseSQLite, "store_prop")
@mock.patch("astarte.device.device_mqtt.bson.dumps")
@mock.patch.object(Introspection, "get_interface")
def test_send_wrong_payload_type_raises(
def test_send_none_payload_type_raises_validation_err(
self, mock_get_interface, mock_bson_dumps, mock_db_store, mock_mqtt_publish
):
device = self.helper_initialize_device(loop=None)
Expand All @@ -653,7 +681,7 @@ def test_send_wrong_payload_type_raises(

interface_name = "interface name"
interface_path = "interface path"
payload = {"something": 12}
payload = None
timestamp = datetime.now()
self.assertRaises(
ValidationError, lambda: device.send(interface_name, interface_path, payload, timestamp)
Expand All @@ -670,26 +698,27 @@ def test_send_wrong_payload_type_raises(
@mock.patch.object(AstarteDatabaseSQLite, "store_prop")
@mock.patch("astarte.device.device_mqtt.bson.dumps")
@mock.patch.object(Introspection, "get_interface")
def test_send_to_a_server_owned_interface_raises(
def test_send_wrong_payload_type_raises_validation_err(
self, mock_get_interface, mock_bson_dumps, mock_db_store, mock_mqtt_publish
):
device = self.helper_initialize_device(loop=None)

mock_interface = mock.MagicMock()
mock_interface.is_server_owned.return_value = True
mock_interface.is_server_owned.return_value = False
mock_interface.is_aggregation_object.return_value = False
mock_get_interface.return_value = mock_interface

interface_name = "interface name"
interface_path = "interface path"
payload = 12
payload = {"something": 12}
timestamp = datetime.now()
self.assertRaises(
ValidationError, lambda: device.send(interface_name, interface_path, payload, timestamp)
)

mock_get_interface.assert_called_once_with("interface name")
mock_get_interface.return_value.is_server_owned.assert_called_once()
mock_get_interface.return_value.is_aggregation_object.assert_not_called()
mock_interface.is_server_owned.assert_called_once()
mock_get_interface.return_value.is_aggregation_object.assert_called_once()
mock_bson_dumps.assert_not_called()
mock_db_store.assert_not_called()
mock_mqtt_publish.assert_not_called()
Expand Down Expand Up @@ -790,6 +819,35 @@ def test_send_aggregate_non_existing_interface_raises_interface_not_found(
mock_db_store.assert_not_called()
mock_mqtt_publish.assert_not_called()

@mock.patch.object(Client, "publish")
@mock.patch.object(AstarteDatabaseSQLite, "store_prop")
@mock.patch("astarte.device.device_mqtt.bson.dumps")
@mock.patch.object(Introspection, "get_interface")
def test_send_aggregate_server_owned_interface_raises_validation_err(
self, mock_get_interface, mock_bson_dumps, mock_db_store, mock_mqtt_publish
):
device = self.helper_initialize_device(loop=None)

mock_interface = mock.MagicMock()
mock_interface.name = "interface name"
mock_interface.is_server_owned.return_value = True
mock_get_interface.return_value = mock_interface

interface_name = "interface name"
interface_path = "interface path"
payload = {"something": 12}
timestamp = datetime.now()
self.assertRaises(
ValidationError,
lambda: device.send_aggregate(interface_name, interface_path, payload, timestamp),
)

mock_get_interface.assert_called_once_with("interface name")
mock_interface.is_server_owned.assert_called_once()
mock_bson_dumps.assert_not_called()
mock_db_store.assert_not_called()
mock_mqtt_publish.assert_not_called()

@mock.patch.object(Client, "publish")
@mock.patch.object(AstarteDatabaseSQLite, "store_prop")
@mock.patch("astarte.device.device_mqtt.bson.dumps")
Expand Down Expand Up @@ -823,6 +881,39 @@ def test_send_aggregate_is_not_an_aggregate_raises_validation_err(
mock_interface.get_reliability.assert_not_called()
mock_mqtt_publish.assert_not_called()

@mock.patch.object(Client, "publish")
@mock.patch.object(AstarteDatabaseSQLite, "store_prop")
@mock.patch("astarte.device.device_mqtt.bson.dumps")
@mock.patch.object(Introspection, "get_interface")
def test_send_aggregate_none_payload_type_raises(
self, mock_get_interface, mock_bson_dumps, mock_db_store, mock_mqtt_publish
):
device = self.helper_initialize_device(loop=None)

mock_interface = mock.MagicMock()
mock_interface.is_server_owned.return_value = False
mock_interface.is_aggregation_object.return_value = True
mock_interface.is_type_properties.return_value = False
mock_get_interface.return_value = mock_interface

interface_name = "interface name"
interface_path = "interface path"
payload = None
timestamp = datetime.now()
self.assertRaises(
ValidationError,
lambda: device.send_aggregate(interface_name, interface_path, payload, timestamp),
)

mock_get_interface.assert_called_once_with(interface_name)
mock_interface.is_server_owned.assert_called_once()
mock_interface.is_aggregation_object.assert_called_once()
mock_interface.validate.assert_not_called()
mock_bson_dumps.assert_not_called()
mock_db_store.assert_not_called()
mock_interface.get_reliability.assert_not_called()
mock_mqtt_publish.assert_not_called()

@mock.patch.object(Client, "publish")
@mock.patch.object(AstarteDatabaseSQLite, "store_prop")
@mock.patch("astarte.device.device_mqtt.bson.dumps")
Expand Down Expand Up @@ -856,6 +947,41 @@ def test_send_aggregate_wrong_payload_type_raises(
mock_interface.get_reliability.assert_not_called()
mock_mqtt_publish.assert_not_called()

@mock.patch.object(Client, "publish")
@mock.patch.object(AstarteDatabaseSQLite, "store_prop")
@mock.patch("astarte.device.device_mqtt.bson.dumps")
@mock.patch.object(Introspection, "get_interface")
def test_send_aggregate_interface_validate_raises_validation_err(
self, mock_get_interface, mock_bson_dumps, mock_db_store, mock_mqtt_publish
):
device = self.helper_initialize_device(loop=None)

mock_interface = mock.MagicMock()
mock_interface.name = "interface name"
mock_interface.is_server_owned.return_value = False
mock_interface.is_aggregation_object.return_value = True
mock_interface.is_type_properties.return_value = False
mock_interface.validate.return_value = ValidationError("Error msg")
mock_get_interface.return_value = mock_interface

interface_name = "interface name"
interface_path = "interface path"
payload = {"something": 12}
timestamp = datetime.now()
self.assertRaises(
ValidationError,
lambda: device.send_aggregate(interface_name, interface_path, payload, timestamp),
)

mock_get_interface.assert_called_once_with(interface_name)
mock_interface.is_server_owned.assert_called_once()
mock_interface.is_aggregation_object.assert_called_once()
mock_interface.validate.assert_called_once_with(interface_path, payload, timestamp)
mock_bson_dumps.assert_not_called()
mock_db_store.assert_not_called()
mock_interface.get_reliability.assert_not_called()
mock_mqtt_publish.assert_not_called()

@mock.patch.object(Client, "publish")
@mock.patch.object(AstarteDatabaseSQLite, "store_prop")
@mock.patch("astarte.device.device_mqtt.bson.dumps")
Expand Down Expand Up @@ -914,6 +1040,35 @@ def test_unset_property_non_existing_interface_raises_interface_not_found(
mock_db_store.assert_not_called()
mock_mqtt_publish.assert_not_called()

@mock.patch.object(Client, "publish")
@mock.patch.object(AstarteDatabaseSQLite, "store_prop")
@mock.patch("astarte.device.device_mqtt.bson.dumps")
@mock.patch.object(Introspection, "get_interface")
def test_unset_property_server_owned_interface_raises(
self, mock_get_interface, mock_bson_dumps, mock_db_store, mock_mqtt_publish
):
device = self.helper_initialize_device(loop=None)

mock_interface = mock.MagicMock()
mock_interface.name = "interface name"
mock_interface.is_server_owned.return_value = True
mock_get_interface.return_value = mock_interface

interface_name = "interface name"
interface_path = "interface path"
self.assertRaises(
ValidationError, lambda: device.unset_property(interface_name, interface_path)
)

mock_get_interface.assert_called_once_with(interface_name)
mock_interface.is_server_owned.assert_called_once()
mock_interface.is_type_properties.assert_not_called()
mock_interface.validate.assert_not_called()
mock_bson_dumps.assert_not_called()
mock_db_store.assert_not_called()
mock_interface.get_reliability.assert_not_called()
mock_mqtt_publish.assert_not_called()

@mock.patch.object(Client, "publish")
@mock.patch.object(AstarteDatabaseSQLite, "store_prop")
@mock.patch("astarte.device.device_mqtt.bson.dumps")
Expand Down

0 comments on commit bbff359

Please sign in to comment.