Skip to content
This repository has been archived by the owner on May 14, 2024. It is now read-only.

Commit

Permalink
Use TestSettings values in tests instead of importing from settings.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Comeani committed Oct 10, 2023
1 parent 8d46b6a commit a987356
Show file tree
Hide file tree
Showing 17 changed files with 120 additions and 127 deletions.
2 changes: 1 addition & 1 deletion bank/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class SettingsSchema(BaseSettings):
default=(60,),
description='Notify users when their proposal is given number of days from expiration.')

def _load_tempalte_file(self, template_file):
def _load_template_file(self, template_file):
try:
return EmailTemplate((CUSTOM_SETTINGS_DIR / template_file).read_text())

Expand Down
2 changes: 1 addition & 1 deletion bank/system/slurm.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def get_cluster_usage_total(

else:
clusters = (cluster,)
# Default to all clusters in settings.clusters if not specified as an argument
# Default to all clusters in ApplicationSettings.clusters if not specified as an argument

total = 0
for cluster_name in clusters:
Expand Down
14 changes: 7 additions & 7 deletions tests/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from sqlalchemy import select

from bank import settings
from tests import TestSettings
from bank.orm import Account, Allocation, DBConnection, Investment, Proposal

TODAY = date.today()
Expand All @@ -11,7 +11,7 @@
DAY_AFTER_TOMORROW = TODAY + timedelta(days=2)
DAY_BEFORE_YESTERDAY = TODAY - timedelta(days=2)

account_subquery = select(Account.id).where(Account.name == settings.test_accounts[0])
account_subquery = select(Account.id).where(Account.name == TestSettings.test_accounts[0])
account_proposals_query = select(Proposal) \
.where(Proposal.account_id.in_(account_subquery))

Expand All @@ -37,7 +37,7 @@ def add_proposal_to_test_account(proposal: Proposal) -> None:
"""Add a Proposal to the test account and commit the addition to the database """

with DBConnection.session() as session:
account = session.execute(select(Account).where(Account.name == settings.test_accounts[0])).scalars().first()
account = session.execute(select(Account).where(Account.name == TestSettings.test_accounts[0])).scalars().first()
account.proposals.extend([proposal])
session.commit()

Expand All @@ -46,7 +46,7 @@ def add_investment_to_test_account(investment: Investment) -> None:
"""Add an Investment to the test account and commit the addition to the database """

with DBConnection.session() as session:
account = session.execute(select(Account).where(Account.name == settings.test_accounts[0])).scalars().first()
account = session.execute(select(Account).where(Account.name == TestSettings.test_accounts[0])).scalars().first()
account.investments.extend([investment])
session.commit()

Expand All @@ -67,7 +67,7 @@ def setUp(self) -> None:
session.commit()

# Create new (empty) accounts
for account in settings.test_accounts:
for account in TestSettings.test_accounts:
session.add(Account(name=account))

session.commit()
Expand Down Expand Up @@ -95,7 +95,7 @@ def setUp(self) -> None:
start = TODAY + ((i - 1) * timedelta(days=365))
end = TODAY + (i * timedelta(days=365))

allocations = [Allocation(cluster_name=settings.test_cluster,
allocations = [Allocation(cluster_name=TestSettings.test_cluster,
service_units_used=0,
service_units_total=self.num_proposal_sus),
Allocation(cluster_name='all_clusters',
Expand Down Expand Up @@ -138,7 +138,7 @@ def setUp(self) -> None:
investments.append(inv)

with DBConnection.session() as session:
result = session.execute(select(Account).where(Account.name == settings.test_accounts[0]))
result = session.execute(select(Account).where(Account.name == TestSettings.test_accounts[0]))
account = result.scalars().first()
account.investments.extend(investments)
session.commit()
74 changes: 37 additions & 37 deletions tests/account_logic/test_AccountServices.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import time_machine
from sqlalchemy import join, select

from bank import settings
from bank.account_logic import AccountServices
from bank.orm import Account, Allocation, DBConnection, Proposal
from bank.system.slurm import SlurmAccount, Slurm
from tests import TestSettings
from tests._utils import active_proposal_query, active_investment_query, InvestmentSetup, ProposalSetup


Expand All @@ -32,23 +32,23 @@ class AccountLocking(TestCase):
def test_account_locked_on_cluster(self) -> None:
"""Test the account is locked on a given cluster"""

slurm_account = SlurmAccount(settings.test_accounts[0])
slurm_account.set_locked_state(False, settings.test_cluster)
slurm_account = SlurmAccount(TestSettings.test_accounts[0])
slurm_account.set_locked_state(False, TestSettings.test_cluster)

account_services = AccountServices(settings.test_accounts[0])
account_services.lock(clusters=[settings.test_cluster])
self.assertTrue(slurm_account.get_locked_state(settings.test_cluster))
account_services = AccountServices(TestSettings.test_accounts[0])
account_services.lock(clusters=[TestSettings.test_cluster])
self.assertTrue(slurm_account.get_locked_state(TestSettings.test_cluster))

@patch.object(Slurm, "partition_names", lambda self: (settings.test_accounts[0],))
@patch.object(Slurm, "partition_names", lambda self: (TestSettings.test_accounts[0],))
def test_account_unlocked_on_investment_partition(self) -> None:
"""Test the account is locked on a given cluster"""

slurm_account = SlurmAccount(settings.test_accounts[0])
slurm_account.set_locked_state(False, settings.test_cluster)
slurm_account = SlurmAccount(TestSettings.test_accounts[0])
slurm_account.set_locked_state(False, TestSettings.test_cluster)

account_services = AccountServices(settings.test_accounts[0])
account_services.lock(clusters=[settings.test_cluster])
self.assertFalse(slurm_account.get_locked_state(settings.test_cluster))
account_services = AccountServices(TestSettings.test_accounts[0])
account_services.lock(clusters=[TestSettings.test_cluster])
self.assertFalse(slurm_account.get_locked_state(TestSettings.test_cluster))


class AccountUnlocking(TestCase):
Expand All @@ -57,12 +57,12 @@ class AccountUnlocking(TestCase):
def test_account_unlocked_on_cluster(self) -> None:
"""Test the account is unlocked on a given cluster"""

slurm_account = SlurmAccount(settings.test_accounts[0])
slurm_account.set_locked_state(True, settings.test_cluster)
slurm_account = SlurmAccount(TestSettings.test_accounts[0])
slurm_account.set_locked_state(True, TestSettings.test_cluster)

account_services = AccountServices(settings.test_accounts[0])
account_services.unlock(clusters=[settings.test_cluster])
self.assertFalse(slurm_account.get_locked_state(settings.test_cluster))
account_services = AccountServices(TestSettings.test_accounts[0])
account_services.unlock(clusters=[TestSettings.test_cluster])
self.assertFalse(slurm_account.get_locked_state(TestSettings.test_cluster))


class BuildUsageTable(ProposalSetup, InvestmentSetup, TestCase):
Expand All @@ -71,8 +71,8 @@ def setUp(self) -> None:
"""Instantiate an AccountServices and SlurmAccount object for the test account"""

super().setUp()
self.account = AccountServices(settings.test_accounts[0])
self.slurm_account = SlurmAccount(settings.test_accounts[0])
self.account = AccountServices(TestSettings.test_accounts[0])
self.slurm_account = SlurmAccount(TestSettings.test_accounts[0])

@patch.object(SlurmAccount,
"get_cluster_usage_per_user",
Expand All @@ -92,7 +92,7 @@ class GetActiveProposalEndDate(ProposalSetup, TestCase):
def setUp(self) -> None:
"""Instantiate an AccountServices object for the test account"""
super().setUp()
self.account = AccountServices(settings.test_accounts[0])
self.account = AccountServices(TestSettings.test_accounts[0])

def test_date_correct(self) -> None:
"""Test that the date is returned correctly in the expected format"""
Expand All @@ -107,7 +107,7 @@ class SetupDBAccountEntry(TestCase):
def test_account_inserted(self) -> None:
"""Test the account has an entry in the DB after insertion"""

account_name = settings.test_accounts[0]
account_name = TestSettings.test_accounts[0]
# Insert an entry into the database for an account with an existing SLURM account
AccountServices.setup_db_account_entry(account_name)

Expand All @@ -118,12 +118,12 @@ def test_account_inserted(self) -> None:

# The account entry should not be empty, and the name should match the name provided
self.assertTrue(account)
self.assertEqual(account.name, settings.test_accounts[0])
self.assertEqual(account.name, TestSettings.test_accounts[0])

def test_insertion_idempotence(self) -> None:
"""Test that multiple additions of the same account entry do not overwrite the initial insertion"""

account_name = settings.test_accounts[0]
account_name = TestSettings.test_accounts[0]
AccountServices.setup_db_account_entry(account_name)
AccountServices.setup_db_account_entry(account_name)

Expand All @@ -137,7 +137,7 @@ def test_account_inserted_AccountServices(self) -> None:
"""Test the account has an entry in the DB upon AccountServices object creation"""

# Create an account services object for an existing SLURM account
acct = AccountServices(settings.test_accounts[0])
acct = AccountServices(TestSettings.test_accounts[0])

with DBConnection.session() as session:
# Query the DB for the account
Expand All @@ -157,7 +157,7 @@ class NotifyAccount(ProposalSetup, InvestmentSetup, TestCase):
def setUp(self) -> None:
super().setUp()

self.account = AccountServices(settings.test_accounts[0])
self.account = AccountServices(TestSettings.test_accounts[0])

with DBConnection.session() as session:
active_proposal = session.execute(active_proposal_query).scalars().first()
Expand Down Expand Up @@ -221,9 +221,9 @@ def setUp(self) -> None:

super().setUp()

self.account = AccountServices(settings.test_accounts[0])
self.account = AccountServices(TestSettings.test_accounts[0])

self.slurm_account = SlurmAccount(settings.test_accounts[0])
self.slurm_account = SlurmAccount(TestSettings.test_accounts[0])

# Ensure account usage is a reproducible value for testing
@patch.object(SlurmAccount,
Expand All @@ -233,7 +233,7 @@ def test_status_locked_on_single_cluster(self) -> None:
"""Test that update_status locks the account on a single cluster that is exceeding usage limits"""

# Unlock SLURM account
self.slurm_account.set_locked_state(False, cluster=settings.test_cluster)
self.slurm_account.set_locked_state(False, cluster=TestSettings.test_cluster)

with DBConnection.session() as session:

Expand All @@ -250,7 +250,7 @@ def test_status_locked_on_single_cluster(self) -> None:
self.account.update_status()

# cluster should be locked due to exceeding usage
self.assertTrue(self.slurm_account.get_locked_state(cluster=settings.test_cluster))
self.assertTrue(self.slurm_account.get_locked_state(cluster=TestSettings.test_cluster))

@patch.object(SlurmAccount,
"get_cluster_usage_per_user",
Expand All @@ -267,7 +267,7 @@ def test_status_locked_on_all_clusters(self) -> None:
"""Test that update_status locks the account on all clusters"""

# Unlock SLURM account
self.slurm_account.set_locked_state(False, cluster=settings.test_cluster)
self.slurm_account.set_locked_state(False, cluster=TestSettings.test_cluster)

with DBConnection.session() as session:
# Proposal is expired
Expand All @@ -293,7 +293,7 @@ def test_status_locked_on_all_clusters(self) -> None:
def test_status_unlocked_with_floating_sus_applied(self) -> None:
"""Test that update_status uses floating SUs to cover usage over limits"""

self.slurm_account.set_locked_state(False, cluster=settings.test_cluster)
self.slurm_account.set_locked_state(False, cluster=TestSettings.test_cluster)

with DBConnection.session() as session:
# Proposal is active and has floating service units
Expand All @@ -315,7 +315,7 @@ def test_status_unlocked_with_floating_sus_applied(self) -> None:
joined_tables = join(join(Allocation, Proposal), Account)
floating_alloc_used_query = select(Allocation.service_units_used) \
.select_from(joined_tables) \
.where(Account.name == settings.test_accounts[0]) \
.where(Account.name == TestSettings.test_accounts[0]) \
.where(Allocation.cluster_name == "all_clusters") \
.where(Proposal.is_active)

Expand All @@ -326,15 +326,15 @@ def test_status_unlocked_with_floating_sus_applied(self) -> None:
self.assertEqual(1100, floating_sus_used)
self.assertEqual(proposal.allocations[0].service_units_used, proposal.allocations[0].service_units_total)

self.assertFalse(self.slurm_account.get_locked_state(cluster=settings.test_cluster))
self.assertFalse(self.slurm_account.get_locked_state(cluster=TestSettings.test_cluster))

@patch.object(SlurmAccount,
"get_cluster_usage_per_user",
lambda self, cluster, in_hours: {'account1': 50, 'account2': 50})
def test_status_unlocked_with_floating_sus_applied_multiple_clusters(self) -> None:
"""Test that update_status uses floating SUs to cover usage over limits"""

self.slurm_account.set_locked_state(False, cluster=settings.test_cluster)
self.slurm_account.set_locked_state(False, cluster=TestSettings.test_cluster)

with DBConnection.session() as session:
# Proposal is active and has floating service units
Expand All @@ -355,7 +355,7 @@ def test_status_unlocked_with_floating_sus_applied_multiple_clusters(self) -> No
joined_tables = join(join(Allocation, Proposal), Account)
floating_alloc_used_query = select(Allocation.service_units_used) \
.select_from(joined_tables) \
.where(Account.name == settings.test_accounts[0]) \
.where(Account.name == TestSettings.test_accounts[0]) \
.where(Allocation.cluster_name == "all_clusters") \
.where(Proposal.is_active)

Expand All @@ -380,7 +380,7 @@ def test_status_unlocked_with_floating_sus_applied_multiple_clusters(self) -> No
def test_status_unlocked_with_investment_sus_applied(self) -> None:
"""Test that update_status uses investment SUs to cover usage over limits"""

self.slurm_account.set_locked_state(False, cluster=settings.test_cluster)
self.slurm_account.set_locked_state(False, cluster=TestSettings.test_cluster)

with DBConnection.session() as session:
# Proposal is expired
Expand All @@ -397,7 +397,7 @@ def test_status_unlocked_with_investment_sus_applied(self) -> None:
self.account.update_status()

# cluster should be unlocked due to exceeding usage being covered by investment
self.assertFalse(self.slurm_account.get_locked_state(cluster=settings.test_cluster))
self.assertFalse(self.slurm_account.get_locked_state(cluster=TestSettings.test_cluster))

with DBConnection.session() as session:
# check that investment SUs were used to cover usage
Expand Down
28 changes: 14 additions & 14 deletions tests/account_logic/test_AdminServices.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

from sqlalchemy import select

from bank import settings
from bank.account_logic import AdminServices
from bank.system.slurm import SlurmAccount
from tests import TestSettings
from tests._utils import EmptyAccountSetup


Expand All @@ -14,42 +14,42 @@ class FindUnlockedAccounts(EmptyAccountSetup, TestCase):
def setUp(self) -> None:
"""Instantiate a SlurmAccount and AdminServices objects for finding account tests"""
super().setUp()
self.slurm_account1 = SlurmAccount(settings.test_accounts[0])
self.slurm_account2 = SlurmAccount(settings.test_accounts[1])
self.slurm_account1 = SlurmAccount(TestSettings.test_accounts[0])
self.slurm_account2 = SlurmAccount(TestSettings.test_accounts[1])
self.admin_services = AdminServices()

def test_unlocked_accounts_found(self) -> None:
"""Test that an unlocked accounts are found"""

# Unlock multiple accounts
self.slurm_account1.set_locked_state(False, settings.test_cluster)
self.slurm_account2.set_locked_state(False, settings.test_cluster)
self.slurm_account1.set_locked_state(False, TestSettings.test_cluster)
self.slurm_account2.set_locked_state(False, TestSettings.test_cluster)

# The accounts should be in the list of unlocked accounts
unlocked_accounts_by_cluster = self.admin_services.find_unlocked_account_names()
self.assertIn(self.slurm_account1.account_name, unlocked_accounts_by_cluster[settings.test_cluster])
self.assertIn(self.slurm_account2.account_name, unlocked_accounts_by_cluster[settings.test_cluster])
self.assertIn(self.slurm_account1.account_name, unlocked_accounts_by_cluster[TestSettings.test_cluster])
self.assertIn(self.slurm_account2.account_name, unlocked_accounts_by_cluster[TestSettings.test_cluster])

def test_locked_accounts_not_found(self) -> None:
"""Test that locked accounts are not found"""

# Lock multiple accounts
self.slurm_account1.set_locked_state(True, settings.test_cluster)
self.slurm_account2.set_locked_state(True, settings.test_cluster)
self.slurm_account1.set_locked_state(True, TestSettings.test_cluster)
self.slurm_account2.set_locked_state(True, TestSettings.test_cluster)

# The accounts should not be in the list of unlocked accounts
unlocked_accounts_by_cluster = self.admin_services.find_unlocked_account_names()
self.assertNotIn(self.slurm_account1.account_name, unlocked_accounts_by_cluster[settings.test_cluster])
self.assertNotIn(self.slurm_account2.account_name, unlocked_accounts_by_cluster[settings.test_cluster])
self.assertNotIn(self.slurm_account1.account_name, unlocked_accounts_by_cluster[TestSettings.test_cluster])
self.assertNotIn(self.slurm_account2.account_name, unlocked_accounts_by_cluster[TestSettings.test_cluster])

def test_unlocked_accounts_only_found(self) -> None:
"""Test that, amongst accounts that are locked, only unlocked accounts are found"""

# Lock one account and unlock another
self.slurm_account1.set_locked_state(True, settings.test_cluster)
self.slurm_account2.set_locked_state(False, settings.test_cluster)
self.slurm_account1.set_locked_state(True, TestSettings.test_cluster)
self.slurm_account2.set_locked_state(False, TestSettings.test_cluster)

# The unlocked account should be in the list of unlocked accounts, while the locked account should not
unlocked_accounts_by_cluster = self.admin_services.find_unlocked_account_names()
self.assertNotIn(self.slurm_account1.account_name, unlocked_accounts_by_cluster[settings.test_cluster])
self.assertNotIn(self.slurm_account1.account_name, unlocked_accounts_by_cluster[TestSettings.test_cluster])
self.assertIn(self.slurm_account2.account_name, unlocked_accounts_by_cluster[settings.test_cluster])
Loading

0 comments on commit a987356

Please sign in to comment.