File size: 4,863 Bytes
7e25f7a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | """Unit tests for cores.location — geography, language, plates."""
from __future__ import annotations
from cores.location import (
coords_to_country, coordinate_to_country, coords_to_city_estimate,
detect_script, script_to_regions, text_to_country_hints,
plate_format_to_country,
LocationEvidenceCollector, LocationEstimator,
)
class TestGeography:
def test_usa_coordinates(self):
# New York
assert coords_to_country(40.7, -74.0) == "United States"
def test_france_coordinates(self):
# Paris
assert coords_to_country(48.85, 2.35) == "France"
def test_japan_coordinates(self):
# Tokyo
assert coords_to_country(35.68, 139.76) == "Japan"
def test_ocean_coordinates_returns_none(self):
# Middle of the Pacific
assert coords_to_country(0.0, -160.0) is None
def test_coordinate_to_country_confidence(self):
# Center of France — high confidence
result = coordinate_to_country(47.0, 2.0)
assert result["country"] == "France"
assert result["confidence"] > 0.5
def test_city_estimate_near_major_city(self):
result = coords_to_city_estimate(40.7, -74.0) # near NYC
assert result is not None
assert result["city"] == "New York"
def test_city_estimate_far_from_cities(self):
# Middle of ocean
result = coords_to_city_estimate(0.0, -160.0)
assert result is None
class TestLanguageDetection:
def test_latin_script(self):
assert detect_script("Hello World") == "latin"
def test_cyrillic_script(self):
assert detect_script("Привет мир") == "cyrillic"
def test_arabic_script(self):
assert detect_script("مرحبا") == "arabic"
def test_cjk_script(self):
assert detect_script("你好世界") == "cjk"
def test_hangul_script(self):
assert detect_script("안녕하세요") == "hangul"
def test_thai_script(self):
assert detect_script("สวัสดี") == "thai"
def test_empty_text(self):
assert detect_script("") == "unknown"
def test_script_to_regions_cyrillic(self):
regions = script_to_regions("cyrillic")
countries = [r["country"] for r in regions]
assert "Russia" in countries
def test_text_to_country_hints_keyword(self):
hints = text_to_country_hints("I visited London last summer")
countries = [h["country"] for h in hints]
assert "United Kingdom" in countries
class TestPlateFormats:
def test_french_plate(self):
result = plate_format_to_country("AB123CD")
assert result is not None
assert result["country"] == "France"
def test_uk_plate(self):
result = plate_format_to_country("AB12CDE")
assert result is not None
assert result["country"] == "United Kingdom"
def test_spanish_plate(self):
result = plate_format_to_country("1234ABC")
assert result is not None
assert result["country"] == "Spain"
def test_russian_plate(self):
result = plate_format_to_country("A123BC77")
assert result is not None
assert result["country"] == "Russia"
def test_empty_plate(self):
assert plate_format_to_country("") is None
class TestLocationEstimator:
def test_gps_only(self):
collector = LocationEvidenceCollector()
collector.add_gps(48.85, 2.35) # Paris
estimator = LocationEstimator()
result = estimator.estimate(collector.evidence, gps={"lat": 48.85, "lon": 2.35})
assert len(result.candidate_countries) > 0
assert result.candidate_countries[0]["country"] == "France"
assert result.candidate_countries[0]["confidence"] > 0.5
def test_ocr_text_evidence(self):
collector = LocationEvidenceCollector()
collector.add_ocr_text("Привет мир") # Russian
estimator = LocationEstimator()
result = estimator.estimate(collector.evidence)
countries = [c["country"] for c in result.candidate_countries]
assert "Russia" in countries
def test_no_evidence(self):
collector = LocationEvidenceCollector()
estimator = LocationEstimator()
result = estimator.estimate(collector.evidence)
assert result.candidate_countries == []
assert result.overall_confidence == 0.0
def test_conflicting_evidence(self):
collector = LocationEvidenceCollector()
# GPS says France
collector.add_gps(48.85, 2.35)
# OCR says Russia (conflict)
collector.add_ocr_text("Привет мир")
estimator = LocationEstimator()
result = estimator.estimate(collector.evidence, gps={"lat": 48.85, "lon": 2.35})
# GPS should win but conflict is noted
assert len(result.candidate_countries) > 0
|