Skip to content

Commit

Permalink
Applies ruff (black) code formatting rules
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenbeam committed Nov 27, 2024
1 parent 6161c64 commit 89ac839
Showing 1 changed file with 88 additions and 64 deletions.
152 changes: 88 additions & 64 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,149 +16,173 @@
# call them with the correct parameters, correctly handle their return values,
# and handle any exceptions they may throw.


@pytest.fixture
def expected_keys():
return set(['environment',
'data_dir',
'auth_id',
'version',
'provider',
'local_output_dir',
'ummg_dir',
'kinesis_stream_name',
'staging_bucket_name',
'write_cnm_file',
'overwrite_ummg',
'checksum_type',
'number'])
return set(
[
"environment",
"data_dir",
"auth_id",
"version",
"provider",
"local_output_dir",
"ummg_dir",
"kinesis_stream_name",
"staging_bucket_name",
"write_cnm_file",
"overwrite_ummg",
"checksum_type",
"number",
]
)


@pytest.fixture
def cfg_parser():
cp = ConfigParser(interpolation=ExtendedInterpolation())
cp['Source'] = {
'data_dir': '/data/example'
}
cp['Collection'] = {
'auth_id': 'DATA-0001',
'version': 42,
'provider': 'FOO'
}
cp['Destination'] = {
'local_output_dir': '/output/here',
'ummg_dir': 'ummg',
'kinesis_stream_name': "xyzzy-${environment}-stream",
'staging_bucket_name': "xyzzy-${environment}-bucket",
'write_cnm_file': False
}
cp['Settings'] = {
'checksum_type': 'SHA256',
'number': 1
cp["Source"] = {"data_dir": "/data/example"}
cp["Collection"] = {"auth_id": "DATA-0001", "version": 42, "provider": "FOO"}
cp["Destination"] = {
"local_output_dir": "/output/here",
"ummg_dir": "ummg",
"kinesis_stream_name": "xyzzy-${environment}-stream",
"staging_bucket_name": "xyzzy-${environment}-bucket",
"write_cnm_file": False,
}
cp["Settings"] = {"checksum_type": "SHA256", "number": 1}
return cp


def test_config_parser_without_filename():
with pytest.raises(ValueError):
config.config_parser_factory(None)

@patch('nsidc.metgen.metgen.os.path.exists', return_value = True)

@patch("nsidc.metgen.metgen.os.path.exists", return_value=True)
def test_config_parser_return_type(mock):
result = config.config_parser_factory('foo.ini')
result = config.config_parser_factory("foo.ini")
assert isinstance(result, ConfigParser)

@patch('nsidc.metgen.metgen.os.path.exists', return_value = True)

@patch("nsidc.metgen.metgen.os.path.exists", return_value=True)
def test_config_parser_handles_empty_strings_for_booleans(mock):
cp = config.config_parser_factory('foo.ini')
cp['foo'] = {
'success': ''
}
assert not cp.getboolean('foo', 'success')
cp = config.config_parser_factory("foo.ini")
cp["foo"] = {"success": ""}
assert not cp.getboolean("foo", "success")


def test_config_from_config_parser(cfg_parser):
cfg = config.configuration(cfg_parser, {}, constants.DEFAULT_CUMULUS_ENVIRONMENT)
assert isinstance(cfg, config.Config)


def test_config_with_no_write_cnm(cfg_parser, expected_keys):
cfg = config.configuration(cfg_parser, {}, constants.DEFAULT_CUMULUS_ENVIRONMENT)

config_keys = set(cfg.__dict__)
assert len(config_keys - expected_keys) == 0

assert cfg.environment == 'uat'
assert cfg.data_dir == '/data/example'
assert cfg.auth_id == 'DATA-0001'
assert cfg.kinesis_stream_name == 'xyzzy-uat-stream'
assert cfg.environment == "uat"
assert cfg.data_dir == "/data/example"
assert cfg.auth_id == "DATA-0001"
assert cfg.kinesis_stream_name == "xyzzy-uat-stream"
assert not cfg.write_cnm_file


def test_config_with_write_cnm(cfg_parser, expected_keys):
cfg_parser.set("Destination", "write_cnm_file", 'True')
cfg_parser.set("Destination", "write_cnm_file", "True")
cfg = config.configuration(cfg_parser, {})

config_keys = set(cfg.__dict__)
assert len(config_keys - expected_keys) == 0

assert cfg.data_dir == '/data/example'
assert cfg.auth_id == 'DATA-0001'
assert cfg.kinesis_stream_name == 'xyzzy-uat-stream'
assert cfg.environment == 'uat'
assert cfg.data_dir == "/data/example"
assert cfg.auth_id == "DATA-0001"
assert cfg.kinesis_stream_name == "xyzzy-uat-stream"
assert cfg.environment == "uat"
assert cfg.write_cnm_file


def test_config_with_no_overwrite_ummg(cfg_parser, expected_keys):
cfg = config.configuration(cfg_parser, {}, constants.DEFAULT_CUMULUS_ENVIRONMENT)

config_keys = set(cfg.__dict__)
assert len(config_keys - expected_keys) == 0
assert not cfg.overwrite_ummg


def test_config_with_overwrite_ummg(cfg_parser, expected_keys):
cfg_parser.set("Destination", "overwrite_ummg", 'True')
cfg_parser.set("Destination", "overwrite_ummg", "True")
cfg = config.configuration(cfg_parser, {})

config_keys = set(cfg.__dict__)
assert len(config_keys - expected_keys) == 0
assert cfg.overwrite_ummg


def test_get_configuration_value(cfg_parser):
environment = constants.DEFAULT_CUMULUS_ENVIRONMENT
result = config._get_configuration_value(environment, "Source", "data_dir", str, cfg_parser, {})
result = config._get_configuration_value(
environment, "Source", "data_dir", str, cfg_parser, {}
)
assert result == cfg_parser.get("Source", "data_dir")


def test_get_configuration_value_with_override(cfg_parser):
environment = constants.DEFAULT_CUMULUS_ENVIRONMENT
overrides = { 'data_dir': 'foobar' }
result = config._get_configuration_value(environment, "Source", "data_dir", str, cfg_parser, overrides)
assert result == overrides['data_dir']
overrides = {"data_dir": "foobar"}
result = config._get_configuration_value(
environment, "Source", "data_dir", str, cfg_parser, overrides
)
assert result == overrides["data_dir"]


def test_get_configuration_value_interpolates_the_environment(cfg_parser):
environment = constants.DEFAULT_CUMULUS_ENVIRONMENT
result = config._get_configuration_value(environment, "Destination", "kinesis_stream_name", str, cfg_parser, {})
result = config._get_configuration_value(
environment, "Destination", "kinesis_stream_name", str, cfg_parser, {}
)
assert result == "xyzzy-uat-stream"

@pytest.mark.parametrize("section,option,expected", [
("Destination", "kinesis_stream_name", f"nsidc-cumulus-{constants.DEFAULT_CUMULUS_ENVIRONMENT}-external_notification"),
("Destination", "staging_bucket_name", f"nsidc-cumulus-{constants.DEFAULT_CUMULUS_ENVIRONMENT}-ingest-staging"),

@pytest.mark.parametrize(
"section,option,expected",
[
(
"Destination",
"kinesis_stream_name",
f"nsidc-cumulus-{constants.DEFAULT_CUMULUS_ENVIRONMENT}-external_notification",
),
(
"Destination",
"staging_bucket_name",
f"nsidc-cumulus-{constants.DEFAULT_CUMULUS_ENVIRONMENT}-ingest-staging",
),
("Destination", "write_cnm_file", constants.DEFAULT_WRITE_CNM_FILE),
("Settings", "checksum_type", constants.DEFAULT_CHECKSUM_TYPE),
("Settings", "number", constants.DEFAULT_NUMBER),
])
],
)
def test_configuration_has_good_defaults(cfg_parser, section, option, expected):
cfg_parser.remove_option(section, option)
result = config.configuration(cfg_parser, {}, constants.DEFAULT_CUMULUS_ENVIRONMENT)
result_dict = dataclasses.asdict(result)
assert result_dict[option] == expected


@patch('nsidc.metgen.metgen.os.path.exists', return_value = True)
@patch('nsidc.metgen.metgen.aws.kinesis_stream_exists', return_value = True)
@patch('nsidc.metgen.metgen.aws.staging_bucket_exists', return_value = True)
@patch("nsidc.metgen.metgen.os.path.exists", return_value=True)
@patch("nsidc.metgen.metgen.aws.kinesis_stream_exists", return_value=True)
@patch("nsidc.metgen.metgen.aws.staging_bucket_exists", return_value=True)
def test_validate_with_valid_checks(m1, m2, m3, cfg_parser):
cfg = config.configuration(cfg_parser, {})
valid = config.validate(cfg)
assert valid

@patch('nsidc.metgen.metgen.os.path.exists', return_value = False)
@patch('nsidc.metgen.metgen.aws.kinesis_stream_exists', return_value = False)
@patch('nsidc.metgen.metgen.aws.staging_bucket_exists', return_value = False)

@patch("nsidc.metgen.metgen.os.path.exists", return_value=False)
@patch("nsidc.metgen.metgen.aws.kinesis_stream_exists", return_value=False)
@patch("nsidc.metgen.metgen.aws.staging_bucket_exists", return_value=False)
def test_validate_with_invalid_checks(m1, m2, m3, cfg_parser):
cfg = config.configuration(cfg_parser, {})
with pytest.raises(config.ValidationError) as exc_info:
Expand Down

0 comments on commit 89ac839

Please sign in to comment.