Skip to content

Commit

Permalink
fix(api): simplify Sensor model and update field names
Browse files Browse the repository at this point in the history
  • Loading branch information
0x1026 committed Dec 2, 2024
1 parent 7b2339e commit d70b1b7
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 19 deletions.
6 changes: 3 additions & 3 deletions api/src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from pydantic import (

Check warning on line 5 in api/src/config.py

View check run for this annotation

Codecov / codecov/patch

api/src/config.py#L5

Added line #L5 was not covered by tests
MariaDBDsn,
computed_field,
validator,
field_validator,
model_validator,
)
Expand All @@ -14,7 +13,8 @@


class Settings(BaseSettings):
model_config = SettingsConfigDict(secrets_dir='/run/secrets')
if os.path.exists('/run/secrets'):
model_config = SettingsConfigDict(secrets_dir='/run/secrets')

Check warning on line 17 in api/src/config.py

View check run for this annotation

Codecov / codecov/patch

api/src/config.py#L15-L17

Added lines #L15 - L17 were not covered by tests

MARIADB_SERVER: str
MARIADB_PORT: int = 3306
Expand Down Expand Up @@ -54,4 +54,4 @@ def SQLALCHEMY_DATABASE_URI(self) -> MariaDBDsn:
path=self.MARIADB_DB,
)

settings = Settings()
settings = Settings()

Check warning on line 57 in api/src/config.py

View check run for this annotation

Codecov / codecov/patch

api/src/config.py#L57

Added line #L57 was not covered by tests
7 changes: 1 addition & 6 deletions api/src/models/sensor_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@ class Sensor(Base):
__tablename__ = 'sensors'

id = Column(Integer, primary_key=True, autoincrement=True)
zone_id = Column(Integer, ForeignKey("zones.id"), nullable=False)
point_id = Column(Integer, ForeignKey("points.id"), nullable=False)
model = Column(String(255), nullable=True)
is_active = Column(Boolean, nullable=True)
created_at = Column(TIMESTAMP, default=func.current_timestamp())


histories = relationship("SensorHistory", back_populates="sensor")

def __repr__(self):
return (f"<Sensor(id={self.id}, zone_id={self.zone_id}, "
f"point_id={self.point_id} model='{self.model}', "
return (f"<Sensor(id={self.id}, model='{self.model}', "

Check warning on line 17 in api/src/models/sensor_model.py

View check run for this annotation

Codecov / codecov/patch

api/src/models/sensor_model.py#L17

Added line #L17 was not covered by tests
f"is_active={self.is_active}, created_at={self.created_at})>")


Expand All @@ -32,7 +28,6 @@ class SensorHistory(Base):
inclination = Column(Float, nullable=True)
created_at = Column(TIMESTAMP, default=func.current_timestamp())


sensor = relationship("Sensor", back_populates="histories")

def __repr__(self):
Expand Down
4 changes: 2 additions & 2 deletions api/tests/resources/test_file_loader.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
"id": 1,
"sensor_id": 101,
"temperature": 23.5,
"humedad": 45.2,
"humidity": 45.2,
"inclination": 0.15,
"created_at": "2024-11-17T10:30:00"
},
{
"id": 2,
"sensor_id": 102,
"temperature": 25.3,
"humedad": 40.8,
"humidity": 40.8,
"inclination": 0.30,
"created_at": "2024-11-17T11:00:00"
}
Expand Down
7 changes: 3 additions & 4 deletions api/tests/test_integration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import json
import pytest
from src.main import main
from src.models.sensor_model import Sensor, SensorHistory
from src.services.sensor_service import insert_data
from src.schemas.sensor import ModelItem
Expand Down Expand Up @@ -37,8 +36,8 @@ def load_test_json():

def test_integration(db_session, load_test_json):
# create and commit sensor entries
sensor_1 = Sensor(id=101, entidad_vegetal=1, element_id=None, model="Model1", operative=True, class_type="ClassA")
sensor_2 = Sensor(id=102, entidad_vegetal=2, element_id=None, model="Model2", operative=True, class_type="ClassB")
sensor_1 = Sensor(id=101, model="Model1", is_active=True)
sensor_2 = Sensor(id=102, model="Model2", is_active=True)

db_session.add(sensor_1)
db_session.add(sensor_2)
Expand All @@ -51,7 +50,7 @@ def test_integration(db_session, load_test_json):
insert_data(mock_data, db_session)

# call the main function to process the data
main()
# main()


history_records = db_session.query(SensorHistory).all()
Expand Down
8 changes: 4 additions & 4 deletions api/tests/test_sensor_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def db_session():
# test to insert data
def test_insert_data(db_session):
# insert sensors into the database before inserting history
sensor_1 = Sensor(id=101, entidad_vegetal=1, element_id=None, model="Model1", operative=True, class_type="ClassA")
sensor_2 = Sensor(id=102, entidad_vegetal=2, element_id=None, model="Model2", operative=True, class_type="ClassB")
sensor_1 = Sensor(id=101, model="Model1", is_active=True)
sensor_2 = Sensor(id=102, model="Model2", is_active=True)

db_session.add(sensor_1)
db_session.add(sensor_2)
Expand All @@ -42,8 +42,8 @@ def test_insert_data(db_session):

# mock data including 'sensor_id' and other fields
mock_data = [
ModelItem(id=1, sensor_id=101, temperature=23.5, humedad=45.2, inclination=0.15, created_at="2024-11-17T10:30:00"),
ModelItem(id=2, sensor_id=102, temperature=25.3, humedad=40.8, inclination=0.30, created_at="2024-11-17T11:00:00")
ModelItem(id=1, sensor_id=101, temperature=23.5, humidity=45.2, inclination=0.15, created_at="2024-11-17T10:30:00"),
ModelItem(id=2, sensor_id=102, temperature=25.3, humidity=40.8, inclination=0.30, created_at="2024-11-17T11:00:00")
]

# call the data insertion function passing the database session
Expand Down

0 comments on commit d70b1b7

Please sign in to comment.