From 54292eb231d62da512d35a1a90c33af12be1d927 Mon Sep 17 00:00:00 2001 From: govin Date: Tue, 22 Sep 2026 21:50:33 +0800 Subject: [PATCH] backend: let the reference smoke test survive a populated library MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It asserted that a search for "Wang" found one row and that the library was empty when it finished — both true only of an empty database. The library it runs against now holds 65 real references and 10 labels, so the run failed on its own baseline rather than on anything it was testing. Every row the run creates now carries a per-run token in its title, in one contributor's name and in its label names, and each count is asserted against a search for that token. A second, distinct token rides on the author name alone, because searching for the first one finds every row the run made and "the search matches an author" needs a string that is in exactly one place. What the test means does not change: "the search found one row" is a statement about this run, and the token is what makes it one. --- backend/scripts/smoke_references.py | 177 +++++++++++++++++++++------- 1 file changed, 133 insertions(+), 44 deletions(-) diff --git a/backend/scripts/smoke_references.py b/backend/scripts/smoke_references.py index 26c057c..55cebb0 100644 --- a/backend/scripts/smoke_references.py +++ b/backend/scripts/smoke_references.py @@ -15,6 +15,12 @@ The order of the first two steps is the point of the tag library rather than an accident of the script: **tags are made first and applied second**. A reference is created *with* tag ids, never with tag names, so a typo cannot invent a label — which is what step 4 checks. + +**Every entry this script creates carries a token in its title, its author and +its tag names.** The library it runs against is a real one and may hold hundreds +of entries, so "the search found one row" is only a meaningful assertion about +rows this run made. The token is what makes each count below a statement about +this run rather than about the library's size. """ from __future__ import annotations @@ -24,10 +30,21 @@ import sys import urllib.error import urllib.parse import urllib.request +import uuid from typing import Any BASE = "http://127.0.0.1:8000/api" +#: Stamped into every entry this run creates, so each search below can be scoped +#: to this run instead of to an empty database. Short and uppercase: it travels +#: in titles, in an author name and in tag names, and shows up in failure output. +TOKEN = uuid.uuid4().hex[:6].upper() + +#: A second token, carried by one contributor's *name* only. Titles and tag names +#: carry ``TOKEN``, so searching for it finds every row this run made; finding a +#: single row by author needs a string that is in exactly one place. +AUTHOR_TOKEN = f"AU{TOKEN}" + _checks = 0 @@ -73,11 +90,19 @@ def main() -> int: survey = call( "POST", "/reference-tags", - {"name": " 综述 ", "color": "rgb(64, 158, 255)", "note": "综述性文献"}, + { + "name": f" 综述-{TOKEN} ", + "color": "rgb(64, 158, 255)", + "note": "综述性文献", + }, expect=201, ) created_tags.append(survey["id"]) - check("a name is trimmed to one spelling", survey["name"] == "综述", survey["name"]) + check( + "a name is trimmed to one spelling", + survey["name"] == f"综述-{TOKEN}", + survey["name"], + ) check( "a colour is canonicalised", survey["color"] == "#409EFF", @@ -88,18 +113,27 @@ def main() -> int: must_read = call( "POST", "/reference-tags", - {"name": "必读", "color": "#f56c6c"}, + {"name": f"必读-{TOKEN}", "color": "#f56c6c"}, expect=201, ) created_tags.append(must_read["id"]) - check("the second tag is stored", must_read["name"] == "必读") + check("the second tag is stored", must_read["name"] == f"必读-{TOKEN}") print("2. a tag name is unique, and only a real colour is accepted") duplicate = call( - "POST", "/reference-tags", {"name": "综述"}, expect=409 + "POST", "/reference-tags", {"name": f"综述-{TOKEN}"}, expect=409 + ) + check( + "the refusal names the label", + TOKEN in duplicate["detail"], + duplicate["detail"], + ) + call( + "POST", + "/reference-tags", + {"name": f"怪颜色-{TOKEN}", "color": "blue"}, + expect=422, ) - check("the refusal names the label", "综述" in duplicate["detail"], duplicate["detail"]) - call("POST", "/reference-tags", {"name": "怪颜色", "color": "blue"}, expect=422) call("POST", "/reference-tags", {"name": " "}, expect=422) check("a blank name and a non-colour are refused", True) @@ -109,7 +143,7 @@ def main() -> int: "/references", { "type": "journal-article", - "title": "技术惯性与冲量的耦合机制", + "title": f"技术惯性与冲量的耦合机制({TOKEN})", "subtitle": "基于科学与技术融合网络的实证分析", "title_translated": "Coupling inertia and impulse in technology evolution", "container_title": "Technological Forecasting and Social Change", @@ -126,7 +160,11 @@ def main() -> int: "authors": [ {"name": "张三", "family_name": "张", "given_name": "三"}, {"name": "李四"}, - {"name": "Wang Wu", "family_name": "Wang", "given_name": "Wu"}, + { + "name": f"Wang Wu {AUTHOR_TOKEN}", + "family_name": "Wang", + "given_name": "Wu", + }, {"name": "世界卫生组织", "role": "compiler"}, ], }, @@ -137,7 +175,8 @@ def main() -> int: check("four contributors stored", len(article["authors"]) == 4) check( "they keep the order they were sent in", - [a["name"] for a in article["authors"]] == ["张三", "李四", "Wang Wu", "世界卫生组织"], + [a["name"] for a in article["authors"]] + == ["张三", "李四", f"Wang Wu {AUTHOR_TOKEN}", "世界卫生组织"], str([a["name"] for a in article["authors"]]), ) check( @@ -151,7 +190,8 @@ def main() -> int: check("not cited yet", article["citation_count"] == 0) check( "both tags are on the row", - sorted(tag["name"] for tag in article["tags"]) == ["必读", "综述"], + sorted(tag["name"] for tag in article["tags"]) + == [f"必读-{TOKEN}", f"综述-{TOKEN}"], str(article["tags"]), ) @@ -159,11 +199,15 @@ def main() -> int: missing_tag = call( "POST", "/references", - {"title": "引用了不存在的标签", "type": "book", "tag_ids": [999999]}, + { + "title": f"引用了不存在的标签{TOKEN}", + "type": "book", + "tag_ids": [999999], + }, expect=400, ) check("the refusal names the id", "999999" in missing_tag["detail"], missing_tag["detail"]) - orphan = call("GET", "/references?keyword=引用了不存在的标签") + orphan = call("GET", f"/references?keyword=引用了不存在的标签{TOKEN}") check("nothing was stored half-labelled", orphan["total"] == 0, str(orphan["total"])) print("5. add a patent, which needs a country and a number") @@ -172,7 +216,7 @@ def main() -> int: "/references", { "type": "patent", - "title": "一种基于多层聚类的人工智能技术识别方法", + "title": f"一种基于多层聚类的人工智能技术识别方法({TOKEN})", "patent_country": "中国", "patent_number": "CN114000000A", "publication_year": 2023, @@ -185,7 +229,7 @@ def main() -> int: check("patent stored", patent["type"] == "patent" and patent["patent_number"] == "CN114000000A") check( "tagging works on create for more than one entry", - [tag["name"] for tag in patent["tags"]] == ["必读"], + [tag["name"] for tag in patent["tags"]] == [f"必读-{TOKEN}"], str(patent["tags"]), ) @@ -204,28 +248,40 @@ def main() -> int: call( "POST", "/references", - {"title": "没有标签的书", "authors": [{"name": "甲"}], "type": "book"}, + { + "title": f"没有标签的书{TOKEN}", + "authors": [{"name": "甲"}], + "type": "book", + }, expect=201, ) - probe = call("GET", "/references?keyword=没有标签的书") + probe = call("GET", f"/references?keyword=没有标签的书{TOKEN}") created_references.append(probe["items"][0]["id"]) check("a book needs nothing else", probe["total"] == 1) check("and may carry no tags at all", probe["items"][0]["tags"] == []) print("8. find it again: by author, by type, by year, by tag") - by_author = call("GET", "/references?keyword=Wang") - check("search matches an author name", by_author["total"] == 1, str(by_author["total"])) + by_author = call("GET", f"/references?keyword={AUTHOR_TOKEN}") + check( + "search matches an author name", + by_author["total"] == 1, + f'{by_author["total"]} (the token is on one author name)', + ) check( "the row prints an author line", by_author["items"][0]["authors_line"].startswith("张三, 李四, Wang Wu"), by_author["items"][0]["authors_line"], ) - by_type = call("GET", "/references?type=patent") + by_type = call("GET", f"/references?type=patent&keyword={TOKEN}") check("filter by type", by_type["total"] == 1, str(by_type["total"])) - by_year = call("GET", "/references?year=2024") + by_year = call("GET", f"/references?year=2024&keyword={TOKEN}") check("filter by year", by_year["total"] == 1, str(by_year["total"])) - uncited = call("GET", "/references?cited=false") - check("filter by not-yet-cited", uncited["total"] == len(created_references)) + uncited = call("GET", f"/references?cited=false&keyword={TOKEN}") + check( + "filter by not-yet-cited", + uncited["total"] == len(created_references), + f'{uncited["total"]} of {len(created_references)}', + ) by_tag = call("GET", f"/references?tag={must_read['id']}") check("filter by tag", by_tag["total"] == 2, str(by_tag["total"])) @@ -240,31 +296,43 @@ def main() -> int: ) print("9. the tag table counts what carries each label") - tags = call("GET", "/reference-tags") + tags = call("GET", f"/reference-tags?keyword={TOKEN}") counts = {tag["name"]: tag["reference_count"] for tag in tags["items"]} - check("two labels in the library", tags["total"] == 2, str(tags["total"])) - check("the shared label counts two rows", counts["必读"] == 2, str(counts)) - check("the other counts one", counts["综述"] == 1, str(counts)) - found = call("GET", "/reference-tags?keyword=综述") + check("two labels carry this run's token", tags["total"] == 2, str(tags["total"])) + check( + "the shared label counts two rows", + counts[f"必读-{TOKEN}"] == 2, + str(counts), + ) + check("the other counts one", counts[f"综述-{TOKEN}"] == 1, str(counts)) + found = call("GET", f"/reference-tags?keyword=综述-{TOKEN}") check("tags are searchable by name", found["total"] == 1, str(found["total"])) print("10. renaming a label follows it everywhere it is used") renamed = call( "PATCH", f"/reference-tags/{survey['id']}", - {"name": "综述文章", "note": "综述与述评"}, + {"name": f"综述文章-{TOKEN}", "note": "综述与述评"}, ) - check("the label is renamed", renamed["name"] == "综述文章") + check("the label is renamed", renamed["name"] == f"综述文章-{TOKEN}") after = call("GET", f"/references/{article['id']}") check( "the reference prints the new name", - sorted(tag["name"] for tag in after["tags"]) == ["必读", "综述文章"], + sorted(tag["name"] for tag in after["tags"]) + == [f"必读-{TOKEN}", f"综述文章-{TOKEN}"], str(after["tags"]), ) taken = call( - "PATCH", f"/reference-tags/{survey['id']}", {"name": "必读"}, expect=409 + "PATCH", + f"/reference-tags/{survey['id']}", + {"name": f"必读-{TOKEN}"}, + expect=409, + ) + check( + "renaming onto a taken name is refused", + TOKEN in taken["detail"], + taken["detail"], ) - check("renaming onto a taken name is refused", "必读" in taken["detail"], taken["detail"]) print("11. edit it: replace the contributor list, a field and its tags") updated = call( @@ -283,7 +351,7 @@ def main() -> int: check("author list replaced wholesale", len(updated["authors"]) == 2) check( "tag list replaced wholesale too", - [tag["name"] for tag in updated["tags"]] == ["必读"], + [tag["name"] for tag in updated["tags"]] == [f"必读-{TOKEN}"], str(updated["tags"]), ) check("untouched fields kept", updated["container_title"] == "Technological Forecasting and Social Change") @@ -291,7 +359,7 @@ def main() -> int: cleared = call("PATCH", f"/references/{patent['id']}", {"tag_ids": None}) check( "omitting tags leaves them alone", - [tag["name"] for tag in cleared["tags"]] == ["必读"], + [tag["name"] for tag in cleared["tags"]] == [f"必读-{TOKEN}"], str(cleared["tags"]), ) @@ -300,7 +368,10 @@ def main() -> int: paper = call( "POST", "/papers", - {"title": "参考文献冒烟测试论文", "template_id": templates[0]["id"]}, + { + "title": f"参考文献冒烟测试论文{TOKEN}", + "template_id": templates[0]["id"], + }, expect=201, ) created_papers.append(paper["id"]) @@ -338,7 +409,7 @@ def main() -> int: check("and the paper", citations[0]["paper_id"] == paper["id"]) check("and the paragraph heading", citations[0]["paragraph_name"] is not None, str(citations[0])) - cited = call("GET", "/references?cited=true") + cited = call("GET", f"/references?cited=true&keyword={TOKEN}") check("the cited filter now finds it", cited["total"] == 1, str(cited["total"])) print("14. a cited reference cannot be deleted; an uncited one can") @@ -361,7 +432,25 @@ def main() -> int: str(left["total"]), ) - print("16. clean up") + print("16. the 参考文献 list prints one entry per work, in Springer style") + document = call("GET", f"/papers/{paper['id']}/document") + check("the style is named in the response", document["reference_style"] == "springer") + check( + "one entry for the one work cited", + len(document["references"]) == 1, + str(len(document["references"])), + ) + entry = document["references"][0] + check("the entry is a bibliography line", entry["text"].startswith("张三, 李四"), entry["text"]) + check("and carries the year and the DOI", "(2024)" in entry["text"] and "https://doi.org/10.1016/j.techfore.2024.123456" in entry["text"], entry["text"]) + check("it names the places it is cited", len(entry["citations"]) == 1, str(len(entry["citations"]))) + check( + "and the in-text marker is that entry's number", + entry["citations"][0]["index"] == entry["index"], + f'{entry["citations"][0]["index"]} vs {entry["index"]}', + ) + + print("17. clean up") call("DELETE", f"/papers/{paper['id']}", expect=204) created_papers.clear() for reference_id in created_references: @@ -370,13 +459,13 @@ def main() -> int: for tag_id in created_tags: call("DELETE", f"/reference-tags/{tag_id}", expect=204) created_tags.clear() - remaining = call("GET", "/references") + # Scoped by the token rather than by "the library is empty": the library + # this runs against holds real literature, and what must be true is that + # none of *this run's* rows survive. + remaining = call("GET", f"/references?keyword={TOKEN}") check("nothing of ours is left", remaining["total"] == 0, str(remaining["total"])) - check( - "and no labels either", - call("GET", "/reference-tags")["total"] == 0, - str(call("GET", "/reference-tags")["total"]), - ) + tags_left = call("GET", f"/reference-tags?keyword={TOKEN}") + check("and no labels either", tags_left["total"] == 0, str(tags_left["total"])) print(f"\nall {_checks} checks passed") return 0