From 2da7d101fdd3530c8dd4b93f376054bfe855a18e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20H=C3=B6ning?= Date: Mon, 1 May 2023 16:09:18 +0200 Subject: [PATCH 1/2] fix get_account_roles service function for non-existing account MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nicolas Höning --- flexmeasures/data/services/account.py | 3 ++- flexmeasures/data/tests/test_account.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/flexmeasures/data/services/account.py b/flexmeasures/data/services/account.py index ea188dfe5..f9326e859 100644 --- a/flexmeasures/data/services/account.py +++ b/flexmeasures/data/services/account.py @@ -12,5 +12,6 @@ def get_number_of_assets_in_account(account_id: int) -> int: def get_account_roles(account_id: int) -> AccountRole: account = Account.query.filter_by(id=account_id).one_or_none() - + if account is None: + return [] return account.account_roles diff --git a/flexmeasures/data/tests/test_account.py b/flexmeasures/data/tests/test_account.py index 5efd14875..0e1cf0485 100644 --- a/flexmeasures/data/tests/test_account.py +++ b/flexmeasures/data/tests/test_account.py @@ -11,11 +11,12 @@ def test_get_number_of_assets_in_account(db, setup_assets): 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"] From 1c873ec5fba1b5db9cca400b4447294f6917c505 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20H=C3=B6ning?= Date: Mon, 1 May 2023 16:18:50 +0200 Subject: [PATCH 2/2] refactor account-related services into one module; add one test for get_accounts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nicolas Höning --- flexmeasures/data/services/account.py | 17 ----------------- flexmeasures/data/services/accounts.py | 16 ++++++++++++++++ .../tests/{test_account.py => test_accounts.py} | 9 ++++++++- flexmeasures/ui/views/logged_in_user.py | 2 +- 4 files changed, 25 insertions(+), 19 deletions(-) delete mode 100644 flexmeasures/data/services/account.py rename flexmeasures/data/tests/{test_account.py => test_accounts.py} (74%) diff --git a/flexmeasures/data/services/account.py b/flexmeasures/data/services/account.py deleted file mode 100644 index f9326e859..000000000 --- a/flexmeasures/data/services/account.py +++ /dev/null @@ -1,17 +0,0 @@ -from flexmeasures.data.models.generic_assets import GenericAsset -from flexmeasures.data.models.user import Account, AccountRole - - -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) -> AccountRole: - account = Account.query.filter_by(id=account_id).one_or_none() - if account is None: - return [] - return account.account_roles diff --git a/flexmeasures/data/services/accounts.py b/flexmeasures/data/services/accounts.py index 9ef466ab2..d5a454269 100644 --- a/flexmeasures/data/services/accounts.py +++ b/flexmeasures/data/services/accounts.py @@ -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( @@ -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 diff --git a/flexmeasures/data/tests/test_account.py b/flexmeasures/data/tests/test_accounts.py similarity index 74% rename from flexmeasures/data/tests/test_account.py rename to flexmeasures/data/tests/test_accounts.py index 0e1cf0485..3c336e2fe 100644 --- a/flexmeasures/data/tests/test_account.py +++ b/flexmeasures/data/tests/test_accounts.py @@ -1,9 +1,16 @@ -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): + 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 diff --git a/flexmeasures/ui/views/logged_in_user.py b/flexmeasures/ui/views/logged_in_user.py index e2ead4d19..09616edc7 100644 --- a/flexmeasures/ui/views/logged_in_user.py +++ b/flexmeasures/ui/views/logged_in_user.py @@ -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, )