Skip to content

Commit

Permalink
Comment tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
nf679 committed Aug 5, 2024
1 parent 80c10d8 commit 7a5e3be
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
2 changes: 1 addition & 1 deletion nlds/utils/get_quotas.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def extract_tape_quota(self, oauth_token: str, service_name):
else:
raise ValueError(f"Issue getting tape quota for {service_name}. Quota is zero.")
except KeyError:
raise KeyError(f"Issue getting tape quota for {service_name}. No quota field exists.")
raise KeyError(f"Issue getting tape quota for {service_name}. No 'value' field exists.")
else:
raise ValueError(f"No tape resources could be found for {service_name}")
else:
Expand Down
38 changes: 33 additions & 5 deletions tests/nlds/utils/test_get_quotas.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def mock_get(*args, **kwargs):
raise requests.exceptions.ConnectionError
monkeypatch.setattr(requests, 'get', mock_get)

# Check that the ConnectionError in the get triggers a RuntimeError with the right text.
# Check that the ConnectionError in the 'get' triggers a RuntimeError with the right text.
with pytest.raises(RuntimeError, match=re.escape(f"User services url {url} could not be reached.")):
quotas.get_projects_services('dummy_oauth_token', 'test_service')

Expand Down Expand Up @@ -185,7 +185,6 @@ def mock_get(*args, **kwargs):
quotas.get_projects_services('dummy_oauth_token', 'test_service')



def test_extract_tape_quota_success(monkeypatch, quotas):
"""Test a successful instance of extract_tape_quota"""

Expand Down Expand Up @@ -252,42 +251,49 @@ def mock_get_projects_services(*args, **kwargs):
def test_extract_tape_quota_services_runtime_error(monkeypatch, quotas):
"""Test an unsuccessful instance of extract_tape_quota due to a runtime error getting services from the projects portal."""
def mock_get_projects_services(*args, **kwargs):
"""Mock the response from get_projects_services to give a RuntimeError."""
raise RuntimeError('Runtime error occurred')

monkeypatch.setattr(Quotas, 'get_projects_services', mock_get_projects_services)

# A RuntimeError should be raised saying a runtime error occurred.
with pytest.raises(RuntimeError, match="Error getting information for test_service: Runtime error occurred"):
quotas.extract_tape_quota('dummy_oauth_token', 'test_service')


def test_extract_tape_quota_services_value_error(monkeypatch, quotas):
"""Test an unsuccessful instance of extract_tape_quota due to a value error getting services from the projects portal."""
def mock_get_projects_services(*args, **kwargs):
"""Mock the response from get_projects_services to give a ValueError."""
raise ValueError('Value error occurred')

monkeypatch.setattr(Quotas, 'get_projects_services', mock_get_projects_services)

# A ValueError should be raised saying a value error occurred.
with pytest.raises(ValueError, match="Error getting information for test_service: Value error occurred"):
quotas.extract_tape_quota('dummy_oauth_token', 'test_service')


def test_extract_tape_quota_no_gws(monkeypatch, quotas):
"""Test an unsuccessful instance of extract_tape_quota due to the given service not being a gws."""
def mock_get_projects_services(*args, **kwargs):
"""Mock the response from get_projects_services to give results with the wrong category (a GWS is 1)."""
return [
{"category": 2, "requirements": []},
{"category": 3, "requirements": []}
]

monkeypatch.setattr(Quotas, 'get_projects_services', mock_get_projects_services)

# A ValueError should be raised saying it cannot find a GWS and to check the category.
with pytest.raises(ValueError, match="Cannot find a Group Workspace with the name test_service. Check the category."):
quotas.extract_tape_quota('dummy_oauth_token', 'test_service')


def test_extract_tape_quota_zero_quota(monkeypatch, quotas):
"""Test an unsuccessful instance of extract_tape_quota due to the quota being zero."""
def mock_get_projects_services(*args, **kwargs):
"""Mock the response from get_projects_services to give a quota of 0."""
return [{
"category": 1,
"requirements": [
Expand All @@ -301,13 +307,15 @@ def mock_get_projects_services(*args, **kwargs):

monkeypatch.setattr(Quotas, 'get_projects_services', mock_get_projects_services)

# A ValueError should be raised saying there was an issue getting tape quota as it is zero.
with pytest.raises(ValueError, match="Issue getting tape quota for test_service. Quota is zero."):
quotas.extract_tape_quota('dummy_oauth_token', 'test_service')


def test_extract_tape_quota_no_quota(monkeypatch, quotas):
"""Test an unsuccessful instance of extract_tape_quota due to there being no quota value."""
"""Test an unsuccessful instance of extract_tape_quota due to there being no quota field."""
def mock_get_projects_services(*args, **kwargs):
"""Mock the response from get_projects_services to give no 'amount' field."""
return [{
"category": 1,
"requirements": [
Expand All @@ -320,7 +328,27 @@ def mock_get_projects_services(*args, **kwargs):

monkeypatch.setattr(Quotas, 'get_projects_services', mock_get_projects_services)

with pytest.raises(KeyError, match="Issue getting tape quota for test_service. No quota field exists."):
# A KeyError should be raised saying there was an issue getting tape quota as no value field exists.
with pytest.raises(KeyError, match="Issue getting tape quota for test_service. No 'value' field exists."):
quotas.extract_tape_quota("dummy_oauth_token", "test_service")

# test 'no provisioned requirements ....'

def test_extract_tape_quota_no_provisioned_resources(monkeypatch, quotas):
"""Test an unsuccessful instance of extract_tape_quota due to there being no provisioned resources."""
def mock_get_projects_services(*args, **kwargs):
"""Mock the response from get_projects_services to give no provisioned resources (status 50)."""
return [{
"category": 1,
"requirements": [
{
"status": 1,
"resource": {"short_name": "tape"},
}
],
}]

monkeypatch.setattr(Quotas, 'get_projects_services', mock_get_projects_services)

# A ValueError should be raised saying there were no provisioned requirements found and to check the status of requested resources.
with pytest.raises(ValueError, match="No provisioned requirements found for test_service. Check the status of your requested resources."):
quotas.extract_tape_quota("dummy_oauth_token", "test_service")

0 comments on commit 7a5e3be

Please sign in to comment.