Skip to content

Commit

Permalink
unit test on config_file.py
Browse files Browse the repository at this point in the history
Signed-off-by: pem70 <[email protected]>
  • Loading branch information
pem70 committed May 28, 2024
1 parent c7060a7 commit c99b0b0
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 17 deletions.
22 changes: 6 additions & 16 deletions src/core/zowe/core_for_zowe_sdk/config_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,6 @@ def filepath(self) -> Optional[str]:
def location(self) -> Optional[str]:
return self._location

@property
def schema_path(self) -> Optional[str]:
return self.schema_property

@location.setter
def location(self, dirname: str) -> None:
if os.path.isdir(dirname):
Expand Down Expand Up @@ -137,7 +133,7 @@ def init_from_file(
self.defaults = profile_jsonc.get("defaults", {})
self.jsonc = profile_jsonc

if self.schema_property and validate_schema:
if validate_schema:
self.validate_schema()

CredentialManager.load_secure_props()
Expand All @@ -151,17 +147,11 @@ def validate_schema(self) -> None:
-------
file_path to the $schema property
"""

path_schema_json = None

path_schema_json = self.schema_path
if path_schema_json is None: # check if the $schema property is not defined
if self.schema_property is None: # check if the $schema property is not defined
self.__logger.warning(f"Could not find $schema property")
warnings.warn(f"Could not find $schema property")

# validate the $schema property
if path_schema_json:
validate_config_json(self.jsonc, path_schema_json, cwd=self.location)
else:
validate_config_json(self.jsonc, self.schema_property, cwd=self.location)

def schema_list(self, cwd=None) -> list:
"""
Expand Down Expand Up @@ -277,9 +267,9 @@ def get_profilename_from_profiletype(self, profile_type: str) -> str:
try:
profilename = self.defaults[profile_type]
except KeyError:
self.__logger.warn(f"Given profile type '{profile_type}' has no default profilename")
self.__logger.warn(f"Given profile type '{profile_type}' has no default profile name")
warnings.warn(
f"Given profile type '{profile_type}' has no default profilename",
f"Given profile type '{profile_type}' has no default profile name",
ProfileParsingWarning,
)
else:
Expand Down
53 changes: 52 additions & 1 deletion tests/unit/test_zowe_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,60 @@ def test_profile_loading_exception(self, get_pass_func, mock_logger_warning: moc
shutil.copy(self.original_file_path, cwd_up_file_path)

# Test
self.setUpCreds(cwd_up_file_path, secure_props={})
config_file = ConfigFile(name=self.custom_appname, type="team_config")
props: dict = config_file.get_profile(profile_name="non_existent_profile", validate_schema=False)
mock_logger_warning.assert_called()
self.assertEqual(mock_logger_warning.call_args[0][0], "Profile non_existent_profile not found")

@mock.patch("logging.Logger.error")
@mock.patch("zowe.secrets_for_zowe_sdk.keyring.get_password", side_effect=keyring_get_password)
def test_profile_empty_exception(self, get_pass_func, mock_logger_error: mock.MagicMock):
"""
Test correct exceptions are being thrown when a profile is
not found.
Filename and Filetype will be set to None.
"""
with self.assertRaises(exceptions.ProfileNotFound):
# Setup
cwd_up_dir_path = os.path.dirname(CWD)
cwd_up_file_path = os.path.join(cwd_up_dir_path, f"{self.custom_appname}.config.json")
shutil.copy(self.original_file_path, cwd_up_file_path)

# Test
self.setUpCreds(cwd_up_file_path, secure_props={})
config_file = ConfigFile(name=self.custom_appname, type="team_config")
props: dict = config_file.get_profile(profile_name=None,profile_type=None,validate_schema=False)
self.assertEqual(mock_logger_error.call_args[0][0], "Failed to load profile 'None' because Could not find profile as both profile_name and profile_type is not set")

Check warning on line 461 in tests/unit/test_zowe_core.py

View check run for this annotation

Codecov / codecov/patch

tests/unit/test_zowe_core.py#L461

Added line #L461 was not covered by tests

@mock.patch("logging.Logger.error")
@mock.patch("logging.Logger.warning")
@mock.patch("zowe.secrets_for_zowe_sdk.keyring.get_password", side_effect=keyring_get_password)
def test_get_profilename_from_profiletype_invalid_profile_type(self, get_pass_func, mock_logger_warning: mock.MagicMock, mock_logger_error: mock.MagicMock):
"""
Test correct warnings and exceptions are being thrown with
empty default, invalid profile type.
"""
with self.assertRaises(exceptions.ProfileNotFound):
config_file = ConfigFile(name=self.custom_appname, type="team_config", defaults={}, profiles={'a': {'type' : 'none'}})
config_file.get_profilename_from_profiletype('test')
self.assertEqual(mock_logger_warning.call_args[0][0], "Given profile type 'test' has no default profile name")
self.assertEqual(mock_logger_warning.call_args[1][0], "Profile 'a' has no type attribute")
self.assertEqual(mock_logger_error.call_args[0][0], "No profile with matching profile_type 'test' found")

Check warning on line 477 in tests/unit/test_zowe_core.py

View check run for this annotation

Codecov / codecov/patch

tests/unit/test_zowe_core.py#L475-L477

Added lines #L475 - L477 were not covered by tests

@mock.patch("logging.Logger.warning")
@mock.patch("zowe.secrets_for_zowe_sdk.keyring.get_password", side_effect=keyring_get_password)
def test_validate_schema(self, get_pass_func, mock_logger_warning: mock.MagicMock):
"""
Test correct exceptions are being thrown when schema property is empty.
Schema property will be initialized to None.
"""
with self.assertWarns(UserWarning):
config_file = ConfigFile(name=self.custom_appname, type="team_config")
config_file.validate_schema()
self.assertEqual(mock_logger_warning.call_args[0][0], "Could not find $schema property")

@mock.patch("zowe.secrets_for_zowe_sdk.keyring.get_password", side_effect=keyring_get_password_exception)
def test_secure_props_loading_warning(self, get_pass_func):
Expand Down

0 comments on commit c99b0b0

Please sign in to comment.