TestDigitalLibraryApiClient.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # Copyright (c) 2021 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from unittest.mock import MagicMock
  4. import pytest
  5. from cura.CuraApplication import CuraApplication
  6. from src.DigitalFactoryApiClient import DigitalFactoryApiClient
  7. from src.PaginationManager import PaginationManager
  8. @pytest.fixture
  9. def application():
  10. app = MagicMock(spec=CuraApplication, name = "Mocked Cura Application")
  11. return app
  12. @pytest.fixture
  13. def pagination_manager():
  14. manager = MagicMock(name = "Mocked Pagination Manager")
  15. return manager
  16. @pytest.fixture
  17. def api_client(application, pagination_manager):
  18. api_client = DigitalFactoryApiClient(application, MagicMock())
  19. api_client._projects_pagination_mgr = pagination_manager
  20. return api_client
  21. def test_getProjectsFirstPage(api_client):
  22. # setup
  23. http_manager = MagicMock()
  24. api_client._http = http_manager
  25. pagination_manager = api_client._projects_pagination_mgr
  26. pagination_manager.limit = 20
  27. finished_callback = MagicMock()
  28. failed_callback = MagicMock()
  29. # Call
  30. api_client.getProjectsFirstPage(search_filter = "filter", on_finished = finished_callback, failed = failed_callback)
  31. # Asserts
  32. pagination_manager.reset.assert_called_once() # Should be called since we asked for new set of projects
  33. http_manager.get.assert_called_once()
  34. args = http_manager.get.call_args_list[0]
  35. # Ensure that it's called with the right limit
  36. assert args[0][0] == "https://api.ultimaker.com/cura/v1/projects?limit=20&search=filter"
  37. # Change the limit & try again
  38. http_manager.get.reset_mock()
  39. pagination_manager.limit = 80
  40. api_client.getProjectsFirstPage(search_filter = "filter", on_finished = finished_callback, failed = failed_callback)
  41. args = http_manager.get.call_args_list[0]
  42. # Ensure that it's called with the right limit
  43. assert args[0][0] == "https://api.ultimaker.com/cura/v1/projects?limit=80&search=filter"
  44. def test_getMoreProjects_noNewProjects(api_client):
  45. api_client.hasMoreProjectsToLoad = MagicMock(return_value = False)
  46. http_manager = MagicMock()
  47. api_client._http = http_manager
  48. finished_callback = MagicMock()
  49. failed_callback = MagicMock()
  50. api_client.getMoreProjects(finished_callback, failed_callback)
  51. http_manager.get.assert_not_called()
  52. def test_getMoreProjects_hasNewProjects(api_client):
  53. api_client.hasMoreProjectsToLoad = MagicMock(return_value = True)
  54. http_manager = MagicMock()
  55. api_client._http = http_manager
  56. finished_callback = MagicMock()
  57. failed_callback = MagicMock()
  58. api_client.getMoreProjects(finished_callback, failed_callback)
  59. http_manager.get.assert_called_once()
  60. def test_clear(api_client):
  61. api_client.clear()
  62. api_client._projects_pagination_mgr.reset.assert_called_once()