From f7e44bf364c916cd15da152fd5b28249517a2aa8 Mon Sep 17 00:00:00 2001 From: Marvin Buss Date: Mon, 27 Apr 2020 21:58:12 +0200 Subject: [PATCH] added unit tests --- code/utils.py | 6 ------ tests/test_main.py | 35 +++++++++++++++++++++++++++++++++++ tests/test_utils.py | 39 ++++++++++++++++++++++++++++++++++----- 3 files changed, 69 insertions(+), 11 deletions(-) create mode 100644 tests/test_main.py diff --git a/code/utils.py b/code/utils.py index b0b2780..881aea7 100644 --- a/code/utils.py +++ b/code/utils.py @@ -12,12 +12,6 @@ def mask_parameter(parameter): def load_json(file_path): - paths = [] - for root, dirs, files in os.walk("code"): - for filename in files: - paths.append(filename) - print(f"Path List: {paths}") - with open(file_path) as f: json_object = json.load(f) return json_object diff --git a/tests/test_main.py b/tests/test_main.py new file mode 100644 index 0000000..6049e19 --- /dev/null +++ b/tests/test_main.py @@ -0,0 +1,35 @@ +import os +import sys +import pytest + +myPath = os.path.dirname(os.path.abspath(__file__)) +sys.path.insert(0, os.path.join(myPath, "..", "code")) + +from main import main +from utils import AMLConfigurationException + + +def test_main_no_input(): + """ + Unit test to check the main function with no inputs + """ + with pytest.raises(AMLConfigurationException): + assert main() + + +def test_main_invalid_azure_credentials(): + os.environ["INPUT_AZURE_CREDENTIALS"] = "" + with pytest.raises(AMLConfigurationException): + assert main() + + +def test_main_invalid_parameters_file(): + os.environ["INPUT_AZURE_CREDENTIALS"] = """{ + 'clientId': 'test', + 'clientSecret': 'test', + 'subscriptionId': 'test', + 'tenantId': 'test' + }""" + os.environ["INPUT_PARAMETERS_FILE"] = "wrongfile.json" + with pytest.raises(AMLConfigurationException): + assert main() diff --git a/tests/test_utils.py b/tests/test_utils.py index 128ec0a..bc0b99f 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -29,15 +29,30 @@ def test_load_json_invalid_input(): ) -def test_validate_json(): +def test_validate_json_valid_inputs(): """ - Unit test to check the validate_json function + Unit test to check the validate_json function with valid inputs """ - json_path = os.path.join(".cloud", ".azure", "workspace.json") + json_object = {} schema_path = os.path.join("code", "schemas", "workspace_schema.json") - json_object = load_json( - file_path=json_path + schema_object = load_json( + file_path=schema_path ) + validate_json( + data=json_object, + schema=schema_object, + input_name="PARAMETERS_FILE" + ) + + +def test_validate_json_invalid_json(): + """ + Unit test to check the validate_json function with invalid json_object inputs + """ + json_object = { + "sku": "" + } + schema_path = os.path.join("code", "schemas", "workspace_schema.json") schema_object = load_json( file_path=schema_path ) @@ -47,3 +62,17 @@ def test_validate_json(): schema=schema_object, input_name="PARAMETERS_FILE" ) + + +def test_validate_json_invalid_schema(): + """ + Unit test to check the validate_json function with invalid schema inputs + """ + json_object = {} + schema_object = {} + with pytest.raises(Exception): + assert validate_json( + data=json_object, + schema=schema_object, + input_name="PARAMETERS_FILE" + )