Skip to content

Commit

Permalink
Merge 470e8e0 into 0791fa3
Browse files Browse the repository at this point in the history
  • Loading branch information
nhoening committed May 1, 2023
2 parents 0791fa3 + 470e8e0 commit 69c8630
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
16 changes: 0 additions & 16 deletions flexmeasures/data/services/account.py

This file was deleted.

16 changes: 16 additions & 0 deletions flexmeasures/data/services/accounts.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import List, Optional

from flexmeasures.data.models.user import Account, AccountRole
from flexmeasures.data.models.generic_assets import GenericAsset


def get_accounts(
Expand All @@ -19,3 +20,18 @@ def get_accounts(
return []

return account_query.all()


def get_number_of_assets_in_account(account_id: int) -> int:
"""Get the number of assets in an account."""
number_of_assets_in_account = GenericAsset.query.filter(
GenericAsset.account_id == account_id
).count()
return number_of_assets_in_account


def get_account_roles(account_id: int) -> List[AccountRole]:
account = Account.query.filter_by(id=account_id).one_or_none()
if account is None:
return []
return account.account_roles
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
from flexmeasures.data.services.account import (
from flexmeasures.data.services.accounts import (
get_accounts,
get_number_of_assets_in_account,
get_account_roles,
)


def test_get_accounts(db, setup_assets):
no_accounts = get_accounts("Not-an-existing-role")
assert len(no_accounts) == 0
dummy_accounts = get_accounts("Dummy")
assert len(dummy_accounts) == 2 # Dummy and Multi-Role
assert dummy_accounts[0].name == "Test Dummy Account"


def test_get_number_of_assets_in_account(db, setup_assets):
"""Get the number of assets in the testing accounts"""
assert get_number_of_assets_in_account(1) == 3
assert get_number_of_assets_in_account(2) == 0
assert get_number_of_assets_in_account(3) == 0


def test_get_account_roles(db):
def test_get_account_roles(db, setup_assets):
"""Get the account roles"""
assert get_account_roles(1)[0].name == "Prosumer"
assert get_account_roles(2)[0].name == "Supplier"
assert get_account_roles(3)[0].name == "Dummy"
assert get_account_roles(4) == []
assert get_account_roles(9999999) == [] # non-existing account id
multiple_roles = get_account_roles(5)
assert [i.name for i in multiple_roles] == ["Prosumer", "Supplier", "Dummy"]
2 changes: 1 addition & 1 deletion flexmeasures/ui/views/logged_in_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask_security import login_required

from flexmeasures.ui.views import flexmeasures_ui
from flexmeasures.data.services.account import (
from flexmeasures.data.services.accounts import (
get_number_of_assets_in_account,
get_account_roles,
)
Expand Down

0 comments on commit 69c8630

Please sign in to comment.