📝 stop a question or picture from outliving its exam
Browse files at this commit317df96b84d648508a668f254459b959a857def6Parent: 6b7beb1- fix
- 📝 stop a question or picture from outliving its exam
5 changed files +292 -35
- src/domain/bank_question.rs +11 -2
- src/domain/exam.rs +209 -21
- src/domain/exam_attempt.rs +50 -5
- src/domain/question_image.rs +15 -5
- src/domain/subject.rs +7 -2
src/domain/bank_question.rsmodified+11 -2
| @@ -770,8 +770,17 @@ mod tests { | ||
| 770 | 770 | let used_once = template("once").await.unwrap(); |
| 771 | 771 | let unused = template("never").await.unwrap(); |
| 772 | 772 | |
| 773 | - let exam = ExamId::generate(); | |
| 774 | - let other_exam = ExamId::generate(); | |
| 773 | + // Real exam rows: instantiating a template moves the exam's counter | |
| 774 | + // (what keeps a question from outliving its exam), so a minted id | |
| 775 | + // nothing wrote is a 404. | |
| 776 | + let exam = crate::domain::exam::published_exam(&db) | |
| 777 | + .await | |
| 778 | + .get_id() | |
| 779 | + .clone(); | |
| 780 | + let other_exam = crate::domain::exam::published_exam(&db) | |
| 781 | + .await | |
| 782 | + .get_id() | |
| 783 | + .clone(); | |
| 775 | 784 | // A real subject row: an exam question claims a reference on its |
| 776 | 785 | // subject, so a minted id it never wrote would be refused. |
| 777 | 786 | let subject = crate::domain::subject::Subject::create( |
src/domain/exam.rsmodified+209 -21
| @@ -666,31 +666,38 @@ impl Exam { | ||
| 666 | 666 | } |
| 667 | 667 | } |
| 668 | 668 | |
| 669 | +/// An unscheduled published exam — the minimum any test that writes a *child* | |
| 670 | +/// of an exam needs, in any module: every such write moves the exam row (see | |
| 671 | +/// [`ExamAttempt::write_unfrozen_with`](crate::domain::exam_attempt::ExamAttempt) | |
| 672 | +/// and [`crate::domain::exam_answer::ExamAnswer::save`]), so a minted id whose | |
| 673 | +/// row was never created is a 404 rather than a silent orphan. | |
| 674 | +#[cfg(test)] | |
| 675 | +pub(crate) async fn published_exam(db: &Database) -> Exam { | |
| 676 | + let allowed: Vec<ExamKindDef> = crate::domain::settings::Settings::defaults() | |
| 677 | + .get_exam_kinds() | |
| 678 | + .to_vec(); | |
| 679 | + Exam::create( | |
| 680 | + &UserId::generate(), | |
| 681 | + &CourseId::generate(), | |
| 682 | + ExamTitle::try_new("midterm").unwrap(), | |
| 683 | + ExamDescription::try_new("").unwrap(), | |
| 684 | + ExamKind::try_new("midterm", &allowed).unwrap(), | |
| 685 | + ExamSchedule::try_new(None, None, None, None).unwrap(), | |
| 686 | + ExamAttemptLimit::try_new(1).unwrap(), | |
| 687 | + true, | |
| 688 | + false, | |
| 689 | + false, | |
| 690 | + db, | |
| 691 | + ) | |
| 692 | + .await | |
| 693 | + .unwrap() | |
| 694 | +} | |
| 695 | + | |
| 669 | 696 | #[cfg(test)] |
| 670 | 697 | mod tests { |
| 671 | 698 | use super::*; |
| 672 | 699 | |
| 673 | - /// An unscheduled published exam, the minimum this file's write tests need. | |
| 674 | - async fn published(db: &Database) -> Exam { | |
| 675 | - let allowed: Vec<ExamKindDef> = crate::domain::settings::Settings::defaults() | |
| 676 | - .get_exam_kinds() | |
| 677 | - .to_vec(); | |
| 678 | - Exam::create( | |
| 679 | - &UserId::generate(), | |
| 680 | - &CourseId::generate(), | |
| 681 | - ExamTitle::try_new("midterm").unwrap(), | |
| 682 | - ExamDescription::try_new("").unwrap(), | |
| 683 | - ExamKind::try_new("midterm", &allowed).unwrap(), | |
| 684 | - ExamSchedule::try_new(None, None, None, None).unwrap(), | |
| 685 | - ExamAttemptLimit::try_new(1).unwrap(), | |
| 686 | - true, | |
| 687 | - false, | |
| 688 | - false, | |
| 689 | - db, | |
| 690 | - ) | |
| 691 | - .await | |
| 692 | - .unwrap() | |
| 693 | - } | |
| 700 | + use crate::domain::exam::published_exam as published; | |
| 694 | 701 | |
| 695 | 702 | fn edit(exam: &Exam) -> (ExamTitle, ExamDescription, ExamKind, ExamSchedule) { |
| 696 | 703 | ( |
| @@ -1166,4 +1173,185 @@ mod tests { | ||
| 1166 | 1173 | ); |
| 1167 | 1174 | assert_eq!(answers, 0, "an answer outlived its exam"); |
| 1168 | 1175 | } |
| 1176 | + | |
| 1177 | + /// The same defect on the teacher's side of the sheet, and a worse one: a | |
| 1178 | + /// question written inside the delete window kept the reference it claimed | |
| 1179 | + /// on its subject (the cascade's per-subject decrement counted only the | |
| 1180 | + /// rows it could see), and [`crate::domain::subject::Subject::delete`] is | |
| 1181 | + /// gated on that count reading zero — a subject nobody could ever delete | |
| 1182 | + /// again, hanging off an exam nobody could ever see. The freeze gate is a | |
| 1183 | + /// *read* of `exam_attempt` and never survived this window; | |
| 1184 | + /// [`ExamAttempt::write_unfrozen_with`] now writes the exam row too. | |
| 1185 | + /// | |
| 1186 | + /// Same seam, same `#[ignore]`, same reason as the answer twin above: the | |
| 1187 | + /// subject *is* the store's conflict detection, which the in-memory engine | |
| 1188 | + /// does not have. | |
| 1189 | + #[tokio::test(flavor = "multi_thread", worker_threads = 4)] | |
| 1190 | + #[ignore = "needs a real SurrealDB server: podman start hezarfen-surrealdb && cargo test -- --ignored"] | |
| 1191 | + async fn a_question_written_inside_a_delete_never_outlives_the_exam() { | |
| 1192 | + use crate::domain::exam_question::{ | |
| 1193 | + ChoiceInput, ExamQuestion, QuestionKind, QuestionPoints, QuestionSpec, QuestionText, | |
| 1194 | + }; | |
| 1195 | + use crate::domain::subject::Subject; | |
| 1196 | + let (db, _serialized) = crate::database::init_test_server("exam_question_race").await; | |
| 1197 | + db.query( | |
| 1198 | + "DEFINE EVENT hold_the_window ON TABLE exam WHEN $event = 'DELETE' \ | |
| 1199 | + THEN { SLEEP 1s; };", | |
| 1200 | + ) | |
| 1201 | + .await | |
| 1202 | + .unwrap() | |
| 1203 | + .check() | |
| 1204 | + .unwrap(); | |
| 1205 | + | |
| 1206 | + let (mut questions, mut swept, mut stuck) = (0, 0, 0); | |
| 1207 | + for round in 0..4 { | |
| 1208 | + let exam = published(&db).await; | |
| 1209 | + let id = exam.get_id().clone(); | |
| 1210 | + let subject = Subject::create( | |
| 1211 | + &CourseId::generate(), | |
| 1212 | + crate::domain::subject::SubjectName::try_new("topic").unwrap(), | |
| 1213 | + crate::domain::subject::SubjectDescription::try_new("").unwrap(), | |
| 1214 | + &db, | |
| 1215 | + ) | |
| 1216 | + .await | |
| 1217 | + .unwrap(); | |
| 1218 | + | |
| 1219 | + let drop_it = { | |
| 1220 | + let db = db.clone(); | |
| 1221 | + tokio::spawn(async move { exam.delete(&db).await }) | |
| 1222 | + }; | |
| 1223 | + tokio::time::sleep(std::time::Duration::from_millis(300)).await; | |
| 1224 | + let child = { | |
| 1225 | + let (db, exam_id, on) = (db.clone(), id.clone(), subject.get_id().clone()); | |
| 1226 | + tokio::spawn(async move { | |
| 1227 | + let spec = QuestionSpec::try_new( | |
| 1228 | + QuestionKind::try_new("choice").unwrap(), | |
| 1229 | + Some(vec![ | |
| 1230 | + ChoiceInput { | |
| 1231 | + id: Some("a".into()), | |
| 1232 | + text: "5".into(), | |
| 1233 | + }, | |
| 1234 | + ChoiceInput { | |
| 1235 | + id: Some("b".into()), | |
| 1236 | + text: "6".into(), | |
| 1237 | + }, | |
| 1238 | + ]), | |
| 1239 | + Some("b".into()), | |
| 1240 | + &[], | |
| 1241 | + ) | |
| 1242 | + .unwrap(); | |
| 1243 | + ExamQuestion::create( | |
| 1244 | + &exam_id, | |
| 1245 | + on, | |
| 1246 | + QuestionText::try_new("3 + 3?").unwrap(), | |
| 1247 | + QuestionPoints::try_new(5).unwrap(), | |
| 1248 | + spec, | |
| 1249 | + &db, | |
| 1250 | + ) | |
| 1251 | + .await | |
| 1252 | + }) | |
| 1253 | + }; | |
| 1254 | + let (drop_it, child) = (drop_it.await.unwrap(), child.await.unwrap()); | |
| 1255 | + assert!( | |
| 1256 | + !matches!(child, Err(AppError::Db(_))), | |
| 1257 | + "round {round}: a raced question write must be answered, not 500: {child:?}" | |
| 1258 | + ); | |
| 1259 | + | |
| 1260 | + if Exam::read(&id, &db).await.unwrap().is_none() { | |
| 1261 | + swept += 1; | |
| 1262 | + questions += ExamQuestion::list_for_exam(&id, None, 0, &db) | |
| 1263 | + .await | |
| 1264 | + .unwrap() | |
| 1265 | + .0 | |
| 1266 | + .len(); | |
| 1267 | + // The subject has to be free again: a stranded reference is the | |
| 1268 | + // half of this bug a row count alone would not catch. | |
| 1269 | + if Subject::read(subject.get_id(), &db) | |
| 1270 | + .await | |
| 1271 | + .unwrap() | |
| 1272 | + .unwrap() | |
| 1273 | + .delete(&db) | |
| 1274 | + .await | |
| 1275 | + .is_err() | |
| 1276 | + { | |
| 1277 | + stuck += 1; | |
| 1278 | + } | |
| 1279 | + } else if drop_it.is_ok() { | |
| 1280 | + panic!("round {round}: the delete reported success but the exam is still there"); | |
| 1281 | + } | |
| 1282 | + } | |
| 1283 | + eprintln!("Exam::delete raced by a question write: {swept}/4 rounds deleted the exam"); | |
| 1284 | + assert!( | |
| 1285 | + swept > 0, | |
| 1286 | + "no round ever deleted the exam, so the window was never reached" | |
| 1287 | + ); | |
| 1288 | + assert_eq!(questions, 0, "a question outlived its exam"); | |
| 1289 | + assert_eq!(stuck, 0, "an orphan question left its subject undeletable"); | |
| 1290 | + } | |
| 1291 | + | |
| 1292 | + /// A picture is written through the same freeze gate as the question it | |
| 1293 | + /// hangs on, so it had the same hole — and one the row count does not even | |
| 1294 | + /// show: `delete_exam` collects the blob names to unlink *before* it calls | |
| 1295 | + /// [`Exam::delete`], so an image row landing after that snapshot strands | |
| 1296 | + /// its bytes on disk forever as well. | |
| 1297 | + #[tokio::test(flavor = "multi_thread", worker_threads = 4)] | |
| 1298 | + #[ignore = "needs a real SurrealDB server: podman start hezarfen-surrealdb && cargo test -- --ignored"] | |
| 1299 | + async fn a_picture_written_inside_a_delete_never_outlives_the_exam() { | |
| 1300 | + use crate::domain::note_file::FileContentType; | |
| 1301 | + use crate::domain::question_image::QuestionImage; | |
| 1302 | + let (db, _serialized) = crate::database::init_test_server("question_image_race").await; | |
| 1303 | + db.query( | |
| 1304 | + "DEFINE EVENT hold_the_window ON TABLE exam WHEN $event = 'DELETE' \ | |
| 1305 | + THEN { SLEEP 1s; };", | |
| 1306 | + ) | |
| 1307 | + .await | |
| 1308 | + .unwrap() | |
| 1309 | + .check() | |
| 1310 | + .unwrap(); | |
| 1311 | + | |
| 1312 | + let (mut images, mut swept) = (0, 0); | |
| 1313 | + for round in 0..4 { | |
| 1314 | + let exam = published(&db).await; | |
| 1315 | + let question = question_on(&exam, &db).await; | |
| 1316 | + let id = exam.get_id().clone(); | |
| 1317 | + | |
| 1318 | + let drop_it = { | |
| 1319 | + let db = db.clone(); | |
| 1320 | + tokio::spawn(async move { exam.delete(&db).await }) | |
| 1321 | + }; | |
| 1322 | + tokio::time::sleep(std::time::Duration::from_millis(300)).await; | |
| 1323 | + let child = { | |
| 1324 | + let (db, exam_id, on) = (db.clone(), id.clone(), question.get_id().clone()); | |
| 1325 | + tokio::spawn(async move { | |
| 1326 | + QuestionImage::new( | |
| 1327 | + &exam_id, | |
| 1328 | + &on, | |
| 1329 | + None, | |
| 1330 | + FileContentType::try_new("image/png").unwrap(), | |
| 1331 | + 3, | |
| 1332 | + ) | |
| 1333 | + .upsert(&db) | |
| 1334 | + .await | |
| 1335 | + }) | |
| 1336 | + }; | |
| 1337 | + let (drop_it, child) = (drop_it.await.unwrap(), child.await.unwrap()); | |
| 1338 | + assert!( | |
| 1339 | + !matches!(child, Err(AppError::Db(_))), | |
| 1340 | + "round {round}: a raced picture write must be answered, not 500: {child:?}" | |
| 1341 | + ); | |
| 1342 | + | |
| 1343 | + if Exam::read(&id, &db).await.unwrap().is_none() { | |
| 1344 | + swept += 1; | |
| 1345 | + images += QuestionImage::list_for_exam(&id, &db).await.unwrap().len(); | |
| 1346 | + } else if drop_it.is_ok() { | |
| 1347 | + panic!("round {round}: the delete reported success but the exam is still there"); | |
| 1348 | + } | |
| 1349 | + } | |
| 1350 | + eprintln!("Exam::delete raced by a picture write: {swept}/4 rounds deleted the exam"); | |
| 1351 | + assert!( | |
| 1352 | + swept > 0, | |
| 1353 | + "no round ever deleted the exam, so the window was never reached" | |
| 1354 | + ); | |
| 1355 | + assert_eq!(images, 0, "a picture outlived its exam"); | |
| 1356 | + } | |
| 1169 | 1357 | } |
src/domain/exam_attempt.rsmodified+50 -5
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | use surrealdb::types::{RecordId, RecordIdKey, SurrealValue}; |
| 2 | 2 | |
| 3 | -use crate::constant::{EXAM_ATTEMPT_TABLE, EXAM_SAT_TOTAL_FIELD}; | |
| 3 | +use crate::constant::{EXAM_ATTEMPT_TABLE, EXAM_RESULT_COUNT_FIELD, EXAM_SAT_TOTAL_FIELD}; | |
| 4 | 4 | use crate::database::{Database, transaction_with_retry}; |
| 5 | 5 | use crate::domain::exam::Exam; |
| 6 | 6 | use crate::domain::exam::ExamId; |
| @@ -14,6 +14,10 @@ use crate::error::AppError; | ||
| 14 | 14 | /// the two refused. |
| 15 | 15 | const FROZEN_MARK: &str = "questions_frozen"; |
| 16 | 16 | |
| 17 | +/// The `THROW` marker the exam-existence touch aborts with — the exam row this | |
| 18 | +/// write hangs off is gone, so the write is a 404 and nothing lands. | |
| 19 | +const GONE_MARK: &str = "no_exam"; | |
| 20 | + | |
| 17 | 21 | pub(crate) fn frozen_error() -> AppError { |
| 18 | 22 | AppError::Conflict("cannot change questions after attempts have started") |
| 19 | 23 | } |
| @@ -361,7 +365,34 @@ impl ExamAttempt { | ||
| 361 | 365 | /// all once `exam` has an attempt — the freeze gate, made atomic with the |
| 362 | 366 | /// write it guards instead of merely preceding it. `freeze_exam` is bound |
| 363 | 367 | /// here; the caller binds the rest and reads its own results from |
| 364 | - /// [`Self::FROZEN_SLOT`] onwards (`BEGIN` and the `IF` take a slot each). | |
| 368 | + /// [`Self::FROZEN_SLOT`] onwards. | |
| 369 | + /// | |
| 370 | + /// The same transaction *writes* the exam row — bumping its mark counter | |
| 371 | + /// and putting it straight back — which is what ties every child written | |
| 372 | + /// through here to its exam. The freeze gate only *reads* `exam_attempt`, | |
| 373 | + /// and a read does not survive [`Exam::delete`](crate::domain::exam::Exam::delete)'s | |
| 374 | + /// window: a question or picture landing after that delete removed the exam | |
| 375 | + /// but before it committed reads a row that is still there, while the | |
| 376 | + /// cascade's `DELETE exam_question WHERE exam = $ex` ran on a snapshot | |
| 377 | + /// predating this insert — so both commit and the child outlives the exam, | |
| 378 | + /// with neither caller told anything. Writing a key the delete also writes | |
| 379 | + /// makes the two collide and the store refuses one side. It is the shape | |
| 380 | + /// [`crate::domain::exam_answer::ExamAnswer::save`] and | |
| 381 | + /// [`crate::domain::menu::bump_menu_and_write`] already use. | |
| 382 | + /// | |
| 383 | + /// An orphan here is not merely untidy: an `exam_question` that outlives | |
| 384 | + /// its exam keeps the reference it claimed on its subject (the cascade's | |
| 385 | + /// per-subject decrement counted the rows it could see), and | |
| 386 | + /// [`crate::domain::subject::Subject::delete`] is gated on that count | |
| 387 | + /// reading zero — a subject nobody can ever delete again. | |
| 388 | + /// | |
| 389 | + /// The bump is restored *by captured value*, `NONE` included, so the row is | |
| 390 | + /// byte-identical afterwards: the boot backfill still finds the rows it | |
| 391 | + /// keys on (`WHERE result_count = NONE`) and a teacher's PATCH, which pins | |
| 392 | + /// that counter, is not refused because somebody added a question. Writing | |
| 393 | + /// the same value back would not do — an `UPDATE` that leaves the document | |
| 394 | + /// unchanged is elided and never reaches the store's write set, so it | |
| 395 | + /// collides with nothing. | |
| 365 | 396 | /// |
| 366 | 397 | /// This replaces a process-wide `EXAM_LOCK.write()` held across the check |
| 367 | 398 | /// and the write. That lock ordered the two requests but still read the |
| @@ -420,12 +451,18 @@ impl ExamAttempt { | ||
| 420 | 451 | "BEGIN TRANSACTION; |
| 421 | 452 | IF array::len((SELECT VALUE id FROM exam_attempt \ |
| 422 | 453 | WHERE exam = $freeze_exam LIMIT 1)) > 0 {{ THROW '{FROZEN_MARK}' }}; |
| 454 | + LET $was_results = \ | |
| 455 | + (SELECT VALUE {EXAM_RESULT_COUNT_FIELD} FROM ONLY $freeze_exam); | |
| 456 | + LET $touched = (UPDATE $freeze_exam SET {EXAM_RESULT_COUNT_FIELD} = \ | |
| 457 | + ({EXAM_RESULT_COUNT_FIELD} ?? 0) + 1 RETURN VALUE id); | |
| 458 | + IF array::len($touched) = 0 {{ THROW '{GONE_MARK}' }}; | |
| 459 | + UPDATE $freeze_exam SET {EXAM_RESULT_COUNT_FIELD} = $was_results; | |
| 423 | 460 | {statements} |
| 424 | 461 | COMMIT TRANSACTION;" |
| 425 | 462 | ); |
| 426 | 463 | let mut bound = vec![("freeze_exam".into(), exam.record().into_value())]; |
| 427 | 464 | bound.extend(bindings); |
| 428 | - let mut marks = vec![FROZEN_MARK]; | |
| 465 | + let mut marks = vec![FROZEN_MARK, GONE_MARK]; | |
| 429 | 466 | marks.extend(refusals.iter().map(|(marker, _)| *marker)); |
| 430 | 467 | let (result, mut errors) = transaction_with_retry(db, &sql, &bound, &marks).await?; |
| 431 | 468 | let thrown = |marker: &str| { |
| @@ -436,6 +473,12 @@ impl ExamAttempt { | ||
| 436 | 473 | if thrown(FROZEN_MARK) { |
| 437 | 474 | return Err(frozen_error()); |
| 438 | 475 | } |
| 476 | + // The exam is gone: the same 404 every one of these writes' handlers | |
| 477 | + // answers a missing exam with, and it outranks the callers' own gates | |
| 478 | + // for the freeze's reason — there is nothing left to refuse *about*. | |
| 479 | + if thrown(GONE_MARK) { | |
| 480 | + return Err(AppError::NotFound); | |
| 481 | + } | |
| 439 | 482 | for (marker, refusal) in refusals { |
| 440 | 483 | if thrown(marker) { |
| 441 | 484 | return Err(refusal); |
| @@ -447,8 +490,10 @@ impl ExamAttempt { | ||
| 447 | 490 | } |
| 448 | 491 | } |
| 449 | 492 | |
| 450 | - /// The first slot a [`Self::write_unfrozen`] caller's own statements land in. | |
| 451 | - pub(crate) const FROZEN_SLOT: usize = 2; | |
| 493 | + /// The first slot a [`Self::write_unfrozen`] caller's own statements land | |
| 494 | + /// in: `BEGIN`, the freeze `IF`, and the exam touch's two `LET`s, `IF` and | |
| 495 | + /// restoring `UPDATE` take one each. | |
| 496 | + pub(crate) const FROZEN_SLOT: usize = 6; | |
| 452 | 497 | |
| 453 | 498 | /// Whether anyone has started this exam — the gate that freezes `mode` |
| 454 | 499 | /// edits once an attempt exists. |
src/domain/question_image.rsmodified+15 -5
| @@ -229,6 +229,16 @@ mod tests { | ||
| 229 | 229 | use super::*; |
| 230 | 230 | use crate::domain::exam_question::{ChoiceInput, QuestionKind, QuestionSpec}; |
| 231 | 231 | |
| 232 | + /// A real exam row: an image write moves its exam's counter (that is what | |
| 233 | + /// keeps a picture from outliving its exam), so a minted id nothing wrote | |
| 234 | + /// is a 404. | |
| 235 | + async fn exam_row(db: &Database) -> ExamId { | |
| 236 | + crate::domain::exam::published_exam(db) | |
| 237 | + .await | |
| 238 | + .get_id() | |
| 239 | + .clone() | |
| 240 | + } | |
| 241 | + | |
| 232 | 242 | fn png() -> FileContentType { |
| 233 | 243 | FileContentType::try_new("image/png").unwrap() |
| 234 | 244 | } |
| @@ -261,7 +271,7 @@ mod tests { | ||
| 261 | 271 | #[tokio::test] |
| 262 | 272 | async fn upsert_replaces_per_slot() { |
| 263 | 273 | let db = crate::database::init_mem().await.unwrap(); |
| 264 | - let exam = ExamId::generate(); | |
| 274 | + let exam = exam_row(&db).await; | |
| 265 | 275 | let question = ExamQuestionId::generate(); |
| 266 | 276 | let ids = choice_ids(); |
| 267 | 277 | |
| @@ -311,7 +321,7 @@ mod tests { | ||
| 311 | 321 | #[tokio::test] |
| 312 | 322 | async fn only_the_dropped_options_lose_their_pictures() { |
| 313 | 323 | let db = crate::database::init_mem().await.unwrap(); |
| 314 | - let exam = ExamId::generate(); | |
| 324 | + let exam = exam_row(&db).await; | |
| 315 | 325 | let question = ExamQuestionId::generate(); |
| 316 | 326 | let ids = choice_ids(); |
| 317 | 327 | QuestionImage::new(&exam, &question, None, png(), 1) |
| @@ -361,7 +371,7 @@ mod tests { | ||
| 361 | 371 | #[tokio::test] |
| 362 | 372 | async fn an_empty_keep_set_clears_every_option_picture_but_not_the_illustration() { |
| 363 | 373 | let db = crate::database::init_mem().await.unwrap(); |
| 364 | - let exam = ExamId::generate(); | |
| 374 | + let exam = exam_row(&db).await; | |
| 365 | 375 | let question = ExamQuestionId::generate(); |
| 366 | 376 | let ids = choice_ids(); |
| 367 | 377 | QuestionImage::new(&exam, &question, None, png(), 1) |
| @@ -391,8 +401,8 @@ mod tests { | ||
| 391 | 401 | #[tokio::test] |
| 392 | 402 | async fn exam_listing_scopes_by_exam() { |
| 393 | 403 | let db = crate::database::init_mem().await.unwrap(); |
| 394 | - let exam_a = ExamId::generate(); | |
| 395 | - let exam_b = ExamId::generate(); | |
| 404 | + let exam_a = exam_row(&db).await; | |
| 405 | + let exam_b = exam_row(&db).await; | |
| 396 | 406 | QuestionImage::new(&exam_a, &ExamQuestionId::generate(), None, png(), 1) |
| 397 | 407 | .upsert(&db) |
| 398 | 408 | .await |
src/domain/subject.rsmodified+7 -2
| @@ -297,7 +297,6 @@ mod tests { | ||
| 297 | 297 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 298 | 298 | #[ignore = "needs a real SurrealDB server: podman start hezarfen-surrealdb && cargo test -- --ignored"] |
| 299 | 299 | async fn a_delete_racing_a_question_never_answers_500() { |
| 300 | - use crate::domain::exam::ExamId; | |
| 301 | 300 | use crate::domain::exam_question::{ |
| 302 | 301 | ExamQuestion, QuestionKind, QuestionPoints, QuestionSpec, QuestionText, |
| 303 | 302 | }; |
| @@ -307,7 +306,13 @@ mod tests { | ||
| 307 | 306 | let (mut last_delete, mut last_question) = (String::new(), String::new()); |
| 308 | 307 | for round in 0..20 { |
| 309 | 308 | let course = CourseId::generate(); |
| 310 | - let exam = ExamId::generate(); | |
| 309 | + // A real exam row per round: a question write moves its exam's | |
| 310 | + // counter, so a minted id nothing wrote is a 404 and no round would | |
| 311 | + // reach the subject race this test is about. | |
| 312 | + let exam = crate::domain::exam::published_exam(&db) | |
| 313 | + .await | |
| 314 | + .get_id() | |
| 315 | + .clone(); | |
| 311 | 316 | let subject = Subject::create( |
| 312 | 317 | &course, |
| 313 | 318 | SubjectName::try_new("Limits").unwrap(), |