Skip to content

Commit

Permalink
fix conftest
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Garcia Reolid <victor@seita.nl>
  • Loading branch information
victorgarcia98 committed Aug 1, 2023
1 parent 2b10245 commit 48fd692
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
2 changes: 1 addition & 1 deletion flexmeasures/api/common/schemas/tests/test_sensors.py
Expand Up @@ -30,7 +30,7 @@
),
"connection",
"fm0",
"Test battery with no known prices",
"Test battery",
),
],
)
Expand Down
10 changes: 5 additions & 5 deletions flexmeasures/conftest.py
Expand Up @@ -249,7 +249,9 @@ def create_test_markets(db) -> dict[str, Market]:
weekly_seasonality=True,
yearly_seasonality=True,
)

db.session.add(day_ahead)

epex_da = Market(
name="epex_da",
market_type_name="day_ahead",
Expand Down Expand Up @@ -598,11 +600,9 @@ def add_market_prices(
]
db.session.add_all(day3_beliefs_production)

return {
yield {
"epex_da": setup_markets["epex_da"].corresponding_sensor,
"epex_da (production)": setup_markets[
"epex_da_production"
].corresponding_sensor,
"epex_da_production": setup_markets["epex_da_production"].corresponding_sensor,
}


Expand All @@ -615,7 +615,7 @@ def add_battery_assets(

@pytest.fixture(scope="function")
def add_battery_assets_fresh_db(
fresh_db, setup_roles_users_fresh_db, setup_markets_fresh_db
fresh_db, setup_roles_users_fresh_db, setup_markets_fresh_db, create
) -> dict[str, Asset]:
return create_test_battery_assets(
fresh_db, setup_roles_users_fresh_db, setup_markets_fresh_db
Expand Down
41 changes: 38 additions & 3 deletions flexmeasures/data/models/generic_assets.py
Expand Up @@ -528,6 +528,33 @@ def get_timerange(cls, sensors: List["Sensor"]) -> Dict[str, datetime]: # noqa
return dict(start=start, end=end)


def get_or_create_generic_asset(
name: str,
generic_asset_type_id: int,
attributes: dict,
flush: bool = True,
) -> DataSource:

_generic_asset = (
GenericAsset.query.filter(GenericAsset.name == name)
.filter(GenericAsset.generic_asset_type_id == generic_asset_type_id)
.one_or_none()
)

if _generic_asset is None:
_generic_asset = GenericAsset(
name=name,
generic_asset_type_id=generic_asset_type_id,
attributes=attributes,
)
db.session.add(_generic_asset)
if flush:
# assigns id so that we can reference the new object in the current db session
db.session.flush()

return _generic_asset


def create_generic_asset(generic_asset_type: str, **kwargs) -> GenericAsset:
"""Create a GenericAsset and assigns it an id.
Expand All @@ -550,16 +577,24 @@ def create_generic_asset(generic_asset_type: str, **kwargs) -> GenericAsset:
).one_or_none()
if generic_asset_type is None:
raise ValueError(f"Cannot find GenericAssetType {asset_type_name} in database.")
new_generic_asset = GenericAsset(

new_generic_asset = get_or_create_generic_asset(
name=kwargs["name"],
generic_asset_type_id=generic_asset_type.id,
attributes=kwargs["attributes"] if "attributes" in kwargs else {},
)

# GenericAsset(
# name=kwargs["name"],
# generic_asset_type_id=generic_asset_type.id,
# attributes=kwargs["attributes"] if "attributes" in kwargs else {},
# )

for arg in ("latitude", "longitude", "account_id"):
if arg in kwargs:
setattr(new_generic_asset, arg, kwargs[arg])
db.session.add(new_generic_asset)
db.session.flush() # generates the pkey for new_generic_asset
# db.session.add(new_generic_asset)
# db.session.flush() # generates the pkey for new_generic_asset
return new_generic_asset


Expand Down

0 comments on commit 48fd692

Please sign in to comment.