From 9896bdb4f2abfd6120ae67a171b5d70b6a5a63cb Mon Sep 17 00:00:00 2001 From: cortadocodes Date: Sat, 10 Oct 2020 12:59:29 +0100 Subject: [PATCH 1/8] REF: Simplify BaseTestCase path attribute --- tests/base.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/base.py b/tests/base.py index d0f8b1e..1f8a2f6 100644 --- a/tests/base.py +++ b/tests/base.py @@ -6,7 +6,4 @@ class BaseTestCase(unittest.TestCase): """ Base test case for twined: - sets a path to the test data directory """ - - def setUp(self): - self.path = str(os.path.join(os.path.dirname(os.path.abspath(__file__)), "data", "")) - super().setUp() + path = os.path.join(os.path.dirname(__file__), "data", "") From cacac3b2ed8742b3d754ec5fcca717746fde1a82 Mon Sep 17 00:00:00 2001 From: cortadocodes Date: Sat, 10 Oct 2020 13:11:58 +0100 Subject: [PATCH 2/8] REF: Use os.path.join in tests; simplify base path --- tests/base.py | 2 +- tests/test_children.py | 27 ++++++++++---------- tests/test_credentials.py | 16 ++++++------ tests/test_manifest_strands.py | 45 +++++++++++++++++----------------- tests/test_schema_strands.py | 45 +++++++++++++++++----------------- tests/test_twine.py | 13 +++++----- tests/test_utils.py | 3 ++- 7 files changed, 78 insertions(+), 73 deletions(-) diff --git a/tests/base.py b/tests/base.py index 1f8a2f6..b95a1d5 100644 --- a/tests/base.py +++ b/tests/base.py @@ -6,4 +6,4 @@ class BaseTestCase(unittest.TestCase): """ Base test case for twined: - sets a path to the test data directory """ - path = os.path.join(os.path.dirname(__file__), "data", "") + path = os.path.join(os.path.dirname(__file__), "data") diff --git a/tests/test_children.py b/tests/test_children.py index e898edd..2f34da5 100644 --- a/tests/test_children.py +++ b/tests/test_children.py @@ -1,3 +1,4 @@ +import os import unittest from twined import Twine, exceptions @@ -13,7 +14,7 @@ def test_invalid_children_dict_not_array(self): """ Ensures InvalidTwine exceptions are raised when instantiating twines where `children` entry is incorrectly specified as a dict, not an array """ - twine_file = self.path + "twines/invalid_children_dict_not_array_twine.json" + twine_file = os.path.join(self.path, "twines/invalid_children_dict_not_array_twine.json") with self.assertRaises(exceptions.InvalidTwine): Twine(source=twine_file) @@ -21,21 +22,21 @@ def test_invalid_children_no_key(self): """ Ensures InvalidTwine exceptions are raised when instantiating twines where a child is specified without the required `key` field """ - twine_file = self.path + "twines/invalid_children_no_key_twine.json" + twine_file = os.path.join(self.path, "twines/invalid_children_no_key_twine.json") with self.assertRaises(exceptions.InvalidTwine): Twine(source=twine_file) def test_valid_children(self): """ Ensures that a twine can be instantiated with correctly specified children """ - twine_file = self.path + "twines/valid_children_twine.json" + twine_file = os.path.join(self.path, "twines/valid_children_twine.json") twine = Twine(source=twine_file) self.assertEqual(len(twine._raw["children"]), 1) def test_empty_children(self): """ Ensures that a twine file will validate with an empty list object as children """ - twine_file = self.path + "twines/valid_empty_children_twine.json" + twine_file = os.path.join(self.path, "twines/valid_empty_children_twine.json") twine = Twine(source=twine_file) self.assertEqual(len(twine._raw["children"]), 0) @@ -53,7 +54,7 @@ def test_no_children(self): def test_missing_children(self): """ Test that a twine with children will not validate on an empty children input """ - twine = Twine(source=self.path + "twines/valid_children_twine.json") + twine = Twine(source=os.path.join(self.path, "twines/valid_children_twine.json")) with self.assertRaises(exceptions.InvalidValuesContents): twine.validate_children(source="[]") @@ -62,33 +63,33 @@ def test_extra_children(self): """ twine = Twine() # Creates empty twine with self.assertRaises(exceptions.InvalidValuesContents): - twine.validate_children(source=self.path + "children/valid.json") + twine.validate_children(source=os.path.join(self.path, "children/valid.json")) def test_extra_key(self): """ Test that children with extra data will not raise validation error """ twine = Twine() # Creates empty twine with self.assertRaises(exceptions.InvalidValuesContents): - twine.validate_children(source=self.path + "children/extra_key.json") + twine.validate_children(source=os.path.join(self.path, "children/extra_key.json")) def test_extra_property(self): """ Test that children with extra data will not raise validation error # TODO review this behaviour - possibly should raise an error but allow for a user specified extra_data property """ - twine = Twine(source=self.path + "twines/valid_children_twine.json") - twine.validate_children(source=self.path + "children/extra_property.json") + twine = Twine(source=os.path.join(self.path, "twines/valid_children_twine.json")) + twine.validate_children(source=os.path.join(self.path, "children/extra_property.json")) def test_invalid_env_name(self): """ Test that a child uri env name not in ALL_CAPS_SNAKE_CASE doesn't validate """ twine = Twine() # Creates empty twine with self.assertRaises(exceptions.InvalidValuesContents): - twine.validate_children(source=self.path + "children/invalid_env_name.json") + twine.validate_children(source=os.path.join(self.path, "children/invalid_env_name.json")) def test_invalid_json(self): """ Tests that a children entry with invalid json will raise an error """ - twine = Twine(source=self.path + "twines/valid_children_twine.json") + twine = Twine(source=os.path.join(self.path, "twines/valid_children_twine.json")) with self.assertRaises(exceptions.InvalidValuesJson): twine.validate_children(source="[") @@ -97,8 +98,8 @@ def test_valid(self): Valiantly and Validly validating validity since 1983. To those reading this, know that YOU'RE valid. """ - twine = Twine(source=self.path + "twines/valid_children_twine.json") - twine.validate_children(source=self.path + "children/valid.json") + twine = Twine(source=os.path.join(self.path, "twines/valid_children_twine.json")) + twine.validate_children(source=os.path.join(self.path, "children/valid.json")) if __name__ == "__main__": diff --git a/tests/test_credentials.py b/tests/test_credentials.py index cbfc8b2..519d1f0 100644 --- a/tests/test_credentials.py +++ b/tests/test_credentials.py @@ -15,7 +15,7 @@ def test_fails_on_no_name(self): """ Ensures InvalidTwine exceptions are raised when instantiating twines with a missing `name` field in a credential """ - twine_file = self.path + "twines/invalid_credentials_no_name_twine.json" + twine_file = os.path.join(self.path, "twines/invalid_credentials_no_name_twine.json") with self.assertRaises(exceptions.InvalidTwine): Twine(source=twine_file) @@ -23,7 +23,7 @@ def test_fails_on_lowercase_name(self): """ Ensures InvalidTwine exceptions are raised when instantiating twines with lowercase letters in the `name` field """ - twine_file = self.path + "twines/invalid_credentials_lowercase_name_twine.json" + twine_file = os.path.join(self.path, "twines/invalid_credentials_lowercase_name_twine.json") with self.assertRaises(exceptions.InvalidTwine): Twine(source=twine_file) @@ -31,12 +31,12 @@ def test_fails_on_dict(self): """ Ensures InvalidTwine exceptions are raised when instantiating twines with invalid `credentials` entries (given as a dict, not an array) """ - twine_file = self.path + "twines/invalid_credentials_dict_not_array_twine.json" + twine_file = os.path.join(self.path, "twines/invalid_credentials_dict_not_array_twine.json") with self.assertRaises(exceptions.InvalidTwine): Twine(source=twine_file) def test_fails_on_name_whitespace(self): - twine_file = self.path + "twines/invalid_credentials_space_in_name_twine.json" + twine_file = os.path.join(self.path, "twines/invalid_credentials_space_in_name_twine.json") with self.assertRaises(exceptions.InvalidTwine): Twine(source=twine_file) @@ -48,20 +48,20 @@ class TestCredentialsValidation(BaseTestCase): def test_no_credentials(self): """ Test that a twine with no credentials will validate straightforwardly """ - twine = Twine(source=self.path + "twines/valid_schema_twine.json") + twine = Twine(source=os.path.join(self.path, "twines/valid_schema_twine.json")) twine.validate_credentials() def test_missing_credentials(self): """ Test that a twine with credentials will not validate where they are missing from the environment """ - twine = Twine(source=self.path + "twines/valid_credentials_twine.json") + twine = Twine(source=os.path.join(self.path, "twines/valid_credentials_twine.json")) with self.assertRaises(exceptions.CredentialNotFound): twine.validate_credentials() def test_default_credentials(self): """ Test that a twine with credentials will validate where ones with defaults are missing from the environment """ - twine = Twine(source=self.path + "twines/valid_credentials_twine.json") + twine = Twine(source=os.path.join(self.path, "twines/valid_credentials_twine.json")) with mock.patch.dict(os.environ, {"SECRET_THE_FIRST": "a value", "SECRET_THE_SECOND": "another value"}): credentials = twine.validate_credentials() @@ -73,7 +73,7 @@ def test_default_credentials(self): def test_nondefault_credentials(self): """ Test that the environment will override a default value for a credential """ - twine = Twine(source=self.path + "twines/valid_credentials_twine.json") + twine = Twine(source=os.path.join(self.path, "twines/valid_credentials_twine.json")) with mock.patch.dict( os.environ, {"SECRET_THE_FIRST": "a value", "SECRET_THE_SECOND": "another value", "SECRET_THE_THIRD": "nondefault"}, diff --git a/tests/test_manifest_strands.py b/tests/test_manifest_strands.py index dca2962..329e496 100644 --- a/tests/test_manifest_strands.py +++ b/tests/test_manifest_strands.py @@ -1,3 +1,4 @@ +import os import unittest from twined import Twine, exceptions @@ -11,9 +12,9 @@ class TestManifestStrands(BaseTestCase): def test_missing_manifest_files(self): """ Ensures that if you try to read values from missing files, the right exceptions get raised """ - twine_file = self.path + "twines/valid_manifest_twine.json" + twine_file = os.path.join(self.path, "twines/valid_manifest_twine.json") twine = Twine(source=twine_file) - file = self.path + "not_a_file.json" + file = os.path.join(self.path, "not_a_file.json") with self.assertRaises(exceptions.ConfigurationManifestFileNotFound): twine.validate_configuration_manifest(source=file) @@ -26,65 +27,65 @@ def test_missing_manifest_files(self): def test_valid_manifest_files(self): """ Ensures that a manifest file will validate """ - twine_file = self.path + "twines/valid_manifest_twine.json" + twine_file = os.path.join(self.path, "twines/valid_manifest_twine.json") twine = Twine(source=twine_file) - file = self.path + "manifests/configuration/configuration_valid.json" + file = os.path.join(self.path, "manifests/configuration/configuration_valid.json") twine.validate_input_manifest(source=file) - file = self.path + "manifests/inputs/input_valid.json" + file = os.path.join(self.path, "manifests/inputs/input_valid.json") twine.validate_input_manifest(source=file) - file = self.path + "manifests/outputs/output_valid.json" + file = os.path.join(self.path, "manifests/outputs/output_valid.json") twine.validate_output_manifest(source=file) # def test_empty_values(self): # """ Ensures that appropriate errors are generated for invalid values # """ - # twine_file = self.path + "twines/valid_schema_twine.json" + # twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") # twine = Twine(file=twine_file) - # values_file = self.path + "configurations/empty.json" + # values_file = os.path.join(self.path, "configurations/empty.json") # with self.assertRaises(exceptions.InvalidValuesJson): # twine.validate_configuration(file=values_file) # # def test_incorrect_values(self): # """ Ensures that appropriate errors are generated for invalid values # """ - # twine_file = self.path + "twines/valid_schema_twine.json" + # twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") # twine = Twine(file=twine_file) - # values_file = self.path + "configurations/incorrect.json" + # values_file = os.path.join(self.path, "configurations/incorrect.json") # with self.assertRaises(exceptions.InvalidValuesContents): # twine.validate_configuration(file=values_file) # # def test_missing_not_required_values(self): # """ Ensures that appropriate errors are generated for missing values # """ - # twine_file = self.path + "twines/valid_schema_twine.json" + # twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") # twine = Twine(file=twine_file) - # values_file = self.path + "outputs/missing_not_required.json" + # values_file = os.path.join(self.path, "outputs/missing_not_required.json") # twine.validate_output_values(file=values_file) # # def test_missing_required_values(self): # """ Ensures that appropriate errors are generated for missing values # """ - # twine_file = self.path + "twines/valid_schema_twine.json" + # twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") # twine = Twine(file=twine_file) - # values_file = self.path + "inputs/missing_required.json" + # values_file = os.path.join(self.path, "inputs/missing_required.json") # with self.assertRaises(exceptions.InvalidValuesContents): # twine.validate_input_values(file=values_file) # # def test_valid_values_files(self): # """ Ensures that values can be read and validated correctly from files on disk # """ - # twine_file = self.path + "twines/valid_schema_twine.json" + # twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") # twine = Twine(file=twine_file) - # twine.validate_configuration(file=self.path + "configurations/valid.json") - # twine.validate_input_values(file=self.path + "inputs/valid.json") - # twine.validate_output_values(file=self.path + "outputs/valid.json") + # twine.validate_configuration(file=os.path.join(self.path, "configurations/valid.json")) + # twine.validate_input_values(file=os.path.join(self.path, "inputs/valid.json")) + # twine.validate_output_values(file=os.path.join(self.path, "outputs/valid.json")) # # def test_valid_values_json(self): # """ Ensures that values can be read and validated correctly from a json string # """ - # twine_file = self.path + "twines/valid_schema_twine.json" + # twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") # twine = Twine(file=twine_file) - # values_file = self.path + "configurations/valid.json" + # values_file = os.path.join(self.path, "configurations/valid.json") # with open(values_file, "r", encoding="utf-8") as f: # json_string = f.read() # twine.validate_configuration(json=json_string) @@ -92,9 +93,9 @@ def test_valid_manifest_files(self): # def test_valid_with_extra_values(self): # """ Ensures that extra values get ignored # """ - # twine_file = self.path + "twines/valid_schema_twine.json" + # twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") # twine = Twine(file=twine_file) - # values_file = self.path + "configurations/valid_with_extra.json" + # values_file = os.path.join(self.path, "configurations/valid_with_extra.json") # twine.validate_configuration(file=values_file) diff --git a/tests/test_schema_strands.py b/tests/test_schema_strands.py index 4970d3d..821d5a6 100644 --- a/tests/test_schema_strands.py +++ b/tests/test_schema_strands.py @@ -1,3 +1,4 @@ +import os import unittest from twined import Twine, exceptions @@ -13,9 +14,9 @@ def test_invalid_strand(self): Note: This tests an internal method. The current API doesn't allow this error to emerge but tthis check allows us to extend to a generic method """ - twine_file = self.path + "twines/valid_schema_twine.json" + twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") twine = Twine(source=twine_file) - values_file = self.path + "values/configurations/configuration_valid.json" + values_file = os.path.join(self.path, "values/configurations/configuration_valid.json") data = twine._load_json("configuration", source=values_file) with self.assertRaises(exceptions.UnknownStrand): twine._validate_against_schema("not_a_strand_name", data) @@ -23,9 +24,9 @@ def test_invalid_strand(self): def test_missing_values_files(self): """ Ensures that if you try to read values from missing files, the right exceptions get raised """ - twine_file = self.path + "twines/valid_schema_twine.json" + twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") twine = Twine(source=twine_file) - values_file = self.path + "not_a_file.json" + values_file = os.path.join(self.path, "not_a_file.json") with self.assertRaises(exceptions.ConfigurationValuesFileNotFound): twine.validate_configuration_values(source=values_file) @@ -38,7 +39,7 @@ def test_missing_values_files(self): def test_no_values(self): """ Ensures that giving no data source raises an invalidJson error """ - twine_file = self.path + "twines/valid_schema_twine.json" + twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") twine = Twine(source=twine_file) with self.assertRaises(exceptions.InvalidValuesJson): twine.validate_configuration_values(source=None) @@ -46,16 +47,16 @@ def test_no_values(self): def test_empty_values(self): """ Ensures that appropriate errors are generated for invalid values """ - twine_file = self.path + "twines/valid_schema_twine.json" + twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") twine = Twine(source=twine_file) - values_file = self.path + "values/configurations/configuration_empty.json" + values_file = os.path.join(self.path, "values/configurations/configuration_empty.json") with self.assertRaises(exceptions.InvalidValuesJson): twine.validate_configuration_values(source=values_file) def test_strand_not_found(self): """ Ensures that if a twine doesn't have a strand, you can't validate against it """ - twine_file = self.path + "twines/valid_no_output_schema_twine.json" + twine_file = os.path.join(self.path, "twines/valid_no_output_schema_twine.json") twine = Twine(source=twine_file) with self.assertRaises(exceptions.StrandNotFound): twine.validate_output_values(source="{}") @@ -63,44 +64,44 @@ def test_strand_not_found(self): def test_incorrect_values(self): """ Ensures that appropriate errors are generated for invalid values """ - twine_file = self.path + "twines/valid_schema_twine.json" + twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") twine = Twine(source=twine_file) - values_file = self.path + "values/configurations/configuration_incorrect.json" + values_file = os.path.join(self.path, "values/configurations/configuration_incorrect.json") with self.assertRaises(exceptions.InvalidValuesContents): twine.validate_configuration_values(source=values_file) def test_missing_not_required_values(self): """ Ensures that appropriate errors are generated for missing values """ - twine_file = self.path + "twines/valid_schema_twine.json" + twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") twine = Twine(source=twine_file) - values_file = self.path + "values/outputs/output_missing_not_required.json" + values_file = os.path.join(self.path, "values/outputs/output_missing_not_required.json") twine.validate_output_values(source=values_file) def test_missing_required_values(self): """ Ensures that appropriate errors are generated for missing values """ - twine_file = self.path + "twines/valid_schema_twine.json" + twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") twine = Twine(source=twine_file) - values_file = self.path + "values/inputs/input_missing_required.json" + values_file = os.path.join(self.path, "values/inputs/input_missing_required.json") with self.assertRaises(exceptions.InvalidValuesContents): twine.validate_input_values(source=values_file) def test_valid_values_files(self): """ Ensures that values can be read and validated correctly from files on disk """ - twine_file = self.path + "twines/valid_schema_twine.json" + twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") twine = Twine(source=twine_file) - twine.validate_configuration_values(source=self.path + "values/configurations/configuration_valid.json") - twine.validate_input_values(source=self.path + "values/inputs/input_valid.json") - twine.validate_output_values(source=self.path + "values/outputs/output_valid.json") + twine.validate_configuration_values(source=os.path.join(self.path, "values/configurations/configuration_valid.json")) + twine.validate_input_values(source=os.path.join(self.path, "values/inputs/input_valid.json")) + twine.validate_output_values(source=os.path.join(self.path, "values/outputs/output_valid.json")) def test_valid_values_json(self): """ Ensures that values can be read and validated correctly from a json string """ - twine_file = self.path + "twines/valid_schema_twine.json" + twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") twine = Twine(source=twine_file) - values_file = self.path + "values/configurations/configuration_valid.json" + values_file = os.path.join(self.path, "values/configurations/configuration_valid.json") with open(values_file, "r", encoding="utf-8") as f: json_string = f.read() twine.validate_configuration_values(source=json_string) @@ -108,9 +109,9 @@ def test_valid_values_json(self): def test_valid_with_extra_values(self): """ Ensures that extra values get ignored """ - twine_file = self.path + "twines/valid_schema_twine.json" + twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") twine = Twine(source=twine_file) - values_file = self.path + "values/configurations/configuration_valid_with_extra.json" + values_file = os.path.join(self.path, "values/configurations/configuration_valid_with_extra.json") twine.validate_configuration_values(source=values_file) diff --git a/tests/test_twine.py b/tests/test_twine.py index 1f5fb2f..a65d5b1 100644 --- a/tests/test_twine.py +++ b/tests/test_twine.py @@ -1,3 +1,4 @@ +import os import unittest from twined import Twine, exceptions @@ -11,13 +12,13 @@ class TestTwine(BaseTestCase): def test_init_twine_with_filename(self): """ Ensures that the twine class can be instantiated with a file """ - twine_file = self.path + "apps/simple_app/twine.json" + twine_file = os.path.join(self.path, "apps/simple_app/twine.json") Twine(source=twine_file) def test_init_twine_with_json(self): """ Ensures that a twine can be instantiated with a json string """ - with open(self.path + "apps/simple_app/twine.json", "r", encoding="utf-8") as f: + with open(os.path.join(self.path, "apps/simple_app/twine.json"), "r", encoding="utf-8") as f: json_string = f.read() Twine(source=json_string) @@ -29,7 +30,7 @@ def test_no_twine(self): def test_incorrect_version_twine(self): """ Ensures exception is thrown on mismatch between installed and specified versions of twined """ - twine_file = self.path + "twines/incorrect_version_twine.json" + twine_file = os.path.join(self.path, "twines/incorrect_version_twine.json") with self.assertRaises(exceptions.TwineVersionConflict): Twine(source=twine_file) @@ -46,19 +47,19 @@ def test_empty_twine(self): def test_example_twine(self): """ Ensures that the example (full) twine can be loaded and validated """ - twine_file = self.path + "apps/example_app/twine.json" + twine_file = os.path.join(self.path, "apps/example_app/twine.json") Twine(source=twine_file) def test_simple_twine(self): """ Ensures that the simple app schema can be loaded and used to parse some basic config and values data """ - twine_file = self.path + "apps/simple_app/twine.json" + twine_file = os.path.join(self.path, "apps/simple_app/twine.json") Twine(source=twine_file) def test_broken_json_twine(self): """ Ensures that an invalid json file raises an InvalidTwine exception """ - twine_file = self.path + "twines/invalid_json_twine.json" + twine_file = os.path.join(self.path, "twines/invalid_json_twine.json") with self.assertRaises(exceptions.InvalidTwineJson): Twine(source=twine_file) diff --git a/tests/test_utils.py b/tests/test_utils.py index f26e31d..d6e6607 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,4 +1,5 @@ import json +import os import unittest from unittest import mock import numpy as np @@ -15,7 +16,7 @@ class TestUtils(BaseTestCase): def test_load_json_with_file_like(self): """ Ensures that json can be loaded from a file-like object """ - file_name = self.path + "twines/valid_schema_twine.json" + file_name = os.path.join(self.path, "twines/valid_schema_twine.json") with open(file_name, "r") as file_like: data = load_json(file_like) for key in data.keys(): From 41c37f88cd942859cb3bb077cf3389a24aec9502 Mon Sep 17 00:00:00 2001 From: cortadocodes Date: Sat, 10 Oct 2020 13:15:23 +0100 Subject: [PATCH 3/8] REF: Fully utilise os.path.join in tests --- tests/test_children.py | 26 ++++++++++----------- tests/test_credentials.py | 16 ++++++------- tests/test_manifest_strands.py | 42 +++++++++++++++++----------------- tests/test_schema_strands.py | 42 +++++++++++++++++----------------- tests/test_twine.py | 12 +++++----- tests/test_utils.py | 2 +- 6 files changed, 70 insertions(+), 70 deletions(-) diff --git a/tests/test_children.py b/tests/test_children.py index 2f34da5..24d7e1f 100644 --- a/tests/test_children.py +++ b/tests/test_children.py @@ -14,7 +14,7 @@ def test_invalid_children_dict_not_array(self): """ Ensures InvalidTwine exceptions are raised when instantiating twines where `children` entry is incorrectly specified as a dict, not an array """ - twine_file = os.path.join(self.path, "twines/invalid_children_dict_not_array_twine.json") + twine_file = os.path.join(self.path, "twines", "invalid_children_dict_not_array_twine.json") with self.assertRaises(exceptions.InvalidTwine): Twine(source=twine_file) @@ -22,21 +22,21 @@ def test_invalid_children_no_key(self): """ Ensures InvalidTwine exceptions are raised when instantiating twines where a child is specified without the required `key` field """ - twine_file = os.path.join(self.path, "twines/invalid_children_no_key_twine.json") + twine_file = os.path.join(self.path, "twines", "invalid_children_no_key_twine.json") with self.assertRaises(exceptions.InvalidTwine): Twine(source=twine_file) def test_valid_children(self): """ Ensures that a twine can be instantiated with correctly specified children """ - twine_file = os.path.join(self.path, "twines/valid_children_twine.json") + twine_file = os.path.join(self.path, "twines", "valid_children_twine.json") twine = Twine(source=twine_file) self.assertEqual(len(twine._raw["children"]), 1) def test_empty_children(self): """ Ensures that a twine file will validate with an empty list object as children """ - twine_file = os.path.join(self.path, "twines/valid_empty_children_twine.json") + twine_file = os.path.join(self.path, "twines", "valid_empty_children_twine.json") twine = Twine(source=twine_file) self.assertEqual(len(twine._raw["children"]), 0) @@ -54,7 +54,7 @@ def test_no_children(self): def test_missing_children(self): """ Test that a twine with children will not validate on an empty children input """ - twine = Twine(source=os.path.join(self.path, "twines/valid_children_twine.json")) + twine = Twine(source=os.path.join(self.path, "twines", "valid_children_twine.json")) with self.assertRaises(exceptions.InvalidValuesContents): twine.validate_children(source="[]") @@ -63,33 +63,33 @@ def test_extra_children(self): """ twine = Twine() # Creates empty twine with self.assertRaises(exceptions.InvalidValuesContents): - twine.validate_children(source=os.path.join(self.path, "children/valid.json")) + twine.validate_children(source=os.path.join(self.path, "children", "valid.json")) def test_extra_key(self): """ Test that children with extra data will not raise validation error """ twine = Twine() # Creates empty twine with self.assertRaises(exceptions.InvalidValuesContents): - twine.validate_children(source=os.path.join(self.path, "children/extra_key.json")) + twine.validate_children(source=os.path.join(self.path, "children", "extra_key.json")) def test_extra_property(self): """ Test that children with extra data will not raise validation error # TODO review this behaviour - possibly should raise an error but allow for a user specified extra_data property """ - twine = Twine(source=os.path.join(self.path, "twines/valid_children_twine.json")) - twine.validate_children(source=os.path.join(self.path, "children/extra_property.json")) + twine = Twine(source=os.path.join(self.path, "twines", "valid_children_twine.json")) + twine.validate_children(source=os.path.join(self.path, "children", "extra_property.json")) def test_invalid_env_name(self): """ Test that a child uri env name not in ALL_CAPS_SNAKE_CASE doesn't validate """ twine = Twine() # Creates empty twine with self.assertRaises(exceptions.InvalidValuesContents): - twine.validate_children(source=os.path.join(self.path, "children/invalid_env_name.json")) + twine.validate_children(source=os.path.join(self.path, "children", "invalid_env_name.json")) def test_invalid_json(self): """ Tests that a children entry with invalid json will raise an error """ - twine = Twine(source=os.path.join(self.path, "twines/valid_children_twine.json")) + twine = Twine(source=os.path.join(self.path, "twines", "valid_children_twine.json")) with self.assertRaises(exceptions.InvalidValuesJson): twine.validate_children(source="[") @@ -98,8 +98,8 @@ def test_valid(self): Valiantly and Validly validating validity since 1983. To those reading this, know that YOU'RE valid. """ - twine = Twine(source=os.path.join(self.path, "twines/valid_children_twine.json")) - twine.validate_children(source=os.path.join(self.path, "children/valid.json")) + twine = Twine(source=os.path.join(self.path, "twines", "valid_children_twine.json")) + twine.validate_children(source=os.path.join(self.path, "children", "valid.json")) if __name__ == "__main__": diff --git a/tests/test_credentials.py b/tests/test_credentials.py index 519d1f0..e7bb4f3 100644 --- a/tests/test_credentials.py +++ b/tests/test_credentials.py @@ -15,7 +15,7 @@ def test_fails_on_no_name(self): """ Ensures InvalidTwine exceptions are raised when instantiating twines with a missing `name` field in a credential """ - twine_file = os.path.join(self.path, "twines/invalid_credentials_no_name_twine.json") + twine_file = os.path.join(self.path, "twines", "invalid_credentials_no_name_twine.json") with self.assertRaises(exceptions.InvalidTwine): Twine(source=twine_file) @@ -23,7 +23,7 @@ def test_fails_on_lowercase_name(self): """ Ensures InvalidTwine exceptions are raised when instantiating twines with lowercase letters in the `name` field """ - twine_file = os.path.join(self.path, "twines/invalid_credentials_lowercase_name_twine.json") + twine_file = os.path.join(self.path, "twines", "invalid_credentials_lowercase_name_twine.json") with self.assertRaises(exceptions.InvalidTwine): Twine(source=twine_file) @@ -31,12 +31,12 @@ def test_fails_on_dict(self): """ Ensures InvalidTwine exceptions are raised when instantiating twines with invalid `credentials` entries (given as a dict, not an array) """ - twine_file = os.path.join(self.path, "twines/invalid_credentials_dict_not_array_twine.json") + twine_file = os.path.join(self.path, "twines", "invalid_credentials_dict_not_array_twine.json") with self.assertRaises(exceptions.InvalidTwine): Twine(source=twine_file) def test_fails_on_name_whitespace(self): - twine_file = os.path.join(self.path, "twines/invalid_credentials_space_in_name_twine.json") + twine_file = os.path.join(self.path, "twines", "invalid_credentials_space_in_name_twine.json") with self.assertRaises(exceptions.InvalidTwine): Twine(source=twine_file) @@ -48,20 +48,20 @@ class TestCredentialsValidation(BaseTestCase): def test_no_credentials(self): """ Test that a twine with no credentials will validate straightforwardly """ - twine = Twine(source=os.path.join(self.path, "twines/valid_schema_twine.json")) + twine = Twine(source=os.path.join(self.path, "twines", "valid_schema_twine.json")) twine.validate_credentials() def test_missing_credentials(self): """ Test that a twine with credentials will not validate where they are missing from the environment """ - twine = Twine(source=os.path.join(self.path, "twines/valid_credentials_twine.json")) + twine = Twine(source=os.path.join(self.path, "twines", "valid_credentials_twine.json")) with self.assertRaises(exceptions.CredentialNotFound): twine.validate_credentials() def test_default_credentials(self): """ Test that a twine with credentials will validate where ones with defaults are missing from the environment """ - twine = Twine(source=os.path.join(self.path, "twines/valid_credentials_twine.json")) + twine = Twine(source=os.path.join(self.path, "twines", "valid_credentials_twine.json")) with mock.patch.dict(os.environ, {"SECRET_THE_FIRST": "a value", "SECRET_THE_SECOND": "another value"}): credentials = twine.validate_credentials() @@ -73,7 +73,7 @@ def test_default_credentials(self): def test_nondefault_credentials(self): """ Test that the environment will override a default value for a credential """ - twine = Twine(source=os.path.join(self.path, "twines/valid_credentials_twine.json")) + twine = Twine(source=os.path.join(self.path, "twines", "valid_credentials_twine.json")) with mock.patch.dict( os.environ, {"SECRET_THE_FIRST": "a value", "SECRET_THE_SECOND": "another value", "SECRET_THE_THIRD": "nondefault"}, diff --git a/tests/test_manifest_strands.py b/tests/test_manifest_strands.py index 329e496..23f5a67 100644 --- a/tests/test_manifest_strands.py +++ b/tests/test_manifest_strands.py @@ -12,7 +12,7 @@ class TestManifestStrands(BaseTestCase): def test_missing_manifest_files(self): """ Ensures that if you try to read values from missing files, the right exceptions get raised """ - twine_file = os.path.join(self.path, "twines/valid_manifest_twine.json") + twine_file = os.path.join(self.path, "twines", "valid_manifest_twine.json") twine = Twine(source=twine_file) file = os.path.join(self.path, "not_a_file.json") with self.assertRaises(exceptions.ConfigurationManifestFileNotFound): @@ -27,65 +27,65 @@ def test_missing_manifest_files(self): def test_valid_manifest_files(self): """ Ensures that a manifest file will validate """ - twine_file = os.path.join(self.path, "twines/valid_manifest_twine.json") + twine_file = os.path.join(self.path, "twines", "valid_manifest_twine.json") twine = Twine(source=twine_file) - file = os.path.join(self.path, "manifests/configuration/configuration_valid.json") + file = os.path.join(self.path, "manifests", "configuration", "configuration_valid.json") twine.validate_input_manifest(source=file) - file = os.path.join(self.path, "manifests/inputs/input_valid.json") + file = os.path.join(self.path, "manifests", "inputs", "input_valid.json") twine.validate_input_manifest(source=file) - file = os.path.join(self.path, "manifests/outputs/output_valid.json") + file = os.path.join(self.path, "manifests", "outputs", "output_valid.json") twine.validate_output_manifest(source=file) # def test_empty_values(self): # """ Ensures that appropriate errors are generated for invalid values # """ - # twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") + # twine_file = os.path.join(self.path, "twines", "valid_schema_twine.json") # twine = Twine(file=twine_file) - # values_file = os.path.join(self.path, "configurations/empty.json") + # values_file = os.path.join(self.path, "configurations", "empty.json") # with self.assertRaises(exceptions.InvalidValuesJson): # twine.validate_configuration(file=values_file) # # def test_incorrect_values(self): # """ Ensures that appropriate errors are generated for invalid values # """ - # twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") + # twine_file = os.path.join(self.path, "twines", "valid_schema_twine.json") # twine = Twine(file=twine_file) - # values_file = os.path.join(self.path, "configurations/incorrect.json") + # values_file = os.path.join(self.path, "configurations", "incorrect.json") # with self.assertRaises(exceptions.InvalidValuesContents): # twine.validate_configuration(file=values_file) # # def test_missing_not_required_values(self): # """ Ensures that appropriate errors are generated for missing values # """ - # twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") + # twine_file = os.path.join(self.path, "twines", "valid_schema_twine.json") # twine = Twine(file=twine_file) - # values_file = os.path.join(self.path, "outputs/missing_not_required.json") + # values_file = os.path.join(self.path, "outputs", "missing_not_required.json") # twine.validate_output_values(file=values_file) # # def test_missing_required_values(self): # """ Ensures that appropriate errors are generated for missing values # """ - # twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") + # twine_file = os.path.join(self.path, "twines", "valid_schema_twine.json") # twine = Twine(file=twine_file) - # values_file = os.path.join(self.path, "inputs/missing_required.json") + # values_file = os.path.join(self.path, "inputs", "missing_required.json") # with self.assertRaises(exceptions.InvalidValuesContents): # twine.validate_input_values(file=values_file) # # def test_valid_values_files(self): # """ Ensures that values can be read and validated correctly from files on disk # """ - # twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") + # twine_file = os.path.join(self.path, "twines", "valid_schema_twine.json") # twine = Twine(file=twine_file) - # twine.validate_configuration(file=os.path.join(self.path, "configurations/valid.json")) - # twine.validate_input_values(file=os.path.join(self.path, "inputs/valid.json")) - # twine.validate_output_values(file=os.path.join(self.path, "outputs/valid.json")) + # twine.validate_configuration(file=os.path.join(self.path, "configurations", "valid.json")) + # twine.validate_input_values(file=os.path.join(self.path, "inputs", "valid.json")) + # twine.validate_output_values(file=os.path.join(self.path, "outputs", "valid.json")) # # def test_valid_values_json(self): # """ Ensures that values can be read and validated correctly from a json string # """ - # twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") + # twine_file = os.path.join(self.path, "twines", "valid_schema_twine.json") # twine = Twine(file=twine_file) - # values_file = os.path.join(self.path, "configurations/valid.json") + # values_file = os.path.join(self.path, "configurations", "valid.json") # with open(values_file, "r", encoding="utf-8") as f: # json_string = f.read() # twine.validate_configuration(json=json_string) @@ -93,9 +93,9 @@ def test_valid_manifest_files(self): # def test_valid_with_extra_values(self): # """ Ensures that extra values get ignored # """ - # twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") + # twine_file = os.path.join(self.path, "twines", "valid_schema_twine.json") # twine = Twine(file=twine_file) - # values_file = os.path.join(self.path, "configurations/valid_with_extra.json") + # values_file = os.path.join(self.path, "configurations", "valid_with_extra.json") # twine.validate_configuration(file=values_file) diff --git a/tests/test_schema_strands.py b/tests/test_schema_strands.py index 821d5a6..31bb90d 100644 --- a/tests/test_schema_strands.py +++ b/tests/test_schema_strands.py @@ -14,9 +14,9 @@ def test_invalid_strand(self): Note: This tests an internal method. The current API doesn't allow this error to emerge but tthis check allows us to extend to a generic method """ - twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") + twine_file = os.path.join(self.path, "twines", "valid_schema_twine.json") twine = Twine(source=twine_file) - values_file = os.path.join(self.path, "values/configurations/configuration_valid.json") + values_file = os.path.join(self.path, "values", "configurations", "configuration_valid.json") data = twine._load_json("configuration", source=values_file) with self.assertRaises(exceptions.UnknownStrand): twine._validate_against_schema("not_a_strand_name", data) @@ -24,7 +24,7 @@ def test_invalid_strand(self): def test_missing_values_files(self): """ Ensures that if you try to read values from missing files, the right exceptions get raised """ - twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") + twine_file = os.path.join(self.path, "twines", "valid_schema_twine.json") twine = Twine(source=twine_file) values_file = os.path.join(self.path, "not_a_file.json") with self.assertRaises(exceptions.ConfigurationValuesFileNotFound): @@ -39,7 +39,7 @@ def test_missing_values_files(self): def test_no_values(self): """ Ensures that giving no data source raises an invalidJson error """ - twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") + twine_file = os.path.join(self.path, "twines", "valid_schema_twine.json") twine = Twine(source=twine_file) with self.assertRaises(exceptions.InvalidValuesJson): twine.validate_configuration_values(source=None) @@ -47,16 +47,16 @@ def test_no_values(self): def test_empty_values(self): """ Ensures that appropriate errors are generated for invalid values """ - twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") + twine_file = os.path.join(self.path, "twines", "valid_schema_twine.json") twine = Twine(source=twine_file) - values_file = os.path.join(self.path, "values/configurations/configuration_empty.json") + values_file = os.path.join(self.path, "values", "configurations", "configuration_empty.json") with self.assertRaises(exceptions.InvalidValuesJson): twine.validate_configuration_values(source=values_file) def test_strand_not_found(self): """ Ensures that if a twine doesn't have a strand, you can't validate against it """ - twine_file = os.path.join(self.path, "twines/valid_no_output_schema_twine.json") + twine_file = os.path.join(self.path, "twines", "valid_no_output_schema_twine.json") twine = Twine(source=twine_file) with self.assertRaises(exceptions.StrandNotFound): twine.validate_output_values(source="{}") @@ -64,44 +64,44 @@ def test_strand_not_found(self): def test_incorrect_values(self): """ Ensures that appropriate errors are generated for invalid values """ - twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") + twine_file = os.path.join(self.path, "twines", "valid_schema_twine.json") twine = Twine(source=twine_file) - values_file = os.path.join(self.path, "values/configurations/configuration_incorrect.json") + values_file = os.path.join(self.path, "values", "configurations", "configuration_incorrect.json") with self.assertRaises(exceptions.InvalidValuesContents): twine.validate_configuration_values(source=values_file) def test_missing_not_required_values(self): """ Ensures that appropriate errors are generated for missing values """ - twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") + twine_file = os.path.join(self.path, "twines", "valid_schema_twine.json") twine = Twine(source=twine_file) - values_file = os.path.join(self.path, "values/outputs/output_missing_not_required.json") + values_file = os.path.join(self.path, "values", "outputs", "output_missing_not_required.json") twine.validate_output_values(source=values_file) def test_missing_required_values(self): """ Ensures that appropriate errors are generated for missing values """ - twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") + twine_file = os.path.join(self.path, "twines", "valid_schema_twine.json") twine = Twine(source=twine_file) - values_file = os.path.join(self.path, "values/inputs/input_missing_required.json") + values_file = os.path.join(self.path, "values", "inputs", "input_missing_required.json") with self.assertRaises(exceptions.InvalidValuesContents): twine.validate_input_values(source=values_file) def test_valid_values_files(self): """ Ensures that values can be read and validated correctly from files on disk """ - twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") + twine_file = os.path.join(self.path, "twines", "valid_schema_twine.json") twine = Twine(source=twine_file) - twine.validate_configuration_values(source=os.path.join(self.path, "values/configurations/configuration_valid.json")) - twine.validate_input_values(source=os.path.join(self.path, "values/inputs/input_valid.json")) - twine.validate_output_values(source=os.path.join(self.path, "values/outputs/output_valid.json")) + twine.validate_configuration_values(source=os.path.join(self.path, "values", "configurations", "configuration_valid.json")) + twine.validate_input_values(source=os.path.join(self.path, "values", "inputs", "input_valid.json")) + twine.validate_output_values(source=os.path.join(self.path, "values", "outputs", "output_valid.json")) def test_valid_values_json(self): """ Ensures that values can be read and validated correctly from a json string """ - twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") + twine_file = os.path.join(self.path, "twines", "valid_schema_twine.json") twine = Twine(source=twine_file) - values_file = os.path.join(self.path, "values/configurations/configuration_valid.json") + values_file = os.path.join(self.path, "values", "configurations", "configuration_valid.json") with open(values_file, "r", encoding="utf-8") as f: json_string = f.read() twine.validate_configuration_values(source=json_string) @@ -109,9 +109,9 @@ def test_valid_values_json(self): def test_valid_with_extra_values(self): """ Ensures that extra values get ignored """ - twine_file = os.path.join(self.path, "twines/valid_schema_twine.json") + twine_file = os.path.join(self.path, "twines", "valid_schema_twine.json") twine = Twine(source=twine_file) - values_file = os.path.join(self.path, "values/configurations/configuration_valid_with_extra.json") + values_file = os.path.join(self.path, "values", "configurations", "configuration_valid_with_extra.json") twine.validate_configuration_values(source=values_file) diff --git a/tests/test_twine.py b/tests/test_twine.py index a65d5b1..f29338f 100644 --- a/tests/test_twine.py +++ b/tests/test_twine.py @@ -12,13 +12,13 @@ class TestTwine(BaseTestCase): def test_init_twine_with_filename(self): """ Ensures that the twine class can be instantiated with a file """ - twine_file = os.path.join(self.path, "apps/simple_app/twine.json") + twine_file = os.path.join(self.path, "apps", "simple_app", "twine.json") Twine(source=twine_file) def test_init_twine_with_json(self): """ Ensures that a twine can be instantiated with a json string """ - with open(os.path.join(self.path, "apps/simple_app/twine.json"), "r", encoding="utf-8") as f: + with open(os.path.join(self.path, "apps", "simple_app", "twine.json"), "r", encoding="utf-8") as f: json_string = f.read() Twine(source=json_string) @@ -30,7 +30,7 @@ def test_no_twine(self): def test_incorrect_version_twine(self): """ Ensures exception is thrown on mismatch between installed and specified versions of twined """ - twine_file = os.path.join(self.path, "twines/incorrect_version_twine.json") + twine_file = os.path.join(self.path, "twines", "incorrect_version_twine.json") with self.assertRaises(exceptions.TwineVersionConflict): Twine(source=twine_file) @@ -47,19 +47,19 @@ def test_empty_twine(self): def test_example_twine(self): """ Ensures that the example (full) twine can be loaded and validated """ - twine_file = os.path.join(self.path, "apps/example_app/twine.json") + twine_file = os.path.join(self.path, "apps", "example_app", "twine.json") Twine(source=twine_file) def test_simple_twine(self): """ Ensures that the simple app schema can be loaded and used to parse some basic config and values data """ - twine_file = os.path.join(self.path, "apps/simple_app/twine.json") + twine_file = os.path.join(self.path, "apps", "simple_app", "twine.json") Twine(source=twine_file) def test_broken_json_twine(self): """ Ensures that an invalid json file raises an InvalidTwine exception """ - twine_file = os.path.join(self.path, "twines/invalid_json_twine.json") + twine_file = os.path.join(self.path, "twines", "invalid_json_twine.json") with self.assertRaises(exceptions.InvalidTwineJson): Twine(source=twine_file) diff --git a/tests/test_utils.py b/tests/test_utils.py index d6e6607..e8a3b61 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -16,7 +16,7 @@ class TestUtils(BaseTestCase): def test_load_json_with_file_like(self): """ Ensures that json can be loaded from a file-like object """ - file_name = os.path.join(self.path, "twines/valid_schema_twine.json") + file_name = os.path.join(self.path, "twines", "valid_schema_twine.json") with open(file_name, "r") as file_like: data = load_json(file_like) for key in data.keys(): From d77881dd10bb20ba25bfccace35d919986db371c Mon Sep 17 00:00:00 2001 From: cortadocodes Date: Sat, 10 Oct 2020 13:19:42 +0100 Subject: [PATCH 4/8] REF: Remove simple single-use variables from tests --- tests/test_children.py | 12 ++++-------- tests/test_twine.py | 20 +++++++------------- 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/tests/test_children.py b/tests/test_children.py index 24d7e1f..af694a8 100644 --- a/tests/test_children.py +++ b/tests/test_children.py @@ -48,8 +48,7 @@ class TestChildrenValidation(BaseTestCase): def test_no_children(self): """ Test that a twine with no children will validate on an empty children input """ - twine = Twine() # Creates empty twine - twine.validate_children(source="[]") + Twine().validate_children(source="[]") def test_missing_children(self): """ Test that a twine with children will not validate on an empty children input @@ -61,16 +60,14 @@ def test_missing_children(self): def test_extra_children(self): """ Test that a twine with no children will not validate a non-empty children input """ - twine = Twine() # Creates empty twine with self.assertRaises(exceptions.InvalidValuesContents): - twine.validate_children(source=os.path.join(self.path, "children", "valid.json")) + Twine().validate_children(source=os.path.join(self.path, "children", "valid.json")) def test_extra_key(self): """ Test that children with extra data will not raise validation error """ - twine = Twine() # Creates empty twine with self.assertRaises(exceptions.InvalidValuesContents): - twine.validate_children(source=os.path.join(self.path, "children", "extra_key.json")) + Twine().validate_children(source=os.path.join(self.path, "children", "extra_key.json")) def test_extra_property(self): """ Test that children with extra data will not raise validation error @@ -82,9 +79,8 @@ def test_extra_property(self): def test_invalid_env_name(self): """ Test that a child uri env name not in ALL_CAPS_SNAKE_CASE doesn't validate """ - twine = Twine() # Creates empty twine with self.assertRaises(exceptions.InvalidValuesContents): - twine.validate_children(source=os.path.join(self.path, "children", "invalid_env_name.json")) + Twine().validate_children(source=os.path.join(self.path, "children", "invalid_env_name.json")) def test_invalid_json(self): """ Tests that a children entry with invalid json will raise an error diff --git a/tests/test_twine.py b/tests/test_twine.py index f29338f..6264f05 100644 --- a/tests/test_twine.py +++ b/tests/test_twine.py @@ -7,20 +7,18 @@ class TestTwine(BaseTestCase): """ Testing operation of the Twine class - """ + """ def test_init_twine_with_filename(self): """ Ensures that the twine class can be instantiated with a file """ - twine_file = os.path.join(self.path, "apps", "simple_app", "twine.json") - Twine(source=twine_file) + Twine(source=os.path.join(self.path, "apps", "simple_app", "twine.json")) def test_init_twine_with_json(self): """ Ensures that a twine can be instantiated with a json string """ with open(os.path.join(self.path, "apps", "simple_app", "twine.json"), "r", encoding="utf-8") as f: - json_string = f.read() - Twine(source=json_string) + Twine(source=f.read()) def test_no_twine(self): """ Tests that the canonical-but-useless case of no twine provided validates empty @@ -30,9 +28,8 @@ def test_no_twine(self): def test_incorrect_version_twine(self): """ Ensures exception is thrown on mismatch between installed and specified versions of twined """ - twine_file = os.path.join(self.path, "twines", "incorrect_version_twine.json") with self.assertRaises(exceptions.TwineVersionConflict): - Twine(source=twine_file) + Twine(source=os.path.join(self.path, "twines", "incorrect_version_twine.json")) def test_empty_twine(self): """ Ensures that an empty twine file can be loaded @@ -47,21 +44,18 @@ def test_empty_twine(self): def test_example_twine(self): """ Ensures that the example (full) twine can be loaded and validated """ - twine_file = os.path.join(self.path, "apps", "example_app", "twine.json") - Twine(source=twine_file) + Twine(source=os.path.join(self.path, "apps", "example_app", "twine.json")) def test_simple_twine(self): """ Ensures that the simple app schema can be loaded and used to parse some basic config and values data """ - twine_file = os.path.join(self.path, "apps", "simple_app", "twine.json") - Twine(source=twine_file) + Twine(source=os.path.join(self.path, "apps", "simple_app", "twine.json")) def test_broken_json_twine(self): """ Ensures that an invalid json file raises an InvalidTwine exception """ - twine_file = os.path.join(self.path, "twines", "invalid_json_twine.json") with self.assertRaises(exceptions.InvalidTwineJson): - Twine(source=twine_file) + Twine(source=os.path.join(self.path, "twines", "invalid_json_twine.json")) if __name__ == "__main__": From de4544ea6bcfb8100451ac990ea7abda0f0b49c4 Mon Sep 17 00:00:00 2001 From: cortadocodes Date: Sat, 10 Oct 2020 16:32:21 +0100 Subject: [PATCH 5/8] REF: Allow objects in Twine; bring test cases into tests --- tests/data/children/extra_key.json | 13 ---- tests/data/children/extra_property.json | 8 -- tests/data/children/invalid_env_name.json | 7 -- tests/data/children/valid.json | 7 -- ...invalid_children_dict_not_array_twine.json | 3 - .../twines/invalid_children_no_key_twine.json | 9 --- tests/data/twines/valid_children_twine.json | 10 --- .../twines/valid_empty_children_twine.json | 4 - tests/test_children.py | 76 +++++++++++++------ twined/twine.py | 2 +- 10 files changed, 54 insertions(+), 85 deletions(-) delete mode 100644 tests/data/children/extra_key.json delete mode 100644 tests/data/children/extra_property.json delete mode 100644 tests/data/children/invalid_env_name.json delete mode 100644 tests/data/children/valid.json delete mode 100644 tests/data/twines/invalid_children_dict_not_array_twine.json delete mode 100644 tests/data/twines/invalid_children_no_key_twine.json delete mode 100644 tests/data/twines/valid_children_twine.json delete mode 100644 tests/data/twines/valid_empty_children_twine.json diff --git a/tests/data/children/extra_key.json b/tests/data/children/extra_key.json deleted file mode 100644 index 1a14448..0000000 --- a/tests/data/children/extra_key.json +++ /dev/null @@ -1,13 +0,0 @@ -[ - { - "key": "gis", - "id": "some-id", - "uri_env_name": "SOME_ENV_VAR_NAME", - "some_extra_key": "should not be a problem if present" - }, - { - "key": "some_weird_other_child", - "id": "some-other-id", - "uri_env_name": "SOME_ENV_VAR_NAME" - } -] diff --git a/tests/data/children/extra_property.json b/tests/data/children/extra_property.json deleted file mode 100644 index 83afc66..0000000 --- a/tests/data/children/extra_property.json +++ /dev/null @@ -1,8 +0,0 @@ -[ - { - "key": "gis", - "id": "some-id", - "uri_env_name": "SOME_ENV_VAR_NAME", - "some_extra_property": "should not be a problem if present" - } -] diff --git a/tests/data/children/invalid_env_name.json b/tests/data/children/invalid_env_name.json deleted file mode 100644 index 69391f9..0000000 --- a/tests/data/children/invalid_env_name.json +++ /dev/null @@ -1,7 +0,0 @@ -[ - { - "key": "gis", - "id": "some-id", - "uri_env_name": "an environment variable which isnt in CAPS_CASE is invalid per the credentials spec" - } -] diff --git a/tests/data/children/valid.json b/tests/data/children/valid.json deleted file mode 100644 index 37fed88..0000000 --- a/tests/data/children/valid.json +++ /dev/null @@ -1,7 +0,0 @@ -[ - { - "key": "gis", - "id": "some-id", - "uri_env_name": "NAME_OF_SOME_ENV_VAR_THAT_CONTAINS_A_URI" - } -] diff --git a/tests/data/twines/invalid_children_dict_not_array_twine.json b/tests/data/twines/invalid_children_dict_not_array_twine.json deleted file mode 100644 index 66245b2..0000000 --- a/tests/data/twines/invalid_children_dict_not_array_twine.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "children": {} -} diff --git a/tests/data/twines/invalid_children_no_key_twine.json b/tests/data/twines/invalid_children_no_key_twine.json deleted file mode 100644 index 1515fd3..0000000 --- a/tests/data/twines/invalid_children_no_key_twine.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "children": [ - { - "purpose": "Something to do with GIS data.", - "notes": "This filter (which can use the extremely powerful 'lucene' query syntax)\n allows the digital twin to locate other digital twins (public across octue or\n private in your workspace) which can provide the data you need.", - "filters": "tags:gis" - } - ] -} diff --git a/tests/data/twines/valid_children_twine.json b/tests/data/twines/valid_children_twine.json deleted file mode 100644 index 2293ec6..0000000 --- a/tests/data/twines/valid_children_twine.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "children": [ - { - "key": "gis", - "purpose": "Something to do with GIS data.", - "notes": "Some internal note about how the filters work or similar.", - "filters": "tags:gis" - } - ] -} diff --git a/tests/data/twines/valid_empty_children_twine.json b/tests/data/twines/valid_empty_children_twine.json deleted file mode 100644 index 266797e..0000000 --- a/tests/data/twines/valid_empty_children_twine.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "children": [ - ] -} diff --git a/tests/test_children.py b/tests/test_children.py index af694a8..d1be1c7 100644 --- a/tests/test_children.py +++ b/tests/test_children.py @@ -1,4 +1,3 @@ -import os import unittest from twined import Twine, exceptions @@ -14,30 +13,33 @@ def test_invalid_children_dict_not_array(self): """ Ensures InvalidTwine exceptions are raised when instantiating twines where `children` entry is incorrectly specified as a dict, not an array """ - twine_file = os.path.join(self.path, "twines", "invalid_children_dict_not_array_twine.json") with self.assertRaises(exceptions.InvalidTwine): - Twine(source=twine_file) + Twine(source={"children": {}}) def test_invalid_children_no_key(self): """ Ensures InvalidTwine exceptions are raised when instantiating twines where a child is specified without the required `key` field """ - twine_file = os.path.join(self.path, "twines", "invalid_children_no_key_twine.json") + source = { + "children": [{"purpose": "The purpose.", "notes": "Here are some notes.", "filters": "tags:gis"}] + } + with self.assertRaises(exceptions.InvalidTwine): - Twine(source=twine_file) + Twine(source=source) def test_valid_children(self): - """ Ensures that a twine can be instantiated with correctly specified children + """ Ensures that a twine with one child can be instantiated correctly. """ - twine_file = os.path.join(self.path, "twines", "valid_children_twine.json") - twine = Twine(source=twine_file) - self.assertEqual(len(twine._raw["children"]), 1) + source = { + "children": [{"key": "gis", "purpose": "The purpose.", "notes": "Some notes.", "filters": "tags:gis"}] + } + + self.assertEqual(len(Twine(source=source)._raw["children"]), 1) def test_empty_children(self): """ Ensures that a twine file will validate with an empty list object as children """ - twine_file = os.path.join(self.path, "twines", "valid_empty_children_twine.json") - twine = Twine(source=twine_file) + twine = Twine(source={"children": []}) self.assertEqual(len(twine._raw["children"]), 0) @@ -45,57 +47,85 @@ class TestChildrenValidation(BaseTestCase): """ Tests related to whether validation of children occurs successfully (given a valid twine) """ + VALID_TWINE_WITH_CHILDREN = { + "children": [{"key": "gis", "purpose": "The purpose", "notes": "Some notes.", "filters": "tags:gis"}] + } + + VALID_CHILDREN_VALUE = [ + {"key": "gis", "id": "some-id", "uri_env_name": "NAME_OF_SOME_ENV_VAR_THAT_CONTAINS_A_URI"} + ] + def test_no_children(self): """ Test that a twine with no children will validate on an empty children input """ - Twine().validate_children(source="[]") + Twine().validate_children(source=[]) def test_missing_children(self): """ Test that a twine with children will not validate on an empty children input """ - twine = Twine(source=os.path.join(self.path, "twines", "valid_children_twine.json")) with self.assertRaises(exceptions.InvalidValuesContents): - twine.validate_children(source="[]") + Twine(source=self.VALID_TWINE_WITH_CHILDREN).validate_children(source=[]) def test_extra_children(self): """ Test that a twine with no children will not validate a non-empty children input """ with self.assertRaises(exceptions.InvalidValuesContents): - Twine().validate_children(source=os.path.join(self.path, "children", "valid.json")) + Twine().validate_children(source=self.VALID_CHILDREN_VALUE) def test_extra_key(self): """ Test that children with extra data will not raise validation error """ + children_values_with_extra_data = [ + {"key": "gis", "id": "id", "uri_env_name": "VAR_NAME", "an_extra_key": "shouldn't be a problem if present"}, + {"key": "some_weird_other_child", "id": "some-other-id", "uri_env_name": "SOME_ENV_VAR_NAME"} + ] + with self.assertRaises(exceptions.InvalidValuesContents): - Twine().validate_children(source=os.path.join(self.path, "children", "extra_key.json")) + Twine().validate_children(source=children_values_with_extra_data) def test_extra_property(self): """ Test that children with extra data will not raise validation error # TODO review this behaviour - possibly should raise an error but allow for a user specified extra_data property """ - twine = Twine(source=os.path.join(self.path, "twines", "valid_children_twine.json")) - twine.validate_children(source=os.path.join(self.path, "children", "extra_property.json")) + single_child_with_extra_data = [ + { + "key": "gis", + "id": "some-id", + "uri_env_name": "SOME_ENV_VAR_NAME", + "some_extra_property": "should not be a problem if present" + } + ] + + twine = Twine(source=self.VALID_TWINE_WITH_CHILDREN) + twine.validate_children(source=single_child_with_extra_data) def test_invalid_env_name(self): """ Test that a child uri env name not in ALL_CAPS_SNAKE_CASE doesn't validate """ + child_with_invalid_environment_variable_name = [ + { + "key": "gis", + "id": "some-id", + "uri_env_name": "an environment variable which isn't in CAPS_CASE is invalid per the credentials spec" + } + ] + with self.assertRaises(exceptions.InvalidValuesContents): - Twine().validate_children(source=os.path.join(self.path, "children", "invalid_env_name.json")) + Twine().validate_children(source=child_with_invalid_environment_variable_name) def test_invalid_json(self): """ Tests that a children entry with invalid json will raise an error """ - twine = Twine(source=os.path.join(self.path, "twines", "valid_children_twine.json")) with self.assertRaises(exceptions.InvalidValuesJson): - twine.validate_children(source="[") + Twine(source=self.VALID_TWINE_WITH_CHILDREN).validate_children(source="[") def test_valid(self): """ Test that a valid twine will validate valid children Valiantly and Validly validating validity since 1983. To those reading this, know that YOU'RE valid. """ - twine = Twine(source=os.path.join(self.path, "twines", "valid_children_twine.json")) - twine.validate_children(source=os.path.join(self.path, "children", "valid.json")) + twine = Twine(source=self.VALID_TWINE_WITH_CHILDREN) + twine.validate_children(source=self.VALID_CHILDREN_VALUE) if __name__ == "__main__": diff --git a/twined/twine.py b/twined/twine.py index d0e4f87..02baa30 100644 --- a/twined/twine.py +++ b/twined/twine.py @@ -66,7 +66,7 @@ def _load_twine(self, source=None): self._raw = {} logger.warning("No twine source specified. Loading empty twine.") else: - self._raw = self._load_json("twine", source, allowed_kinds=("file-like", "filename", "string")) + self._raw = self._load_json("twine", source, allowed_kinds=("file-like", "filename", "string", "object")) self._validate_against_schema("twine", self._raw) self._validate_twine_version() From 70ef9a5ff01d2cece70366046922d1052cc1bfcb Mon Sep 17 00:00:00 2001 From: cortadocodes Date: Sat, 10 Oct 2020 18:25:30 +0100 Subject: [PATCH 6/8] DOC: Add more information to test docsrtrings --- tests/test_children.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_children.py b/tests/test_children.py index d1be1c7..494f2b5 100644 --- a/tests/test_children.py +++ b/tests/test_children.py @@ -72,8 +72,8 @@ def test_extra_children(self): with self.assertRaises(exceptions.InvalidValuesContents): Twine().validate_children(source=self.VALID_CHILDREN_VALUE) - def test_extra_key(self): - """ Test that children with extra data will not raise validation error + def test_extra_key_validation_on_empty_twine(self): + """ Test that children with extra data will not raise a validation error on an empty twine. """ children_values_with_extra_data = [ {"key": "gis", "id": "id", "uri_env_name": "VAR_NAME", "an_extra_key": "shouldn't be a problem if present"}, @@ -83,8 +83,8 @@ def test_extra_key(self): with self.assertRaises(exceptions.InvalidValuesContents): Twine().validate_children(source=children_values_with_extra_data) - def test_extra_property(self): - """ Test that children with extra data will not raise validation error + def test_extra_key_validation_on_valid_twine(self): + """ Test that children with extra data will not raise a validation error on a non-empty valid twine. # TODO review this behaviour - possibly should raise an error but allow for a user specified extra_data property """ single_child_with_extra_data = [ From b9bf45f5d3e76327f9e60a38ed7e5a484b94fe20 Mon Sep 17 00:00:00 2001 From: cortadocodes Date: Mon, 12 Oct 2020 13:25:36 +0100 Subject: [PATCH 7/8] REV: Make BaseTestCase path an instance attribute again --- tests/base.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/base.py b/tests/base.py index b95a1d5..8018d49 100644 --- a/tests/base.py +++ b/tests/base.py @@ -6,4 +6,7 @@ class BaseTestCase(unittest.TestCase): """ Base test case for twined: - sets a path to the test data directory """ - path = os.path.join(os.path.dirname(__file__), "data") + + def setUp(self): + self.path = os.path.join(os.path.dirname(__file__), "data") + super().setUp() From abb2fe9252cb31c65c1fe809b5fbcabbd67e9d77 Mon Sep 17 00:00:00 2001 From: cortadocodes Date: Mon, 12 Oct 2020 14:11:31 +0100 Subject: [PATCH 8/8] REF: Make test cases strings rather than objects --- tests/test_children.py | 84 ++++++++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 35 deletions(-) diff --git a/tests/test_children.py b/tests/test_children.py index 494f2b5..3b3eb23 100644 --- a/tests/test_children.py +++ b/tests/test_children.py @@ -14,15 +14,17 @@ def test_invalid_children_dict_not_array(self): specified as a dict, not an array """ with self.assertRaises(exceptions.InvalidTwine): - Twine(source={"children": {}}) + Twine(source="""{"children": {}}""") def test_invalid_children_no_key(self): """ Ensures InvalidTwine exceptions are raised when instantiating twines where a child is specified without the required `key` field """ - source = { - "children": [{"purpose": "The purpose.", "notes": "Here are some notes.", "filters": "tags:gis"}] - } + source = """ + { + "children": [{"purpose": "The purpose.", "notes": "Here are some notes.", "filters": "tags:gis"}] + } + """ with self.assertRaises(exceptions.InvalidTwine): Twine(source=source) @@ -30,16 +32,18 @@ def test_invalid_children_no_key(self): def test_valid_children(self): """ Ensures that a twine with one child can be instantiated correctly. """ - source = { - "children": [{"key": "gis", "purpose": "The purpose.", "notes": "Some notes.", "filters": "tags:gis"}] - } + source = """ + { + "children": [{"key": "gis", "purpose": "The purpose.", "notes": "Some notes.", "filters": "tags:gis"}] + } + """ self.assertEqual(len(Twine(source=source)._raw["children"]), 1) def test_empty_children(self): """ Ensures that a twine file will validate with an empty list object as children """ - twine = Twine(source={"children": []}) + twine = Twine(source="""{"children": []}""") self.assertEqual(len(twine._raw["children"]), 0) @@ -47,13 +51,17 @@ class TestChildrenValidation(BaseTestCase): """ Tests related to whether validation of children occurs successfully (given a valid twine) """ - VALID_TWINE_WITH_CHILDREN = { - "children": [{"key": "gis", "purpose": "The purpose", "notes": "Some notes.", "filters": "tags:gis"}] - } + VALID_TWINE_WITH_CHILDREN = """ + { + "children": [{"key": "gis", "purpose": "The purpose", "notes": "Some notes.", "filters": "tags:gis"}] + } + """ - VALID_CHILDREN_VALUE = [ - {"key": "gis", "id": "some-id", "uri_env_name": "NAME_OF_SOME_ENV_VAR_THAT_CONTAINS_A_URI"} - ] + VALID_CHILD_VALUE = """ + [ + {"key": "gis", "id": "some-id", "uri_env_name": "NAME_OF_SOME_ENV_VAR_THAT_CONTAINS_A_URI"} + ] + """ def test_no_children(self): """ Test that a twine with no children will validate on an empty children input @@ -70,15 +78,17 @@ def test_extra_children(self): """ Test that a twine with no children will not validate a non-empty children input """ with self.assertRaises(exceptions.InvalidValuesContents): - Twine().validate_children(source=self.VALID_CHILDREN_VALUE) + Twine().validate_children(source=self.VALID_CHILD_VALUE) def test_extra_key_validation_on_empty_twine(self): """ Test that children with extra data will not raise a validation error on an empty twine. """ - children_values_with_extra_data = [ - {"key": "gis", "id": "id", "uri_env_name": "VAR_NAME", "an_extra_key": "shouldn't be a problem if present"}, - {"key": "some_weird_other_child", "id": "some-other-id", "uri_env_name": "SOME_ENV_VAR_NAME"} - ] + children_values_with_extra_data = """ + [ + {"key": "gis", "id": "id", "uri_env_name": "VAR_NAME", "an_extra_key": "not a problem if present"}, + {"key": "some_weird_other_child", "id": "some-other-id", "uri_env_name": "SOME_ENV_VAR_NAME"} + ] + """ with self.assertRaises(exceptions.InvalidValuesContents): Twine().validate_children(source=children_values_with_extra_data) @@ -87,14 +97,16 @@ def test_extra_key_validation_on_valid_twine(self): """ Test that children with extra data will not raise a validation error on a non-empty valid twine. # TODO review this behaviour - possibly should raise an error but allow for a user specified extra_data property """ - single_child_with_extra_data = [ - { - "key": "gis", - "id": "some-id", - "uri_env_name": "SOME_ENV_VAR_NAME", - "some_extra_property": "should not be a problem if present" - } - ] + single_child_with_extra_data = """ + [ + { + "key": "gis", + "id": "some-id", + "uri_env_name": "SOME_ENV_VAR_NAME", + "some_extra_property": "should not be a problem if present" + } + ] + """ twine = Twine(source=self.VALID_TWINE_WITH_CHILDREN) twine.validate_children(source=single_child_with_extra_data) @@ -102,13 +114,15 @@ def test_extra_key_validation_on_valid_twine(self): def test_invalid_env_name(self): """ Test that a child uri env name not in ALL_CAPS_SNAKE_CASE doesn't validate """ - child_with_invalid_environment_variable_name = [ - { - "key": "gis", - "id": "some-id", - "uri_env_name": "an environment variable which isn't in CAPS_CASE is invalid per the credentials spec" - } - ] + child_with_invalid_environment_variable_name = """ + [ + { + "key": "gis", + "id": "some-id", + "uri_env_name": "an environment variable not in CAPS_CASE is invalid per the credentials spec" + } + ] + """ with self.assertRaises(exceptions.InvalidValuesContents): Twine().validate_children(source=child_with_invalid_environment_variable_name) @@ -125,7 +139,7 @@ def test_valid(self): To those reading this, know that YOU'RE valid. """ twine = Twine(source=self.VALID_TWINE_WITH_CHILDREN) - twine.validate_children(source=self.VALID_CHILDREN_VALUE) + twine.validate_children(source=self.VALID_CHILD_VALUE) if __name__ == "__main__":