From 5f5137d5539c5efeff3fe02329e989abaf0ea235 Mon Sep 17 00:00:00 2001 From: jakeross Date: Mon, 4 Aug 2025 17:24:40 -0600 Subject: [PATCH 1/2] fix: added optional thing_id to the BaseAsset schema. fixed sample tests to use singleton asserts instead of asserting equal dictionaries --- db/sample.py | 2 +- schemas_v2/asset.py | 14 ++++-------- schemas_v2/sample.py | 13 ++++++++++- tests/test_sample.py | 53 ++++++++++++++++++-------------------------- tests/test_search.py | 2 +- 5 files changed, 40 insertions(+), 44 deletions(-) diff --git a/db/sample.py b/db/sample.py index e47084568..70b7152b6 100644 --- a/db/sample.py +++ b/db/sample.py @@ -45,7 +45,7 @@ class Sample(Base, AutoBaseMixin, ReleaseMixin): ) sensor_id: Mapped[Optional[int]] = mapped_column( ForeignKey("sensor.id"), - unique=True, + # unique=True, comment="Foreign key for the specific equipment used.", ) diff --git a/schemas_v2/asset.py b/schemas_v2/asset.py index 68013321f..8461b1442 100644 --- a/schemas_v2/asset.py +++ b/schemas_v2/asset.py @@ -24,12 +24,13 @@ class BaseAsset(BaseModel): storage_path: str mime_type: str size: int + url: str + thing_id: int | None = None # -------- CREATE ---------- class CreateAsset(BaseAsset): - url: str - + pass # -------- RESPONSE -------- class AssetResponse(BaseAsset): @@ -40,19 +41,12 @@ class AssetResponse(BaseAsset): # storage_path: str # mime_type: str # size: int - url: str created_at: datetime storage_service: str # -------- UPDATE ---------- class UpdateAsset(BaseAsset): - label: str | None = None - url: str | None = None - name: str | None = None - storage_path: str | None = None - mime_type: str | None = None - size: int | None = None - + pass # ============= EOF ============================================= diff --git a/schemas_v2/sample.py b/schemas_v2/sample.py index 0634ab68f..37ac6d188 100644 --- a/schemas_v2/sample.py +++ b/schemas_v2/sample.py @@ -60,7 +60,18 @@ class CreateGeothermalSample(BaseModel): class SampleResponse(BaseModel): id: int sample_date: datetime - sample_method: str + sample_method: str | None = None + sample_type: str + field_sample_id: str + sample_matrix: str | None = None + sampler_name: str | None = None + qc_sample: str | None = None + duplicate_sample_number: int | None = None + sample_top: float | None = None + sample_bottom: float | None = None + sensor_id: int | None = None + release_status: str + created_at: datetime thing_id: int diff --git a/tests/test_sample.py b/tests/test_sample.py index 4e2517dc5..6b96ca534 100644 --- a/tests/test_sample.py +++ b/tests/test_sample.py @@ -93,7 +93,7 @@ def test_patch_sample(sample): original_timestamp_patch = sample.sample_date sample_method_patch = "continuous" - sample_date_patch = "2025-01-02T00:00:00+00:00" + sample_date_patch = "2025-01-02T00:00:00Z" response = client.patch( f"/sample/{sample.id}", json={ @@ -103,12 +103,9 @@ def test_patch_sample(sample): ) assert response.status_code == 200 data = response.json() - assert data == { - "id": sample.id, - "sample_date": sample_date_patch.split("+")[0], - "sample_method": sample_method_patch, - "thing_id": sample.thing_id, - } + assert data["id"] == sample.id + assert data["sample_date"] == sample_date_patch[:-1] + assert data["sample_method"] == sample_method_patch # cleanup after patching the sample with session_ctx() as session: @@ -147,15 +144,14 @@ def test_patch_sample_422_thing_id_not_found(sample): ) assert response.status_code == 422 data = response.json() - assert data["detail"] == [ - { - "type": "value_error", - "loc": ["body", "thing_id"], - "msg": f"Value error, Thing with ID {bad_thing_id} does not exist.", - "input": bad_thing_id, - "ctx": {"error": {}}, - } - ] + + assert "detail" in data + assert isinstance(data["detail"], list) + assert len(data["detail"]) == 1 + assert data["detail"][0]["type"] == "value_error" + assert data["detail"][0]["loc"] == ["body", "thing_id"] + assert data["detail"][0]["msg"] == f"Value error, Thing with ID {bad_thing_id} does not exist." + def test_patch_sample_422_invalid_timestamp(sample): @@ -163,7 +159,6 @@ def test_patch_sample_422_invalid_timestamp(sample): Test updating a sample with an invalid collection timestamp. """ bad_sample_date = "3500-01-01T00:00:00Z" - bad_sample_date_dt = datetime.fromisoformat(bad_sample_date.replace("Z", "+00:00")) response = client.patch( f"/sample/{sample.id}", json={ @@ -188,14 +183,12 @@ def test_get_samples(sample): response = client.get("/sample") assert response.status_code == 200 data = response.json() - assert data["items"] == [ - { - "id": sample.id, - "sample_date": sample.sample_date, - "sample_method": sample.sample_method, - "thing_id": sample.thing_id, - } - ] + assert "items" in data + assert len(data["items"]) ==1 + assert data["items"][0]["id"] == sample.id + assert data["items"][0]["sample_date"] == sample.sample_date + assert data["items"][0]["sample_method"] == sample.sample_method + assert data["items"][0]["thing_id"] == sample.thing_id @pytest.mark.skip(reason="Geochemical samples endpoint not implemented yet") @@ -229,12 +222,10 @@ def test_get_sample_by_id(sample): response = client.get(f"/sample/{sample.id}") assert response.status_code == 200 data = response.json() - assert data == { - "id": sample.id, - "sample_date": sample.sample_date, - "sample_method": sample.sample_method, - "thing_id": sample.thing_id, - } + assert data["id"] == sample.id + assert data["sample_date"] == sample.sample_date + assert data["sample_method"] == sample.sample_method + assert data["thing_id"] == sample.thing_id def test_get_sample_by_id_404_not_found(sample): diff --git a/tests/test_search.py b/tests/test_search.py index cb4364d70..57ca21cdc 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -28,7 +28,7 @@ def test_search_api(thing, sample): data = response.json() assert isinstance(data, list) - assert len(data) == 2 + assert len(data) == 3 @pytest.mark.skip(reason="This test is not working .") From 245104f532cb6e8ef1da0b86a3c3d4f628445146 Mon Sep 17 00:00:00 2001 From: jirhiker Date: Mon, 4 Aug 2025 23:24:57 +0000 Subject: [PATCH 2/2] Formatting changes --- schemas_v2/asset.py | 2 ++ tests/test_sample.py | 8 +++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/schemas_v2/asset.py b/schemas_v2/asset.py index 8461b1442..1ee7be652 100644 --- a/schemas_v2/asset.py +++ b/schemas_v2/asset.py @@ -32,6 +32,7 @@ class BaseAsset(BaseModel): class CreateAsset(BaseAsset): pass + # -------- RESPONSE -------- class AssetResponse(BaseAsset): id: int @@ -49,4 +50,5 @@ class AssetResponse(BaseAsset): class UpdateAsset(BaseAsset): pass + # ============= EOF ============================================= diff --git a/tests/test_sample.py b/tests/test_sample.py index 6b96ca534..bb50b4d6e 100644 --- a/tests/test_sample.py +++ b/tests/test_sample.py @@ -150,8 +150,10 @@ def test_patch_sample_422_thing_id_not_found(sample): assert len(data["detail"]) == 1 assert data["detail"][0]["type"] == "value_error" assert data["detail"][0]["loc"] == ["body", "thing_id"] - assert data["detail"][0]["msg"] == f"Value error, Thing with ID {bad_thing_id} does not exist." - + assert ( + data["detail"][0]["msg"] + == f"Value error, Thing with ID {bad_thing_id} does not exist." + ) def test_patch_sample_422_invalid_timestamp(sample): @@ -184,7 +186,7 @@ def test_get_samples(sample): assert response.status_code == 200 data = response.json() assert "items" in data - assert len(data["items"]) ==1 + assert len(data["items"]) == 1 assert data["items"][0]["id"] == sample.id assert data["items"][0]["sample_date"] == sample.sample_date assert data["items"][0]["sample_method"] == sample.sample_method