Review the ORM models and align the live database with them #16

Merged
alexander merged 10 commits from orm-review into develop 2026-12-01 23:06:04 +01:00
6 changed files with 8 additions and 7 deletions
Showing only changes of commit 7d53abc057 - Show all commits

Avoid lazy loads in .__repr__()

`ReplaySimulation`, `ReplayedOrder`, and `Forecast` accessed
relationships in their `.__repr__()`, which emits queries
or fails for detached instances.

They now use the foreign key columns.
Alexander Hess 2026-12-01 22:34:16 +01:00
Signed by: alexander
GPG key ID: B46EDB4DFC26805D

View file

@ -184,8 +184,9 @@ class Forecast(meta.Base):
def __repr__(self) -> str:
"""Non-literal text representation."""
cls = self.__class__.__name__
pixel = f"({self.pixel.n_x}|{self.pixel.n_y})"
return f"<{cls}: {self.prediction} for pixel {pixel} at {self.start_at}>"
return (
f"<{cls}: {self.prediction} for pixel #{self.pixel_id} at {self.start_at}>"
)
@classmethod
def from_dataframe(

View file

@ -411,7 +411,7 @@ class ReplayedOrder(meta.Base):
def __repr__(self) -> str:
"""Non-literal text representation."""
return f"<{self.__class__.__name__}(#{self.actual.id})>"
return f"<{self.__class__.__name__}(#{self.actual_order_id})>"
# Convenience properties

View file

@ -63,4 +63,4 @@ class ReplaySimulation(meta.Base):
def __repr__(self) -> str:
"""Non-literal text representation."""
cls = self.__class__.__name__
return f"<{cls}({self.strategy} #{self.run} in {self.city.name})>"
return f"<{cls}({self.strategy} #{self.run} in city #{self.city_id})>"

View file

@ -21,7 +21,7 @@ class TestSpecialMethods:
"""`ReplayedOrder` has a non-literal text representation."""
result = repr(replayed_order)
assert result == f"<ReplayedOrder(#{replayed_order.actual.id})>"
assert result == f"<ReplayedOrder(#{replayed_order.actual_order_id})>"
@pytest.mark.db

View file

@ -13,5 +13,5 @@ class TestSpecialMethods:
result = repr(simulation)
assert result.startswith("<ReplaySimulation(")
assert simulation.city.name in result
assert f"city #{simulation.city_id}" in result
assert simulation.strategy in result

View file

@ -52,7 +52,7 @@ class TestSpecialMethods:
assert (
result
== f"<Forecast: {forecast.prediction} for pixel ({forecast.pixel.n_x}|{forecast.pixel.n_y}) at {forecast.start_at}>" # noqa:E501
== f"<Forecast: {forecast.prediction} for pixel #{forecast.pixel_id} at {forecast.start_at}>" # noqa:E501
)