TestDFFileExportAndUploadManager.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from unittest.mock import MagicMock, patch
  2. import pytest
  3. from src.DFFileExportAndUploadManager import DFFileExportAndUploadManager
  4. @pytest.fixture
  5. def upload_manager():
  6. file_handler = MagicMock(name = "file_handler")
  7. file_handler.getSupportedFileTypesWrite = MagicMock(return_value = [{
  8. "id": "test",
  9. "extension": ".3mf",
  10. "description": "nope",
  11. "mime_type": "application/vnd.ms-package.3dmanufacturing-3dmodel+xml",
  12. "mode": "binary",
  13. "hide_in_file_dialog": True,
  14. }])
  15. node = MagicMock(name = "SceneNode")
  16. application = MagicMock(name = "CuraApplication")
  17. with patch("cura.CuraApplication.CuraApplication.getInstance", MagicMock(return_value = application)):
  18. return DFFileExportAndUploadManager(file_handlers = {"3mf": file_handler},
  19. nodes = [node],
  20. library_project_id = "test_library_project_id",
  21. library_project_name = "test_library_project_name",
  22. file_name = "file_name",
  23. formats = ["3mf"],
  24. on_upload_error = MagicMock(),
  25. on_upload_success = MagicMock(),
  26. on_upload_finished = MagicMock(),
  27. on_upload_progress = MagicMock())
  28. @pytest.mark.parametrize("input,expected_result",
  29. [("", ""),
  30. ("invalid json! {}", ""),
  31. ("{\"errors\": [{}]}", ""),
  32. ("{\"errors\": [{\"title\": \"some title\"}]}", "some title")])
  33. def test_extractErrorTitle(upload_manager, input, expected_result):
  34. assert upload_manager.extractErrorTitle(input) == expected_result
  35. def test_exportJobError(upload_manager):
  36. mocked_application = MagicMock()
  37. with patch("UM.Application.Application.getInstance", MagicMock(return_value = mocked_application)):
  38. upload_manager._onJobExportError("file_name.3mf")
  39. # Ensure that message was displayed
  40. mocked_application.showMessageSignal.emit.assert_called_once()