63 lines
2.5 KiB
Python
63 lines
2.5 KiB
Python
from services import trip_finder_service
|
|
|
|
|
|
def test_scores_destination_and_duration_match_highest():
|
|
urls = [
|
|
'https://intrepidtravel.com/peru-classic-15-days',
|
|
'https://intrepidtravel.com/blog/about-us',
|
|
'https://intrepidtravel.com/vietnam-explorer-10-days',
|
|
]
|
|
result = trip_finder_service.score_and_rank_urls(urls, 'Peru', 15)
|
|
assert result['url'] == 'https://intrepidtravel.com/peru-classic-15-days'
|
|
assert result['found'] is True
|
|
assert result['confidence'] > 0
|
|
|
|
|
|
def test_no_keyword_match_returns_not_found():
|
|
urls = ['https://intrepidtravel.com/about-us', 'https://intrepidtravel.com/contact']
|
|
result = trip_finder_service.score_and_rank_urls(urls, 'Peru', 15)
|
|
assert result == {'url': None, 'confidence': 0, 'found': False}
|
|
|
|
|
|
def test_empty_url_list_returns_not_found():
|
|
result = trip_finder_service.score_and_rank_urls([], 'Peru', 15)
|
|
assert result == {'url': None, 'confidence': 0, 'found': False}
|
|
|
|
|
|
def test_low_confidence_match_is_not_found():
|
|
# Only a weak category-keyword match, no destination/duration signal —
|
|
# should fall below CONFIDENCE_FOUND_THRESHOLD (0.3).
|
|
urls = ['https://intrepidtravel.com/some-other-trek']
|
|
result = trip_finder_service.score_and_rank_urls(urls, 'Peru', 15)
|
|
assert result['found'] is False
|
|
|
|
|
|
def test_find_competitor_trip_url_handles_firecrawl_failure_gracefully(monkeypatch):
|
|
import requests
|
|
|
|
def _raise(*args, **kwargs):
|
|
raise requests.RequestException('connection refused')
|
|
|
|
monkeypatch.setattr(trip_finder_service.requests, 'post', _raise)
|
|
result = trip_finder_service.find_competitor_trip_url('https://intrepidtravel.com', 'Peru', 15, 'Classic')
|
|
assert result == {'url': None, 'confidence': 0, 'found': False}
|
|
|
|
|
|
def test_find_all_competitor_urls_runs_concurrently_and_preserves_competitor_ids(monkeypatch):
|
|
def _fake_find(root_url, destination, duration, travel_style):
|
|
return {'url': f'{root_url}/match', 'confidence': 0.8, 'found': True}
|
|
|
|
monkeypatch.setattr(trip_finder_service, 'find_competitor_trip_url', _fake_find)
|
|
|
|
competitors = [
|
|
{'id': 'c1', 'name': 'Intrepid', 'website': 'https://intrepidtravel.com'},
|
|
{'id': 'c2', 'name': 'Exodus', 'website': 'https://exodustravels.com'},
|
|
]
|
|
results = trip_finder_service.find_all_competitor_urls(competitors, 'Peru', 15, 'Classic')
|
|
|
|
assert len(results) == 2
|
|
ids = {r['competitor_id'] for r in results}
|
|
assert ids == {'c1', 'c2'}
|
|
for r in results:
|
|
assert r['found'] is True
|