forge
Explore
Log in

Don't you dare go Hollow

🧪 pin the counter backfills to the counts they claim

Browse files at this commit

c391cb5ffe09cb1e93fc1626a7629dbdf84ff5b8Ahmet Kaan GümüşParent: 079af73
  • test
    • 🧪 pin the counter backfills to the counts they claim
Show full message
test: 🧪 pin the counter backfills to the counts they claim

term.course_count, kind_ref and slot_ref were probe-verified by hand and
by nothing else. Both tests age a volume to the pre-counter shape, boot,
assert the exact seeded values, and then assert the bite: a term with
courses refuses its delete, a referenced exam kind and meal slot refuse
their drop. Seeding zero instead makes both fail.
tests/persistence.rsmodified+197 -0
@@ -1606,6 +1606,203 @@ async fn subject_reference_counts_are_seeded_from_the_rows_that_predate_them() {
1606 assert_eq!(res.status, StatusCode::NO_CONTENT, "{}", res.body);1606 assert_eq!(res.status, StatusCode::NO_CONTENT, "{}", res.body);
1607 }1607 }
1608 1608
1609+/// A term written before its refcount existed (2026-07-27) carries none, and an
1610+/// absent counter reads as zero — which would let a manager delete the term half
1611+/// the timetable still points at. The backfill counts the courses that actually
1612+/// link to each term, once; a term nobody links to gets an explicit zero, and a
1613+/// course with no term at all must not be counted into anyone's bucket.
1614+///
1615+/// The bite is the last pair of assertions: seed the counter wrong (or not at
1616+/// all) and the linked term's delete comes back `204` instead of `409`.
1617+#[tokio::test]
1618+async fn term_course_counts_are_seeded_from_the_courses_that_link_to_them() {
1619+ let (app, db) = common::app_and_db().await;
1620+ let manager = common::login_as(&app, &db, "term_seed_m", "manager").await;
1621+
1622+ async fn create_term(app: &Router, cookie: &str, name: &str, starts_at: i64) -> String {
1623+ let res = send(
1624+ app,
1625+ "POST",
1626+ "/terms",
1627+ Some(cookie),
1628+ Some(json!({ "name": name, "starts_at": starts_at, "ends_at": starts_at + 1 })),
1629+ )
1630+ .await;
1631+ assert_eq!(res.status, StatusCode::CREATED, "{}", res.body);
1632+ common::id_of(&res.body)
1633+ }
1634+ async fn link_course(app: &Router, cookie: &str, title: &str, term: Option<&str>) {
1635+ let res = send(
1636+ app,
1637+ "POST",
1638+ "/courses",
1639+ Some(cookie),
1640+ Some(json!({ "title": title, "term_id": term })),
1641+ )
1642+ .await;
1643+ assert_eq!(res.status, StatusCode::CREATED, "{}", res.body);
1644+ }
1645+
1646+ let linked = create_term(&app, &manager, "linked term", 1).await;
1647+ let empty = create_term(&app, &manager, "empty term", 10).await;
1648+ link_course(&app, &manager, "History", Some(&linked)).await;
1649+ link_course(&app, &manager, "Physics", Some(&linked)).await;
1650+ link_course(&app, &manager, "Floating", None).await;
1651+
1652+ // Age both terms into the pre-counter shape.
1653+ db.query("UPDATE term UNSET course_count;")
1654+ .await
1655+ .expect("age the rows")
1656+ .check()
1657+ .expect("age the rows");
1658+
1659+ let app = reboot(&db).await;
1660+
1661+ let mut counted = db
1662+ .query("SELECT VALUE course_count FROM term ORDER BY starts_at ASC")
1663+ .await
1664+ .expect("counter read")
1665+ .check()
1666+ .expect("counter read");
1667+ assert_eq!(
1668+ counted.take::<Vec<i64>>(0).expect("counter column"),
1669+ vec![2, 0],
1670+ "each term must be seeded from the courses that link to it, \
1671+ and the termless course from neither"
1672+ );
1673+
1674+ // The seeded counter is the delete guard itself, not a decoration.
1675+ let res = send(
1676+ &app,
1677+ "DELETE",
1678+ &format!("/terms/{linked}"),
1679+ Some(&manager),
1680+ None,
1681+ )
1682+ .await;
1683+ assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body);
1684+ let res = send(
1685+ &app,
1686+ "DELETE",
1687+ &format!("/terms/{empty}"),
1688+ Some(&manager),
1689+ None,
1690+ )
1691+ .await;
1692+ assert_eq!(res.status, StatusCode::NO_CONTENT, "{}", res.body);
1693+}
1694+
1695+/// The settings removal guards are two reference-counter tables (2026-07-27):
1696+/// `kind_ref:<name>` counts the marks graded under an exam kind, `slot_ref:<name>`
1697+/// the menus published for a meal slot, and a name may leave the list exactly
1698+/// while its counter reads zero. A volume written before those tables existed
1699+/// carries no rows at all, and a missing row reads as zero — which would let a
1700+/// manager drop a kind the whole school is already graded under. The backfill
1701+/// counts what actually points at each name, once; a name nobody ever used needs
1702+/// no row (there is no zero pass here, deliberately).
1703+///
1704+/// The bite is the last three assertions: seed the counters at zero (or not at
1705+/// all) and the removals come back `200` instead of `409`.
1706+#[tokio::test]
1707+async fn settings_reference_counts_are_seeded_from_the_marks_and_menus_that_predate_them() {
1708+ let (app, db) = common::app_and_db().await;
1709+ let teacher = common::login_as(&app, &db, "ref_seed_t", "teacher").await;
1710+ let manager = common::login_as(&app, &db, "ref_seed_m", "manager").await;
1711+ let ali = common::login(&app, "ref_seed_ali").await;
1712+ let veli = common::login(&app, "ref_seed_veli").await;
1713+ let (ali_id, veli_id) = (me_id(&app, &ali).await, me_id(&app, &veli).await);
1714+
1715+ // Two marks under `final`, none under any other kind.
1716+ let course = create_course(&app, &teacher, "Biology").await;
1717+ enroll(&app, &teacher, &course, &ali_id).await;
1718+ enroll(&app, &teacher, &course, &veli_id).await;
1719+ let exam = create_exam(&app, &teacher, &course, "Final", "final").await;
1720+ for student in [&ali_id, &veli_id] {
1721+ let res = send(
1722+ &app,
1723+ "POST",
1724+ &format!("/exams/{exam}/results"),
1725+ Some(&teacher),
1726+ Some(json!({ "user_id": student, "mark": 70 })),
1727+ )
1728+ .await;
1729+ assert_eq!(res.status, StatusCode::OK, "{}", res.body);
1730+ }
1731+
1732+ // Two menus under `lunch`, none under any other slot.
1733+ for date in ["2026-09-21", "2026-09-22"] {
1734+ let res = send(
1735+ &app,
1736+ "POST",
1737+ "/meals/menus",
1738+ Some(&manager),
1739+ Some(json!({ "date": date, "slot": "lunch" })),
1740+ )
1741+ .await;
1742+ assert_eq!(res.status, StatusCode::CREATED, "{}", res.body);
1743+ }
1744+
1745+ // Age the volume into the pre-counter shape: the tables did not exist, so
1746+ // neither did any of their rows.
1747+ db.query("DELETE kind_ref; DELETE slot_ref;")
1748+ .await
1749+ .expect("age the volume")
1750+ .check()
1751+ .expect("age the volume");
1752+
1753+ let app = reboot(&db).await;
1754+
1755+ let mut counted = db
1756+ .query(
1757+ "SELECT VALUE [record::id(id), count ?? 0] FROM kind_ref ORDER BY id ASC; \
1758+ SELECT VALUE [record::id(id), count ?? 0] FROM slot_ref ORDER BY id ASC;",
1759+ )
1760+ .await
1761+ .expect("counter read")
1762+ .check()
1763+ .expect("counter read");
1764+ assert_eq!(
1765+ counted.take::<Vec<(String, i64)>>(0).expect("kind_ref"),
1766+ vec![("final".to_string(), 2)],
1767+ "the graded kind must be seeded from its marks, and only it"
1768+ );
1769+ assert_eq!(
1770+ counted.take::<Vec<(String, i64)>>(1).expect("slot_ref"),
1771+ vec![("lunch".to_string(), 2)],
1772+ "the published slot must be seeded from its menus, and only it"
1773+ );
1774+
1775+ // The seeded counters are the removal guards themselves, not decoration:
1776+ // the referenced names are stuck, an unreferenced one still leaves freely.
1777+ let res = send(
1778+ &app,
1779+ "PATCH",
1780+ "/settings",
1781+ Some(&manager),
1782+ Some(json!({ "exam_kinds": [{ "name": "quiz", "weight": 1 }] })),
1783+ )
1784+ .await;
1785+ assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body);
1786+ let res = send(
1787+ &app,
1788+ "PATCH",
1789+ "/settings",
1790+ Some(&manager),
1791+ Some(json!({ "meal_slots": [{ "name": "breakfast" }, { "name": "snack" }] })),
1792+ )
1793+ .await;
1794+ assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body);
1795+ let res = send(
1796+ &app,
1797+ "PATCH",
1798+ "/settings",
1799+ Some(&manager),
1800+ Some(json!({ "meal_slots": [{ "name": "breakfast" }, { "name": "lunch" }] })),
1801+ )
1802+ .await;
1803+ assert_eq!(res.status, StatusCode::OK, "{}", res.body);
1804+}
1805+
1609 /// A fee plan's installments are an `array<object>` on the plan row, and the1806 /// A fee plan's installments are an `array<object>` on the plan row, and the
1610 /// charges it raised are frozen copies of them. A SCHEMAFULL re-migration1807 /// charges it raised are frozen copies of them. A SCHEMAFULL re-migration
1611 /// rewrites every field definition it owns, so this is where a nested array1808 /// rewrites every field definition it owns, so this is where a nested array