TestPrintInformation.py 3.7 KB

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