test_vfs_fetcher.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # -*- coding: utf-8 -*-
  2. import pytest
  3. from unittest.mock import patch, MagicMock
  4. from app.visa_free_config_fetchers.vfs_fetcher import _select_candidate_with_llm
  5. def test_select_candidate_with_llm():
  6. candidates = [
  7. {"code": "A", "name": "Option A"},
  8. {"code": "B", "name": "Option B"}
  9. ]
  10. prompt_context = "选择 Option B"
  11. mock_resp = MagicMock()
  12. mock_resp.status_code = 200
  13. mock_resp.json.return_value = {
  14. "choices": [
  15. {"message": {"content": "1"}}
  16. ]
  17. }
  18. with patch("curl_cffi.requests.post", return_value=mock_resp):
  19. selected = _select_candidate_with_llm(candidates, prompt_context)
  20. assert selected == {"code": "B", "name": "Option B"}
  21. from app.visa_free_config_fetchers.vfs_fetcher import VFSFetcher
  22. def test_fetch_external_vac_info_success():
  23. fetcher = VFSFetcher()
  24. mock_centers = [
  25. {
  26. "isoCode": "NTDB",
  27. "centerName": "Netherlands Visa Application Center - Dublin",
  28. "address": "130 Francis Street, Dublin"
  29. }
  30. ]
  31. mock_cats = [
  32. {
  33. "visaCategoryCode": "TA",
  34. "name": "Short Stay Visa"
  35. }
  36. ]
  37. mock_subcats = [
  38. {
  39. "subCategoryCode": "To",
  40. "subCategoryName": "Tourist"
  41. }
  42. ]
  43. def mock_get(url, **kwargs):
  44. resp = MagicMock()
  45. resp.status_code = 200
  46. if "master/center" in url:
  47. resp.json.return_value = mock_centers
  48. elif "master/visacategory" in url:
  49. resp.json.return_value = mock_cats
  50. elif "master/subvisacategory" in url:
  51. resp.json.return_value = mock_subcats
  52. else:
  53. resp.status_code = 404
  54. return resp
  55. with patch("curl_cffi.requests.get", side_effect=mock_get):
  56. res = fetcher.fetch_external_vac_info("ie", "nl", "tourist", "dub")
  57. assert res["vac_code"] == "NTDB"
  58. assert res["center_name"] == "Netherlands Visa Application Center - Dublin"
  59. assert res["category_code"] == "TA"
  60. assert res["subcategory_code"] == "To"
  61. def test_fetch_external_vac_info_hard_failure():
  62. fetcher = VFSFetcher()
  63. mock_resp = MagicMock()
  64. mock_resp.status_code = 500
  65. with patch("curl_cffi.requests.get", return_value=mock_resp):
  66. with pytest.raises(RuntimeError):
  67. fetcher.fetch_external_vac_info("ie", "nl", "tourist", "nonexistent")
  68. def test_fetch_free_config_fallback_to_external():
  69. fetcher = VFSFetcher()
  70. mock_ext = {
  71. "routing_key": "slot.test.nl.tourist",
  72. "vac_code": "TEST",
  73. "center_name": "Test Center",
  74. "address": "Test Address",
  75. "category_code": "CAT",
  76. "category_name": "Cat Name",
  77. "subcategory_code": "SUB",
  78. "subcategory_name": "Sub Name"
  79. }
  80. with patch.object(fetcher, "fetch_external_vac_info", return_value=mock_ext):
  81. config = fetcher.fetch_free_config("ie", "nl", "tourist", "unknown_vac_code")
  82. assert "slot.test.nl.tourist" in config["apt_configs"]
  83. assert config["apt_configs"]["slot.test.nl.tourist"]["vac_code"] == "TEST"