oedb-backend/test_osm_cal_ical.py

128 lines
4.2 KiB
Python
Raw Permalink Normal View History

2025-09-26 11:57:54 +02:00
#!/usr/bin/env python3
"""
Test script for the OSM Calendar extractor's iCal functionality.
This script tests the ability of the osm_cal.py script to extract
geographic coordinates and location information from iCal files
for events that don't have this information in the RSS feed.
"""
import sys
import os
from extractors.osm_cal import fetch_ical_data, OSMCAL_EVENT_BASE_URL
from oedb.utils.logging import logger
def test_fetch_ical_data():
"""
Test the fetch_ical_data function with a real OSM Calendar event.
"""
print("\n=== Testing fetch_ical_data function ===")
# Test with a real OSM Calendar event URL
# Note: This event ID might not exist in the future, so the test might fail
event_url = f"{OSMCAL_EVENT_BASE_URL}3973"
print(f"Fetching iCal data for event: {event_url}")
location_name, coordinates = fetch_ical_data(event_url)
print(f"Location name: {location_name}")
print(f"Coordinates: {coordinates}")
if coordinates != [0, 0]:
print("✅ Successfully extracted coordinates from iCal")
else:
print("❌ Failed to extract coordinates from iCal")
if location_name != "Unknown Location":
print("✅ Successfully extracted location name from iCal")
else:
print("❌ Failed to extract location name from iCal")
print("=== Test completed ===\n")
return coordinates != [0, 0] and location_name != "Unknown Location"
def test_with_invalid_url():
"""
Test the fetch_ical_data function with an invalid URL.
"""
print("\n=== Testing fetch_ical_data with invalid URL ===")
# Test with an invalid event URL
event_url = f"{OSMCAL_EVENT_BASE_URL}999999999"
print(f"Fetching iCal data for invalid event: {event_url}")
location_name, coordinates = fetch_ical_data(event_url)
print(f"Location name: {location_name}")
print(f"Coordinates: {coordinates}")
if coordinates == [0, 0]:
print("✅ Correctly returned default coordinates for invalid URL")
else:
print("❌ Unexpectedly returned coordinates for invalid URL")
if location_name == "Unknown Location":
print("✅ Correctly returned default location name for invalid URL")
else:
print("❌ Unexpectedly returned location name for invalid URL")
print("=== Test completed ===\n")
return coordinates == [0, 0] and location_name == "Unknown Location"
def test_with_non_osmcal_url():
"""
Test the fetch_ical_data function with a non-OSM Calendar URL.
"""
print("\n=== Testing fetch_ical_data with non-OSM Calendar URL ===")
# Test with a non-OSM Calendar URL
event_url = "https://example.com/event/123"
print(f"Fetching iCal data for non-OSM Calendar URL: {event_url}")
location_name, coordinates = fetch_ical_data(event_url)
print(f"Location name: {location_name}")
print(f"Coordinates: {coordinates}")
if coordinates == [0, 0]:
print("✅ Correctly returned default coordinates for non-OSM Calendar URL")
else:
print("❌ Unexpectedly returned coordinates for non-OSM Calendar URL")
if location_name == "Unknown Location":
print("✅ Correctly returned default location name for non-OSM Calendar URL")
else:
print("❌ Unexpectedly returned location name for non-OSM Calendar URL")
print("=== Test completed ===\n")
return coordinates == [0, 0] and location_name == "Unknown Location"
def main():
"""
Run all tests.
"""
print("Starting OSM Calendar iCal tests...")
# Run tests
test1 = test_fetch_ical_data()
test2 = test_with_invalid_url()
test3 = test_with_non_osmcal_url()
# Print summary
print("\n=== Test Summary ===")
print(f"Test 1 (fetch_ical_data): {'PASSED' if test1 else 'FAILED'}")
print(f"Test 2 (invalid URL): {'PASSED' if test2 else 'FAILED'}")
print(f"Test 3 (non-OSM Calendar URL): {'PASSED' if test3 else 'FAILED'}")
if test1 and test2 and test3:
print("\n✅ All tests PASSED")
else:
print("\n❌ Some tests FAILED")
print("All tests completed.")
if __name__ == "__main__":
main()