Skip to content
Snippets Groups Projects
Commit 788b7a13 authored by Jedrzej Rybicki's avatar Jedrzej Rybicki
Browse files

test for delete and response models defined

parent 60ee463f
Branches
Tags
No related merge requests found
Pipeline #69477 passed
...@@ -4,6 +4,7 @@ Main module of data catalog api ...@@ -4,6 +4,7 @@ Main module of data catalog api
import logging import logging
from datetime import timedelta from datetime import timedelta
from enum import Enum from enum import Enum
from typing import List, Tuple
from fastapi import FastAPI, HTTPException, Request, status from fastapi import FastAPI, HTTPException, Request, status
from fastapi.param_functions import Depends from fastapi.param_functions import Depends
...@@ -42,16 +43,6 @@ def my_user(token=Depends(oauth2_scheme)): ...@@ -42,16 +43,6 @@ def my_user(token=Depends(oauth2_scheme)):
def my_auth(form_data: OAuth2PasswordRequestForm = Depends()): def my_auth(form_data: OAuth2PasswordRequestForm = Depends()):
return authenticate_user(userdb, form_data.username, form_data.password) return authenticate_user(userdb, form_data.username, form_data.password)
@app.get("/")
async def get_types():
"""
list types of data locations, currently datasets
(will be provided by the pillars) and targets (possible storage
locations for worklfow results or similar)
"""
return [{element.value: "/" + element.value} for element in LocationDataType]
@app.get("/me", response_model=User) @app.get("/me", response_model=User)
async def read_users_me(user=Depends(my_user)): async def read_users_me(user=Depends(my_user)):
"""return information about the currently logged in user""" """return information about the currently logged in user"""
...@@ -73,14 +64,27 @@ async def login_for_access_token(user=Depends(my_auth)): ...@@ -73,14 +64,27 @@ async def login_for_access_token(user=Depends(my_auth)):
) )
return {"access_token": access_token, "token_type": "bearer"} return {"access_token": access_token, "token_type": "bearer"}
@app.get("/", response_model=List[dict[str, str]])
async def get_types():
"""
list types of data locations, currently datasets
(will be provided by the pillars) and targets (possible storage
locations for worklfow results or similar)
"""
return [{element.value: "/" + element.value} for element in LocationDataType]
@app.get("/{location_data_type}") @app.get("/{location_data_type}", response_model=List[Tuple[str, str]])
async def list_datasets(location_data_type: LocationDataType): async def list_datasets(location_data_type: LocationDataType):
"""list id and name of every registered dataset for the specified type""" """list id and name of every registered dataset for the specified type"""
return adapter.get_list(location_data_type) return adapter.get_list(location_data_type)
@app.post("/{location_data_type}") @app.get("/{location_data_type}/{dataset_id}", response_model=LocationData)
async def get_specific_dataset(location_data_type: LocationDataType, dataset_id: str):
"""returns all information about a specific dataset, identified by id"""
return adapter.get_details(location_data_type, dataset_id)
@app.post("/{location_data_type}", response_model=Tuple[str, LocationData])
async def add_dataset(location_data_type: LocationDataType, async def add_dataset(location_data_type: LocationDataType,
dataset: LocationData, dataset: LocationData,
user: User = Depends(my_user)): user: User = Depends(my_user)):
...@@ -88,13 +92,7 @@ async def add_dataset(location_data_type: LocationDataType, ...@@ -88,13 +92,7 @@ async def add_dataset(location_data_type: LocationDataType,
return adapter.add_new(location_data_type, dataset, user.username) return adapter.add_new(location_data_type, dataset, user.username)
@app.get("/{location_data_type}/{dataset_id}") @app.put("/{location_data_type}/{dataset_id}", response_model=Tuple[str, LocationData])
async def get_specific_dataset(location_data_type: LocationDataType, dataset_id: str):
"""returns all information about a specific dataset, identified by id"""
return adapter.get_details(location_data_type, dataset_id)
@app.put("/{location_data_type}/{dataset_id}")
async def update_specific_dataset(location_data_type: LocationDataType, async def update_specific_dataset(location_data_type: LocationDataType,
dataset_id: str, dataset: LocationData, dataset_id: str, dataset: LocationData,
user: User = Depends(my_user)): user: User = Depends(my_user)):
...@@ -111,6 +109,7 @@ async def delete_specific_dataset(location_data_type: LocationDataType, ...@@ -111,6 +109,7 @@ async def delete_specific_dataset(location_data_type: LocationDataType,
return adapter.delete(location_data_type, dataset_id, user.username) return adapter.delete(location_data_type, dataset_id, user.username)
@app.exception_handler(FileNotFoundError) @app.exception_handler(FileNotFoundError)
async def not_found_handler(request: Request, ex: FileNotFoundError): async def not_found_handler(request: Request, ex: FileNotFoundError):
oid=request.path_params.get('dataset_id', '') oid=request.path_params.get('dataset_id', '')
......
...@@ -37,9 +37,7 @@ class UserTests(TestCase): ...@@ -37,9 +37,7 @@ class UserTests(TestCase):
'metadata': {'key': 'value'} 'metadata': {'key': 'value'}
} }
rsp = self.client.post('/dataset', json=my_data) rsp = self.client.post('/dataset', json=my_data)
print(rsp.content)
self.assertEqual(rsp.status_code, 200) self.assertEqual(rsp.status_code, 200)
print(rsp.content)
(oid, dty) = rsp.json() (oid, dty) = rsp.json()
self.assertIsNotNone(oid) self.assertIsNotNone(oid)
...@@ -48,6 +46,22 @@ class UserTests(TestCase): ...@@ -48,6 +46,22 @@ class UserTests(TestCase):
self.client.delete(f"/dataset/{oid}") self.client.delete(f"/dataset/{oid}")
def test_delete(self):
rsp = self.client.delete("/dataset/foo")
self.assertEqual(rsp.status_code, 404, 'deleted called on non-existing')
rsp = self.client.post('/dataset', json={
'name': 'some dataset',
'url': 'http://loc.me/1'}
)
self.assertEqual(rsp.status_code, 200)
(oid, dty) = rsp.json()
rsp = self.client.delete(f"/dataset/{oid}")
self.assertEqual(rsp.status_code, 200)
def test_create_and_get(self): def test_create_and_get(self):
dss = [{'name': f"ds_{i}", 'url': f"http://www.o.com/{i}"} for i in range(5)] dss = [{'name': f"ds_{i}", 'url': f"http://www.o.com/{i}"} for i in range(5)]
for d in dss: for d in dss:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment