Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ml id column sites #155

Merged
merged 5 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pvsite_datamodel/sqlmodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ class SiteSQL(Base, CreatedMixin):
"""

__tablename__ = "sites"
__table_args__ = (
UniqueConstraint("client_site_name", "ml_id", name="uniq_cons_client_name_ml_id"),
)
peterdudfield marked this conversation as resolved.
Show resolved Hide resolved

site_uuid = sa.Column(UUID(as_uuid=True), default=uuid.uuid4, primary_key=True)
client_site_id = sa.Column(
Expand Down Expand Up @@ -165,7 +168,6 @@ class SiteSQL(Base, CreatedMixin):
sa.Integer,
autoincrement=True,
nullable=False,
unique=True,
comment="Auto-incrementing integer ID of the site for use in ML training",
)

Expand Down
4 changes: 3 additions & 1 deletion pvsite_datamodel/write/user_and_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def create_site(
tilt: Optional[float] = None,
inverter_capacity_kw: Optional[float] = None,
module_capacity_kw: Optional[float] = None,
ml_id: Optional[int] = None,
) -> [SiteSQL, str]:
"""
Create a site and adds it to the database.
Expand All @@ -93,6 +94,7 @@ def create_site(
:param tilt: tilt of site, default is 35
:param inverter_capacity_kw: inverter capacity of site in kw
:param module_capacity_kw: module capacity of site in kw
:param ml_id: internal ML modelling id

"""
max_ml_id = session.query(func.max(SiteSQL.ml_id)).scalar()
Expand Down Expand Up @@ -133,7 +135,7 @@ def create_site(
dno = json.dumps(dno)

site = SiteSQL(
ml_id=max_ml_id + 1,
ml_id=ml_id if ml_id else max_ml_id + 1,
client_site_id=client_site_id,
client_site_name=client_site_name,
latitude=latitude,
Expand Down
28 changes: 28 additions & 0 deletions tests/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,34 @@ def test_create_new_site_twice(db_session):
assert site_2.ml_id == 2


# test for create_site to check ml_id duplicates are allowed for separate clients
def test_ml_id_duplicate_for_unique_clients(db_session):
"""Test create sites with duplicate ml_id for different clients"""

site_1, _ = create_site(
session=db_session,
client_site_id=6932,
client_site_name="test_site_name_1",
latitude=1.0,
longitude=1.0,
capacity_kw=1.0,
ml_id=1,
)

site_2, _ = create_site(
session=db_session,
client_site_id=6933,
client_site_name="test_site_name_2",
latitude=1.0,
longitude=1.0,
capacity_kw=1.0,
ml_id=1,
)

assert site_1.ml_id == 1
assert site_2.ml_id == 1


def test_create_user(db_session):
"Test to create a new user."

Expand Down
Loading