"""Tests for stats-based sorting.""" import pytest @pytest.fixture def db(tmp_path): from stats_db import StatsDB db = StatsDB(tmp_path / "test.db") db.init() return db def test_sort_by_downloads_desc(db): """Items sorted by download_count descending.""" from proxy import _sort_items_by_stats db.upsert("h1", {"download_count": 100, "rating": 0, "rating_count": 0, "thumbs_up_count": 0}) db.upsert("h2", {"download_count": 5000, "rating": 0, "rating_count": 0, "thumbs_up_count": 0}) db.upsert("h3", {"download_count": 500, "rating": 0, "rating_count": 0, "thumbs_up_count": 0}) items = [ {"sha256": "h1", "file_name": "a"}, {"sha256": "h2", "file_name": "b"}, {"sha256": "h3", "file_name": "c"}, ] sorted_items = _sort_items_by_stats(items, "downloads", "desc", db) assert [i["sha256"] for i in sorted_items] == ["h2", "h3", "h1"] def test_sort_by_rating_desc(db): """Items sorted by rating descending.""" from proxy import _sort_items_by_stats db.upsert("h1", {"download_count": 0, "rating": 3.0, "rating_count": 0, "thumbs_up_count": 0}) db.upsert("h2", {"download_count": 0, "rating": 4.8, "rating_count": 0, "thumbs_up_count": 0}) items = [{"sha256": "h1"}, {"sha256": "h2"}] sorted_items = _sort_items_by_stats(items, "rating", "desc", db) assert sorted_items[0]["sha256"] == "h2" def test_sort_items_without_stats_go_last(db): """Items without stats sort to the end.""" from proxy import _sort_items_by_stats db.upsert("h1", {"download_count": 100, "rating": 0, "rating_count": 0, "thumbs_up_count": 0}) items = [{"sha256": "no_stats"}, {"sha256": "h1"}] sorted_items = _sort_items_by_stats(items, "downloads", "desc", db) assert sorted_items[0]["sha256"] == "h1" assert sorted_items[1]["sha256"] == "no_stats" def test_is_stats_sort(): """Identify stats-based sort keys.""" from proxy import _is_stats_sort assert _is_stats_sort("downloads") is True assert _is_stats_sort("rating") is True assert _is_stats_sort("thumbsup") is True assert _is_stats_sort("name") is False assert _is_stats_sort("date") is False