From edaa5312a0343fd3bb5e99e568929f1951ecde90 Mon Sep 17 00:00:00 2001 From: Rose Yemelyanova Date: Wed, 13 Jul 2022 08:35:36 +0100 Subject: [PATCH 1/2] making changes to tagOrIdx parameter --- src/diffcalc_API/models/UBCalculation.py | 4 ++++ src/diffcalc_API/routes/UBCalculation.py | 20 +++++++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/diffcalc_API/models/UBCalculation.py b/src/diffcalc_API/models/UBCalculation.py index 4e17582..5d83a86 100644 --- a/src/diffcalc_API/models/UBCalculation.py +++ b/src/diffcalc_API/models/UBCalculation.py @@ -41,3 +41,7 @@ class editOrientationParams(BaseModel): xyz: Optional[Tuple[float, float, float]] = None position: Optional[Tuple[float, float, float, float, float, float]] = None tagOrIdx: Union[int, str] + + +class deleteParams(BaseModel): + tagOrIdx: Union[int, str] diff --git a/src/diffcalc_API/routes/UBCalculation.py b/src/diffcalc_API/routes/UBCalculation.py index 7be25a4..f556b98 100644 --- a/src/diffcalc_API/routes/UBCalculation.py +++ b/src/diffcalc_API/routes/UBCalculation.py @@ -18,6 +18,7 @@ from diffcalc_API.models.UBCalculation import ( addOrientationParams, addReflectionParams, + deleteParams, editOrientationParams, editReflectionParams, setLatticeParams, @@ -26,11 +27,6 @@ router = APIRouter(prefix="/ub", tags=["ub"]) -@router.get("/") -async def read_main(): - return {"msg": "Hello World"} - - @router.put("/{name}/reflection") async def add_reflection( name: str, @@ -54,7 +50,12 @@ async def add_reflection( ) persist(hklCalc, name) - return {"message": f"added reflection for UB Calculation of crystal {name}"} + return { + "message": ( + f"added reflection for UB Calculation of crystal {name}. " + f"Reflist is: {hklCalc.ubcalc.reflist.reflections}" + ) + } @router.put("/{name}/orientation") @@ -111,7 +112,6 @@ async def edit_reflection( persist: Callable[[HklCalculation, str], Path] = Depends(supplyPersist), ): reflection = get_reflection(hklCalc, params.tagOrIdx) - hklCalc.ubcalc.edit_reflection( params.tagOrIdx, params.hkl if params.hkl else (reflection.h, reflection.k, reflection.l), @@ -189,10 +189,12 @@ async def calculate_UB( @router.delete("/{name}/reflection") async def delete_reflection( name: str, - tagOrIdx: Union[str, int] = Body(..., example="refl1"), + tagOrIdx: deleteParams = Body(..., example={"tagOrIdx": "refl1"}), hklCalc: HklCalculation = Depends(unpickleHkl), persist: Callable[[HklCalculation, str], Path] = Depends(supplyPersist), ): + print(tagOrIdx) + print(type(tagOrIdx)) _ = get_reflection(hklCalc, tagOrIdx) hklCalc.ubcalc.del_reflection(tagOrIdx) persist(hklCalc, name) @@ -203,7 +205,7 @@ async def delete_reflection( @router.delete("/{name}/orientation") async def delete_orientation( name: str, - tagOrIdx: Union[str, int] = Body(..., example="plane"), + tagOrIdx: deleteParams = Body(..., example={"tagOrIdx": "plane"}), hklCalc: HklCalculation = Depends(unpickleHkl), persist: Callable[[HklCalculation, str], Path] = Depends(supplyPersist), ): From a6d9f1bda9c162af30ca932197fa866a2e3d738c Mon Sep 17 00:00:00 2001 From: Rose Yemelyanova Date: Wed, 13 Jul 2022 07:51:18 +0000 Subject: [PATCH 2/2] changed route parameters for deleting orientations/reflections to allow integer as well as string inputs --- src/diffcalc_API/routes/UBCalculation.py | 18 ++++++++---------- tests/test_diffcalc_API.py | 5 ----- tests/test_ubcalc.py | 8 ++++---- 3 files changed, 12 insertions(+), 19 deletions(-) delete mode 100644 tests/test_diffcalc_API.py diff --git a/src/diffcalc_API/routes/UBCalculation.py b/src/diffcalc_API/routes/UBCalculation.py index f556b98..513ddb5 100644 --- a/src/diffcalc_API/routes/UBCalculation.py +++ b/src/diffcalc_API/routes/UBCalculation.py @@ -189,28 +189,26 @@ async def calculate_UB( @router.delete("/{name}/reflection") async def delete_reflection( name: str, - tagOrIdx: deleteParams = Body(..., example={"tagOrIdx": "refl1"}), + params: deleteParams = Body(..., example={"tagOrIdx": "refl1"}), hklCalc: HklCalculation = Depends(unpickleHkl), persist: Callable[[HklCalculation, str], Path] = Depends(supplyPersist), ): - print(tagOrIdx) - print(type(tagOrIdx)) - _ = get_reflection(hklCalc, tagOrIdx) - hklCalc.ubcalc.del_reflection(tagOrIdx) + _ = get_reflection(hklCalc, params.tagOrIdx) + hklCalc.ubcalc.del_reflection(params.tagOrIdx) persist(hklCalc, name) - return {"message": f"reflection with tag or index {tagOrIdx} deleted."} + return {"message": f"reflection with tag or index {params.tagOrIdx} deleted."} @router.delete("/{name}/orientation") async def delete_orientation( name: str, - tagOrIdx: deleteParams = Body(..., example={"tagOrIdx": "plane"}), + params: deleteParams = Body(..., example={"tagOrIdx": "plane"}), hklCalc: HklCalculation = Depends(unpickleHkl), persist: Callable[[HklCalculation, str], Path] = Depends(supplyPersist), ): - _ = get_orientation(hklCalc, tagOrIdx) - hklCalc.ubcalc.del_orientation(tagOrIdx) + _ = get_orientation(hklCalc, params.tagOrIdx) + hklCalc.ubcalc.del_orientation(params.tagOrIdx) persist(hklCalc, name) - return {"message": f"reflection with tag or index {tagOrIdx} deleted."} + return {"message": f"reflection with tag or index {params.tagOrIdx} deleted."} diff --git a/tests/test_diffcalc_API.py b/tests/test_diffcalc_API.py deleted file mode 100644 index afc9c96..0000000 --- a/tests/test_diffcalc_API.py +++ /dev/null @@ -1,5 +0,0 @@ -# do tests here... - - -def test_dummy(): - pass diff --git a/tests/test_ubcalc.py b/tests/test_ubcalc.py index d64cda7..41c1993 100644 --- a/tests/test_ubcalc.py +++ b/tests/test_ubcalc.py @@ -68,7 +68,7 @@ def test_edit_reflection(client: TestClient): def test_delete_reflection(client: TestClient): dummyHkl.ubcalc.add_reflection([0, 0, 1], Position(7, 0, 10, 0, 0, 0), 12, "foo") - response = client.delete("/ub/test/reflection", json="foo") + response = client.delete("/ub/test/reflection", json={"tagOrIdx": "foo"}) assert response.status_code == 200 with pytest.raises(Exception): @@ -87,7 +87,7 @@ def test_edit_or_delete_reflection_fails_for_non_existing_reflection( ) deleteResponse = client.delete( "/ub/test/reflection", - json="foo", + json={"tagOrIdx": "foo"}, ) assert editResponse.status_code == codes.get_reflection @@ -134,7 +134,7 @@ def test_delete_orientation(client: TestClient): dummyHkl.ubcalc.add_orientation([0, 0, 1], [0, 0, 1], None, "bar") response = client.delete( "/ub/test/orientation", - json="bar", + json={"tagOrIdx": "bar"}, ) assert response.status_code == 200 @@ -154,7 +154,7 @@ def test_edit_or_delete_orientation_fails_for_non_existing_orientation( ) deleteResponse = client.delete( "/ub/test/orientation", - json="bar", + json={"tagOrIdx": "bar"}, ) assert editResponse.status_code == codes.get_orientation