test_resolve_strategies.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import pytest
  2. from app.resolve import ResolveService
  3. @pytest.mark.anyio
  4. async def test_cache_hit_is_resolved_when_confidence_satisfies_requested_mode():
  5. async def load_entity(_subject):
  6. return {
  7. "atlas_id": "atlas-1",
  8. "label": "Australia",
  9. "type": "atlas:Location",
  10. "description": "country in Oceania",
  11. "needs_curation": True,
  12. "confidence": 0.91,
  13. }
  14. async def fail_lookup(*_args, **_kwargs):
  15. raise AssertionError("wikidata lookup should not run")
  16. async def no_persist(_entity):
  17. return None
  18. svc = ResolveService(load_entity_fn=load_entity, wikidata_lookup_fn=fail_lookup, persist_entity_fn=no_persist)
  19. result = await svc.resolve(
  20. subject="Australia",
  21. constraints={"min_confidence": 0.85},
  22. strategy={"mode": "ranked"},
  23. debug={"include_explanations": True},
  24. )
  25. assert result["status"] == "resolved"
  26. assert result["confidence"] == 0.91
  27. assert result["meta"]["debug"]["used_cache"] is True
  28. @pytest.mark.anyio
  29. async def test_low_confidence_cache_triggers_reresolution_for_ranked_mode():
  30. calls = {"lookup": 0, "persist": 0}
  31. async def load_entity(_subject):
  32. return {
  33. "atlas_id": "atlas-1",
  34. "label": "Australia",
  35. "type": "atlas:Location",
  36. "description": "country in Oceania",
  37. "needs_curation": True,
  38. "confidence": 0.55,
  39. }
  40. async def wikidata_lookup(_subject, _language="en", _limit=5):
  41. calls["lookup"] += 1
  42. return [
  43. {"id": "Q408", "label": "Australia", "description": "country in Oceania", "type": "Q6256"}
  44. ]
  45. async def persist(_entity):
  46. calls["persist"] += 1
  47. svc = ResolveService(load_entity_fn=load_entity, wikidata_lookup_fn=wikidata_lookup, persist_entity_fn=persist)
  48. result = await svc.resolve(
  49. subject="Australia",
  50. context={"language": "en", "realm": "external"},
  51. constraints={"min_confidence": 0.85},
  52. hints={"expected_type": "location", "aliases": []},
  53. strategy={"mode": "ranked", "auto_accept_threshold": 0.85},
  54. debug={"include_explanations": True},
  55. )
  56. assert calls["lookup"] == 1
  57. assert calls["persist"] == 1
  58. assert result["status"] == "resolved"
  59. assert result["meta"]["debug"]["used_cache"] is False
  60. @pytest.mark.anyio
  61. async def test_interactive_returns_all_candidates_without_auto_accepting_below_threshold():
  62. async def no_hit(_subject):
  63. return None
  64. async def wikidata_lookup(_subject, _language="en", _limit=5):
  65. return [
  66. {"id": "Q1225", "label": "Georgia", "description": "country in Eastern Europe and West Asia", "type": "Q6256"},
  67. {"id": "Q1428", "label": "Georgia", "description": "state of the United States of America", "type": "Q35657"},
  68. ]
  69. async def no_persist(_entity):
  70. return None
  71. svc = ResolveService(load_entity_fn=no_hit, wikidata_lookup_fn=wikidata_lookup, persist_entity_fn=no_persist)
  72. result = await svc.resolve(
  73. subject="Georgia",
  74. context={"language": "en", "realm": "external"},
  75. constraints={"max_candidates": 5},
  76. hints={"expected_type": "location", "aliases": []},
  77. strategy={"mode": "interactive", "auto_accept_threshold": 0.95, "interactive_below_threshold": True},
  78. debug={"include_candidates": True, "include_explanations": True},
  79. )
  80. assert result["status"] == "ambiguous"
  81. assert len(result["candidates"]) == 2
  82. assert result["meta"]["debug"]["decision"] == "ambiguous_below_threshold"