TestPrintInformation.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import functools
  2. from cura import PrintInformation
  3. from cura.Settings.MachineManager import MachineManager
  4. from unittest.mock import MagicMock, patch
  5. from UM.Application import Application
  6. from UM.MimeTypeDatabase import MimeTypeDatabase, MimeType
  7. def getPrintInformation(printer_name) -> PrintInformation:
  8. mock_application = MagicMock()
  9. global_container_stack = MagicMock()
  10. global_container_stack.definition.getName = MagicMock(return_value = printer_name)
  11. mock_application.getGlobalContainerStack = MagicMock(return_value = global_container_stack)
  12. multi_build_plate_model = MagicMock()
  13. multi_build_plate_model.maxBuildPlate = 0
  14. mock_application.getMultiBuildPlateModel = MagicMock(return_value = multi_build_plate_model)
  15. # Mock-up the entire machine manager except the function that needs to be tested: getAbbreviatedMachineName
  16. original_get_abbreviated_name = MachineManager.getAbbreviatedMachineName
  17. mock_machine_manager = MagicMock()
  18. mock_machine_manager.getAbbreviatedMachineName = functools.partial(original_get_abbreviated_name, mock_machine_manager)
  19. mock_application.getMachineManager = MagicMock(return_value = mock_machine_manager)
  20. Application.getInstance = MagicMock(return_type = mock_application)
  21. with patch("json.loads", lambda x: {}):
  22. print_information = PrintInformation.PrintInformation(mock_application)
  23. return print_information
  24. def setup_module():
  25. MimeTypeDatabase.addMimeType(
  26. MimeType(
  27. name = "application/vnd.ms-package.3dmanufacturing-3dmodel+xml",
  28. comment = "3MF",
  29. suffixes = ["3mf"]
  30. )
  31. )
  32. MimeTypeDatabase.addMimeType(
  33. MimeType(
  34. name = "application/x-cura-gcode-file",
  35. comment = "Cura GCode File",
  36. suffixes = ["gcode"]
  37. )
  38. )
  39. def test_setProjectName():
  40. print_information = getPrintInformation("ultimaker")
  41. # Test simple name
  42. project_name = ["HelloWorld", ".3mf"]
  43. print_information.setProjectName(project_name[0] + project_name[1])
  44. assert "UM_" + project_name[0] == print_information._job_name
  45. # Test the name with one dot
  46. project_name = ["Hello.World", ".3mf"]
  47. print_information.setProjectName(project_name[0] + project_name[1])
  48. assert "UM_" + project_name[0] == print_information._job_name
  49. # Test the name with two dot
  50. project_name = ["Hello.World.World", ".3mf"]
  51. print_information.setProjectName(project_name[0] + project_name[1])
  52. assert "UM_" + project_name[0] == print_information._job_name
  53. # Test the name with dot at the beginning
  54. project_name = [".Hello.World", ".3mf"]
  55. print_information.setProjectName(project_name[0] + project_name[1])
  56. assert "UM_" + project_name[0] == print_information._job_name
  57. # Test the name with underline
  58. project_name = ["Hello_World", ".3mf"]
  59. print_information.setProjectName(project_name[0] + project_name[1])
  60. assert "UM_" + project_name[0] == print_information._job_name
  61. # Test gcode extension
  62. project_name = ["Hello_World", ".gcode"]
  63. print_information.setProjectName(project_name[0] + project_name[1])
  64. assert "UM_" + project_name[0] == print_information._job_name
  65. # Test empty project name
  66. project_name = ["", ""]
  67. print_information.setProjectName(project_name[0] + project_name[1])
  68. assert print_information.UNTITLED_JOB_NAME == print_information._job_name
  69. # Test wrong file extension
  70. project_name = ["Hello_World", ".test"]
  71. print_information.setProjectName(project_name[0] + project_name[1])
  72. assert "UM_" + project_name[0] != print_information._job_name
  73. def test_setJobName():
  74. print_information = getPrintInformation("ultimaker")
  75. print_information._abbr_machine = "UM"
  76. print_information.setJobName("UM_HelloWorld", is_user_specified_job_name = False)
  77. def test_defineAbbreviatedMachineName():
  78. printer_name = "Test"
  79. print_information = getPrintInformation(printer_name)
  80. # Test not ultimaker printer, name suffix should have first letter from the printer name
  81. project_name = ["HelloWorld", ".3mf"]
  82. print_information.setProjectName(project_name[0] + project_name[1])
  83. assert printer_name[0] + "_" + project_name[0] == print_information._job_name