Skip to content

Commit

Permalink
Add tests for create and teardown commands
Browse files Browse the repository at this point in the history
  • Loading branch information
JimMadge committed Nov 2, 2023
1 parent e331299 commit d12c403
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
8 changes: 5 additions & 3 deletions data_safe_haven/commands/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from rich import print

from data_safe_haven.context import Context
from data_safe_haven.config import ContextSettings
from data_safe_haven.config import Config, ContextSettings
from data_safe_haven.functions import validate_aad_guid

context_command_group = typer.Typer()
Expand Down Expand Up @@ -138,13 +138,15 @@ def remove(
@context_command_group.command()
def create() -> None:
"""Create Data Safe Haven context infrastructure"""
context = Context()
config = Config()
context = Context(config)
context.create()
context.config.upload()


@context_command_group.command()
def teardown() -> None:
"""Tear down Data Safe Haven context infrastructure"""
context = Context()
config = Config()
context = Context(config)
context.teardown()
4 changes: 2 additions & 2 deletions data_safe_haven/context/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
class Context:
"""Azure resources to support Data Safe Haven context"""

def __init__(self) -> None:
def __init__(self, config: Config) -> None:
self.azure_api_: AzureApi | None = None
self.config = Config()
self.config = config
self.tags = {"component": "context"} | self.config.tags.to_dict()

@property
Expand Down
31 changes: 31 additions & 0 deletions tests_/commands/test_context.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from data_safe_haven.commands.context import context_command_group
from data_safe_haven.config import Config
from data_safe_haven.context import Context

from pytest import fixture
from typer.testing import CliRunner
Expand Down Expand Up @@ -167,3 +169,32 @@ def test_remove_invalid(self, runner):
assert result.exit_code == 1
# Unable to check error as this is written outside of any Typer
# assert "No context with key 'invalid'." in result.stdout


class TestCreate:
def test_create(self, runner, monkeypatch):
def mock_create(self):
print("mock create")

def mock_upload(self):
print("mock upload")

monkeypatch.setattr(Context, "create", mock_create)
monkeypatch.setattr(Config, "upload", mock_upload)

result = runner.invoke(context_command_group, ["create"])
assert "mock create" in result.stdout
assert "mock upload" in result.stdout
assert result.exit_code == 0


class TestTeardown:
def test_teardown(self, runner, monkeypatch):
def mock_teardown(self):
print("mock teardown")

monkeypatch.setattr(Context, "teardown", mock_teardown)

result = runner.invoke(context_command_group, ["teardown"])
assert "mock teardown" in result.stdout
assert result.exit_code == 0

0 comments on commit d12c403

Please sign in to comment.