Commit e332fe83 authored by fabian serin's avatar fabian serin
Browse files

Remove on the fly conversion

parent 23f6f087
......@@ -26,25 +26,11 @@ from app.utils import Context
from app.utils import get_ctx
from app.utils import load_schema_example
from app.model.model_utils import to_record, from_record
from app.converter.well_converter import WellConverter
from app.routers.ddms_v3.ddms_v3_utils import DMSV3RouterUtils
router = APIRouter()
async def get_well_as_osdu(wellid: str, ctx: Context) -> Well:
storage_client = await get_storage_record_service(ctx)
well_record = await storage_client.get_record(
id=wellid, data_partition_id=ctx.partition_id
)
res_as_dict = well_record.dict(
exclude_unset=True, exclude_none=True, by_alias=True
)
well = Well.parse_obj(WellConverter.convert_delfi_to_osdu(res_as_dict,
context={"namespace": ctx.partition_id}))
return well
async def get_osdu_well(wellid: str, ctx: Context) -> Well:
storage_client = await get_storage_record_service(ctx)
well_record = await storage_client.get_record(
......@@ -67,18 +53,15 @@ async def get_osdu_well(wellid: str, ctx: Context) -> Well:
async def get_well_osdu(
wellid: str, ctx: Context = Depends(get_ctx)
) -> Well:
deli_conversion, delfi_id = DMSV3RouterUtils.is_osdu_entity_fake_id(wellid)
if deli_conversion:
return await get_well_as_osdu(delfi_id, ctx)
fake_id, _ = DMSV3RouterUtils.is_osdu_entity_fake_id(wellid)
if fake_id:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Id is not OSDU Well")
is_osdu_versioned, osdu_id, version = DMSV3RouterUtils.is_osdu_versionned_well_id(wellid)
if is_osdu_versioned:
return await get_osdu_well(osdu_id, ctx)
if DMSV3RouterUtils.is_osdu_well_id(wellid):
return await get_osdu_well(wellid, ctx)
if DMSV3RouterUtils.is_delfi_id(wellid):
return await get_well_as_osdu(wellid, ctx)
raise HTTPException(status_code=status.HTTP_417_EXPECTATION_FAILED, detail="Id is not a wellbore")
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Id is not OSDU Well")
......
......@@ -27,25 +27,11 @@ from app.utils import Context
from app.utils import get_ctx
from app.utils import load_schema_example
from app.model.model_utils import to_record, from_record
from app.converter.wellbore_converter import WellboreConverter
from app.routers.ddms_v3.ddms_v3_utils import DMSV3RouterUtils
router = APIRouter()
async def get_wellbore_as_osdu(wellboreid: str, ctx: Context) -> Wellbore:
storage_client = await get_storage_record_service(ctx)
wellbore_record = await storage_client.get_record(
id=wellboreid, data_partition_id=ctx.partition_id
)
res_as_dict = wellbore_record.dict(
exclude_unset=True, exclude_none=True, by_alias=True
)
wellbore = Wellbore.parse_obj(WellboreConverter.convert_delfi_to_osdu(res_as_dict,
context={"namespace": ctx.partition_id}))
return wellbore
async def get_osdu_wellbore(wellboreid: str, ctx: Context) -> Wellbore:
storage_client = await get_storage_record_service(ctx)
wellbore_record = await storage_client.get_record(
......@@ -59,9 +45,7 @@ async def get_osdu_wellbore(wellboreid: str, ctx: Context) -> Wellbore:
response_model=Wellbore,
response_model_exclude_unset=True,
summary="Get the Wellbore using osdu schema",
description="""Get the Wellbore object using its **id**.
<p>If the **id** is a Delfi Wellbore Id, tries to convert it on the fly to return the Wellbore as an osdu Wellbore.</p>
{}""".format(REQUIRED_ROLES_READ),
description="""Get the Wellbore object using its **id**.{}""".format(REQUIRED_ROLES_READ),
operation_id="get_wellbore_osdu",
responses={
status.HTTP_404_NOT_FOUND: {"description": "Wellbore not found"}
......@@ -70,18 +54,15 @@ async def get_osdu_wellbore(wellboreid: str, ctx: Context) -> Wellbore:
async def get_wellbore_osdu(
wellboreid: str, ctx: Context = Depends(get_ctx)
) -> Wellbore:
delfi_convertion, delfi_id = DMSV3RouterUtils.is_osdu_entity_fake_id(wellboreid)
if delfi_convertion:
return await get_wellbore_as_osdu(delfi_id, ctx)
fake_id, _ = DMSV3RouterUtils.is_osdu_entity_fake_id(wellboreid)
if fake_id:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Id is not OSDU Wellbore")
is_osdu_versionned, osdu_id, version = DMSV3RouterUtils.is_osdu_versionned_wellbore_id(wellboreid)
if is_osdu_versionned:
return await get_osdu_wellbore(osdu_id, ctx)
if DMSV3RouterUtils.is_osdu_wellbore_id(wellboreid):
return await get_osdu_wellbore(wellboreid, ctx)
if DMSV3RouterUtils.is_delfi_id(wellboreid):
return await get_wellbore_as_osdu(wellboreid, ctx)
raise HTTPException(status_code=status.HTTP_417_EXPECTATION_FAILED, detail="Id is not a wellbore")
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Id is not OSDU Wellbore")
......
......@@ -16832,7 +16832,7 @@
]
},
"get": {
"description": "Get the Wellbore object using its **id**.\n <p>If the **id** is a Delfi Wellbore Id, tries to convert it on the fly to return the Wellbore as an osdu Wellbore.</p> \n \n<p>Required roles: 'users.datalake.viewers' or 'users.datalake.editors' or 'users.datalake.admins'.\n\"In addition, users must be a member of data groups to access the data.</p>\n",
"description": "Get the Wellbore object using its **id**.\n<p>Required roles: 'users.datalake.viewers' or 'users.datalake.editors' or 'users.datalake.admins'.\n\"In addition, users must be a member of data groups to access the data.</p>\n",
"operationId": "get_wellbore_osdu",
"parameters": [
{
......
......@@ -108,7 +108,7 @@ def test_crud_get_as_record(delfi_id, kind, with_wdms_env):
# Get it as osdu wellbore with delfi id
result = build_request(f'crud.osdu_{kind}.get_osdu_{kind}').call(with_wdms_env)
result.assert_ok()
result.assert_status_code(400)
# Get it as osdu wellbore with fake osdu id
id_as_list = delfi_record_id.split(sep=":")
......@@ -117,4 +117,4 @@ def test_crud_get_as_record(delfi_id, kind, with_wdms_env):
fakeid = ":".join(res_as_list)
with_wdms_env.set(f'osdu_{kind}_record_id', fakeid) # stored the record id for the following tests
result = build_request(f'crud.osdu_{kind}.get_osdu_{kind}').call(with_wdms_env)
result.assert_ok()
result.assert_status_code(400)
......@@ -139,12 +139,18 @@ def test_post_records_successful(client):
getas_parameters = [
(Wellbore, "/ddms/v3/wellbores", r"../../converter/wellbore_wks.json", "opendes:wellbore:12345"),
(Wellbore, "/ddms/v3/wellbores",r"../../converter/wellbore_wks.json", "opendes:master-data--Wellbore:6f70656e6465733a646f633a3132333435:"),
(Wellbore, "/ddms/v3/wellbores", r"../../../../app/model_examples/wellbore_v3.json", "opendes:master-data--Wellbore:123"),
(Well, "/ddms/v3/wells", r"../../converter/well_wks.json", "opendes:well:12345"),
(Well, "/ddms/v3/wells", r"../../converter/well_wks.json", "opendes:master-data--Well:6f70656e6465733a646f633a3132333435:"),
(Well, "/ddms/v3/wells", r"../../../../app/model_examples/well_v3.json", "opendes:master-data--Well:12345:456"),
(Wellbore, "/ddms/v3/wellbores", r"../../converter/wellbore_wks.json", "opendes:wellbore:12345",
status.HTTP_400_BAD_REQUEST),
(Wellbore, "/ddms/v3/wellbores", r"../../converter/wellbore_wks.json",
"opendes:master-data--Wellbore:6f70656e6465733a646f633a3132333435:", status.HTTP_400_BAD_REQUEST),
(Wellbore, "/ddms/v3/wellbores", r"../../../../app/model_examples/wellbore_v3.json",
"opendes:master-data--Wellbore:123", status.HTTP_200_OK),
(Well, "/ddms/v3/wells", r"../../converter/well_wks.json", "opendes:well:12345",
status.HTTP_400_BAD_REQUEST),
(Well, "/ddms/v3/wells", r"../../converter/well_wks.json",
"opendes:master-data--Well:6f70656e6465733a646f633a3132333435:", status.HTTP_400_BAD_REQUEST),
(Well, "/ddms/v3/wells", r"../../../../app/model_examples/well_v3.json", "opendes:master-data--Well:12345:456",
status.HTTP_200_OK),
]
......@@ -155,8 +161,8 @@ def replace_template(source_obj_str: str) -> str:
return source_obj_str
@pytest.mark.parametrize("entity_class, base_url, source_file, record_id", getas_parameters)
def test_get_record_as_OSDU(client, entity_class, base_url, source_file, record_id):
@pytest.mark.parametrize("entity_class, base_url, source_file, record_id, expected_response", getas_parameters)
def test_get_record_as_OSDU(client, entity_class, base_url, source_file, record_id, expected_response):
dir_path = os.path.dirname(os.path.realpath(__file__))
with open(os.path.join(dir_path, source_file)) as f:
record_str = replace_template(f.read())
......@@ -174,11 +180,11 @@ def test_get_record_as_OSDU(client, entity_class, base_url, source_file, record_
headers={"data-partition-id": "testing_partition"},
)
assert response.status_code == status.HTTP_200_OK
# assert it validates the input object schema
res = response.json()
entity_class.validate(res)
assert response.status_code == expected_response
if response.status_code == status.HTTP_200_OK:
# assert it validates the input object schema
res = response.json()
entity_class.validate(res)
get_invalid_id_parameters = [
......@@ -193,7 +199,7 @@ def test_get_record_incorrect_id(client, entity_class, base_url, record_id):
f"{base_url}/{record_id}",
headers={"data-partition-id": "testing_partition"},
)
assert response.status_code == status.HTTP_417_EXPECTATION_FAILED
assert response.status_code == status.HTTP_400_BAD_REQUEST
@pytest.mark.parametrize("base_url, id, record_obj", tests_parameters)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment