1from UM.Settings.ContainerRegistry import ContainerRegistry 2from UM.Settings.Models.ContainerPropertyProvider import ContainerPropertyProvider 3import pytest 4from unittest.mock import patch, MagicMock 5 6 7test_validate_data_get_set = [ 8 {"attribute": "containerId", "value": "YAY"}, 9 {"attribute": "watchedProperties", "value": {"beep": "oh noes"}}, 10 {"attribute": "key", "value": "YAY"}, 11] 12 13@pytest.fixture() 14def registry(): 15 mocked_registry = MagicMock() 16 mocked_registry.isReadOnly = MagicMock(return_value=False) 17 mocked_container = MagicMock() 18 mocked_container.getProperty = MagicMock(return_value = "yay") 19 mocked_registry.findContainers = MagicMock(return_value = [mocked_container]) 20 return mocked_registry 21 22 23@pytest.mark.parametrize("data", test_validate_data_get_set) 24def test_getAndSet(data): 25 model = ContainerPropertyProvider() 26 27 # Convert the first letter into a capital 28 attribute = list(data["attribute"]) 29 attribute[0] = attribute[0].capitalize() 30 attribute = "".join(attribute) 31 32 # mock the correct emit 33 setattr(model, data["attribute"] + "Changed", MagicMock()) 34 35 # Attempt to set the value 36 with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance"): 37 getattr(model, "set" + attribute)(data["value"]) 38 39 # Check if signal fired. 40 signal = getattr(model, data["attribute"] + "Changed") 41 assert signal.emit.call_count == 1 42 43 # Ensure that the value got set 44 assert getattr(model, data["attribute"]) == data["value"] 45 46 # Attempt to set the value again 47 getattr(model, "set" + attribute)(data["value"]) 48 # The signal should not fire again 49 assert signal.emit.call_count == 1 50 51 52def test_setPropertyValueNoKey(registry): 53 model = ContainerPropertyProvider() 54 with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value = registry)): 55 model.setContainerId("bloop") 56 model.setPropertyValue("derp", "zomg") 57 # Check if the mocked container got it's setProperty targeted 58 assert ContainerRegistry.getInstance().findContainers()[0].setProperty.call_count == 0 59 60 61def test_setPropertyValueNotWatchedProperty(registry): 62 model = ContainerPropertyProvider() 63 with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value = registry)): 64 model.setContainerId("bloop") 65 model.setKey("whatever") 66 model.setPropertyValue("derp", "zomg") 67 # Check if the mocked container got it's setProperty targeted 68 assert ContainerRegistry.getInstance().findContainers()[0].setProperty.call_count == 0 69 70def test_setPropertyValue(registry): 71 model = ContainerPropertyProvider() 72 with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value = registry)): 73 model.setContainerId("bloop") 74 model.setWatchedProperties(["derp"]) 75 model.setKey("whatever") 76 model.setPropertyValue("derp", "zomg") 77 # Check if the mocked container got it's setProperty targeted 78 assert ContainerRegistry.getInstance().findContainers()[0].setProperty.call_count == 1 79 80 81def test_setPropertyValueCache(registry): 82 model = ContainerPropertyProvider() 83 with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value=registry)): 84 model.setContainerId("bloop") 85 model.setWatchedProperties(["derp", "yay"]) 86 model.setKey("derp") 87 # Fake that we got a signal indicating the value is already set 88 model._onPropertyChanged("derp", "yay") 89 90 model.setPropertyValue("derp", "yay") 91 92 # Check if the mocked container got it's setProperty targeted 93 assert ContainerRegistry.getInstance().findContainers()[0].setProperty.call_count == 0 94