from app.api.scenes import ( _SEARCH_WALK_BUDGET_MS, _SEARCH_WALK_BUDGET_NO_TRGM_MS, _SEARCH_WALK_BUDGET_NO_TRGM_NARROWED_MS, _origin_driven_worth, _rare_title_phrase, _search_walk_budget, ) # 1000 tytulow jak histogram z pg_stats: 20 z "anal", 1 z "hailey rose", reszta obojetna. SAMPLE = ["anal scene %d" % i for i in range(20)] + ["hailey rose pov"] + [ "other title %d" % i for i in range(979) ] def _budget(q, off=0, want=51, narrowed=False): return _search_walk_budget(q, off, want, narrowed, lambda: SAMPLE) def test_common_phrase_walks(): # 20/1000 = 2%: na strone 1 trzeba przejsc ~2,5 tys. scen assert _budget("anal") == _SEARCH_WALK_BUDGET_MS def test_phrase_absent_from_sample_goes_to_trigrams(): assert _budget("jackerman") is None def test_single_hit_phrase_goes_to_trigrams(): # 1/1000 = 0,1%: ~51 tys. scen do przejscia, powyzej progu assert _budget("hailey rose") is None def test_deep_page_of_common_phrase_goes_to_trigrams(): # strona 13 "anal": (600 + 51) / 2% = ~32 tys. assert _budget("anal", off=600) is None def test_narrowed_search_goes_to_trigrams(): assert _budget("anal", narrowed=True) is None def test_short_phrase_always_walks(): # bez trigramu zwykly plan czyta caly indeks; tez przy zawezeniu i poza probka for q in ("vo", "q", "xz", " a "): assert _budget(q) == _SEARCH_WALK_BUDGET_NO_TRGM_MS assert _budget("zq", off=5000) == _SEARCH_WALK_BUDGET_NO_TRGM_MS def test_short_phrase_narrowed_gets_longer_budget(): # z tagiem trafienia sa rzadsze; po 3 s oddawalismy pusta liste mimo wynikow assert _budget("ai", narrowed=True) == _SEARCH_WALK_BUDGET_NO_TRGM_NARROWED_MS def test_sample_not_read_when_not_needed(): def boom(): raise AssertionError("probka niepotrzebna") assert _search_walk_budget("vo", 0, 51, False, boom) == _SEARCH_WALK_BUDGET_NO_TRGM_MS assert _search_walk_budget("vo", 0, 51, True, boom) == _SEARCH_WALK_BUDGET_NO_TRGM_NARROWED_MS assert _search_walk_budget("anal", 0, 51, True, boom) is None def test_origin_driven_for_common_and_short_phrases(): # "anal" 20/1000 w probce: popularna, w malym serwisie zaczynamy od jego scen assert _origin_driven_worth("anal", lambda: SAMPLE) # bez trigramu zawsze od serwisu (inaczej przejscie po calym katalogu) assert _origin_driven_worth("ai", lambda: SAMPLE) def test_origin_driven_skipped_for_rare_phrases(): # nazwisko: trigramy znajda kilka scen szybciej niz tablica id serwisu assert not _origin_driven_worth("hailey rose", lambda: SAMPLE) assert not _origin_driven_worth("jackerman", lambda: SAMPLE) def test_rare_phrase_detection(): assert _rare_title_phrase("hailey rose", lambda: SAMPLE) assert _rare_title_phrase("sunny leone", lambda: SAMPLE) assert not _rare_title_phrase("anal", lambda: SAMPLE) # bez trigramu nie jest "rzadka": trigramy i tak by jej nie znalazly assert not _rare_title_phrase("ai", lambda: SAMPLE)