Skip invalid and protected candidates

This commit is contained in:
Edward Betts 2026-05-18 23:20:42 +01:00
parent bd0c1a26c2
commit 93f49c5554
3 changed files with 223 additions and 17 deletions

113
test_web_view.py Normal file
View file

@ -0,0 +1,113 @@
import unittest
from unittest.mock import patch
import web_view
def hit(title: str) -> web_view.Hit:
return {
"ns": 0,
"title": title,
"pageid": 1,
"size": 1,
"wordcount": 1,
"snippet": "",
"timestamp": "",
}
class ArticlePageSkipTests(unittest.TestCase):
def setUp(self) -> None:
web_view.app.config["TESTING"] = True
self.client = web_view.app.test_client()
def test_session_skips_are_left_out_of_candidates(self) -> None:
hits = [
hit("Mass racial violence in the United States"),
hit("National Register of Historic Places listings in Brookhaven (town), New York"),
hit("Useful candidate"),
]
with self.client.session_transaction() as session:
session["skipped"] = {
"African-American neighborhood": [
"Mass racial violence in the United States",
"National Register of Historic Places listings in Brookhaven (town), New York",
]
}
with (
patch("web_view.api.get_wiki_info", return_value=None),
patch("web_view.search_count", return_value=3),
patch("web_view.search_count_with_link", return_value=0),
patch("web_view.search_no_link", return_value=(3, hits)),
):
response = self.client.get("/link/African-American_neighborhood")
body = response.get_data(as_text=True)
self.assertEqual(response.status_code, 200)
self.assertNotIn(
'data-title="Mass racial violence in the United States"', body
)
self.assertNotIn(
'data-title="National Register of Historic Places listings in Brookhaven (town), New York"',
body,
)
self.assertIn("Useful candidate", body)
class ProtectedCandidateTests(unittest.TestCase):
def test_edit_protected_hits_are_filtered_out(self) -> None:
hits = [hit("Editable"), hit("Protected")]
hits[0]["pageid"] = 10
hits[1]["pageid"] = 20
with patch(
"web_view.get_page_info_by_id",
return_value={
10: {"pageid": 10, "title": "Editable", "protection": []},
20: {
"pageid": 20,
"title": "Protected",
"protection": [
{"type": "edit", "level": "sysop", "expiry": "infinity"}
],
},
},
):
filtered = web_view.filter_edit_protected_hits(hits)
self.assertEqual([item["title"] for item in filtered], ["Editable"])
def test_search_no_link_filters_edit_protected_results(self) -> None:
hits = [hit("Editable"), hit("Protected")]
hits[0]["pageid"] = 10
hits[1]["pageid"] = 20
with (
patch(
"web_view.run_search",
return_value={"searchinfo": {"totalhits": 2}, "search": hits},
),
patch(
"web_view.get_page_info_by_id",
return_value={
10: {"pageid": 10, "title": "Editable", "protection": []},
20: {
"pageid": 20,
"title": "Protected",
"protection": [
{"type": "edit", "level": "sysop", "expiry": "infinity"}
],
},
},
),
):
totalhits, filtered = web_view.search_no_link("Target")
self.assertEqual(totalhits, 2)
self.assertEqual([item["title"] for item in filtered], ["Editable"])
if __name__ == "__main__":
unittest.main()