diff --git a/src/diffcalc_API/errors/ub.py b/src/diffcalc_API/errors/ub.py index 931665b..92f9d0e 100644 --- a/src/diffcalc_API/errors/ub.py +++ b/src/diffcalc_API/errors/ub.py @@ -1,4 +1,4 @@ -from typing import Union +from typing import Optional, Union import numpy as np @@ -14,11 +14,32 @@ class ErrorCodes(ErrorCodesBase): INVALID_SET_LATTICE_PARAMS = 400 REFERENCE_RETRIEVAL_ERROR = 403 INVALID_PROPERTY = 400 + NO_TAG_OR_IDX_PROVIDED = 400 + BOTH_TAG_OR_IDX_PROVIDED = 400 responses = {code: ALL_RESPONSES[code] for code in np.unique(ErrorCodes.all_codes())} +class NoTagOrIdxProvidedError(DiffcalcAPIException): + def __init__(self): + self.detail = ( + "One of the following must be provided as a query parameter:" + + " tag (string), index (integer)" + ) + self.status_code = ErrorCodes.NO_TAG_OR_IDX_PROVIDED + + +class BothTagAndIdxProvidedError(DiffcalcAPIException): + def __init__(self): + self.detail = ( + "both the tag and index have been provided. These are identifiers" + + " for a specific orientation or reflection, and so both cannot be" + + " used. Retry with just one tag or index query parameter." + ) + self.status_code = ErrorCodes.BOTH_TAG_OR_IDX_PROVIDED + + class InvalidSetLatticeParamsError(DiffcalcAPIException): def __init__(self): self.detail = ("please provide lattice parameters in request body",) @@ -26,7 +47,7 @@ def __init__(self): class ReferenceRetrievalError(DiffcalcAPIException): - def __init__(self, handle: Union[str, int], reference_type: str) -> None: + def __init__(self, handle: Optional[Union[str, int]], reference_type: str) -> None: self.detail = f"cannot retrieve {reference_type} with tag or index {handle}" self.status_code = ErrorCodes.REFERENCE_RETRIEVAL_ERROR diff --git a/src/diffcalc_API/examples/ub.py b/src/diffcalc_API/examples/ub.py index 491638c..f5c4d8a 100644 --- a/src/diffcalc_API/examples/ub.py +++ b/src/diffcalc_API/examples/ub.py @@ -14,26 +14,23 @@ "hkl": HklModel(h=0, k=0, l=1), "position": PositionModel(mu=7.31, delta=0.0, nu=10.62, eta=0, chi=0.0, phi=0), "energy": 12.39842, - "tag": "refl1", } ) edit_reflection: EditReflectionParams = EditReflectionParams( - **{"energy": 12.45, "tag_or_idx": "refl1"} + **{"energy": 12.45, "set_tag": "refl2"} ) add_orientation: AddOrientationParams = AddOrientationParams( **{ "hkl": HklModel(h=0, k=1, l=0), "xyz": XyzModel(x=0, y=1, z=0), - "tag": "plane", } ) edit_orientation: EditOrientationParams = EditOrientationParams( **{ "hkl": HklModel(h=0, k=1, l=0), - "tag_or_idx": "plane", } ) diff --git a/src/diffcalc_API/models/ub.py b/src/diffcalc_API/models/ub.py index e6ce562..27b6cb2 100644 --- a/src/diffcalc_API/models/ub.py +++ b/src/diffcalc_API/models/ub.py @@ -1,4 +1,4 @@ -from typing import Optional, Union +from typing import Optional from pydantic import BaseModel @@ -25,7 +25,7 @@ class PositionModel(BaseModel): class SetLatticeParams(BaseModel): - system: Optional[Union[str, float]] = None + system: Optional[str] = None a: Optional[float] = None b: Optional[float] = None c: Optional[float] = None @@ -38,29 +38,27 @@ class AddReflectionParams(BaseModel): hkl: HklModel position: PositionModel energy: float - tag: Optional[str] = None class AddOrientationParams(BaseModel): hkl: HklModel xyz: XyzModel position: Optional[PositionModel] = None - tag: Optional[str] = None class EditReflectionParams(BaseModel): hkl: Optional[HklModel] = None position: Optional[PositionModel] = None energy: Optional[float] = None - tag_or_idx: Union[int, str] + set_tag: Optional[str] = None class EditOrientationParams(BaseModel): hkl: Optional[HklModel] = None xyz: Optional[XyzModel] = None position: Optional[PositionModel] = None - tag_or_idx: Union[int, str] + set_tag: Optional[str] = None -class DeleteParams(BaseModel): - tag_or_idx: Union[int, str] +def select_idx_or_tag_str(idx: Optional[int], tag: Optional[str]): + return f"index {idx}" if idx is not None else f"tag {tag}" diff --git a/src/diffcalc_API/routes/constraints.py b/src/diffcalc_API/routes/constraints.py index 2e2ed90..8139d07 100644 --- a/src/diffcalc_API/routes/constraints.py +++ b/src/diffcalc_API/routes/constraints.py @@ -1,4 +1,4 @@ -from typing import Dict, Optional, Union +from typing import Dict, Optional from fastapi import APIRouter, Body, Depends, Query @@ -21,9 +21,7 @@ async def get_constraints( @router.post("/{name}") async def set_constraints( name: str, - constraints: Dict[str, Union[float, bool]] = Body( - example={"qaz": 0, "alpha": 0, "eta": 0} - ), + constraints: Dict[str, float] = Body(example={"qaz": 0, "alpha": 0, "eta": 0}), store: HklCalcStore = Depends(get_store), collection: Optional[str] = Query(default=None, example="B07"), ): @@ -58,7 +56,7 @@ async def remove_constraint( async def set_constraint( name: str, property: str, - value: Union[float, bool] = Body(...), + value: float = Body(...), store: HklCalcStore = Depends(get_store), collection: Optional[str] = Query(default=None, example="B07"), ): diff --git a/src/diffcalc_API/routes/hkl.py b/src/diffcalc_API/routes/hkl.py index 482de4d..71cc2c8 100644 --- a/src/diffcalc_API/routes/hkl.py +++ b/src/diffcalc_API/routes/hkl.py @@ -1,4 +1,4 @@ -from typing import List, Optional, Union +from typing import List, Optional from fastapi import APIRouter, Depends, Query @@ -12,19 +12,22 @@ @router.get("/{name}/UB") async def calculate_ub( name: str, - first_tag: Optional[Union[int, str]] = Query(default=None, example="refl1"), - second_tag: Optional[Union[int, str]] = Query(default=None, example="plane"), + tag1: Optional[str] = Query(default=None, example="refl1"), + idx1: Optional[int] = Query(default=None), + tag2: Optional[str] = Query(default=None, example="plane"), + idx2: Optional[int] = Query(default=None), store: HklCalcStore = Depends(get_store), collection: Optional[str] = Query(default=None, example="B07"), ): - content = await service.calculate_ub(name, first_tag, second_tag, store, collection) + content = await service.calculate_ub( + name, store, collection, tag1, idx1, tag2, idx2 + ) return {"payload": content} @router.get("/{name}/position/lab") async def lab_position_from_miller_indices( name: str, - # miller_indices: List[float] = Query(example=[0, 0, 1]), miller_indices: HklModel = Depends(), wavelength: float = Query(..., example=1.0), store: HklCalcStore = Depends(get_store), @@ -40,10 +43,7 @@ async def lab_position_from_miller_indices( @router.get("/{name}/position/hkl") async def miller_indices_from_lab_position( name: str, - pos: PositionModel = Depends( - # ..., example={"mu": 7.31, "delta": 0, "nu": 10.62, - # "eta": 0, "chi": 0, "phi": 0} - ), + pos: PositionModel = Depends(), wavelength: float = Query(..., example=1.0), store: HklCalcStore = Depends(get_store), collection: Optional[str] = Query(default=None, example="B07"), @@ -59,7 +59,7 @@ async def scan_hkl( name: str, start: List[float] = Query(..., example=[1, 0, 1]), stop: List[float] = Query(..., example=[2, 0, 2]), - inc: List[float] = Query(..., example=(0.1, 0, 0.1)), + inc: List[float] = Query(..., example=[0.1, 0, 0.1]), wavelength: float = Query(..., example=1), store: HklCalcStore = Depends(get_store), collection: Optional[str] = Query(default=None, example="B07"), @@ -76,7 +76,6 @@ async def scan_wavelength( start: float = Query(..., example=1.0), stop: float = Query(..., example=2.0), inc: float = Query(..., example=0.2), - # hkl: PositionType = Query(..., example=(1, 0, 1)), hkl: HklModel = Depends(), store: HklCalcStore = Depends(get_store), collection: Optional[str] = Query(default=None, example="B07"), @@ -94,7 +93,6 @@ async def scan_constraint( start: float = Query(..., example=1), stop: float = Query(..., example=4), inc: float = Query(..., example=1), - # hkl: PositionType = Query(..., example=(1, 0, 1)), hkl: HklModel = Depends(), wavelength: float = Query(..., example=1.0), store: HklCalcStore = Depends(get_store), diff --git a/src/diffcalc_API/routes/ub.py b/src/diffcalc_API/routes/ub.py index c077ad6..684d516 100644 --- a/src/diffcalc_API/routes/ub.py +++ b/src/diffcalc_API/routes/ub.py @@ -3,16 +3,21 @@ from fastapi import APIRouter, Body, Depends, Query from diffcalc_API.config import VECTOR_PROPERTIES -from diffcalc_API.errors.ub import InvalidPropertyError, InvalidSetLatticeParamsError +from diffcalc_API.errors.ub import ( + BothTagAndIdxProvidedError, + InvalidPropertyError, + InvalidSetLatticeParamsError, + NoTagOrIdxProvidedError, +) from diffcalc_API.examples import ub as examples from diffcalc_API.models.ub import ( AddOrientationParams, AddReflectionParams, - DeleteParams, EditOrientationParams, EditReflectionParams, HklModel, SetLatticeParams, + select_idx_or_tag_str, ) from diffcalc_API.services import ub as service from diffcalc_API.stores.protocol import HklCalcStore, get_store @@ -36,8 +41,9 @@ async def add_reflection( params: AddReflectionParams = Body(..., example=examples.add_reflection), store: HklCalcStore = Depends(get_store), collection: Optional[str] = Query(default=None, example="B07"), + tag: Optional[str] = Query(default=None, example="refl1"), ): - await service.add_reflection(name, params, store, collection) + await service.add_reflection(name, params, store, collection, tag) return { "message": ( f"added reflection for UB Calculation of crystal {name} in " @@ -52,31 +58,39 @@ async def edit_reflection( params: EditReflectionParams = Body(..., example=examples.edit_reflection), store: HklCalcStore = Depends(get_store), collection: Optional[str] = Query(default=None, example="B07"), + tag: Optional[str] = Query(default=None, example="refl1"), + idx: Optional[int] = Query(default=None), ): - await service.edit_reflection(name, params, store, collection) + if (tag is None) and (idx is None): + raise NoTagOrIdxProvidedError() + + if (tag is not None) and (idx is not None): + raise BothTagAndIdxProvidedError() + + await service.edit_reflection(name, params, store, collection, tag, idx) return { - "message": ( - f"reflection of crystal {name} in collection {collection} " - + f"with tag/index {params.tag_or_idx} edited. " - ) + "message": f"reflection of crystal {name} in collection {collection} with " + + f"{select_idx_or_tag_str(idx, tag)} edited" } @router.delete("/{name}/reflection") async def delete_reflection( name: str, - params: DeleteParams = Body(..., example={"tag_or_idx": "refl1"}), store: HklCalcStore = Depends(get_store), collection: Optional[str] = Query(default=None, example="B07"), + tag: Optional[str] = Query(default=None, example="refl1"), + idx: Optional[int] = Query(default=None), ): - await service.delete_reflection( - name, params.tag_or_idx, store, collection - ) # TODO Change this! + if (idx is None) and (tag is None): + raise NoTagOrIdxProvidedError() + if (idx is not None) and (tag is not None): + raise BothTagAndIdxProvidedError() + + await service.delete_reflection(name, store, collection, tag, idx) return { - "message": ( - f"reflection of crystal {name} in collection {collection} " - + f"with tag/index {params.tag_or_idx} deleted." - ) + "message": f"reflection of crystal {name} in collection {collection} " + + f"with {select_idx_or_tag_str(idx, tag)} deleted" } @@ -86,13 +100,12 @@ async def add_orientation( params: AddOrientationParams = Body(..., example=examples.add_orientation), store: HklCalcStore = Depends(get_store), collection: Optional[str] = Query(default=None, example="B07"), + tag: Optional[str] = Query(default=None, example="plane"), ): - await service.add_orientation(name, params, store, collection) + await service.add_orientation(name, params, store, collection, tag) return { - "message": ( - f"added orientation for UB Calculation of crystal {name} in " - + f"collection {collection}" - ) + "message": f"added orientation for UB Calculation of crystal {name} in " + + f"collection {collection}" } @@ -102,29 +115,38 @@ async def edit_orientation( params: EditOrientationParams = Body(..., example=examples.edit_orientation), store: HklCalcStore = Depends(get_store), collection: Optional[str] = Query(default=None, example="B07"), + tag: Optional[str] = Query(default=None), + idx: Optional[int] = Query(default=None, example=0), ): - await service.edit_orientation(name, params, store, collection) + if (idx is None) and (tag is None): + raise NoTagOrIdxProvidedError() + if (idx is not None) and (tag is not None): + raise BothTagAndIdxProvidedError() + + await service.edit_orientation(name, params, store, collection, tag, idx) return { - "message": ( - f"orientation of crystal {name} in collection {collection} " - + f"with tag/index {params.tag_or_idx} edited." - ) + "message": f"orientation of crystal {name} in collection {collection} with " + f"{select_idx_or_tag_str(idx, tag)} edited" } @router.delete("/{name}/orientation") async def delete_orientation( name: str, - params: DeleteParams = Body(..., example={"tag_or_idx": "plane"}), store: HklCalcStore = Depends(get_store), collection: Optional[str] = Query(default=None, example="B07"), + tag: Optional[str] = Query(default=None, example="plane"), + idx: Optional[int] = Query(default=None), ): - await service.delete_orientation(name, params.tag_or_idx, store, collection) + if (idx is None) and (tag is None): + raise NoTagOrIdxProvidedError() + if (idx is not None) and (tag is not None): + raise BothTagAndIdxProvidedError() + + await service.delete_orientation(name, store, collection, tag, idx) return { - "message": ( - f"reflection of crystal {name} in collection {collection} with " - + f"tag or index {params.tag_or_idx} deleted." - ) + "message": f"orientation of crystal {name} in collection {collection} " + + f"with {select_idx_or_tag_str(idx, tag)} deleted" } @@ -142,10 +164,8 @@ async def set_lattice( await service.set_lattice(name, params, store, collection) return { - "message": ( - f"lattice has been set for UB calculation of crystal {name} in " - + f"collection {collection}" - ) + "message": f"lattice has been set for UB calculation of crystal {name} in " + + f"collection {collection}" } @@ -162,8 +182,6 @@ async def modify_property( await service.modify_property(name, property, target_value, store, collection) return { - "message": ( - f"{property} has been set for UB calculation of crystal {name} in " - + f"collection {collection}" - ) + "message": f"{property} has been set for UB calculation of crystal {name} in " + + f"collection {collection}" } diff --git a/src/diffcalc_API/server.py b/src/diffcalc_API/server.py index 1d851ca..92a596d 100644 --- a/src/diffcalc_API/server.py +++ b/src/diffcalc_API/server.py @@ -46,7 +46,7 @@ async def server_exceptions_middleware(request: Request, call_next): try: return await call_next(request) except Exception as e: - # you probably want some kind of logging here + # TODO: implement proper logging tb = traceback.format_exc() print(tb) diff --git a/src/diffcalc_API/services/constraints.py b/src/diffcalc_API/services/constraints.py index 4f1b9be..0c85e90 100644 --- a/src/diffcalc_API/services/constraints.py +++ b/src/diffcalc_API/services/constraints.py @@ -1,4 +1,4 @@ -from typing import Dict, Optional, Union +from typing import Dict, Optional from diffcalc.hkl.constraints import Constraints @@ -16,7 +16,7 @@ async def get_constraints( async def set_constraints( name: str, - constraints: Dict[str, Union[float, bool]], + constraints: Dict[str, float], store: HklCalcStore, collection: Optional[str], ) -> None: @@ -52,7 +52,7 @@ async def remove_constraint( async def set_constraint( name: str, property: str, - value: Union[float, bool], + value: float, store: HklCalcStore, collection: Optional[str], ) -> None: diff --git a/src/diffcalc_API/services/hkl.py b/src/diffcalc_API/services/hkl.py index cb6a9ca..05bd607 100644 --- a/src/diffcalc_API/services/hkl.py +++ b/src/diffcalc_API/services/hkl.py @@ -140,14 +140,19 @@ def combine_lab_position_results( async def calculate_ub( name: str, - first_tag: Optional[Union[int, str]], - second_tag: Optional[Union[int, str]], store: HklCalcStore, collection: Optional[str], + tag1: Optional[str], + idx1: Optional[int], + tag2: Optional[str], + idx2: Optional[int], ) -> List[List[float]]: hklcalc = await store.load(name, collection) - hklcalc.ubcalc.calc_ub(first_tag, second_tag) + first_retrieve: Optional[Union[str, int]] = tag1 if tag1 else idx1 + second_retrieve: Optional[Union[str, int]] = tag2 if tag2 else idx2 + + hklcalc.ubcalc.calc_ub(first_retrieve, second_retrieve) await store.save(name, hklcalc, collection) return np.round(hklcalc.ubcalc.UB, 6).tolist() diff --git a/src/diffcalc_API/services/ub.py b/src/diffcalc_API/services/ub.py index e61ff37..aa23743 100644 --- a/src/diffcalc_API/services/ub.py +++ b/src/diffcalc_API/services/ub.py @@ -25,6 +25,7 @@ async def add_reflection( params: AddReflectionParams, store: HklCalcStore, collection: Optional[str], + tag: Optional[str], ) -> None: hklcalc = await store.load(name, collection) @@ -32,7 +33,7 @@ async def add_reflection( tuple(params.hkl.dict().values()), Position(**params.position.dict()), params.energy, - params.tag, + tag, ) await store.save(name, hklcalc, collection) @@ -43,42 +44,59 @@ async def edit_reflection( params: EditReflectionParams, store: HklCalcStore, collection: Optional[str], + tag: Optional[str], + idx: Optional[int], ) -> None: hklcalc = await store.load(name, collection) + retrieve: Union[int, str] = ( + tag if tag is not None else (idx if idx is not None else 0) + ) + try: - reflection = hklcalc.ubcalc.get_reflection(params.tag_or_idx) + reflection = hklcalc.ubcalc.get_reflection(retrieve) except (IndexError, ValueError): - raise ReferenceRetrievalError(params.tag_or_idx, "reflection") - - # TODO: make this more readable... - hklcalc.ubcalc.edit_reflection( - params.tag_or_idx, - tuple(params.hkl.dict().values()) - if params.hkl - else (reflection.h, reflection.k, reflection.l), - Position(params.position.dict()) if params.position else reflection.pos, - params.energy if params.energy else reflection.energy, - params.tag_or_idx if isinstance(params.tag_or_idx, str) else None, - ) + raise ReferenceRetrievalError(retrieve, "reflection") + + inputs = { + "idx": retrieve, + "hkl": (reflection.h, reflection.k, reflection.l), + "position": reflection.pos, + "energy": reflection.energy, + "tag": params.set_tag if params.set_tag else reflection.tag, + } + + if params.hkl: + inputs["hkl"] = tuple(params.hkl.dict().values()) + if params.position: + inputs["position"] = Position(**params.position.dict()) + if params.energy: + inputs["energy"] = params.energy + + hklcalc.ubcalc.edit_reflection(**inputs) await store.save(name, hklcalc, collection) async def delete_reflection( name: str, - tag_or_idx: Union[str, int], store: HklCalcStore, collection: Optional[str], + tag: Optional[str], + idx: Optional[int], ) -> None: hklcalc = await store.load(name, collection) + retrieve: Union[str, int] = ( + tag if tag is not None else (idx if idx is not None else 0) + ) + try: - hklcalc.ubcalc.get_reflection(tag_or_idx) + hklcalc.ubcalc.get_reflection(retrieve) except (IndexError, ValueError): - raise ReferenceRetrievalError(tag_or_idx, "reflection") + raise ReferenceRetrievalError(retrieve, "reflection") - hklcalc.ubcalc.del_reflection(tag_or_idx) + hklcalc.ubcalc.del_reflection(retrieve) await store.save(name, hklcalc, collection) @@ -88,6 +106,7 @@ async def add_orientation( params: AddOrientationParams, store: HklCalcStore, collection: Optional[str], + tag: Optional[str], ) -> None: hklcalc = await store.load(name, collection) @@ -96,7 +115,7 @@ async def add_orientation( tuple(params.hkl.dict().values()), tuple(params.xyz.dict().values()), position, - params.tag, + tag, ) await store.save(name, hklcalc, collection) @@ -107,43 +126,59 @@ async def edit_orientation( params: EditOrientationParams, store: HklCalcStore, collection: Optional[str], + tag: Optional[str], + idx: Optional[int], ) -> None: hklcalc = await store.load(name, collection) + retrieve: Union[int, str] = ( + tag if tag is not None else (idx if idx is not None else 0) + ) + try: - orientation = hklcalc.ubcalc.get_orientation(params.tag_or_idx) + orientation = hklcalc.ubcalc.get_orientation(retrieve) except (IndexError, ValueError): - raise ReferenceRetrievalError(params.tag_or_idx, "orientation") - - hklcalc.ubcalc.edit_orientation( - params.tag_or_idx, - tuple(params.hkl.dict().values()) - if params.hkl - else (orientation.h, orientation.k, orientation.l), - tuple(params.xyz.dict().values()) - if params.xyz - else (orientation.x, orientation.y, orientation.z), - Position(params.position.dict()) if params.position else orientation.pos, - params.tag_or_idx if isinstance(params.tag_or_idx, str) else None, - ) + raise ReferenceRetrievalError(retrieve, "reflection") + + inputs = { + "idx": retrieve, + "hkl": (orientation.h, orientation.k, orientation.l), + "xyz": (orientation.x, orientation.y, orientation.z), + "position": orientation.pos, + "tag": params.set_tag if params.set_tag else orientation.tag, + } + + if params.hkl: + inputs["hkl"] = tuple(params.hkl.dict().values()) + if params.xyz: + inputs["xyz"] = tuple(params.xyz.dict().values()) + if params.position: + inputs["position"] = Position(**params.position.dict()) + + hklcalc.ubcalc.edit_orientation(**inputs) await store.save(name, hklcalc, collection) async def delete_orientation( name: str, - tag_or_idx: Union[str, int], store: HklCalcStore, collection: Optional[str], + tag: Optional[str], + idx: Optional[int], ) -> None: hklcalc = await store.load(name, collection) + retrieve: Union[int, str] = ( + tag if tag is not None else (idx if idx is not None else 0) + ) + try: - hklcalc.ubcalc.get_orientation(tag_or_idx) + hklcalc.ubcalc.get_orientation(retrieve) except (IndexError, ValueError): - raise ReferenceRetrievalError(tag_or_idx, "orientation") + raise ReferenceRetrievalError(retrieve, "orientation") - hklcalc.ubcalc.del_orientation(tag_or_idx) + hklcalc.ubcalc.del_orientation(retrieve) await store.save(name, hklcalc, collection) diff --git a/tests/test_hklcalc.py b/tests/test_hklcalc.py index 43cb99e..b49bbaa 100644 --- a/tests/test_hklcalc.py +++ b/tests/test_hklcalc.py @@ -192,8 +192,6 @@ def test_calc_ub(client: TestClient): def test_calc_ub_fails_when_incorrect_tags(client: TestClient): - response = client.get( - "/hkl/test/UB", params={"first_tag": "one", "second_tag": "two"} - ) + response = client.get("/hkl/test/UB", params={"tag1": "one", "idx2": 2}) assert response.status_code == 400 diff --git a/tests/test_ubcalc.py b/tests/test_ubcalc.py index 365750d..caeaabd 100644 --- a/tests/test_ubcalc.py +++ b/tests/test_ubcalc.py @@ -53,12 +53,11 @@ def test_get_ub(client: TestClient): def test_add_reflection(client: TestClient): response = client.post( - "/ub/test/reflection", + "/ub/test/reflection?tag=foo", json={ "hkl": {"h": 0, "k": 0, "l": 1}, "position": {"mu": 7, "delta": 0, "nu": 10, "eta": 0, "chi": 0, "phi": 0}, "energy": 12, - "tag": "foo", }, ) @@ -71,23 +70,32 @@ def test_add_reflection(client: TestClient): def test_edit_reflection(client: TestClient): dummy_hkl.ubcalc.add_reflection([0, 0, 1], Position(7, 0, 10, 0, 0, 0), 12, "foo") response = client.put( - "/ub/test/reflection", - json={ - "energy": 13, - "tag_or_idx": "foo", - }, + "/ub/test/reflection?tag=foo", + json={"energy": 13, "set_tag": "bar"}, ) - reflection = dummy_hkl.ubcalc.get_reflection("foo") + reflection = dummy_hkl.ubcalc.get_reflection("bar") assert response.status_code == 200 assert reflection.energy == 13 - dummy_hkl.ubcalc.del_reflection("foo") + response_idx = client.put( + "/ub/test/reflection?idx=0", + json={"hkl": {"h": 0, "k": 3, "l": 1}}, + ) + + assert response_idx.status_code == 200 + reflection_idx = dummy_hkl.ubcalc.get_reflection(0) + + assert reflection_idx.h == 0 + assert reflection_idx.k == 3 + assert reflection_idx.tag == "bar" + + dummy_hkl.ubcalc.del_reflection(0) def test_delete_reflection(client: TestClient): dummy_hkl.ubcalc.add_reflection([0, 0, 1], Position(7, 0, 10, 0, 0, 0), 12, "foo") - response = client.delete("/ub/test/reflection", json={"tag_or_idx": "foo"}) + response = client.delete("/ub/test/reflection?tag=foo") assert response.status_code == 200 with pytest.raises(Exception): @@ -98,16 +106,10 @@ def test_edit_or_delete_reflection_fails_for_non_existing_reflection( client: TestClient, ): edit_response = client.put( - "/ub/test/reflection", - json={ - "energy": 13, - "tag_or_idx": "foo", - }, - ) - delete_response = client.delete( - "/ub/test/reflection", - json={"tag_or_idx": "foo"}, + "/ub/test/reflection?tag=foo", + json={"energy": 13}, ) + delete_response = client.delete("/ub/test/reflection?tag=foo") assert edit_response.status_code == ErrorCodes.REFERENCE_RETRIEVAL_ERROR assert delete_response.status_code == ErrorCodes.REFERENCE_RETRIEVAL_ERROR @@ -115,11 +117,10 @@ def test_edit_or_delete_reflection_fails_for_non_existing_reflection( def test_add_orientation(client: TestClient): response = client.post( - "/ub/test/orientation", + "/ub/test/orientation?tag=bar", json={ "hkl": {"h": 0, "k": 1, "l": 0}, "xyz": {"x": 0, "y": 1, "z": 0}, - "tag": "bar", }, ) @@ -132,11 +133,8 @@ def test_add_orientation(client: TestClient): def test_edit_orientation(client: TestClient): dummy_hkl.ubcalc.add_orientation([0, 0, 1], [0, 0, 1], None, "bar") response = client.put( - "/ub/test/orientation", - json={ - "xyz": {"x": 1, "y": 1, "z": 0}, - "tag_or_idx": "bar", - }, + "/ub/test/orientation?tag=bar", + json={"xyz": {"x": 1, "y": 1, "z": 0}, "set_tag": "bar"}, ) orientation = dummy_hkl.ubcalc.get_orientation("bar") @@ -151,10 +149,7 @@ def test_edit_orientation(client: TestClient): def test_delete_orientation(client: TestClient): dummy_hkl.ubcalc.add_orientation([0, 0, 1], [0, 0, 1], None, "bar") - response = client.delete( - "/ub/test/orientation", - json={"tag_or_idx": "bar"}, - ) + response = client.delete("/ub/test/orientation?idx=0") assert response.status_code == 200 with pytest.raises(Exception): @@ -165,15 +160,11 @@ def test_edit_or_delete_orientation_fails_for_non_existing_orientation( client: TestClient, ): edit_response = client.put( - "/ub/test/orientation", - json={ - "xyz": {"x": 1, "y": 1, "z": 0}, - "tag_or_idx": "bar", - }, + "/ub/test/orientation?tag=bar", + json={"xyz": {"x": 1, "y": 1, "z": 0}}, ) delete_response = client.delete( - "/ub/test/orientation", - json={"tag_or_idx": "bar"}, + "/ub/test/orientation?tag=bar", ) assert edit_response.status_code == ErrorCodes.REFERENCE_RETRIEVAL_ERROR