From 51913f015b7ee24741049d80f50c58523e305ec9 Mon Sep 17 00:00:00 2001 From: Georg-Stahn Date: Sat, 2 Dec 2023 12:57:44 +0100 Subject: [PATCH] Georg-Stahn-georgs (#25) Co-authored-by: sara-pervana Co-authored-by: ui-asset-bot Co-authored-by: Onur Arslan Co-authored-by: Arslan, Erdem Co-authored-by: Luis-Hebendanz Reviewed-on: https://gitea.gchq.icu/IoSL/service-aware-frontend/pulls/25 Co-authored-by: Georg-Stahn Co-committed-by: Georg-Stahn --- .gitignore | 4 + pkgs/clan-cli/clan_cli/webui/api_outputs.py | 54 ++++++++ pkgs/clan-cli/clan_cli/webui/schemas.py | 4 +- pkgs/clan-cli/clan_cli/webui/sql_app.db | Bin 20480 -> 118784 bytes pkgs/clan-cli/clan_cli/webui/sql_db.py | 5 +- pkgs/clan-cli/clan_cli/webui/sql_models.py | 3 +- pkgs/clan-cli/tests/test_db_api.py | 134 ++++++++++++++++++++ 7 files changed, 199 insertions(+), 5 deletions(-) create mode 100644 pkgs/clan-cli/clan_cli/webui/api_outputs.py create mode 100644 pkgs/clan-cli/tests/test_db_api.py diff --git a/.gitignore b/.gitignore index 04d7fce..d8948dd 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,7 @@ pkgs.pyproj .reports .ruff_cache htmlcov + +# georgs +pkgs/.vs/ +pkgs/clan-cli/.hypothesis/ diff --git a/pkgs/clan-cli/clan_cli/webui/api_outputs.py b/pkgs/clan-cli/clan_cli/webui/api_outputs.py new file mode 100644 index 0000000..f0f2632 --- /dev/null +++ b/pkgs/clan-cli/clan_cli/webui/api_outputs.py @@ -0,0 +1,54 @@ +from enum import Enum + +from pydantic import BaseModel, Field + + +class Status(Enum): + ONLINE = "online" + OFFLINE = "offline" + UNKNOWN = "unknown" + + +class Machine(BaseModel): + name: str + status: Status + + +class RepositoryBase(BaseModel): + title: str + description: str | None = None + + +class RepositoryCreate(RepositoryBase): + pass + + +class Repository(RepositoryBase): + id: int + prod_id: str + + class Config: + orm_mode = True + + +class ProducerBase(BaseModel): + id: int + + +class ProducerCreate(ProducerBase): + jsonblob: int = Field( + 42, + title="The Json", + description="this is the value of json", + gt=30, + lt=50, + list=[1, 2, "3"], + ) + + +class Producer(ProducerBase): + id: int + repos: list[Repository] = [] + + class Config: + orm_mode = True diff --git a/pkgs/clan-cli/clan_cli/webui/schemas.py b/pkgs/clan-cli/clan_cli/webui/schemas.py index f259b8e..96987f9 100644 --- a/pkgs/clan-cli/clan_cli/webui/schemas.py +++ b/pkgs/clan-cli/clan_cli/webui/schemas.py @@ -1,6 +1,6 @@ from datetime import datetime from enum import Enum -from typing import List, Optional +from typing import List from pydantic import BaseModel @@ -101,7 +101,7 @@ class EntityCreate(EntityBase): class Entity(EntityCreate): producers: List[Producer] = [] consumers: List[Consumer] = [] - repository: Optional[Repository] = None + repository: List[Repository] = [] class Config: orm_mode = True diff --git a/pkgs/clan-cli/clan_cli/webui/sql_app.db b/pkgs/clan-cli/clan_cli/webui/sql_app.db index 382350b21428fe4ac0daedc1e08532fd7f155e42..6d1f62ff087420f261feba70882b3ede2aef8603 100644 GIT binary patch literal 118784 zcmWFz^vNtqRY=P(%1ta$FlG>7U}R))P*7lCV5nkXV31`%fKmnq1{MUDff0#~i&@2> zm$!?T{|AE%6E_3DGXHVDgM8V1(!3{l{_s?D|K)Dt7T`R*95EL&MTm`^grGf!jU#%1`ZI8{R+NQO;ZTb?m9GcP5zBC{esIX|zsG&i-VI6gB4 z&X#o!a&-)GRq*t4ag9(wQ>EbVrvO)}ppluP*<>QcE-o+6*kWFin3R(WmxMEFx*8sevS=5ajCS8szHd>>3Qwk(pYopplZ9f}sh;cuiz|FqvWn4Ul^^ zxik%>*~DGt85zJ)QCyglS&|x`SXz<~<|9RnAv_#qY9-mlT~!$yWx;APE8>e%3-XII zOY(~{Q;Xw^Q;W(nlT+jK5_40LC8VIi506e{Nfce6xJ8y!&_Fg*Gh2dP+)|aXkrli3 zC5a`a#mL$v@mma5kHt!`xMqqto4BbeV=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~Y@>G6W1}H38UmvsFd71*Aut*OqaiRF0;3@?8UmvsFd71*Apj47(f&U?C`K8h nAut*OqaiRF0;3@?8UmvsFd71*Aut*OqaiRF0;3@?Y(oG5zzy9P delta 596 zcmZozz}~Qcae}lUI|Bm)D+2+r3)Pnrt%#!?~%+zAm$ro8=?K4vp{QVS=#T7I%Q#2bLnc2lv zRT-OHOA?cEQjz76*({SC*<>f%@ru`H73b$A<>V(Rcm@0W=_qh<78K>D#Al``c>0C7 zy1NE}gaU#*eI0`$6}(*|!Ipr;-28)FJ>C64Vj55_nhHU#ZmvPDe$KAJ3Lvr4lGNgocu?3WSt%&NjEYY#N=+ None: + # print(type()) + for info in infos: + assert info in response.keys() + # TODO maybe check the content of the extra info ... + response.pop(info) + assert response == request_body + + +def make_test_post_and_get( + api: TestClient, + request_body: dict[str, Any], + paramter: str, + get_request: str = default_entity_did_url, + apiversion: str = "v1", +) -> None: + # test post + response = api.post( + f"/api/{apiversion}/create_{paramter}", + json=request_body, + headers={"Content-Type": "application/json"}, + ) + assert response.status_code == 200 + if paramter == "repository": + assert_extra_info(["time_created"], request_body, response.json()) + elif paramter == "consumer": + assert_extra_info(["id"], request_body, response.json()) + elif paramter == "entity": + assert_extra_info( + ["consumers", "producers", "repository"], request_body, response.json() + ) + else: + assert response.json() == request_body + # test get + response = api.get( + f"api/{apiversion}/get_{paramter}?{get_request}&skip=0&limit=100" + ) + assert response.status_code == 200 + if paramter == "repository": + assert_extra_info(["time_created"], request_body, response.json()[0]) + elif paramter == "consumer": + assert_extra_info(["id"], request_body, response.json()[0]) + elif paramter == "entity": + assert_extra_info( + ["consumers", "producers", "repository"], request_body, response.json() + ) + else: + assert response.json() == [request_body] + + +######################### +# # +# Producer # +# # +######################### +def test_producer(api: TestClient) -> None: + request_body = { + "uuid": "8e285c0c-4e40-430a-a477-26b3b81e30df", + "service_name": "Carlo'''s Printing", + "service_type": "3D Printing", + "endpoint_url": "http://127.0.0.1:8000", + "status": "unknown", + "other": {"test": "test"}, + "entity_did": default_entity_did, + } + paramter = "producer" + # get_request = "entity_did=did%3Asov%3Atest%3A1234" + make_test_post_and_get(api, request_body, paramter) + + +######################### +# # +# Consumer # +# # +######################### +def test_consumer(api: TestClient) -> None: + request_body = { + "entity_did": default_entity_did, + "producer_uuid": "8e285c0c-4e40-430a-a477-26b3b81e30df", + "other": {"test": "test"}, + } + paramter = "consumer" + # get_request = "entity_did=did%3Asov%3Atest%3A1234" + make_test_post_and_get(api, request_body, paramter) + + +######################### +# # +# REPOSITORY # +# # +######################### +def test_repository(api: TestClient) -> None: + request_body = { + "uuid": "8e285c0c-4e40-430a-a477-26b3b81e30df", + "service_name": "Carlo'''s Printing", + "service_type": "3D Printing", + "endpoint_url": "http://127.0.0.1:8000", + "status": "unknown", + "other": {"test": "test"}, + "entity_did": default_entity_did, + } + paramter = "repository" + # get_request = "entity_did=did%3Asov%3Atest%3A1234" + make_test_post_and_get(api, request_body, paramter) + + +######################### +# # +# Entity # +# # +######################### +def test_entity(api: TestClient) -> None: + request_body = { + "did": default_entity_did, + "name": "C1", + "ip": "127.0.0.1", + "attached": False, + "other": {"test": "test"}, + } + paramter = "entity" + # get_request = "entity_did=did%3Asov%3Atest%3A1234" + make_test_post_and_get(api, request_body, paramter)