Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions api/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
editor_dependency,
viewer_dependency,
)
from db import Observation, Sample, Sensor
from db import Observation, Sensor, Deployment, Thing
from schemas.sensor import SensorResponse, CreateSensor, UpdateSensor
from services.crud_helper import model_patcher, model_deleter, model_adder
from services.exceptions_helper import PydanticStyleException
Expand Down Expand Up @@ -138,22 +138,23 @@ async def get_sensors(
This endpoint is a placeholder and should be implemented with actual logic.
"""
sql = select(Sensor)
# TODO: a sensor is not yet related to observation, so this won't work at the moment
if thing_id is not None or observed_property is not None:
conditions = []
joins = []
if observed_property is not None:
joins.append(Observation)
conditions.append(Observation.observed_property == observed_property)

if thing_id is not None:
joins.append(Sample)
conditions.append(Sample.thing_id == thing_id)
joins.append(Deployment)
joins.append(Thing)
conditions.append(Thing.id == thing_id)

if conditions:
sql = sql.join(Observation)
if joins:
for j in joins:
sql = sql.join(j)

if conditions:
sql = sql.where(and_(*conditions))

sql = order_sort_filter(sql, Sensor, sort=sort, order=order, filter_=filter_)
Expand Down
3 changes: 3 additions & 0 deletions core/lexicon.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@
{"categories": [{"name": "unit", "description": null}], "term": "deg C", "definition": "degree Celsius"},
{"categories": [{"name": "unit", "description": null}], "term": "deg second", "definition": "degree second"},
{"categories": [{"name": "unit", "description": null}], "term": "deg minute", "definition": "degree minute"},
{"categories": [{"name": "unit", "description": null}], "term": "second", "definition": "second"},
{"categories": [{"name": "unit", "description": null}], "term": "minute", "definition": "minute"},
{"categories": [{"name": "unit", "description": null}], "term": "hour", "definition": "hour"},

{"categories": [{"name": "observed_property", "description": null}], "term": "groundwater level", "definition": "groundwater level measurement" },
{"categories": [{"name": "observed_property", "description": null}], "term": "temperature", "definition": "Temperature measurement"},
Expand Down
20 changes: 20 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,26 @@ def second_sensor():
session.commit()


@pytest.fixture(scope="session")
def sensor_to_water_well_thing_deployment(sensor, water_well_thing):
with session_ctx() as session:
deployment = Deployment(
sensor_id=sensor.id,
thing_id=water_well_thing.id,
installation_date="2023-01-01",
removal_date=None,
recording_interval=24,
recording_interval_units="hour",
hanging_cable_length=10,
hanging_point_height=0,
hanging_point_description="hang 10",
notes="deployment fixture",
)
session.add(deployment)
session.commit()
yield deployment


@pytest.fixture(scope="session")
def contact(water_well_thing):
with session_ctx() as session:
Expand Down
23 changes: 23 additions & 0 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,29 @@ def test_get_sensors(sensor):
assert data["items"][0]["notes"] == sensor.notes


def test_get_sensors_by_thing_id(
sensor,
sensor_to_water_well_thing_deployment,
water_well_thing,
):
response = client.get(f"/sensor?thing_id={water_well_thing.id}")
assert response.status_code == 200
data = response.json()
assert data["total"] == 1
assert data["items"][0]["id"] == sensor.id
assert data["items"][0]["created_at"] == sensor.created_at.isoformat().replace(
"+00:00", "Z"
)
assert data["items"][0]["release_status"] == sensor.release_status
assert data["items"][0]["name"] == sensor.name
assert data["items"][0]["model"] == sensor.model
assert data["items"][0]["serial_no"] == sensor.serial_no
assert data["items"][0]["datetime_installed"] == sensor.datetime_installed
assert data["items"][0]["datetime_removed"] == sensor.datetime_removed
assert data["items"][0]["recording_interval"] == sensor.recording_interval
assert data["items"][0]["notes"] == sensor.notes


def test_get_sensor_by_id(sensor):
response = client.get(f"/sensor/{sensor.id}")
assert response.status_code == 200
Expand Down
Loading