File: TestXmlMaterialProfile.py

package info (click to toggle)
cura 5.0.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 122,888 kB
  • sloc: python: 44,572; sh: 81; xml: 32; makefile: 16
file content (74 lines) | stat: -rw-r--r-- 2,950 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from unittest.mock import patch, MagicMock

from UM.Qt.QtApplication import QtApplication  # QtApplication import is required, even though it isn't used.

import pytest
import XmlMaterialProfile

def createXmlMaterialProfile(material_id):
    try:
        return XmlMaterialProfile.XmlMaterialProfile.XmlMaterialProfile(material_id)
    except AttributeError:
        return XmlMaterialProfile.XmlMaterialProfile(material_id)


def test_setName():
    material_1 = createXmlMaterialProfile("herpderp")
    material_2 = createXmlMaterialProfile("OMGZOMG")

    material_1.getMetaData()["base_file"] = "herpderp"
    material_2.getMetaData()["base_file"] = "herpderp"

    container_registry = MagicMock()
    container_registry.isReadOnly = MagicMock(return_value = False)
    container_registry.findInstanceContainers = MagicMock(return_value = [material_1, material_2])

    with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value = container_registry)):
        material_1.setName("beep!")

    assert material_1.getName() == "beep!"
    assert material_2.getName() == "beep!"


def test_setDirty():
    material_1 = createXmlMaterialProfile("herpderp")
    material_2 = createXmlMaterialProfile("OMGZOMG")

    material_1.getMetaData()["base_file"] = "herpderp"
    material_2.getMetaData()["base_file"] = "herpderp"

    container_registry = MagicMock()
    container_registry.isReadOnly = MagicMock(return_value=False)
    container_registry.findContainers = MagicMock(return_value=[material_1, material_2])

    # Sanity check. Since we did a hacky thing to set the metadata, the container should not be dirty.
    # But this test assumes that it works like that, so we need to validate that.
    assert not material_1.isDirty()
    assert not material_2.isDirty()

    with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value=container_registry)):
        material_2.setDirty(True)

    assert material_1.isDirty()
    assert material_2.isDirty()

    # Setting the base material dirty does not set it's child as dirty.
    with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value=container_registry)):
        material_1.setDirty(False)

    assert not material_1.isDirty()
    assert material_2.isDirty()


def test_serializeNonBaseMaterial():
    material_1 = createXmlMaterialProfile("herpderp")
    material_1.getMetaData()["base_file"] = "omgzomg"

    container_registry = MagicMock()
    container_registry.isReadOnly = MagicMock(return_value=False)
    container_registry.findContainers = MagicMock(return_value=[material_1])

    with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value=container_registry)):
        with pytest.raises(NotImplementedError):
            # This material is not a base material, so it can't be serialized!
            material_1.serialize()