Skip to content

Commit

Permalink
fix: failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
hanaawad24 committed Nov 27, 2024
1 parent b9694cf commit 062c8c7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pvsite_datamodel/read/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def assign_model_to_site(session: Session, model_uuid: str, site_uuid: str) -> s
"""
site = session.query(SiteSQL).filter(SiteSQL.site_uuid == site_uuid).first()

model = session.query(MLModelSQL).filter(MLModelSQL.ml_model_id == model_uuid).first()
model = session.query(MLModelSQL).filter(MLModelSQL.model_uuid == model_uuid).first()

if site is None:
raise KeyError(f"Site with uuid {site_uuid} not found")
Expand Down
14 changes: 2 additions & 12 deletions pvsite_datamodel/sqlmodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from typing import List, Optional

import sqlalchemy as sa
from sqlalchemy import Column, ForeignKey, Integer
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import Mapped, declarative_base, relationship
from sqlalchemy.schema import UniqueConstraint
Expand Down Expand Up @@ -37,8 +36,6 @@ class MLModelSQL(Base, CreatedMixin):
)
sites: Mapped[List["SiteSQL"]] = relationship("SiteSQL", back_populates="ml_model")

sites: Mapped[List["SiteSQL"]] = relationship("SiteSQL", back_populates="ml_model")


class UserSQL(Base, CreatedMixin):
"""Class representing the users table.
Expand Down Expand Up @@ -185,13 +182,6 @@ class SiteSQL(Base, CreatedMixin):
comment="The UUID of the client this site belongs to",
)

ml_model_uuid = sa.Column(
UUID(as_uuid=True),
sa.ForeignKey("ml_model.model_uuid"),
nullable=True,
comment="The ML Model which should be used for this site",
)

forecasts: Mapped[List["ForecastSQL"]] = relationship("ForecastSQL", back_populates="site")
generation: Mapped[List["GenerationSQL"]] = relationship("GenerationSQL")
inverters: Mapped[List["InverterSQL"]] = relationship(
Expand All @@ -203,8 +193,8 @@ class SiteSQL(Base, CreatedMixin):
client: Mapped[List["ClientSQL"]] = relationship("ClientSQL", back_populates="sites")
ml_model: Mapped[Optional[MLModelSQL]] = relationship("MLModelSQL", back_populates="sites")

ml_model_id = Column(Integer, ForeignKey("ml_models.id"))
ml_model = relationship("MLModel", back_populates="sites")
ml_model_uuid = sa.Column(UUID(as_uuid=True), sa.ForeignKey("ml_model.model_uuid"))
ml_model: Mapped[Optional[MLModelSQL]] = relationship("MLModelSQL", back_populates="sites")


class ClientSQL(Base, CreatedMixin):
Expand Down
10 changes: 5 additions & 5 deletions tests/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,23 +363,23 @@ def test_assign_model_name_to_site(db_session):
def test_assign_model_to_site(db_session):
"""Test to assign a model to a site"""
site = make_fake_site(db_session=db_session)
model = get_or_create_model(session=db_session, model_name="Test Model")
model = get_or_create_model(session=db_session, name="Test Model")

message = assign_model_to_site(db_session, model.ml_model_uuid, site.site_uuid)
message = assign_model_to_site(db_session, model.model_uuid, site.site_uuid)

assert site.ml_model_uuid == model.ml_model_uuid
assert site.ml_model_uuid == model.model_uuid
assert message == (
f"Model {model.ml_model_uuid} successfully assigned to site {site.site_uuid}"
)


def test_assign_model_to_nonexistent_site(db_session):
"""Test assigning a model to a nonexistent site"""
model = get_or_create_model(session=db_session, model_name="Test Model")
model = get_or_create_model(session=db_session, name="Test Model")
nonexistent_site_uuid = str(uuid.uuid4())

with pytest.raises(KeyError, match=f"Site with uuid {nonexistent_site_uuid} not found"):
assign_model_to_site(db_session, model.ml_model_uuid, nonexistent_site_uuid)
assign_model_to_site(db_session, model.model_uuid, nonexistent_site_uuid)


def test_assign_nonexistent_model_to_site(db_session):
Expand Down

0 comments on commit 062c8c7

Please sign in to comment.