TestDiscoveredPrintersModel.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from unittest.mock import MagicMock, PropertyMock
  2. import pytest
  3. from cura.Machines.Models.DiscoveredPrintersModel import DiscoveredPrintersModel, DiscoveredPrinter
  4. @pytest.fixture()
  5. def discovered_printer_model(application) -> DiscoveredPrintersModel:
  6. return DiscoveredPrintersModel(application)
  7. @pytest.fixture()
  8. def discovered_printer() -> DiscoveredPrinter:
  9. return DiscoveredPrinter("127.0.0.1", "zomg", "yay", None, "bleep", MagicMock())
  10. @pytest.mark.skip # TODO: This has some unknown dependency on the application / registry, which is hard to patch out. (which doesn't mean we shouldn't fix it!)
  11. def test_discoveredPrinters(discovered_printer_model):
  12. mocked_device = MagicMock()
  13. cluster_size = PropertyMock(return_value = 1)
  14. type(mocked_device).clusterSize = cluster_size
  15. mocked_callback = MagicMock()
  16. discovered_printer_model.addDiscoveredPrinter("ip", "key", "name", mocked_callback, "machine_type", mocked_device)
  17. device = discovered_printer_model.discoveredPrinters[0]
  18. discovered_printer_model.createMachineFromDiscoveredPrinter(device)
  19. mocked_callback.assert_called_with("key")
  20. assert len(discovered_printer_model.discoveredPrinters) == 1
  21. discovered_printer_model.discoveredPrintersChanged = MagicMock()
  22. # Test if removing it works
  23. discovered_printer_model.removeDiscoveredPrinter("ip")
  24. assert len(discovered_printer_model.discoveredPrinters) == 0
  25. assert discovered_printer_model.discoveredPrintersChanged.emit.call_count == 1
  26. # Removing it again shouldn't cause another signal emit
  27. discovered_printer_model.removeDiscoveredPrinter("ip")
  28. assert discovered_printer_model.discoveredPrintersChanged.emit.call_count == 1
  29. test_validate_data_get_set = [
  30. {"attribute": "name", "value": "zomg"},
  31. {"attribute": "machineType", "value": "BHDHAHHADAD"},
  32. ]
  33. @pytest.mark.parametrize("data", test_validate_data_get_set)
  34. def test_getAndSet(data, discovered_printer):
  35. # Attempt to set the value
  36. # Convert the first letter into a capital
  37. attribute = list(data["attribute"])
  38. attribute[0] = attribute[0].capitalize()
  39. attribute = "".join(attribute)
  40. # Attempt to set the value
  41. getattr(discovered_printer, "set" + attribute)(data["value"])
  42. # Ensure that the value got set
  43. assert getattr(discovered_printer, data["attribute"]) == data["value"]
  44. def test_isHostofGroup(discovered_printer):
  45. discovered_printer.device.clusterSize = 0
  46. assert not discovered_printer.isHostOfGroup
  47. discovered_printer.device.clusterSize = 2
  48. assert discovered_printer.isHostOfGroup