-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into 928-fix-discrepancies-between-db-and-models
- Loading branch information
Showing
11 changed files
with
177 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from flexmeasures import Asset, AssetType, Account, Sensor | ||
from flexmeasures.ui.utils.breadcrumb_utils import get_ancestry | ||
|
||
|
||
def test_get_ancestry(app, db): | ||
account = Account(name="Test Account") | ||
asset_type = AssetType(name="TestAssetType") | ||
|
||
parent_asset = Asset(name="Parent", generic_asset_type=asset_type, owner=account) | ||
assets = [parent_asset] | ||
for i in range(4): | ||
child_asset = Asset( | ||
name=f"Child {i}", | ||
generic_asset_type=asset_type, | ||
owner=account, | ||
parent_asset=parent_asset, | ||
) | ||
assets.append(child_asset) | ||
parent_asset = child_asset | ||
|
||
sensor = Sensor(name="Test Sensor", generic_asset=child_asset) | ||
|
||
db.session.add_all([account, asset_type, sensor] + assets) | ||
db.session.commit() | ||
|
||
# ancestry of a public account | ||
assert get_ancestry(None) == [{"url": None, "name": "PUBLIC", "type": "Account"}] | ||
|
||
# ancestry of an account | ||
account_id = account.id | ||
assert get_ancestry(account) == [ | ||
{"url": f"/accounts/{account_id}", "name": "Test Account", "type": "Account"} | ||
] | ||
|
||
# ancestry of a parentless asset | ||
assert get_ancestry(assets[0]) == [ | ||
{"url": f"/accounts/{account_id}", "name": "Test Account", "type": "Account"}, | ||
{"url": f"/assets/{assets[0].id}/", "name": "Parent", "type": "Asset"}, | ||
] | ||
|
||
# check that the number of elements of the ancestry of each assets corresponds to 2 + levels | ||
for i, asset in enumerate(assets): | ||
assert len(get_ancestry(asset)) == i + 2 | ||
|
||
# ancestry of the sensor | ||
sensor_ancestry = get_ancestry(sensor) | ||
assert sensor_ancestry[-1]["type"] == "Sensor" | ||
assert sensor_ancestry[0]["type"] == "Account" | ||
assert all(b["type"] == "Asset" for b in sensor_ancestry[1:-1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from __future__ import annotations | ||
|
||
from flexmeasures import Sensor, Asset, Account | ||
from flask import url_for | ||
|
||
|
||
def get_breadcrumb_info(entity: Sensor | Asset | Account | None) -> dict: | ||
return { | ||
"ancestors": get_ancestry(entity), | ||
} | ||
|
||
|
||
def get_ancestry(entity: Sensor | Asset | Account | None) -> list[dict]: | ||
|
||
# Public account | ||
if entity is None: | ||
return [{"url": None, "name": "PUBLIC", "type": "Account"}] | ||
|
||
# account | ||
if isinstance(entity, Account): | ||
return [ | ||
{ | ||
"url": url_for("AccountCrudUI:get", account_id=entity.id), | ||
"name": entity.name, | ||
"type": "Account", | ||
} | ||
] | ||
|
||
# sensor | ||
if isinstance(entity, Sensor): | ||
current_entity_info = [ | ||
{ | ||
"url": url_for("SensorUI:get", id=entity.id), | ||
"name": entity.name, | ||
"type": "Sensor", | ||
} | ||
] | ||
|
||
return get_ancestry(entity.generic_asset) + current_entity_info | ||
|
||
# asset | ||
if isinstance(entity, Asset): | ||
current_entity_info = [ | ||
{ | ||
"url": url_for("AssetCrudUI:get", id=entity.id), | ||
"name": entity.name, | ||
"type": "Asset", | ||
} | ||
] | ||
|
||
# asset without parent | ||
if entity.parent_asset is None: | ||
return get_ancestry(entity.owner) + current_entity_info | ||
else: # asset with parent | ||
return get_ancestry(entity.parent_asset) + current_entity_info | ||
|
||
return [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters