TestDigitalLibraryApiClient.py 2.6 KB

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