test_octiva.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import arklog
  2. from pathlib import Path
  3. import requests
  4. from fastapi.testclient import TestClient
  5. from mocka.main import get_application
  6. from mocka.configuration import Configuration, Server
  7. from mocka.artefact import Artefact
  8. client = TestClient(get_application(Configuration(Server("localhost", 8585))))
  9. def test_post_octiva():
  10. # NOTE Needs the backend to be running to work... :(
  11. image_file_path = Path("octiva/rails_0.jpg")
  12. image_file_uri = f"http://localhost:5000/files/file/{image_file_path.name}"
  13. # Send the file to the endpoint as it might not be there yet.
  14. full_image_file_path = Path(__file__).parent / Path("octiva/rails_0.jpg")
  15. r = requests.put(f"http://localhost:5000/files/file/{image_file_path.name}", data=full_image_file_path.read_bytes())
  16. arklog.debug(r.status_code)
  17. image_artefact = Artefact("reference", image_file_uri, image_file_path.name, "image/jpg")
  18. algo_artefact = Artefact("inline", f"print('hello')", "algorithm.py", "text/plain")
  19. mock_input = {
  20. "ctrl": "cin",
  21. "input": {
  22. "image": image_artefact.as_dict(),
  23. "algorithm": algo_artefact.as_dict(),
  24. }
  25. }
  26. try:
  27. response = client.post("/octiva/", json=mock_input)
  28. except ConnectionError as e:
  29. raise RuntimeError(f"{e} Please run the DTD backend when doing these tests.")
  30. assert response.status_code == 200
  31. annotated_image_artefact = Artefact("reference", f"http://localhost:5000/files/file/rail_image_annotated.jpg", "rail_image_annotated.jpg", "image/jpg")
  32. expected_data_content = "D: [[[108 847 108 0]]\n\n [[477 847 477 0]]]\nP: [0, 1]"
  33. data_artefact = Artefact("inline", expected_data_content, "rail_image_annotated.txt", "text/plain")
  34. assert response.json() == {
  35. "ctrl": "ok",
  36. "output": {
  37. "image": annotated_image_artefact.as_dict(),
  38. "data": data_artefact.as_dict(),
  39. }
  40. }