Skip to content

Commit fbe9bd4

Browse files
committed
[NRT-771] Count AH-DB queries in the submit guard, not reader method names
Review caught the guard being weaker than it claimed. It spied on `note_data` and `note_type_dict` by name, and the fix removed `note_data` from that path entirely, so the observed pair was (0, 1) for both runs. Regressing to call the *batched* `notes_data_for_anki_nids([nid])` once per note would issue thousands of queries at the bulk cap and leave the test green — the earlier check only proved it catches a return to the one implementation it was written against. Count at the peewee layer instead, so a per-note read reintroduced through any helper is caught. Verified against that scenario: it now fails 12 == 24. Submit both new-note and change candidates too. The media step runs over each list separately, so a change-suggestion-only regression was uncovered.
1 parent 01b4921 commit fbe9bd4

1 file changed

Lines changed: 25 additions & 15 deletions

File tree

tests/addon/test_integration.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2546,32 +2546,42 @@ def test_bulk_submit_does_not_read_the_ah_db_per_note(
25462546
install_ah_deck: InstallAHDeck,
25472547
next_deterministic_uuid: Callable[[], uuid.UUID],
25482548
import_ah_note_type: ImportAHNoteType,
2549+
import_ah_note: ImportAHNote,
25492550
add_anki_note: AddAnkiNote,
25502551
):
25512552
"""Submitting reads the AnkiHub DB per batch and per note type, never per note.
2552-
Asserting the counts don't grow with the note count rather than pinning exact
2553-
numbers keeps this robust to unrelated changes in the submit path. At the
2554-
2000-note bulk cap a reintroduced per-note read costs thousands of queries, and
2555-
no other test would notice.
2553+
2554+
Counts queries at the peewee layer rather than spying on particular reader
2555+
methods, so a per-note read reintroduced through any helper is caught, not just
2556+
a return to the specific one this was written against. Asserting the count
2557+
doesn't grow with the note count rather than pinning an exact number keeps it
2558+
robust to unrelated changes in the submit path. At the 2000-note bulk cap a
2559+
per-note read costs thousands of queries and no other test would notice.
25562560
"""
25572561
with anki_session_with_addon_data.profile_loaded():
25582562
ah_did = install_ah_deck()
25592563
note_type = import_ah_note_type(ah_did=ah_did)
2564+
mid = NotetypeId(note_type["id"])
25602565
mocker.patch.object(AnkiHubClient, "create_suggestions_in_bulk", return_value={})
2561-
note_data_spy = mocker.spy(ankihub_db, "note_data")
2562-
note_type_dict_spy = mocker.spy(ankihub_db, "note_type_dict")
2566+
query_spy = mocker.spy(ankihub_db.db, "execute_sql")
25632567

2564-
def ah_db_reads_for_submitting(note_count: int) -> Tuple[int, int]:
2568+
def ah_db_queries_for_submitting(note_count: int) -> int:
2569+
# Both suggestion kinds: the media step runs over each list separately.
25652570
notes = []
25662571
for i in range(note_count):
2567-
note = add_anki_note(note_type=note_type)
2568-
note["Front"] = f"front_{len(notes)}_{i}"
2569-
aqt.mw.col.update_note(note)
2570-
notes.append(note)
2572+
new_note = add_anki_note(note_type=note_type)
2573+
new_note["Front"] = f"new_{note_count}_{i}"
2574+
aqt.mw.col.update_note(new_note)
2575+
notes.append(new_note)
2576+
2577+
note_info = import_ah_note(ah_did=ah_did, mid=mid)
2578+
changed_note = aqt.mw.col.get_note(ankihub_db.anki_nid_for_ankihub_nid(note_info.ah_nid))
2579+
changed_note["Front"] = f"changed_{note_count}_{i}"
2580+
aqt.mw.col.update_note(changed_note)
2581+
notes.append(changed_note)
25712582

25722583
mocker.patch("uuid.uuid4", side_effect=[next_deterministic_uuid() for _ in range(note_count)])
2573-
note_data_spy.reset_mock()
2574-
note_type_dict_spy.reset_mock()
2584+
query_spy.reset_mock()
25752585

25762586
suggest_notes_in_bulk(
25772587
ankihub_did=ah_did,
@@ -2581,9 +2591,9 @@ def ah_db_reads_for_submitting(note_count: int) -> Tuple[int, int]:
25812591
comment="test",
25822592
media_upload_cb=mocker.stub(),
25832593
)
2584-
return note_data_spy.call_count, note_type_dict_spy.call_count
2594+
return query_spy.call_count
25852595

2586-
assert ah_db_reads_for_submitting(2) == ah_db_reads_for_submitting(6)
2596+
assert ah_db_queries_for_submitting(2) == ah_db_queries_for_submitting(6)
25872597

25882598
@pytest.mark.parametrize(
25892599
"note_has_changes, note_is_marked_as_deleted",

0 commit comments

Comments
 (0)