From d12c403bb2007323cc3781b5171081fd3ae6d509 Mon Sep 17 00:00:00 2001 From: Jim Madge Date: Thu, 2 Nov 2023 15:17:16 +0000 Subject: [PATCH] Add tests for create and teardown commands --- data_safe_haven/commands/context.py | 8 +++++--- data_safe_haven/context/context.py | 4 ++-- tests_/commands/test_context.py | 31 +++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/data_safe_haven/commands/context.py b/data_safe_haven/commands/context.py index da70dbfd87..c61e4f65d2 100644 --- a/data_safe_haven/commands/context.py +++ b/data_safe_haven/commands/context.py @@ -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() @@ -138,7 +138,8 @@ 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() @@ -146,5 +147,6 @@ def create() -> None: @context_command_group.command() def teardown() -> None: """Tear down Data Safe Haven context infrastructure""" - context = Context() + config = Config() + context = Context(config) context.teardown() diff --git a/data_safe_haven/context/context.py b/data_safe_haven/context/context.py index bbb037c2ca..234dc6c4c0 100644 --- a/data_safe_haven/context/context.py +++ b/data_safe_haven/context/context.py @@ -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 diff --git a/tests_/commands/test_context.py b/tests_/commands/test_context.py index 621a4885c6..047cdd5c48 100644 --- a/tests_/commands/test_context.py +++ b/tests_/commands/test_context.py @@ -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 @@ -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