Added unittest for PrintInformation class

This commit is contained in:
Aleksei S 2018-09-28 10:46:14 +02:00
parent d83241f13a
commit 9bd4ab2faa
2 changed files with 114 additions and 5 deletions

View file

@ -10,7 +10,6 @@ from typing import Dict
from PyQt5.QtCore import QObject, pyqtSignal, pyqtProperty, pyqtSlot
from UM.i18n import i18nCatalog
from UM.Logger import Logger
from UM.Qt.Duration import Duration
from UM.Scene.SceneNode import SceneNode
@ -52,6 +51,8 @@ class PrintInformation(QObject):
super().__init__(parent)
self._application = application
self.UNTITLED_JOB_NAME = "Untitled"
self.initializeCuraMessagePrintTimeProperties()
self._material_lengths = {} # indexed by build plate number
@ -70,12 +71,13 @@ class PrintInformation(QObject):
self._base_name = ""
self._abbr_machine = ""
self._job_name = ""
self._project_name = ""
self._active_build_plate = 0
self._initVariablesWithBuildPlate(self._active_build_plate)
self._multi_build_plate_model = self._application.getMultiBuildPlateModel()
ss = self._multi_build_plate_model.maxBuildPlate
self._application.globalContainerStackChanged.connect(self._updateJobName)
self._application.globalContainerStackChanged.connect(self.setToZeroPrintInformation)
self._application.fileLoaded.connect(self.setBaseName)
@ -300,13 +302,13 @@ class PrintInformation(QObject):
def _updateJobName(self):
if self._base_name == "":
self._job_name = "Untitled"
self._job_name = self.UNTITLED_JOB_NAME
self._is_user_specified_job_name = False
self.jobNameChanged.emit()
return
base_name = self._stripAccents(self._base_name)
self._setAbbreviatedMachineName()
self._defineAbbreviatedMachineName()
# Only update the job name when it's not user-specified.
if not self._is_user_specified_job_name:
@ -382,7 +384,7 @@ class PrintInformation(QObject):
## Created an acronym-like abbreviated machine name from the currently
# active machine name.
# Called each time the global stack is switched.
def _setAbbreviatedMachineName(self):
def _defineAbbreviatedMachineName(self):
global_container_stack = self._application.getGlobalContainerStack()
if not global_container_stack:
self._abbr_machine = ""

View file

@ -0,0 +1,107 @@
from cura import PrintInformation
from unittest.mock import MagicMock, patch
from UM.Application import Application
from UM.MimeTypeDatabase import MimeTypeDatabase, MimeType
def getPrintInformation(printer_name) -> PrintInformation:
mock_application = MagicMock()
global_container_stack = MagicMock()
global_container_stack.definition.getName = MagicMock(return_value=printer_name)
mock_application.getGlobalContainerStack = MagicMock(return_value=global_container_stack)
multiBuildPlateModel = MagicMock()
multiBuildPlateModel.maxBuildPlate = 0
mock_application.getMultiBuildPlateModel = MagicMock(return_value=multiBuildPlateModel)
Application.getInstance = MagicMock(return_type=mock_application)
with patch("json.loads", lambda x: {}):
print_information = PrintInformation.PrintInformation(mock_application)
return print_information
def setup_module():
MimeTypeDatabase.addMimeType(
MimeType(
name="application/vnd.ms-package.3dmanufacturing-3dmodel+xml",
comment="3MF",
suffixes=["3mf"]
)
)
MimeTypeDatabase.addMimeType(
MimeType(
name="application/x-cura-gcode-file",
comment="Cura GCode File",
suffixes=["gcode"]
)
)
def test_setProjectName():
print_information = getPrintInformation("ultimaker")
# Test simple name
project_name = ["HelloWorld",".3mf"]
print_information.setProjectName(project_name[0] + project_name[1])
assert "UM_" + project_name[0] == print_information._job_name
# Test the name with one dot
project_name = ["Hello.World",".3mf"]
print_information.setProjectName(project_name[0] + project_name[1])
assert "UM_" + project_name[0] == print_information._job_name
# Test the name with two dot
project_name = ["Hello.World.World",".3mf"]
print_information.setProjectName(project_name[0] + project_name[1])
assert "UM_" + project_name[0] == print_information._job_name
# Test the name with dot at the beginning
project_name = [".Hello.World",".3mf"]
print_information.setProjectName(project_name[0] + project_name[1])
assert "UM_" + project_name[0] == print_information._job_name
# Test the name with underline
project_name = ["Hello_World",".3mf"]
print_information.setProjectName(project_name[0] + project_name[1])
assert "UM_" + project_name[0] == print_information._job_name
# Test gcode extension
project_name = ["Hello_World",".gcode"]
print_information.setProjectName(project_name[0] + project_name[1])
assert "UM_" + project_name[0] == print_information._job_name
# Test empty project name
project_name = ["",""]
print_information.setProjectName(project_name[0] + project_name[1])
assert print_information.UNTITLED_JOB_NAME == print_information._job_name
# Test wrong file extension
project_name = ["Hello_World",".test"]
print_information.setProjectName(project_name[0] + project_name[1])
assert "UM_" + project_name[0] != print_information._job_name
def test_setJobName():
print_information = getPrintInformation("ultimaker")
print_information._abbr_machine = "UM"
print_information.setJobName("UM_HelloWorld", is_user_specified_job_name=False)
def test_defineAbbreviatedMachineName():
printer_name = "Test"
print_information = getPrintInformation(printer_name)
# Test not ultimaker printer, name suffix should have first letter from the printer name
project_name = ["HelloWorld",".3mf"]
print_information.setProjectName(project_name[0] + project_name[1])
assert printer_name[0] + "_" + project_name[0] == print_information._job_name