From 6161c649215f29ffe3223b6c3840e0e566832a38 Mon Sep 17 00:00:00 2001 From: "Kevin W. Beam" Date: Wed, 27 Nov 2024 14:35:52 -0700 Subject: [PATCH] Applies ruff (black) code formatting rules --- tests/test_metgen.py | 124 ++++++++++++++++++++++++++----------------- 1 file changed, 74 insertions(+), 50 deletions(-) diff --git a/tests/test_metgen.py b/tests/test_metgen.py index c73f045..21689a7 100644 --- a/tests/test_metgen.py +++ b/tests/test_metgen.py @@ -15,107 +15,125 @@ # metgen functions call them with the correct parameters, correctly handle # their return values, and handle any exceptions they may throw. + @pytest.fixture def granule_metadata_list(): return { - 'first_id': { - 'size_in_bytes': 100, - 'production_date_time': 'then', - 'temporal': 'now', - 'geometry': 'big' + "first_id": { + "size_in_bytes": 100, + "production_date_time": "then", + "temporal": "now", + "geometry": "big", + }, + "second_id": { + "size_in_bytes": 200, + "production_date_time": "before", + "temporal": "after", + "geometry": "small", }, - 'second_id': { - 'size_in_bytes': 200, - 'production_date_time': 'before', - 'temporal': 'after', - 'geometry': 'small' - } } + @pytest.fixture def one_granule_metadata(): return { - 'first_id': { - 'size_in_bytes': 150, - 'production_date_time': 'then', - 'temporal': 'now', - 'geometry': 'big' + "first_id": { + "size_in_bytes": 150, + "production_date_time": "then", + "temporal": "now", + "geometry": "big", } } + @pytest.fixture def fake_config(): - return config.Config('uat', 'data', 'auth_id', 'version', - 'foobar', 'output', 'ummg', 'stream', - 'bucket', True, True, 'sha', 3) + return config.Config( + "uat", + "data", + "auth_id", + "version", + "foobar", + "output", + "ummg", + "stream", + "bucket", + True, + True, + "sha", + 3, + ) + def test_banner(): assert len(metgen.banner()) > 0 + def test_gets_single_file_size(one_granule_metadata): summary = metgen.metadata_summary(one_granule_metadata) - assert summary['size_in_bytes'] == 150 + assert summary["size_in_bytes"] == 150 + def test_sums_multiple_file_sizes(granule_metadata_list): summary = metgen.metadata_summary(granule_metadata_list) - assert summary['size_in_bytes'] == 300 + assert summary["size_in_bytes"] == 300 + def test_uses_first_file_as_default(granule_metadata_list): summary = metgen.metadata_summary(granule_metadata_list) - assert summary['production_date_time'] == 'then' - assert summary['temporal'] == 'now' - assert summary['geometry'] == 'big' + assert summary["production_date_time"] == "then" + assert summary["temporal"] == "now" + assert summary["geometry"] == "big" + def test_returns_only_gpolygon(): - result = metgen.populate_spatial({'points': 'some list of points'}) + result = metgen.populate_spatial({"points": "some list of points"}) assert "GPolygons" in result + def test_returns_single_datetime(): result = metgen.populate_temporal([123]) assert '"SingleDateTime": "123"' in result + def test_returns_datetime_range(): result = metgen.populate_temporal([123, 456]) - assert 'RangeDateTime' in result + assert "RangeDateTime" in result assert '"BeginningDateTime": "123"' in result assert '"EndingDateTime": "456"' in result + def test_s3_object_path_has_no_leading_slash(): - granule = metgen.Granule( - 'foo', - metgen.Collection('ABCD', 2), - uuid='abcd-1234' - ) - expected = 'external/ABCD/2/abcd-1234/xyzzy.bin' - assert metgen.s3_object_path(granule, 'xyzzy.bin') == expected + granule = metgen.Granule("foo", metgen.Collection("ABCD", 2), uuid="abcd-1234") + expected = "external/ABCD/2/abcd-1234/xyzzy.bin" + assert metgen.s3_object_path(granule, "xyzzy.bin") == expected + def test_s3_url_simple_case(): - staging_bucket_name = 'xyzzy-bucket' - granule = metgen.Granule( - 'foo', - metgen.Collection('ABCD', 2), - uuid='abcd-1234' - ) - expected = 's3://xyzzy-bucket/external/ABCD/2/abcd-1234/xyzzy.bin' - assert metgen.s3_url(staging_bucket_name, granule, 'xyzzy.bin') == expected + staging_bucket_name = "xyzzy-bucket" + granule = metgen.Granule("foo", metgen.Collection("ABCD", 2), uuid="abcd-1234") + expected = "s3://xyzzy-bucket/external/ABCD/2/abcd-1234/xyzzy.bin" + assert metgen.s3_url(staging_bucket_name, granule, "xyzzy.bin") == expected + @patch("nsidc.metgen.metgen.dt.datetime") def test_start_ledger(mock_datetime): now = dt.datetime(2099, 7, 4, 10, 11, 12) mock_datetime.now.return_value = now - granule = metgen.Granule('abcd-1234') + granule = metgen.Granule("abcd-1234") actual = metgen.start_ledger(granule) assert actual.granule == granule assert actual.startDatetime == now + @patch("nsidc.metgen.metgen.dt.datetime") def test_end_ledger(mock_datetime): now = dt.datetime(2099, 7, 4, 10, 11, 12) mock_datetime.now.return_value = now - granule = metgen.Granule('abcd-1234') - ledger = metgen.Ledger(granule, [metgen.Action('foo', True, '')], startDatetime = now) + granule = metgen.Granule("abcd-1234") + ledger = metgen.Ledger(granule, [metgen.Action("foo", True, "")], startDatetime=now) actual = metgen.end_ledger(ledger) @@ -124,14 +142,17 @@ def test_end_ledger(mock_datetime): assert actual.startDatetime == now assert actual.endDatetime == now + @patch("nsidc.metgen.metgen.dt.datetime") def test_end_ledger_with_unsuccessful_actions(mock_datetime): now = dt.datetime(2099, 7, 4, 10, 11, 12) mock_datetime.now.return_value = now - granule = metgen.Granule('abcd-1234') - ledger = metgen.Ledger(granule, - [metgen.Action('foo', False, ''), metgen.Action('bar', False, 'Oops')], - startDatetime = now) + granule = metgen.Granule("abcd-1234") + ledger = metgen.Ledger( + granule, + [metgen.Action("foo", False, ""), metgen.Action("bar", False, "Oops")], + startDatetime=now, + ) actual = metgen.end_ledger(ledger) @@ -140,8 +161,9 @@ def test_end_ledger_with_unsuccessful_actions(mock_datetime): assert actual.startDatetime == now assert actual.endDatetime == now + def test_recorder(): - granule = metgen.Granule('abcd-1234') + granule = metgen.Granule("abcd-1234") ledger = metgen.start_ledger(granule) new_ledger = partial(metgen.recorder, identity)(ledger) @@ -149,9 +171,11 @@ def test_recorder(): assert new_ledger.granule == ledger.granule assert len(new_ledger.actions) == 1 + def test_recorder_with_failing_operation(): - granule = metgen.Granule('abcd-1234') + granule = metgen.Granule("abcd-1234") ledger = metgen.start_ledger(granule) + def failing_op(): raise Exception()