mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-07-13 01:37:51 -06:00
STAR-322: Finishing the output device manager tests
This commit is contained in:
parent
bfe04a55f9
commit
134f97d5f1
3 changed files with 51 additions and 15 deletions
|
@ -22,7 +22,6 @@ from .Utils import findChanges
|
||||||
#
|
#
|
||||||
# API spec is available on https://api.ultimaker.com/docs/connect/spec/.
|
# API spec is available on https://api.ultimaker.com/docs/connect/spec/.
|
||||||
#
|
#
|
||||||
|
|
||||||
class CloudOutputDeviceManager:
|
class CloudOutputDeviceManager:
|
||||||
META_CLUSTER_ID = "um_cloud_cluster_id"
|
META_CLUSTER_ID = "um_cloud_cluster_id"
|
||||||
|
|
||||||
|
@ -32,9 +31,7 @@ class CloudOutputDeviceManager:
|
||||||
# The translation catalog for this device.
|
# The translation catalog for this device.
|
||||||
I18N_CATALOG = i18nCatalog("cura")
|
I18N_CATALOG = i18nCatalog("cura")
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self) -> None:
|
||||||
super().__init__()
|
|
||||||
|
|
||||||
# Persistent dict containing the remote clusters for the authenticated user.
|
# Persistent dict containing the remote clusters for the authenticated user.
|
||||||
self._remote_clusters = {} # type: Dict[str, CloudOutputDevice]
|
self._remote_clusters = {} # type: Dict[str, CloudOutputDevice]
|
||||||
|
|
||||||
|
@ -86,6 +83,7 @@ class CloudOutputDeviceManager:
|
||||||
for removed_cluster in removed_devices:
|
for removed_cluster in removed_devices:
|
||||||
if removed_cluster.isConnected():
|
if removed_cluster.isConnected():
|
||||||
removed_cluster.disconnect()
|
removed_cluster.disconnect()
|
||||||
|
removed_cluster.close()
|
||||||
self._output_device_manager.removeOutputDevice(removed_cluster.key)
|
self._output_device_manager.removeOutputDevice(removed_cluster.key)
|
||||||
del self._remote_clusters[removed_cluster.key]
|
del self._remote_clusters[removed_cluster.key]
|
||||||
|
|
||||||
|
@ -124,20 +122,17 @@ class CloudOutputDeviceManager:
|
||||||
return
|
return
|
||||||
|
|
||||||
device = next((c for c in self._remote_clusters.values() if c.matchesNetworkKey(local_network_key)), None)
|
device = next((c for c in self._remote_clusters.values() if c.matchesNetworkKey(local_network_key)), None)
|
||||||
if not device:
|
if device:
|
||||||
return
|
active_machine.setMetaDataEntry(self.META_CLUSTER_ID, device.key)
|
||||||
|
device.connect()
|
||||||
active_machine.setMetaDataEntry(self.META_CLUSTER_ID, device.key)
|
|
||||||
return device.connect()
|
|
||||||
|
|
||||||
## Handles an API error received from the cloud.
|
## Handles an API error received from the cloud.
|
||||||
# \param errors: The errors received
|
# \param errors: The errors received
|
||||||
def _onApiError(self, errors: List[CloudErrorObject]) -> None:
|
def _onApiError(self, errors: List[CloudErrorObject]) -> None:
|
||||||
message = ". ".join(e.title for e in errors) # TODO: translate errors
|
message = ". ".join(e.title for e in errors) # TODO: translate errors
|
||||||
message = Message(
|
Message(
|
||||||
text = message,
|
text = message,
|
||||||
title = self.I18N_CATALOG.i18nc("@info:title", "Error"),
|
title = self.I18N_CATALOG.i18nc("@info:title", "Error"),
|
||||||
lifetime = 10,
|
lifetime = 10,
|
||||||
dismissable = True
|
dismissable = True
|
||||||
)
|
).show()
|
||||||
message.show()
|
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
}, {
|
}, {
|
||||||
"cluster_id": "NWKV6vJP_LdYsXgXqAcaNCR0YcLJwar1ugh0ikEZsZs8",
|
"cluster_id": "NWKV6vJP_LdYsXgXqAcaNCR0YcLJwar1ugh0ikEZsZs8",
|
||||||
"host_guid": "e0ace90a-91ee-1257-4403-e8050a44c9b7",
|
"host_guid": "e0ace90a-91ee-1257-4403-e8050a44c9b7",
|
||||||
"host_name": "ultimakersystem-ccbdd30044ec",
|
"host_name": "ultimakersystem-30044ecccbdd",
|
||||||
"host_version": "5.1.2.20180807",
|
"host_version": "5.1.2.20180807",
|
||||||
"is_online": true,
|
"is_online": true,
|
||||||
"status": "active"
|
"status": "active"
|
||||||
|
|
|
@ -30,13 +30,18 @@ class TestCloudOutputDeviceManager(TestCase):
|
||||||
# let the network send replies
|
# let the network send replies
|
||||||
self.network.flushReplies()
|
self.network.flushReplies()
|
||||||
# get the created devices
|
# get the created devices
|
||||||
devices = self.app.getOutputDeviceManager().getOutputDevices()
|
device_manager = self.app.getOutputDeviceManager()
|
||||||
|
devices = device_manager.getOutputDevices()
|
||||||
# get the server data
|
# get the server data
|
||||||
clusters = self.clusters_response["data"]
|
clusters = self.clusters_response.get("data", [])
|
||||||
self.assertEqual([CloudOutputDevice] * len(clusters), [type(d) for d in devices])
|
self.assertEqual([CloudOutputDevice] * len(clusters), [type(d) for d in devices])
|
||||||
self.assertEqual({cluster["cluster_id"] for cluster in clusters}, {device.key for device in devices})
|
self.assertEqual({cluster["cluster_id"] for cluster in clusters}, {device.key for device in devices})
|
||||||
self.assertEqual({cluster["host_name"] for cluster in clusters}, {device.host_name for device in devices})
|
self.assertEqual({cluster["host_name"] for cluster in clusters}, {device.host_name for device in devices})
|
||||||
|
|
||||||
|
for device in clusters:
|
||||||
|
device_manager.getOutputDevice(device["cluster_id"]).close()
|
||||||
|
device_manager.removeOutputDevice(device["cluster_id"])
|
||||||
|
|
||||||
## Runs the initial request to retrieve the clusters.
|
## Runs the initial request to retrieve the clusters.
|
||||||
def _loadData(self, network_mock):
|
def _loadData(self, network_mock):
|
||||||
network_mock.return_value = self.network
|
network_mock.return_value = self.network
|
||||||
|
@ -64,3 +69,39 @@ class TestCloudOutputDeviceManager(TestCase):
|
||||||
self.network.prepareGetClusters(self.clusters_response)
|
self.network.prepareGetClusters(self.clusters_response)
|
||||||
|
|
||||||
self.manager._update_timer.timeout.emit()
|
self.manager._update_timer.timeout.emit()
|
||||||
|
|
||||||
|
@patch("cura.CuraApplication.CuraApplication.getGlobalContainerStack")
|
||||||
|
def test_device_connects_by_cluster_id(self, global_container_stack_mock, network_mock):
|
||||||
|
active_machine_mock = global_container_stack_mock.return_value
|
||||||
|
cluster1, cluster2 = self.clusters_response["data"]
|
||||||
|
cluster_id = cluster1["cluster_id"]
|
||||||
|
active_machine_mock.getMetaDataEntry.side_effect = {"um_cloud_cluster_id": cluster_id}.get
|
||||||
|
|
||||||
|
self._loadData(network_mock)
|
||||||
|
self.network.flushReplies()
|
||||||
|
|
||||||
|
self.assertTrue(self.app.getOutputDeviceManager().getOutputDevice(cluster1["cluster_id"]).isConnected())
|
||||||
|
self.assertFalse(self.app.getOutputDeviceManager().getOutputDevice(cluster2["cluster_id"]).isConnected())
|
||||||
|
|
||||||
|
@patch("cura.CuraApplication.CuraApplication.getGlobalContainerStack")
|
||||||
|
def test_device_connects_by_network_key(self, global_container_stack_mock, network_mock):
|
||||||
|
active_machine_mock = global_container_stack_mock.return_value
|
||||||
|
|
||||||
|
cluster1, cluster2 = self.clusters_response["data"]
|
||||||
|
network_key = cluster2["host_name"] + ".ultimaker.local"
|
||||||
|
active_machine_mock.getMetaDataEntry.side_effect = {"um_network_key": network_key}.get
|
||||||
|
|
||||||
|
self._loadData(network_mock)
|
||||||
|
self.network.flushReplies()
|
||||||
|
|
||||||
|
self.assertFalse(self.app.getOutputDeviceManager().getOutputDevice(cluster1["cluster_id"]).isConnected())
|
||||||
|
self.assertTrue(self.app.getOutputDeviceManager().getOutputDevice(cluster2["cluster_id"]).isConnected())
|
||||||
|
|
||||||
|
active_machine_mock.setMetaDataEntry.assert_called_once_with("um_cloud_cluster_id", cluster2["cluster_id"])
|
||||||
|
|
||||||
|
@patch("UM.Message.Message.show")
|
||||||
|
def test_api_error(self, message_mock, network_mock):
|
||||||
|
self.clusters_response = {"errors": [{"id": "notFound"}]}
|
||||||
|
self.network.prepareGetClusters(self.clusters_response)
|
||||||
|
self._loadData(network_mock)
|
||||||
|
message_mock.assert_called_once_with()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue