Skip to content

Commit 748d419

Browse files
refactor: improve code formatting and readability in workflow and test scripts
1 parent 1361458 commit 748d419

File tree

4 files changed

+43
-24
lines changed

4 files changed

+43
-24
lines changed

scripts/test_complete_workflow.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ def s3_to_https(s3_url: str, gateway_url: str = "https://s3.explorer.eopf.copern
1919
return f"{gateway_base}/{bucket}{path}"
2020

2121

22-
def https_to_s3(https_url: str, gateway_url: str = "https://s3.explorer.eopf.copernicus.eu") -> str | None:
22+
def https_to_s3(
23+
https_url: str, gateway_url: str = "https://s3.explorer.eopf.copernicus.eu"
24+
) -> str | None:
2325
"""Convert https:// URL back to s3:// URL."""
2426
if not https_url.startswith("https://"):
2527
return None
@@ -121,18 +123,22 @@ def process_item_with_gateway(item_dict: dict, s3_endpoint: str) -> dict:
121123
if s3_url:
122124
new_href = s3_to_https(s3_url)
123125
link["href"] = new_href
124-
print(f" 🔄 Converted store link:")
126+
print(" 🔄 Converted store link:")
125127
print(f" From: {old_href}")
126128
print(f" To: {new_href}")
127129

128130
print(f"\n✅ Processed {processed_count} assets with gateway format and alternate S3 URLs")
129131
return item_dict
130132

131133

132-
def main():
134+
def main() -> int:
133135
"""Test the complete workflow."""
134136
# Read the sample JSON
135-
sample_file = Path(__file__).parent.parent / "stac" / "S2A_MSIL2A_20250831T103701_N0511_R008_T31TFL_20250831T145420.json"
137+
sample_file = (
138+
Path(__file__).parent.parent
139+
/ "stac"
140+
/ "S2A_MSIL2A_20250831T103701_N0511_R008_T31TFL_20250831T145420.json"
141+
)
136142

137143
if not sample_file.exists():
138144
print(f"❌ Sample file not found: {sample_file}")
@@ -156,7 +162,7 @@ def main():
156162
break
157163

158164
# Process with new gateway format
159-
print(f"\n🔧 Processing with new gateway format...\n")
165+
print("\n🔧 Processing with new gateway format...\n")
160166
s3_endpoint = "https://s3.de.io.cloud.ovh.net"
161167
modified_item = process_item_with_gateway(item_dict, s3_endpoint)
162168

scripts/test_gateway_format.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ def s3_to_https(s3_url: str, gateway_url: str = "https://s3.explorer.eopf.copern
1717
return f"{gateway_base}/{bucket}{path}"
1818

1919

20-
def https_to_s3(https_url: str, gateway_url: str = "https://s3.explorer.eopf.copernicus.eu") -> str | None:
20+
def https_to_s3(
21+
https_url: str, gateway_url: str = "https://s3.explorer.eopf.copernicus.eu"
22+
) -> str | None:
2123
"""Convert https:// URL back to s3:// URL."""
2224
if not https_url.startswith("https://"):
2325
return None
@@ -47,7 +49,7 @@ def https_to_s3(https_url: str, gateway_url: str = "https://s3.explorer.eopf.cop
4749
return None
4850

4951

50-
def main():
52+
def main() -> int:
5153
"""Test the gateway format conversions."""
5254
print("Testing S3 Gateway Format Conversions")
5355
print("=" * 70)
@@ -88,28 +90,30 @@ def main():
8890
print("-" * 70)
8991
for test in test_cases:
9092
https_url = test["expected_https"]
91-
result = https_to_s3(https_url)
92-
passed = result == test["s3"]
93+
s3_result = https_to_s3(https_url)
94+
passed = s3_result == test["s3"]
9395
all_passed = all_passed and passed
9496
status = "✅ PASS" if passed else "❌ FAIL"
9597
print(f"\n{status} - {test['name']}")
9698
print(f" Input: {https_url}")
9799
print(f" Expected: {test['s3']}")
98-
print(f" Got: {result}")
100+
print(f" Got: {s3_result}")
99101

100102
# Test backwards compatibility with old format
101103
print("\n\n3. Testing backwards compatibility with old S3 format")
102104
print("-" * 70)
103-
old_format_url = "https://esa-zarr-sentinel-explorer-fra.s3.de.io.cloud.ovh.net/tests-output/file.zarr"
105+
old_format_url = (
106+
"https://esa-zarr-sentinel-explorer-fra.s3.de.io.cloud.ovh.net/tests-output/file.zarr"
107+
)
104108
expected_s3 = "s3://esa-zarr-sentinel-explorer-fra/tests-output/file.zarr"
105-
result = https_to_s3(old_format_url)
106-
passed = result == expected_s3
109+
old_result = https_to_s3(old_format_url)
110+
passed = old_result == expected_s3
107111
all_passed = all_passed and passed
108112
status = "✅ PASS" if passed else "❌ FAIL"
109113
print(f"\n{status} - Old S3 subdomain format")
110114
print(f" Input: {old_format_url}")
111115
print(f" Expected: {expected_s3}")
112-
print(f" Got: {result}")
116+
print(f" Got: {old_result}")
113117

114118
# Summary
115119
print("\n" + "=" * 70)

stac/S2A_MSIL2A_20250831T103701_N0511_R008_T31TFL_20250831T145420_new_gateway.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,4 +600,4 @@
600600
"https://stac-extensions.github.io/alternate-assets/v1.2.0/schema.json",
601601
"https://stac-extensions.github.io/storage/v2.0.0/schema.json"
602602
]
603-
}
603+
}

