Skip to content
Snippets Groups Projects
Commit d06bfef3 authored by Thorge Petersen's avatar Thorge Petersen
Browse files

test: add unit tests for URI replacements and resource clearing

parent e7b06056
No related branches found
No related tags found
1 merge request!1Update Formats, Dependencies, and Dockerfile Configuration
......@@ -29,10 +29,13 @@ class TestDcatCatalogCheck(unittest.TestCase):
"XML": ["application/xml"],
}
self.assertTrue(self.dcc.is_mime_type_compatible("JSON", "application/json"))
self.assertFalse(self.dcc.is_mime_type_compatible("JSON", "application/xml"))
self.assertTrue(self.dcc.is_mime_type_compatible(
"JSON", "application/json"))
self.assertFalse(self.dcc.is_mime_type_compatible(
"JSON", "application/xml"))
self.assertFalse(
self.dcc.is_mime_type_compatible("UnknownFormat", "application/json")
self.dcc.is_mime_type_compatible(
"UnknownFormat", "application/json")
)
def test_read_allowed_file_formats(self):
......@@ -44,7 +47,8 @@ class TestDcatCatalogCheck(unittest.TestCase):
):
formats = self.dcc.read_allowed_file_formats()
self.assertEqual(
formats, {"JSON": ["application/json"], "XML": ["application/xml"]}
formats, {"JSON": ["application/json"],
"XML": ["application/xml"]}
)
def test_load_uri_replacements(self):
......@@ -55,7 +59,8 @@ class TestDcatCatalogCheck(unittest.TestCase):
),
):
replacements = self.dcc.load_uri_replacements()
self.assertEqual(replacements, [{"regex": "old", "replaced_by": "new"}])
self.assertEqual(
replacements, [{"regex": "old", "replaced_by": "new"}])
# Simulate that the file does not exist
......@@ -375,6 +380,82 @@ class TestDcatCatalogCheck(unittest.TestCase):
'Line 1 is missing \'url\': {"status": "valid", "format": "JSON"}'
)
def test_apply_uri_replacements(self):
"""Test the apply_uri_replacements method."""
# Setup URI replacements
self.dcc.uri_replacements = [
{"regex": r"example\.com", "replaced_by": "test.com"},
{"regex": r"http://", "replaced_by": "https://"},
]
# URL matching both replacements
url = "http://example.com/path"
result = self.dcc.apply_uri_replacements(url)
self.assertEqual(result, "https://test.com/path")
# URL matching only one replacement
url = "http://other.com/path"
result = self.dcc.apply_uri_replacements(url)
self.assertEqual(result, "https://other.com/path")
# URL with no matches
url = "https://unchanged.com/path"
result = self.dcc.apply_uri_replacements(url)
self.assertEqual(result, "https://unchanged.com/path")
# Empty URL
url = ""
result = self.dcc.apply_uri_replacements(url)
self.assertEqual(result, "")
# No URI replacements defined
self.dcc.uri_replacements = []
url = "http://example.com/path"
result = self.dcc.apply_uri_replacements(url)
self.assertEqual(result, "http://example.com/path")
def test_clear_result(self):
"""Test the _clear_result method."""
# Define a resource dictionary with keys to clear and some additional keys
resource = {
"accessible": True,
"checksum_ok": True,
"duration": 1.23,
"error": "Some error",
"etag": "some-etag",
"http_status": 200,
"last_check": "2024-12-27T12:34:56Z",
"mimetype": "application/json",
"mimetype_mismatch": False,
"valid": True,
"url": "http://example.com/data", # This key should remain untouched
"format": "JSON", # This key should remain untouched
}
# Call the _clear_result method
self.dcc._clear_result(resource)
# Check that all keys to clear have been removed
for key in [
"accessible",
"checksum_ok",
"duration",
"error",
"etag",
"http_status",
"last_check",
"mimetype",
"mimetype_mismatch",
"valid",
]:
self.assertNotIn(key, resource)
# Check that unrelated keys remain
self.assertIn("url", resource)
self.assertIn("format", resource)
self.assertEqual(resource["url"], "http://example.com/data")
self.assertEqual(resource["format"], "JSON")
if __name__ == "__main__":
unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment