def best_match(title: str, base_url: str = DEFAULT_BASE_URL) -> Optional[Film]: response = requests.get(f"{base_url}/", params={"title": title}) if response.ok: return Film.parse_raw(response.content, content_type=response.headers["Content-Type"]) if response.status_code == 404: return None response.raise_for_status()
def test_best_autocomplete_match_with_multiple_results( search_interstellar_json): result = best_autocomplete_match(search_interstellar_json, title="Interstellar") assert result == Film(title="Interstellar", age_rating=AgeRating.AGE_12)
def test_best_autocomplete_match_with_one_result(search_a_silent_voice_json): result = best_autocomplete_match(search_a_silent_voice_json, title="A Silent Voice") assert result == Film(title="A Silent Voice", age_rating=AgeRating.AGE_12)
async def test_best_match(mock_search_interstellar): result = await best_match(title="interstellar") assert result == Film(title="Interstellar", age_rating=AgeRating.AGE_12)
def test_integration_best_match(app_url): actual = best_match("Interstellar", base_url=app_url) assert actual == Film(title="Interstellar", age_rating=AgeRating.AGE_12)
def _result_to_film(result: Dict[str, Any]) -> Film: return Film( title=result["title"], age_rating=_classification_to_age_rating(result["classification"]), )