tests/test_s3_gateway.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Unit tests for S3 gateway format and alternate asset extension."""
22

3-
import pytest
43
from pystac import Asset, Item
4+
55
from scripts.register_v1 import (
66
add_alternate_s3_assets,
77
https_to_s3,
@@ -75,7 +75,9 @@ def test_new_gateway_format_simple(self):
7575
def test_new_gateway_format_deep_path(self):
7676
"""Test conversion from new gateway format - deep nested path."""
7777
https_url = "https://s3.explorer.eopf.copernicus.eu/esa-zarr-sentinel-explorer-fra/tests-output/sentinel-2-l2a-staging/file.zarr"
78-
expected = "s3://esa-zarr-sentinel-explorer-fra/tests-output/sentinel-2-l2a-staging/file.zarr"
78+
expected = (
79+
"s3://esa-zarr-sentinel-explorer-fra/tests-output/sentinel-2-l2a-staging/file.zarr"
80+
)
7981
assert https_to_s3(https_url) == expected
8082

8183
def test_new_gateway_format_bucket_only(self):
@@ -92,7 +94,9 @@ def test_new_gateway_format_bucket_with_slash(self):
9294

9395
def test_old_s3_format_de_region(self):
9496
"""Test backwards compatibility with old S3 format (de region)."""
95-
https_url = "https://esa-zarr-sentinel-explorer-fra.s3.de.io.cloud.ovh.net/tests-output/file.zarr"
97+
https_url = (
98+
"https://esa-zarr-sentinel-explorer-fra.s3.de.io.cloud.ovh.net/tests-output/file.zarr"
99+
)
96100
expected = "s3://esa-zarr-sentinel-explorer-fra/tests-output/file.zarr"
97101
assert https_to_s3(https_url) == expected
98102

@@ -289,8 +293,13 @@ def test_add_alternate_to_gateway_url(self):
289293
add_alternate_s3_assets(item, s3_endpoint)
290294

291295
# Check extensions were added
292-
assert "https://stac-extensions.github.io/alternate-assets/v1.2.0/schema.json" in item.stac_extensions
293-
assert "https://stac-extensions.github.io/storage/v2.0.0/schema.json" in item.stac_extensions
296+
assert (
297+
"https://stac-extensions.github.io/alternate-assets/v1.2.0/schema.json"
298+
in item.stac_extensions
299+
)
300+
assert (
301+
"https://stac-extensions.github.io/storage/v2.0.0/schema.json" in item.stac_extensions
302+
)
294303

295304
# Check alternate was added
296305
asset = item.assets["data"]
@@ -510,11 +519,11 @@ def test_extensions_not_duplicated(self):
510519
add_alternate_s3_assets(item, "https://s3.de.io.cloud.ovh.net")
511520

512521
# Count occurrences of alternate-assets extension
513-
alternate_count = sum(
514-
1 for ext in item.stac_extensions if "alternate-assets" in ext
515-
)
522+
alternate_count = sum(1 for ext in item.stac_extensions if "alternate-assets" in ext)
516523
assert alternate_count == 1
517-
assert "https://stac-extensions.github.io/storage/v2.0.0/schema.json" in item.stac_extensions
524+
assert (
525+
"https://stac-extensions.github.io/storage/v2.0.0/schema.json" in item.stac_extensions
526+
)
518527

519528

520529
class TestEdgeCases:

0 commit comments

Comments
 (0)