| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- import base64
- from pathlib import Path
- import cv2
- from fastapi.testclient import TestClient
- from mocka.main import get_application
- from mocka.configuration import Configuration, Server
- from mocka.artefact import Artefact
- client = TestClient(get_application(Configuration(Server("localhost", 8585))))
- # def test_read_main():
- # response = client.get("/", headers={})
- # assert response.status_code == 201
- # assert response.json() == {
- # "port": "ok",
- # "output": {
- # "artefact_1": "<uri>",
- # "artefact_2": "<uri>",
- # }
- # }
- # def test_post_main():
- # file_path = Path(__file__).parent.parent / Path("data/mock_requirements.txt")
- # requirements = file_path.read_text()
- # mock_input = {
- # "ctrl": "cin",
- # "input": {
- # "din": {
- # "type": "inline",
- # "content": requirements,
- # "encoding": "text/plain"
- # }
- # }
- # }
- # response = client.post("/", json=mock_input)
- # assert response.status_code == 200
- # assert response.json() == {
- # "ctrl": "ok",
- # "output": {
- # "dout": {
- # "type": "inline",
- # "content": requirements + "\n\nChecked!",
- # "name": file_path.name,
- # "encoding": "text/plain"
- # }
- # }
- # }
- def test_post_octiva():
- file_path = Path(__file__).parent / Path("octiva/rails_0.jpg")
- rail_image = cv2.imread(str(file_path))
- retval, buffer = cv2.imencode(file_path.suffix, rail_image)
- data = base64.b64encode(buffer).decode()
- image_artefact = Artefact("inline", data, "", "base64")
- algo_artefact = Artefact("inline", f"print('hello')", "", "text/plain")
- mock_input = {
- "ctrl": "cin",
- "input": {
- "image": image_artefact.as_dict(),
- "algorithm": algo_artefact.as_dict(),
- }
- }
- response = client.post("/octiva/", json=mock_input)
- assert response.status_code == 200
- temp_image_loc = Path(__file__).parent.parent / Path("data") / Path("rail_image_annotated.jpg")
- with temp_image_loc.open("rb") as image_file:
- data = base64.b64encode(image_file.read()).decode()
- annotated_image_artefact = Artefact("inline", data, "", "base64")
- expected_data_content = "D: [[[108 847 108 0]]\n\n [[477 847 477 0]]]\nP: [0, 1]"
- data_artefact = Artefact("inline", expected_data_content, "", "text/plain")
- assert response.json() == {
- "ctrl": "ok",
- "output": {
- "image": annotated_image_artefact.as_dict(),
- "data": data_artefact.as_dict(),
- }
- }
|