dp-zp-agent/test/presentation/test_record_answers.py
2026-11-24 17:19:00 +01:00

190 lines
12 KiB
Python

"""Questions a ZP Wiki profile answers alone: the record's page, a topic in it, keyword (tag) conditions, two people."""
import pytest
from tools.answers import combined as C
from tools.answers.presentation import unified as U
PAGES = [
{"name": "Ana Nová", "url": "https://zp.test/ana", "kind": "student", "start_year": 2022, "tags": ["nlp", "rag"],
"categories": ["bp2025"], "person_kind": "student", "source_document": "ana.md"},
{"name": "Ben Starý", "url": "https://zp.test/ben", "kind": "student", "start_year": 2021, "tags": ["nlp"],
"categories": ["bp2024"], "person_kind": "student", "source_document": "ben.md"},
{"name": "Eva Biela", "url": "https://zp.test/eva", "kind": "intern", "start_year": None, "tags": ["hatespeech"],
"categories": ["iaeste"], "person_kind": "intern", "source_document": "eva.md"},
]
TEXTS = {"ana.md": "# Bakalárska práca 2025\nStav:\n- Implementovaný PageRank v [Elasticsearch](https://es.test).\n"
"- Použité PyTorch modely.\n",
"ben.md": "# Bakalárska práca 2024\nStav:\n- Hotový chatbot.\n",
"eva.md": "Goal: Help with the [Hate Speech Project](/topics/hatespeech)\n"}
WORKS = {"Ana Nová": [{"type": "bachelor_thesis", "year": 2025, "title": "Vyhľadávanie", "source": "wiki"}],
"Ben Starý": [{"type": "bachelor_thesis", "year": 2024, "title": "Chatbot", "source": "wiki"}]}
@pytest.fixture
def data(monkeypatch):
monkeypatch.setattr(U, "wiki_pages", lambda: PAGES)
monkeypatch.setattr(U, "catalog", lambda: {"people": {p["name"]: p for p in PAGES}, "works": WORKS})
monkeypatch.setattr(U, "page_chunks", lambda path: [TEXTS.get(path, "")])
monkeypatch.setattr(U, "person_works", lambda person, rows=None: [dict(w) for w in WORKS.get(person, [])])
monkeypatch.setattr(U, "spelled", lambda name: name)
names = [p["name"] for p in PAGES]
monkeypatch.setattr(C, "_people", lambda q: [n for n in names if n.split()[0][:3] in q])
return names
def answer(question):
named = [n for n in ("Ana Nová", "Ben Starý", "Eva Biela") if n.split()[0][:3] in question]
return C.record_answer(question, {"name": named[0]} if len(named) == 1 else None)
def test_where_the_record_of_a_person_is(data):
out = answer("Kde v ZP Wiki nájdem záznam o osobe Ana Nová?")
assert "https://zp.test/ana" in out["text"] and out["urls"] == ["https://zp.test/ana"]
assert "(stážistka v ZP Wiki)" in answer("Nájdi dokument o osobe Eva Biela.")["text"]
assert "(študentka v ZP Wiki)" in out["text"]
def test_the_lines_of_a_record_that_name_a_topic(data):
out = answer("Ktorý záznam v ZP Wiki spája osobu Ana Nová s témou „PageRank“?")
assert "Bakalárska práca 2025" in out["text"] and "„Implementovaný PageRank v Elasticsearch.“" in out["text"]
assert "PyTorch" in answer("Ktorý dokument alebo študent súvisí s témou PyTorch a osobou Ana Nová?")["text"]
assert "Hate Speech Project" in answer("Ktorá stáž osoby Eva Biela súvisela s témou Hate Speech?")["text"]
assert "sa téma „Docker“ nespomína" in answer("Ktorý dokument alebo študent súvisí s témou Docker a osobou Ana Nová?")["text"]
def test_a_keyword_condition_of_a_title_is_checked(data):
assert "„Vyhľadávanie“" in answer("Uveď názov práce osoby Ana Nová v roku 2025 s kľúčovým slovom nlp.")["text"]
missing = answer("Aký je názov práce osoby Ana Nová, ak jej dokument zároveň obsahuje kľúčové slovo Raspberry Pi?")
assert "kľúčové slovo „Raspberry Pi“ nemá" in missing["text"] and "Vyhľadávanie" not in missing["text"]
assert "prácu z roku 2019 však v zázname nemá" in answer("Uveď názov práce osoby Ana Nová v roku 2019 s kľúčovým slovom nlp.")["text"]
def test_two_people_keywords_and_study_starts(data):
assert answer("Majú osoby Ana Nová a Ben Starý spoločné kľúčové slovo nlp?")["text"].startswith("Áno")
no = answer("Over spoločné kľúčové slovo rag pri osobách Ana Nová a Ben Starý.")["text"]
assert no.startswith("Nie") and "má ho len Ana Nová" in no
which = answer("Ktorá z osôb Ana Nová a Ben Starý má kľúčové slovo nlp a jej práca je z roku 2024?")["text"]
assert which.startswith("Podmienky spĺňa") and "Ben Starý: bakalárska práca (2024)" in which and "Ana Nová nemá prácu z roku 2024" in which
start = answer("Aký spoločný rok začiatku štúdia majú osoby Ana Nová a Ben Starý?")["text"]
assert start.startswith("Nie, spoločný rok začiatku štúdia nemajú") and "2022" in start and "2021" in start
@pytest.mark.parametrize("question", [
"Kto je vedúci diplomovej práce Ana Nová?", "Na čom pracovala Ana Nová?", "Porovnaj výsledky Ana Nová a Ben Starý.",
"Kto robil s PageRank?", "Aký je názov bakalárskej práce Ana Nová?"])
def test_other_questions_are_left_to_the_usual_answers(data, question):
assert answer(question) is None
def test_a_misspelled_topic_word_still_finds_its_lines(data):
out = answer("Ktorý dokument alebo študent súvisí s témou PagReank a osobou Ana Nová?")
assert "PageRank" in out["text"] and "nespomína" not in out["text"]
assert "nespomína" in answer("Ktorý dokument alebo študent súvisí s témou Pagr a osobou Ana Nová?")["text"] # short: exact only
def test_a_study_start_both_people_are_linked_by(data):
out = answer("Cez ktorý rok začiatku štúdia sú prepojené osoby Ana Nová a Ben Starý?")
assert out and "2022" in out["text"] and "2021" in out["text"]
def test_an_intern_goal_written_on_one_line(monkeypatch, data):
TEXTS["eva.md"] = "Eva Biela\n\nIAESTE Intern Summer 2023, two months\n\nGoal: Help with the [Hate Speech Project](/topics/hs)\n"
out = U.intern_overview("Eva Biela")
assert "pobyt IAESTE Intern Summer 2023" in out["text"] and "- Help with the Hate Speech Project" in out["text"]
def test_a_work_read_twice_keeps_the_copy_with_the_register_link():
old = {"title": "A", "year": 2024, "url": None, "opponents": ["Malý, Igor"]}
new = {"title": "A", "year": 2024, "url": "https://opac.test/a", "opponents": []}
assert U._better_copy(old, new) == {"title": "A", "year": 2024, "url": "https://opac.test/a", "opponents": ["Malý, Igor"]}
assert U._better_copy(new, old)["url"] == "https://opac.test/a"
assert U._better_copy(None, old) is old
ROW = lambda sup, opp: {"supervisors": [sup], "opponents": [opp], "url": "https://opac.test/x"}
@pytest.fixture
def theses(monkeypatch, data):
works = {"Ana Nová": [
{"type": "diploma_thesis", "year": 2027, "title": "Grafy", "source": "wiki", "heading": "Diplomová práca 2027",
"row": ROW("Hládek, Daniel", "Staš, Ján")},
{"type": "bachelor_thesis", "year": 2025, "title": "Vyhľadávanie", "source": "wiki", "heading": "Bakalárska práca 2025",
"row": ROW("Hládek, Daniel", "Pleva, Matúš")}]}
monkeypatch.setattr(U, "person_works", lambda person, rows=None: [dict(w) for w in works.get(person, [])])
monkeypatch.setattr(U, "units", lambda person: [])
return works
def test_a_claimed_supervisor_is_checked_not_confirmed(theses):
no = answer("Viem, že vedúcim Ana Nová je Peter Novák, len mi to potvrď.")["text"]
assert no.startswith("Nie, Peter Novák v záznamoch nie je uvedený ako vedúci.") and "Daniel Hládek" in no
yes = answer("Viem, že vedúcim Ana Nová je Daniel Hládek, len mi to potvrď.")["text"]
assert yes.startswith("Áno, podľa záznamov to sedí.")
assert answer("Kto je vedúci diplomovej práce Ana Nová?") is None # no claim: the usual answer
def test_the_same_supervisor_at_both_theses_is_said_without_asking(theses):
works = U.person_works("Ana Nová")
assert U.same_role_people("Ana Nová", works, "supervisor")
assert U.same_role_people("Ana Nová", works, "opponent") is None # Staš and Pleva: the answer asks which thesis
works[1]["row"] = {} # a thesis without its own record: ask
assert U.same_role_people("Ana Nová", works, "supervisor") is None
def test_a_work_year_in_a_title_question_is_kept(theses):
none = answer("Aký názov práce má osoba Ana Nová v roku 2018?")["text"]
assert "z roku 2018 v zázname nie je uvedená" in none and "Grafy" in none
one = answer("Uveď názov práce osoby Ana Nová a rok práce pre rok 2025.")["text"]
assert "Vyhľadávanie" in one and "Grafy" not in one
assert answer("Ako sa volá bakalárska práca Ana Nová z roku 2025?") is None # the year leaves nothing out
def test_an_interns_stay_year_topic_and_missing_thesis(data):
TEXTS["eva.md"] = "Eva Biela\n\nIAESTE Intern Summer 2023, two months\n\nTopic: Hate speech detection\n"
assert "v roku 2023" in answer("V ktorom roku prebiehala stáž osoby Eva Biela?")["text"]
assert "Hate speech detection" in answer("Aká bola téma stáže Eva Biela?")["text"]
assert "dizertačná práca osoby v podkladoch nie je uvedená" in answer("Kto bol oponentom dizertačnej práce Eva Biela?")["text"]
assert answer("V ktorom roku prebiehala stáž osoby Ana Nová?") is None or "stáž" not in answer("V ktorom roku prebiehala stáž osoby Ana Nová?")["text"]
def test_a_topic_page_is_found_by_its_title(monkeypatch, data):
monkeypatch.setattr(U, "topic_pages", lambda: [{"title": "Anotácia textových dát", "url": "https://zp.test/topics/prodigy"}])
out = C.record_answer("Nájdi projektovú alebo informačnú stránku „Anotácia textových dát“.", None)
assert out["urls"] == ["https://zp.test/topics/prodigy"] and "„Anotácia textových dát“" in out["text"]
assert C.record_answer("Nájdi projektovú alebo informačnú stránku „Úplne iná téma“.", None) is None
assert C.record_answer("Kto robil anotáciu textových dát?", None) is None
def test_a_tag_with_a_hyphen_is_read_as_written(data):
PAGES[0]["tags"].append("mind-maps")
try:
out = U.tag_answer("Kto v ZP Wiki rieši tému s tagom mind-maps?")
assert "**mind-maps**" in out["text"] and "Ana Nová" in out["text"]
assert "**nlp**" in U.tag_answer("Ktorí študenti majú tag nlp?")["text"]
finally:
PAGES[0]["tags"].remove("mind-maps")
def test_the_work_a_student_is_doing_is_the_newest_wiki_thesis(theses):
works = U.person_works("Ana Nová")
works[1]["row"] = {"supervisors": ["Pleva, Matúš"], "opponents": ["Staš, Ján"]} # the theses differ
monkeypatch_works = lambda person, rows=None: works
U.person_works, old = monkeypatch_works, U.person_works
try:
low = U.normalized("Kto je vedúci práce, ktorú robí Ana Nová?")
out = U.roles("Ana Nová", works, low)
assert "options" not in out and "Daniel Hládek" in out["text"] and "Pleva" not in out["text"]
asked = U.roles("Ana Nová", works, U.normalized("Kto bol vedúci práce Ana Nová?"))
assert asked.get("options") # no present tense: which thesis?
finally:
U.person_works = old
def test_a_year_the_register_and_the_wiki_give_differently_is_said_with_both():
work = {"type": "bachelor_thesis", "year": 2020, "source": "wiki", "row": {"year": 2021}}
assert U.year_note(work) == " (v ZP Wiki je pri práci uvedený rok 2020)"
assert U.year_note({**work, "row": {"year": 2020}}) == "" # the same year: nothing added
assert U.year_note({**work, "type": "bachelor_project"}) == "" # a project has no defence year
assert U.year_note({"type": "bachelor_thesis", "year": 2021, "source": "crzp", "row": {"year": 2021}}) == ""