test_octiva.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import base64
  2. from pathlib import Path
  3. import cv2
  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_read_main():
  10. # response = client.get("/", headers={})
  11. # assert response.status_code == 201
  12. # assert response.json() == {
  13. # "port": "ok",
  14. # "output": {
  15. # "artefact_1": "<uri>",
  16. # "artefact_2": "<uri>",
  17. # }
  18. # }
  19. # def test_post_main():
  20. # file_path = Path(__file__).parent.parent / Path("data/mock_requirements.txt")
  21. # requirements = file_path.read_text()
  22. # mock_input = {
  23. # "ctrl": "cin",
  24. # "input": {
  25. # "din": {
  26. # "type": "inline",
  27. # "content": requirements,
  28. # "encoding": "text/plain"
  29. # }
  30. # }
  31. # }
  32. # response = client.post("/", json=mock_input)
  33. # assert response.status_code == 200
  34. # assert response.json() == {
  35. # "ctrl": "ok",
  36. # "output": {
  37. # "dout": {
  38. # "type": "inline",
  39. # "content": requirements + "\n\nChecked!",
  40. # "name": file_path.name,
  41. # "encoding": "text/plain"
  42. # }
  43. # }
  44. # }
  45. def test_post_octiva():
  46. file_path = Path(__file__).parent / Path("octiva/rails_0.jpg")
  47. rail_image = cv2.imread(str(file_path))
  48. retval, buffer = cv2.imencode(file_path.suffix, rail_image)
  49. data = base64.b64encode(buffer).decode()
  50. image_artefact = Artefact("inline", data, "", "base64")
  51. algo_artefact = Artefact("inline", f"print('hello')", "", "text/plain")
  52. mock_input = {
  53. "ctrl": "cin",
  54. "input": {
  55. "image": image_artefact.as_dict(),
  56. "algorithm": algo_artefact.as_dict(),
  57. }
  58. }
  59. response = client.post("/octiva/", json=mock_input)
  60. assert response.status_code == 200
  61. temp_image_loc = Path(__file__).parent.parent / Path("data") / Path("rail_image_annotated.jpg")
  62. with temp_image_loc.open("rb") as image_file:
  63. data = base64.b64encode(image_file.read()).decode()
  64. annotated_image_artefact = Artefact("inline", data, "", "base64")
  65. expected_data_content = "D: [[[108 847 108 0]]\n\n [[477 847 477 0]]]\nP: [0, 1]"
  66. data_artefact = Artefact("inline", expected_data_content, "", "text/plain")
  67. assert response.json() == {
  68. "ctrl": "ok",
  69. "output": {
  70. "image": annotated_image_artefact.as_dict(),
  71. "data": data_artefact.as_dict(),
  72. }
  73. }