Пример #1
0
    def test__onGetRemoteMaterials_withUpdatedMaterial(self,
                                                       get_instance_mock):
        reply_mock = MagicMock()
        device_mock = MagicMock()
        application_mock = get_instance_mock.return_value
        container_registry_mock = application_mock.getContainerRegistry.return_value
        material_manager_mock = application_mock.getMaterialManager.return_value

        container_registry_mock.getContainerFilePathById = lambda x: _FILES_MAP.get(
            x)

        device_mock.createFormPart.return_value = "_xXx_"

        material_manager_mock.getAllMaterialGroups.return_value = self._LOCAL_MATERIAL_WHITE_NEWER_ALL_RESULT.copy(
        )

        reply_mock.attribute.return_value = 200
        reply_mock.readAll.return_value = QByteArray(
            json.dumps([self._REMOTE_MATERIAL_WHITE]).encode("ascii"))

        job = SendMaterialJob(device_mock)
        job._onGetRemoteMaterials(reply_mock)

        self.assertEqual(1, device_mock.createFormPart.call_count)
        self.assertEqual(1, device_mock.postFormWithParts.call_count)
        self.assertEquals([
            call.createFormPart(
                "name=\"file\"; filename=\"generic_pla_white.xml.fdm_material\"",
                "<xml></xml>"),
            call.postFormWithParts(target="materials/",
                                   parts=["_xXx_"],
                                   on_finished=job.sendingFinished)
        ], device_mock.method_calls)
Пример #2
0
    def test__onGetRemoteMaterials_withFailedRequest(self, reply_mock, device_mock):
        reply_mock.attribute.return_value = 404
        job = SendMaterialJob(device_mock)
        job._onGetRemoteMaterials(reply_mock)

        # We expect the device not to be called for any follow up.
        self.assertEqual(0, device_mock.createFormPart.call_count)
Пример #3
0
    def test_run(self, device_mock, reply_mock):
        job = SendMaterialJob(device_mock)
        job.run()

        # We expect the materials endpoint to be called when the job runs.
        device_mock.get.assert_called_with(
            "materials/", on_finished=job._onGetRemoteMaterials)
Пример #4
0
    def test__onGetRemoteMaterials_withNewMaterial(self, application_mock,
                                                   container_registry_mock,
                                                   reply_mock, device_mock):
        application_mock.getContainerRegistry.return_value = container_registry_mock

        device_mock.createFormPart.return_value = "_xXx_"

        container_registry_mock.findContainersMetadata.return_value = [
            self._LOCAL_MATERIAL_WHITE, self._LOCAL_MATERIAL_BLACK
        ]

        reply_mock.attribute.return_value = 200
        reply_mock.readAll.return_value = QByteArray(
            json.dumps([self._REMOTE_MATERIAL_BLACK]).encode("ascii"))

        with mock.patch.object(Application,
                               "getInstance",
                               new=lambda: application_mock):
            job = SendMaterialJob(device_mock)
            job._onGetRemoteMaterials(reply_mock)

        self.assertEqual(1, device_mock.createFormPart.call_count)
        self.assertEqual(1, device_mock.postFormWithParts.call_count)
        self.assertEquals([
            call.createFormPart(
                "name=\"file\"; filename=\"generic_pla_white.xml.fdm_material\"",
                "<xml></xml>"),
            call.postFormWithParts(target="materials/",
                                   parts=["_xXx_"],
                                   on_finished=job.sendingFinished)
        ], device_mock.method_calls)
Пример #5
0
    def test__onGetRemoteMaterials_withWrongEncoding(self, reply_mock, device_mock):
        reply_mock.attribute.return_value = 200
        reply_mock.readAll.return_value = QByteArray(json.dumps([self._REMOTE_MATERIAL_WHITE]).encode("cp500"))
        job = SendMaterialJob(device_mock)
        job._onGetRemoteMaterials(reply_mock)

        # Given that the parsing fails we do no expect the device to be called for any follow up.
        self.assertEqual(0, device_mock.createFormPart.call_count)
Пример #6
0
    def test__onGetRemoteMaterials_withBadJsonAnswer(self, reply_mock, device_mock):
        reply_mock.attribute.return_value = 200
        reply_mock.readAll.return_value = QByteArray(b"Six sick hicks nick six slick bricks with picks and sticks.")
        job = SendMaterialJob(device_mock)
        job._onGetRemoteMaterials(reply_mock)

        # Given that the parsing fails we do no expect the device to be called for any follow up.
        self.assertEqual(0, device_mock.createFormPart.call_count)
Пример #7
0
    def test__onGetRemoteMaterials_withFailedRequest(self, reply_mock,
                                                     device_mock):
        reply_mock.attribute.return_value = 404
        job = SendMaterialJob(device_mock)
        job._onGetRemoteMaterials(reply_mock)

        # We expect the device not to be called for any follow up.
        self.assertEqual(0, device_mock.createFormPart.call_count)
