diff --git a/src/diffcalc_API/database.py b/src/diffcalc_API/database.py index 106cf6c..b455bad 100644 --- a/src/diffcalc_API/database.py +++ b/src/diffcalc_API/database.py @@ -1,5 +1,7 @@ import motor.motor_asyncio from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorDatabase -client: AsyncIOMotorClient = motor.motor_asyncio.AsyncIOMotorClient() +client: AsyncIOMotorClient = motor.motor_asyncio.AsyncIOMotorClient( + "172.23.169.16:27017" +) database: AsyncIOMotorDatabase = client.test_db diff --git a/src/diffcalc_API/errors/constraints.py b/src/diffcalc_API/errors/constraints.py index b34541c..9ca1867 100644 --- a/src/diffcalc_API/errors/constraints.py +++ b/src/diffcalc_API/errors/constraints.py @@ -4,23 +4,21 @@ from diffcalc_API.errors.definitions import ( ALL_RESPONSES, DiffcalcAPIException, - ErrorCodes, + ErrorCodesBase, ) -class Codes(ErrorCodes): - CHECK_CONSTRAINT_EXISTS = 400 +class ErrorCodes(ErrorCodesBase): + INVALID_CONSTRAINT = 400 -responses = {code: ALL_RESPONSES[code] for code in np.unique(Codes.all_codes())} +responses = {code: ALL_RESPONSES[code] for code in np.unique(ErrorCodes.all_codes())} -def check_constraint_exists(constraint: str) -> None: - if constraint not in ALL_CONSTRAINTS: - raise DiffcalcAPIException( - status_code=Codes.CHECK_CONSTRAINT_EXISTS, - detail=( - f"property {constraint} does not exist as a valid constraint." - f" Choose one of {ALL_CONSTRAINTS}" - ), +class InvalidConstraintError(DiffcalcAPIException): + def __init__(self, constraint: str): + self.detail = ( + f"property {constraint} does not exist as a valid constraint." + f" Valid constraints are: {ALL_CONSTRAINTS}" ) + self.status_code = ErrorCodes.INVALID_CONSTRAINT diff --git a/src/diffcalc_API/errors/definitions.py b/src/diffcalc_API/errors/definitions.py index 6c90536..20f68f6 100644 --- a/src/diffcalc_API/errors/definitions.py +++ b/src/diffcalc_API/errors/definitions.py @@ -1,7 +1,6 @@ from enum import IntEnum from typing import Any, Dict, List, Union -from diffcalc.util import DiffcalcException from pydantic import BaseModel ####################################################################################### @@ -15,18 +14,12 @@ def __init__(self, status_code: int, detail: str): self.detail = detail -class DiffcalcCoreException(DiffcalcException): - def __init__(self, status_code: int, detail: str): - self.status_code = status_code - self.detail = detail - - class DiffcalcExceptionModel(BaseModel): status_code: int detail: str -class ErrorCodes(IntEnum): +class ErrorCodesBase(IntEnum): @classmethod def all_codes(cls) -> List[int]: return [val.value for val in cls] diff --git a/src/diffcalc_API/errors/hkl.py b/src/diffcalc_API/errors/hkl.py index 2091190..0738d69 100644 --- a/src/diffcalc_API/errors/hkl.py +++ b/src/diffcalc_API/errors/hkl.py @@ -1,55 +1,30 @@ -from typing import Optional, Tuple, Union - import numpy as np -from diffcalc.hkl.calc import HklCalculation from diffcalc_API.errors.definitions import ( ALL_RESPONSES, DiffcalcAPIException, - ErrorCodes, + ErrorCodesBase, ) -class Codes(ErrorCodes): - CHECK_VALID_MILLER_INDICES = 400 - CHECK_VALID_SCAN_BOUNDS = 400 - CALCULATE_UB_MATRIX = 400 +class ErrorCodes(ErrorCodesBase): + INVALID_MILLER_INDICES = 400 + INVALID_SCAN_BOUNDS = 400 -responses = {code: ALL_RESPONSES[code] for code in np.unique(Codes.all_codes())} +responses = {code: ALL_RESPONSES[code] for code in np.unique(ErrorCodes.all_codes())} -def check_valid_miller_indices(miller_indices: Tuple[float, float, float]) -> None: - if sum(miller_indices) == 0: - raise DiffcalcAPIException( - status_code=Codes.CHECK_VALID_MILLER_INDICES, - detail="At least one of the hkl indices must be non-zero", - ) - return +class InvalidMillerIndicesError(DiffcalcAPIException): + def __init__(self) -> None: + self.detail = "At least one of the hkl indices must be non-zero" + self.status_code = ErrorCodes.INVALID_MILLER_INDICES -def check_valid_scan_bounds(start: float, stop: float, inc: float): - if len(np.arange(start, stop + inc, inc)) == 0: - raise DiffcalcAPIException( - status_code=Codes.CHECK_VALID_SCAN_BOUNDS, - detail=( - f"numpy range cannot be formed from start: {start}" - f" to stop: {stop} in increments of: {inc}" - ), - ) - return - - -def calculate_ub_matrix( - hkl: HklCalculation, - first_tag: Optional[Union[int, str]], - second_tag: Optional[Union[int, str]], -) -> None: - try: - hkl.ubcalc.calc_ub(first_tag, second_tag) - except Exception as e: - raise DiffcalcAPIException( - status_code=Codes.CALCULATE_UB_MATRIX, - detail=f"Error calculating UB matrix: {str(e)}", +class InvalidScanBoundsError(DiffcalcAPIException): + def __init__(self, start: float, stop: float, inc: float) -> None: + self.detail = ( + f"numpy range cannot be formed from start: {start}" + f" to stop: {stop} in increments of: {inc}" ) - return + self.status_code = ErrorCodes.INVALID_SCAN_BOUNDS diff --git a/src/diffcalc_API/errors/ub.py b/src/diffcalc_API/errors/ub.py index d17514e..931665b 100644 --- a/src/diffcalc_API/errors/ub.py +++ b/src/diffcalc_API/errors/ub.py @@ -1,71 +1,37 @@ from typing import Union import numpy as np -from diffcalc.hkl.calc import HklCalculation -from diffcalc.ub.reference import Orientation, Reflection from diffcalc_API.config import VECTOR_PROPERTIES from diffcalc_API.errors.definitions import ( ALL_RESPONSES, DiffcalcAPIException, - ErrorCodes, + ErrorCodesBase, ) -from diffcalc_API.models.ub import SetLatticeParams -class Codes(ErrorCodes): - CHECK_PARAMS_NOT_EMPTY = 400 - GET_REFLECTION = 403 - GET_ORIENTATION = 403 - CHECK_PROPERTY_IS_VALID = 400 +class ErrorCodes(ErrorCodesBase): + INVALID_SET_LATTICE_PARAMS = 400 + REFERENCE_RETRIEVAL_ERROR = 403 + INVALID_PROPERTY = 400 -responses = {code: ALL_RESPONSES[code] for code in np.unique(Codes.all_codes())} +responses = {code: ALL_RESPONSES[code] for code in np.unique(ErrorCodes.all_codes())} -def check_params_not_empty(params: SetLatticeParams) -> None: - non_empty_vars = [var for var, value in params if value is not None] +class InvalidSetLatticeParamsError(DiffcalcAPIException): + def __init__(self): + self.detail = ("please provide lattice parameters in request body",) + self.status_code = ErrorCodes.INVALID_SET_LATTICE_PARAMS - if len(non_empty_vars) == 0: - raise DiffcalcAPIException( - status_code=Codes.CHECK_PARAMS_NOT_EMPTY, - detail="please provide parameters in request body", - ) +class ReferenceRetrievalError(DiffcalcAPIException): + def __init__(self, handle: 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 -def get_reflection(hkl: HklCalculation, tag_or_idx: Union[str, int]) -> Reflection: - try: - reflection = hkl.ubcalc.get_reflection(tag_or_idx) - except Exception: - raise DiffcalcAPIException( - status_code=Codes.GET_REFLECTION, - detail=( - "Cannot edit or delete reflection: " - "No reflection with this tag or index" - ), - ) - return reflection - - -def get_orientation(hkl: HklCalculation, tag_or_idx: Union[str, int]) -> Orientation: - try: - orientation = hkl.ubcalc.get_orientation(tag_or_idx) - except Exception: - raise DiffcalcAPIException( - status_code=Codes.GET_ORIENTATION, - detail=( - "Cannot edit or delete orientation: " - "No orientation with this tag or index" - ), - ) - - return orientation - - -def check_property_is_valid(property: str) -> None: - if property not in VECTOR_PROPERTIES: - raise DiffcalcAPIException( - status_code=Codes.CHECK_PROPERTY_IS_VALID, - detail=f"invalid property. Choose one of: {VECTOR_PROPERTIES}", - ) +class InvalidPropertyError(DiffcalcAPIException): + def __init__(self): + self.detail = f"invalid property. Choose one of: {VECTOR_PROPERTIES}" + self.status_code = ErrorCodes.INVALID_PROPERTY diff --git a/src/diffcalc_API/routes/ub.py b/src/diffcalc_API/routes/ub.py index 99403c8..88b8a84 100644 --- a/src/diffcalc_API/routes/ub.py +++ b/src/diffcalc_API/routes/ub.py @@ -2,7 +2,8 @@ from fastapi import APIRouter, Body, Depends, Query, Response -from diffcalc_API.errors.ub import check_params_not_empty, check_property_is_valid +from diffcalc_API.config import VECTOR_PROPERTIES +from diffcalc_API.errors.ub import InvalidPropertyError, InvalidSetLatticeParamsError from diffcalc_API.examples import ub as examples from diffcalc_API.models.ub import ( AddOrientationParams, @@ -131,9 +132,13 @@ async def set_lattice( name: str, params: SetLatticeParams = Body(example=examples.set_lattice), store: HklCalcStore = Depends(get_store), - _=Depends(check_params_not_empty), collection: Optional[str] = Query(default=None, example="B07"), ): + non_empty_vars = [var for var, value in params if value is not None] + + if len(non_empty_vars) == 0: + raise InvalidSetLatticeParamsError() + await service.set_lattice(name, params, store, collection) return { "message": ( @@ -149,9 +154,11 @@ async def modify_property( property: str, target_value: Tuple[float, float, float] = Body(..., example=[1, 0, 0]), store: HklCalcStore = Depends(get_store), - _=Depends(check_property_is_valid), collection: Optional[str] = Query(default=None, example="B07"), ): + if property not in VECTOR_PROPERTIES: + raise InvalidPropertyError() + await service.modify_property(name, property, target_value, store, collection) return { "message": ( diff --git a/src/diffcalc_API/services/constraints.py b/src/diffcalc_API/services/constraints.py index 60118ea..4f1b9be 100644 --- a/src/diffcalc_API/services/constraints.py +++ b/src/diffcalc_API/services/constraints.py @@ -2,8 +2,8 @@ from diffcalc.hkl.constraints import Constraints -from diffcalc_API.config import CONSTRAINTS_WITH_NO_VALUE -from diffcalc_API.errors.constraints import check_constraint_exists +from diffcalc_API.config import ALL_CONSTRAINTS, CONSTRAINTS_WITH_NO_VALUE +from diffcalc_API.errors.constraints import InvalidConstraintError from diffcalc_API.stores.protocol import HklCalcStore @@ -41,7 +41,9 @@ async def remove_constraint( ) -> None: hklcalc = await store.load(name, collection) - check_constraint_exists(property) + if property not in ALL_CONSTRAINTS: + raise InvalidConstraintError(property) + setattr(hklcalc.constraints, property, None) await store.save(name, hklcalc, collection) @@ -56,7 +58,9 @@ async def set_constraint( ) -> None: hklcalc = await store.load(name, collection) - check_constraint_exists(property) + if property not in ALL_CONSTRAINTS: + raise InvalidConstraintError(property) + if property in CONSTRAINTS_WITH_NO_VALUE: value = bool(value) diff --git a/src/diffcalc_API/services/hkl.py b/src/diffcalc_API/services/hkl.py index 22d6b61..7ab9a3d 100644 --- a/src/diffcalc_API/services/hkl.py +++ b/src/diffcalc_API/services/hkl.py @@ -4,11 +4,7 @@ import numpy as np from diffcalc.hkl.geometry import Position -from diffcalc_API.errors.hkl import ( - calculate_ub_matrix, - check_valid_miller_indices, - check_valid_scan_bounds, -) +from diffcalc_API.errors.hkl import InvalidMillerIndicesError, InvalidScanBoundsError from diffcalc_API.stores.protocol import HklCalcStore PositionType = Tuple[float, float, float] @@ -23,7 +19,9 @@ async def lab_position_from_miller_indices( ) -> List[Dict[str, float]]: hklcalc = await store.load(name, collection) - check_valid_miller_indices(miller_indices) + if all([idx == 0 for idx in miller_indices]): + raise InvalidMillerIndicesError() + all_positions = hklcalc.get_position(*miller_indices, wavelength) return combine_lab_position_results(all_positions) @@ -59,7 +57,9 @@ async def scan_hkl( results = {} for h, k, l in product(*axes_values): - check_valid_miller_indices((h, k, l)) + if all([idx == 0 for idx in (h, k, l)]): + raise InvalidMillerIndicesError() # what if this goes through 0? + all_positions = hklcalc.get_position(h, k, l, wavelength) results[f"({h}, {k}, {l})"] = combine_lab_position_results(all_positions) @@ -76,7 +76,10 @@ async def scan_wavelength( collection: Optional[str], ) -> Dict[str, List[Dict[str, float]]]: hklcalc = await store.load(name, collection) - check_valid_scan_bounds(start, stop, inc) + + if len(np.arange(start, stop + inc, inc)) == 0: + raise InvalidScanBoundsError(start, stop, inc) + wavelengths = np.arange(start, stop + inc, inc) result = {} @@ -99,7 +102,10 @@ async def scan_constraint( collection: Optional[str], ) -> Dict[str, List[Dict[str, float]]]: hklcalc = await store.load(name, collection) - check_valid_scan_bounds(start, stop, inc) + + if len(np.arange(start, stop + inc, inc)) == 0: + raise InvalidScanBoundsError(start, stop, inc) + result = {} for value in np.arange(start, stop + inc, inc): setattr(hklcalc, constraint, value) @@ -110,7 +116,9 @@ async def scan_constraint( def generate_axis(start: float, stop: float, inc: float): - check_valid_scan_bounds(start, stop, inc) + if len(np.arange(start, stop + inc, inc)) == 0: + raise InvalidScanBoundsError(start, stop, inc) + return np.arange(start, stop + inc, inc) @@ -134,7 +142,7 @@ async def calculate_ub( ) -> str: hklcalc = await store.load(name, collection) - calculate_ub_matrix(hklcalc, first_tag, second_tag) + hklcalc.ubcalc.calc_ub(first_tag, second_tag) await store.save(name, hklcalc, collection) return str(np.round(hklcalc.ubcalc.UB, 6)) diff --git a/src/diffcalc_API/services/ub.py b/src/diffcalc_API/services/ub.py index c37f96e..0959f05 100644 --- a/src/diffcalc_API/services/ub.py +++ b/src/diffcalc_API/services/ub.py @@ -2,7 +2,7 @@ from diffcalc.hkl.geometry import Position -from diffcalc_API.errors.ub import get_orientation, get_reflection +from diffcalc_API.errors.ub import ReferenceRetrievalError from diffcalc_API.models.ub import ( AddOrientationParams, AddReflectionParams, @@ -45,7 +45,11 @@ async def edit_reflection( ) -> None: hklcalc = await store.load(name, collection) - reflection = get_reflection(hklcalc, params.tag_or_idx) + try: + reflection = hklcalc.ubcalc.get_reflection(params.tag_or_idx) + except (IndexError, ValueError): + raise ReferenceRetrievalError(params.tag_or_idx, "reflection") + hklcalc.ubcalc.edit_reflection( params.tag_or_idx, params.hkl if params.hkl else (reflection.h, reflection.k, reflection.l), @@ -65,7 +69,11 @@ async def delete_reflection( ) -> None: hklcalc = await store.load(name, collection) - _ = get_reflection(hklcalc, tag_or_idx) + try: + hklcalc.ubcalc.get_reflection(tag_or_idx) + except (IndexError, ValueError): + raise ReferenceRetrievalError(tag_or_idx, "reflection") + hklcalc.ubcalc.del_reflection(tag_or_idx) await store.save(name, hklcalc, collection) @@ -98,7 +106,11 @@ async def edit_orientation( ) -> None: hklcalc = await store.load(name, collection) - orientation = get_orientation(hklcalc, params.tag_or_idx) + try: + orientation = hklcalc.ubcalc.get_orientation(params.tag_or_idx) + except (IndexError, ValueError): + raise ReferenceRetrievalError(params.tag_or_idx, "orientation") + hklcalc.ubcalc.edit_orientation( params.tag_or_idx, params.hkl if params.hkl else (orientation.h, orientation.k, orientation.l), @@ -118,7 +130,11 @@ async def delete_orientation( ) -> None: hklcalc = await store.load(name, collection) - _ = get_orientation(hklcalc, tag_or_idx) + try: + hklcalc.ubcalc.get_orientation(tag_or_idx) + except (IndexError, ValueError): + raise ReferenceRetrievalError(tag_or_idx, "orientation") + hklcalc.ubcalc.del_orientation(tag_or_idx) await store.save(name, hklcalc, collection) diff --git a/src/diffcalc_API/stores/mongo.py b/src/diffcalc_API/stores/mongo.py index 8a53770..db8cbb9 100644 --- a/src/diffcalc_API/stores/mongo.py +++ b/src/diffcalc_API/stores/mongo.py @@ -11,11 +11,11 @@ from diffcalc_API.errors.definitions import ( ALL_RESPONSES, DiffcalcAPIException, - ErrorCodes, + ErrorCodesBase, ) -class Codes(ErrorCodes): +class ErrorCodes(ErrorCodesBase): OVERWRITE_ERROR = 405 DOCUMENT_NOT_FOUND_ERROR = 404 @@ -27,13 +27,13 @@ def __init__(self, name: str) -> None: f"\nEither delete via DELETE request to this URL " f"or change the existing properties. " ) - self.status_code = Codes.OVERWRITE_ERROR + self.status_code = ErrorCodes.OVERWRITE_ERROR class DocumentNotFoundError(DiffcalcAPIException): def __init__(self, name: str, action: str) -> None: self.detail = f"Document for crystal {name} not found! Cannot {action}." - self.error_code = Codes.DOCUMENT_NOT_FOUND_ERROR + self.error_code = ErrorCodes.DOCUMENT_NOT_FOUND_ERROR class MongoHklCalcStore: @@ -41,7 +41,7 @@ def __init__( self, ) -> None: self.responses = { - code: ALL_RESPONSES[code] for code in np.unique(Codes.all_codes()) + code: ALL_RESPONSES[code] for code in np.unique(ErrorCodes.all_codes()) } async def create(self, name: str, collection: Optional[str]) -> None: diff --git a/src/diffcalc_API/stores/pickling.py b/src/diffcalc_API/stores/pickling.py index d57de1d..b653395 100644 --- a/src/diffcalc_API/stores/pickling.py +++ b/src/diffcalc_API/stores/pickling.py @@ -11,11 +11,11 @@ from diffcalc_API.errors.definitions import ( ALL_RESPONSES, DiffcalcAPIException, - ErrorCodes, + ErrorCodesBase, ) -class Codes(ErrorCodes): +class ErrorCodes(ErrorCodesBase): OVERWRITE_ERROR = 405 FILE_NOT_FOUND_ERROR = 404 @@ -27,7 +27,7 @@ def __init__(self, name): f"\nEither delete via DELETE request to this URL " f"or change the existing properties. " ) - self.status_code = Codes.OVERWRITE_ERROR + self.status_code = ErrorCodes.OVERWRITE_ERROR class FileNotFoundError(DiffcalcAPIException): @@ -38,7 +38,7 @@ def __init__(self, name): f" http://localhost:8000/{name}" f" first to generate the pickled file.\n" ) - self.status_code = Codes.FILE_NOT_FOUND_ERROR + self.status_code = ErrorCodes.FILE_NOT_FOUND_ERROR class PicklingHklCalcStore: @@ -46,7 +46,7 @@ class PicklingHklCalcStore: def __init__(self) -> None: self.responses = { - code: ALL_RESPONSES[code] for code in np.unique(Codes.all_codes()) + code: ALL_RESPONSES[code] for code in np.unique(ErrorCodes.all_codes()) } async def create(self, name: str, collection: Optional[str]) -> None: diff --git a/tests/test_constraints.py b/tests/test_constraints.py index 05d2707..bca6912 100644 --- a/tests/test_constraints.py +++ b/tests/test_constraints.py @@ -6,7 +6,7 @@ from diffcalc.ub.calc import UBCalculation from fastapi.testclient import TestClient -from diffcalc_API.errors.constraints import Codes +from diffcalc_API.errors.constraints import ErrorCodes from diffcalc_API.server import app from diffcalc_API.stores.protocol import HklCalcStore, get_store from tests.conftest import FakeHklCalcStore @@ -25,6 +25,26 @@ def client() -> TestClient: return TestClient(app) +def test_get_constraints(client: TestClient): + response = client.get( + "/constraints/test?collection=B07", + ) + + assert response.content == ( + b" DET REF SAMP\n " + + b"----------- ----------- -----------\n " + + b"delta a_eq_b mu\n " + + b"nu alpha eta\n " + + b"qaz beta chi\n " + + b"naz psi phi\n " + + b" bin_eq_bout bisect\n " + + b" betain omega\n " + + b" betaout\n\n! " + + b"3 more constraints required\n" + ) + assert response.status_code == 200 + + def test_set_constraints(client: TestClient): response = client.post( "/constraints/test?collection=B07", @@ -103,10 +123,10 @@ def test_set_or_remove_nonexisting_constraint(client: TestClient): json=1, ) - assert set_response.status_code == Codes.CHECK_CONSTRAINT_EXISTS + assert set_response.status_code == ErrorCodes.INVALID_CONSTRAINT assert dummy_hkl.constraints.asdict == {} remove_response = client.delete("/constraints/test/fake") - assert remove_response.status_code == Codes.CHECK_CONSTRAINT_EXISTS + assert remove_response.status_code == ErrorCodes.INVALID_CONSTRAINT assert dummy_hkl.constraints.asdict == {} diff --git a/tests/test_hklcalc.py b/tests/test_hklcalc.py index 2ce40c4..3635e65 100644 --- a/tests/test_hklcalc.py +++ b/tests/test_hklcalc.py @@ -6,7 +6,7 @@ from diffcalc.ub.calc import UBCalculation from fastapi.testclient import TestClient -from diffcalc_API.errors.hkl import Codes +from diffcalc_API.errors.hkl import ErrorCodes from diffcalc_API.server import app from diffcalc_API.stores.protocol import HklCalcStore, get_store from tests.conftest import FakeHklCalcStore @@ -132,7 +132,7 @@ def test_invalid_scans(client: TestClient): }, ) - assert invalid_miller_indices.status_code == Codes.CHECK_VALID_MILLER_INDICES + assert invalid_miller_indices.status_code == ErrorCodes.INVALID_MILLER_INDICES invalid_wavelength_scan = client.get( "/hkl/test/scan/wavelength", @@ -144,7 +144,7 @@ def test_invalid_scans(client: TestClient): }, ) - assert invalid_wavelength_scan.status_code == Codes.CHECK_VALID_SCAN_BOUNDS + assert invalid_wavelength_scan.status_code == ErrorCodes.INVALID_SCAN_BOUNDS def test_calc_ub(client: TestClient): @@ -165,4 +165,4 @@ def test_calc_ub_fails_when_incorrect_tags(client: TestClient): "/hkl/test/UB", params={"first_tag": "one", "second_tag": "two"} ) - assert response.status_code == Codes.CALCULATE_UB_MATRIX + assert response.status_code == 400 diff --git a/tests/test_ubcalc.py b/tests/test_ubcalc.py index a36d564..7b585de 100644 --- a/tests/test_ubcalc.py +++ b/tests/test_ubcalc.py @@ -6,7 +6,7 @@ from diffcalc.ub.calc import UBCalculation from fastapi.testclient import TestClient -from diffcalc_API.errors.ub import Codes +from diffcalc_API.errors.ub import ErrorCodes from diffcalc_API.server import app from diffcalc_API.stores.protocol import HklCalcStore, get_store from tests.conftest import FakeHklCalcStore @@ -25,6 +25,30 @@ def client() -> TestClient: return TestClient(app) +def test_get_ub(client: TestClient): + response = client.get("/ub/test") + + assert response.content == ( + b"UBCALC\n\n" + + b" name: dummy" + + b"\n\nREFERNCE\n\n" + + b" n_hkl: 1.00000 0.00000 0.00000 <- set\n" + + b" n_phi: None" + + b"\n\nSURFACE NORMAL\n\n" + + b" n_hkl: 0.00000 0.00000 1.00000\n" + + b" n_phi: 0.00000 0.00000 1.00000 <- set" + + b"\n\nCRYSTAL\n\n" + + b" <<< none specified >>>" + + b"\n\nUB MATRIX\n\n" + + b" <<< none calculated >>>" + + b"\n\nREFLECTIONS\n\n" + + b" <<< none specified >>>" + + b"\n\nCRYSTAL ORIENTATIONS\n\n" + + b" <<< none specified >>>" + ) + assert response.status_code == 200 + + def test_add_reflection(client: TestClient): response = client.post( "/ub/test/reflection", @@ -83,8 +107,8 @@ def test_edit_or_delete_reflection_fails_for_non_existing_reflection( json={"tag_or_idx": "foo"}, ) - assert edit_response.status_code == Codes.GET_REFLECTION - assert delete_response.status_code == Codes.GET_REFLECTION + assert edit_response.status_code == ErrorCodes.REFERENCE_RETRIEVAL_ERROR + assert delete_response.status_code == ErrorCodes.REFERENCE_RETRIEVAL_ERROR def test_add_orientation(client: TestClient): @@ -150,8 +174,8 @@ def test_edit_or_delete_orientation_fails_for_non_existing_orientation( json={"tag_or_idx": "bar"}, ) - assert edit_response.status_code == Codes.GET_ORIENTATION - assert delete_response.status_code == Codes.GET_ORIENTATION + assert edit_response.status_code == ErrorCodes.REFERENCE_RETRIEVAL_ERROR + assert delete_response.status_code == ErrorCodes.REFERENCE_RETRIEVAL_ERROR def test_set_lattice(client: TestClient): @@ -175,8 +199,10 @@ def test_set_lattice_fails_for_empty_data(client: TestClient): json={"unknown": "fields"}, ) - assert response_with_wrong_input.status_code == Codes.CHECK_PARAMS_NOT_EMPTY - assert response_with_no_input.status_code == Codes.CHECK_PARAMS_NOT_EMPTY + assert ( + response_with_wrong_input.status_code == ErrorCodes.INVALID_SET_LATTICE_PARAMS + ) + assert response_with_no_input.status_code == ErrorCodes.INVALID_SET_LATTICE_PARAMS def test_modify_property(client: TestClient): @@ -194,4 +220,4 @@ def test_modify_non_existent_property(client: TestClient): "/ub/test/silly_property", json=[0, 0, 1], ) - assert response.status_code == Codes.CHECK_PROPERTY_IS_VALID + assert response.status_code == ErrorCodes.INVALID_PROPERTY