Пример #8
0
    def test__onGetRemoteMaterials_withBadJsonAnswer(self, reply_mock,
                                                     device_mock):
        reply_mock.attribute.return_value = 200
        reply_mock.readAll.return_value = QByteArray(
            b"Six sick hicks nick six slick bricks with picks and sticks.")
        job = SendMaterialJob(device_mock)
        job._onGetRemoteMaterials(reply_mock)

        # Given that the parsing fails we do no expect the device to be called for any follow up.
        self.assertEqual(0, device_mock.createFormPart.call_count)
Пример #9
0
    def test__onGetRemoteMaterials_withWrongEncoding(self, reply_mock,
                                                     device_mock):
        reply_mock.attribute.return_value = 200
        reply_mock.readAll.return_value = QByteArray(
            json.dumps([self._REMOTE_MATERIAL_WHITE]).encode("cp500"))
        job = SendMaterialJob(device_mock)
        job._onGetRemoteMaterials(reply_mock)

        # Given that the parsing fails we do no expect the device to be called for any follow up.
        self.assertEqual(0, device_mock.createFormPart.call_count)
Пример #10
0
    def test__onGetRemoteMaterials_withMissingGuidInRemoteMaterial(self, reply_mock, device_mock):
        reply_mock.attribute.return_value = 200
        remote_material_without_guid = self._REMOTE_MATERIAL_WHITE.copy()
        del remote_material_without_guid["guid"]
        reply_mock.readAll.return_value = QByteArray(json.dumps([remote_material_without_guid]).encode("ascii"))
        job = SendMaterialJob(device_mock)
        job._onGetRemoteMaterials(reply_mock)

        # Given that parsing fails we do not expect the device to be called for any follow up.
        self.assertEqual(0, device_mock.createFormPart.call_count)
Пример #11
0
    def test__onGetRemoteMaterials_withMissingGuidInRemoteMaterial(
            self, reply_mock, device_mock):
        reply_mock.attribute.return_value = 200
        remote_material_without_guid = self._REMOTE_MATERIAL_WHITE.copy()
        del remote_material_without_guid["guid"]
        reply_mock.readAll.return_value = QByteArray(
            json.dumps([remote_material_without_guid]).encode("ascii"))
        job = SendMaterialJob(device_mock)
        job._onGetRemoteMaterials(reply_mock)

        # Given that parsing fails we do not expect the device to be called for any follow up.
        self.assertEqual(0, device_mock.createFormPart.call_count)
Пример #12
0
    def test__onGetRemoteMaterials_withInvalidVersionInLocalMaterial(self, application_mock, container_registry_mock,
                                                                     reply_mock, device_mock):
        reply_mock.attribute.return_value = 200
        reply_mock.readAll.return_value = QByteArray(json.dumps([self._REMOTE_MATERIAL_WHITE]).encode("ascii"))

        localMaterialWhiteWithInvalidVersion = self._LOCAL_MATERIAL_WHITE.copy()
        localMaterialWhiteWithInvalidVersion["version"] = "one"
        container_registry_mock.findContainersMetadata.return_value = [localMaterialWhiteWithInvalidVersion]

        application_mock.getContainerRegistry.return_value = container_registry_mock

        with mock.patch.object(Application, "getInstance", new = lambda: application_mock):
            job = SendMaterialJob(device_mock)
            job._onGetRemoteMaterials(reply_mock)

        self.assertEqual(0, device_mock.createFormPart.call_count)
Пример #13
0
    def test__onGetRemoteMaterials_withNoUpdate(self, application_mock, container_registry_mock, reply_mock,
                                                device_mock):
        application_mock.getContainerRegistry.return_value = container_registry_mock

        device_mock.createFormPart.return_value = "_xXx_"

        container_registry_mock.findContainersMetadata.return_value = [self._LOCAL_MATERIAL_WHITE]

        reply_mock.attribute.return_value = 200
        reply_mock.readAll.return_value = QByteArray(json.dumps([self._REMOTE_MATERIAL_WHITE]).encode("ascii"))

        with mock.patch.object(Application, "getInstance", new = lambda: application_mock):
            job = SendMaterialJob(device_mock)
            job._onGetRemoteMaterials(reply_mock)

        self.assertEqual(0, device_mock.createFormPart.call_count)
        self.assertEqual(0, device_mock.postFormWithParts.call_count)
Пример #14
0
    def test__onGetRemoteMaterials_withInvalidVersionInLocalMaterial(
            self, application_mock, container_registry_mock,
            material_manager_mock):
        reply_mock = MagicMock()
        device_mock = MagicMock()
        application_mock.getContainerRegistry.return_value = container_registry_mock
        application_mock.getMaterialManager.return_value = material_manager_mock

        reply_mock.attribute.return_value = 200
        reply_mock.readAll.return_value = QByteArray(
            json.dumps([self._REMOTE_MATERIAL_WHITE]).encode("ascii"))

        material_manager_mock.getAllMaterialGroups.return_value = self._LOCAL_MATERIAL_WHITE_INVALID_VERSION_ALL_RESULT.copy(
        )

        with mock.patch.object(Application,
                               "getInstance",
                               new=lambda: application_mock):
            job = SendMaterialJob(device_mock)
            job._onGetRemoteMaterials(reply_mock)

        self.assertEqual(0, device_mock.createFormPart.call_count)
Пример #15
0
    def test__onGetRemoteMaterials_withNewMaterial(self, application_mock, container_registry_mock, reply_mock,
                                                   device_mock):
        application_mock.getContainerRegistry.return_value = container_registry_mock

        device_mock.createFormPart.return_value = "_xXx_"

        container_registry_mock.findContainersMetadata.return_value = [self._LOCAL_MATERIAL_WHITE,
                                                                       self._LOCAL_MATERIAL_BLACK]

        reply_mock.attribute.return_value = 200
        reply_mock.readAll.return_value = QByteArray(json.dumps([self._REMOTE_MATERIAL_BLACK]).encode("ascii"))

        with mock.patch.object(Application, "getInstance", new = lambda: application_mock):
            job = SendMaterialJob(device_mock)
            job._onGetRemoteMaterials(reply_mock)

        self.assertEqual(1, device_mock.createFormPart.call_count)
        self.assertEqual(1, device_mock.postFormWithParts.call_count)
        self.assertEquals(
            [call.createFormPart("name=\"file\"; filename=\"generic_pla_white.xml.fdm_material\"", "<xml></xml>"),
             call.postFormWithParts(target = "materials/", parts = ["_xXx_"], on_finished = job.sendingFinished)],
            device_mock.method_calls)
Пример #16
0
    def test__onGetRemoteMaterials_withInvalidVersionInLocalMaterial(
            self, application_mock, container_registry_mock, reply_mock,
            device_mock):
        reply_mock.attribute.return_value = 200
        reply_mock.readAll.return_value = QByteArray(
            json.dumps([self._REMOTE_MATERIAL_WHITE]).encode("ascii"))

        localMaterialWhiteWithInvalidVersion = self._LOCAL_MATERIAL_WHITE.copy(
        )
        localMaterialWhiteWithInvalidVersion["version"] = "one"
        container_registry_mock.findContainersMetadata.return_value = [
            localMaterialWhiteWithInvalidVersion
        ]

        application_mock.getContainerRegistry.return_value = container_registry_mock

        with mock.patch.object(Application,
                               "getInstance",
                               new=lambda: application_mock):
            job = SendMaterialJob(device_mock)
            job._onGetRemoteMaterials(reply_mock)

        self.assertEqual(0, device_mock.createFormPart.call_count)
Пример #17
0
    def test__onGetRemoteMaterials_withNoUpdate(self, application_mock,
                                                container_registry_mock,
                                                reply_mock, device_mock):
        application_mock.getContainerRegistry.return_value = container_registry_mock

        device_mock.createFormPart.return_value = "_xXx_"

        container_registry_mock.findContainersMetadata.return_value = [
            self._LOCAL_MATERIAL_WHITE
        ]

        reply_mock.attribute.return_value = 200
        reply_mock.readAll.return_value = QByteArray(
            json.dumps([self._REMOTE_MATERIAL_WHITE]).encode("ascii"))

        with mock.patch.object(Application,
                               "getInstance",
                               new=lambda: application_mock):
            job = SendMaterialJob(device_mock)
            job._onGetRemoteMaterials(reply_mock)

        self.assertEqual(0, device_mock.createFormPart.call_count)
        self.assertEqual(0, device_mock.postFormWithParts.call_count)
Пример #18
0
    def test__onGetRemoteMaterials_withNoUpdate(self, application_mock):
        reply_mock = MagicMock()
        device_mock = MagicMock()
        container_registry_mock = application_mock.getContainerRegistry.return_value
        material_manager_mock = application_mock.getMaterialManager.return_value

        device_mock.createFormPart.return_value = "_xXx_"

        material_manager_mock.getAllMaterialGroups.return_value = self._LOCAL_MATERIAL_WHITE_ALL_RESULT.copy(
        )

        reply_mock.attribute.return_value = 200
        reply_mock.readAll.return_value = QByteArray(
            json.dumps([self._REMOTE_MATERIAL_WHITE]).encode("ascii"))

        with mock.patch.object(Application,
                               "getInstance",
                               new=lambda: application_mock):
            job = SendMaterialJob(device_mock)
            job._onGetRemoteMaterials(reply_mock)

        self.assertEqual(0, device_mock.createFormPart.call_count)
        self.assertEqual(0, device_mock.postFormWithParts.call_count)
Пример #19
0
    def test_run(self, device_mock, reply_mock):
        job = SendMaterialJob(device_mock)
        job.run()

        # We expect the materials endpoint to be called when the job runs.
        device_mock.get.assert_called_with("materials/", on_finished = job._onGetRemoteMaterials)