| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | use std::collections::HashSet; |
| 8 | |
| 9 | use sqlx::PgConnection; |
| 10 | |
| 11 | use crate::database::{Database, tx_with_retry}; |
| 12 | use crate::db::exam_attempt::freeze_gate; |
| 13 | use crate::db::page::{PagedList, Param}; |
| 14 | use crate::domain::bank_question::BankQuestionId; |
| 15 | use crate::domain::exam::ExamId; |
| 16 | use crate::domain::exam_question::{ |
| 17 | Choice, ChoiceId, ExamQuestion, ExamQuestionId, QuestionKind, QuestionPoints, QuestionSpec, |
| 18 | QuestionText, |
| 19 | }; |
| 20 | use crate::domain::subject::SubjectId; |
| 21 | use crate::error::{AppError, ValidationError}; |
| 22 | use sqlx::types::Json; |
| 23 | |
| 24 | |
| 25 | |
| 26 | fn dead_subject() -> AppError { |
| 27 | AppError::Validation(ValidationError::Invalid { |
| 28 | field: "subject_id", |
| 29 | reason: "subject does not exist", |
| 30 | }) |
| 31 | } |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | fn choices_as_value(choices: &[Choice]) -> serde_json::Value { |
| 37 | serde_json::to_value(choices).expect("Choice serialization cannot fail") |
| 38 | } |
| 39 | |
| 40 | pub async fn create( |
| 41 | db: &Database, |
| 42 | exam: &ExamId, |
| 43 | subject: SubjectId, |
| 44 | text: QuestionText, |
| 45 | points: QuestionPoints, |
| 46 | spec: QuestionSpec, |
| 47 | ) -> Result<ExamQuestion, AppError> { |
| 48 | insert(db, exam, subject, text, points, spec, None).await |
| 49 | } |
| 50 | |
| 51 | |
| 52 | |
| 53 | pub async fn create_from_bank( |
| 54 | db: &Database, |
| 55 | exam: &ExamId, |
| 56 | subject: SubjectId, |
| 57 | text: QuestionText, |
| 58 | points: QuestionPoints, |
| 59 | spec: QuestionSpec, |
| 60 | source: BankQuestionId, |
| 61 | ) -> Result<ExamQuestion, AppError> { |
| 62 | insert(db, exam, subject, text, points, spec, Some(source)).await |
| 63 | } |
| 64 | |
| 65 | #[allow(clippy::too_many_arguments)] |
| 66 | async fn insert( |
| 67 | db: &Database, |
| 68 | exam: &ExamId, |
| 69 | subject: SubjectId, |
| 70 | text: QuestionText, |
| 71 | points: QuestionPoints, |
| 72 | spec: QuestionSpec, |
| 73 | from_bank: Option<BankQuestionId>, |
| 74 | ) -> Result<ExamQuestion, AppError> { |
| 75 | let question = ExamQuestion { |
| 76 | id: ExamQuestionId::generate(), |
| 77 | exam: exam.clone(), |
| 78 | subject, |
| 79 | text, |
| 80 | points, |
| 81 | kind: spec.kind, |
| 82 | choices: spec.choices.map(Json), |
| 83 | correct: spec.correct, |
| 84 | from_bank, |
| 85 | |
| 86 | banked_as: None, |
| 87 | }; |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | let exam = exam.clone(); |
| 98 | tx_with_retry(db, false, async move |conn| { |
| 99 | freeze_gate(conn, &exam).await?; |
| 100 | claim_subject_and_insert(conn, &question).await |
| 101 | }) |
| 102 | .await |
| 103 | } |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | async fn claim_subject_and_insert( |
| 111 | conn: &mut PgConnection, |
| 112 | question: &ExamQuestion, |
| 113 | ) -> Result<ExamQuestion, AppError> { |
| 114 | let created = sqlx::query_as!( |
| 115 | ExamQuestion, |
| 116 | r#"WITH seat AS ( |
| 117 | UPDATE subject SET exam_question_count = subject.exam_question_count + 1 |
| 118 | WHERE id = $1 |
| 119 | RETURNING 1) |
| 120 | INSERT INTO exam_question (id, exam, subject, text, kind, points, |
| 121 | choices, correct, from_bank, banked_as) |
| 122 | SELECT $2, $3, $1, $4, $5, $6, $7, $8, $9, NULL |
| 123 | WHERE EXISTS (SELECT 1 FROM seat) |
| 124 | RETURNING id AS "id: ExamQuestionId", exam AS "exam: ExamId", |
| 125 | subject AS "subject: SubjectId", text AS "text: QuestionText", |
| 126 | kind AS "kind: QuestionKind", points AS "points: QuestionPoints", |
| 127 | choices AS "choices: Json<Vec<Choice>>", |
| 128 | correct AS "correct: ChoiceId", |
| 129 | from_bank AS "from_bank: BankQuestionId", |
| 130 | banked_as AS "banked_as: BankQuestionId""#, |
| 131 | question.subject.uuid(), |
| 132 | question.id.uuid(), |
| 133 | question.exam.uuid(), |
| 134 | question.text.as_str(), |
| 135 | question.kind.as_str(), |
| 136 | question.points.as_i64(), |
| 137 | question |
| 138 | .choices |
| 139 | .as_ref() |
| 140 | .map(|json| choices_as_value(&json.0)), |
| 141 | question.correct.as_ref().map(ChoiceId::as_str), |
| 142 | question.from_bank.as_ref().map(BankQuestionId::uuid), |
| 143 | ) |
| 144 | .fetch_optional(&mut *conn) |
| 145 | .await?; |
| 146 | created.ok_or_else(dead_subject) |
| 147 | } |
| 148 | |
| 149 | pub async fn read(db: &Database, id: &ExamQuestionId) -> Result<Option<ExamQuestion>, AppError> { |
| 150 | Ok(sqlx::query_as!( |
| 151 | ExamQuestion, |
| 152 | r#"SELECT id AS "id: ExamQuestionId", exam AS "exam: ExamId", |
| 153 | subject AS "subject: SubjectId", text AS "text: QuestionText", |
| 154 | kind AS "kind: QuestionKind", points AS "points: QuestionPoints", |
| 155 | choices AS "choices: Json<Vec<Choice>>", |
| 156 | correct AS "correct: ChoiceId", |
| 157 | from_bank AS "from_bank: BankQuestionId", |
| 158 | banked_as AS "banked_as: BankQuestionId" |
| 159 | FROM exam_question WHERE id = $1"#, |
| 160 | id.uuid(), |
| 161 | ) |
| 162 | .fetch_optional(db) |
| 163 | .await?) |
| 164 | } |
| 165 | |
| 166 | |
| 167 | pub async fn list_for_exam( |
| 168 | db: &Database, |
| 169 | exam: &ExamId, |
| 170 | limit: Option<i64>, |
| 171 | offset: i64, |
| 172 | ) -> Result<(Vec<ExamQuestion>, i64), AppError> { |
| 173 | PagedList::new("exam_question WHERE exam = $1", "ORDER BY id ASC") |
| 174 | .bind(Param::Uuid(exam.uuid())) |
| 175 | .run::<ExamQuestion>(limit, offset, db) |
| 176 | .await |
| 177 | } |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | pub async fn list_shared_with( |
| 200 | db: &Database, |
| 201 | exam: &ExamId, |
| 202 | live: &[ExamId], |
| 203 | ) -> Result<HashSet<String>, AppError> { |
| 204 | if live.is_empty() { |
| 205 | return Ok(HashSet::new()); |
| 206 | } |
| 207 | let live = live.iter().map(ExamId::uuid).collect::<Vec<_>>(); |
| 208 | let rows = sqlx::query!( |
| 209 | r#"WITH shared AS ( |
| 210 | SELECT from_bank AS t FROM exam_question |
| 211 | WHERE exam = ANY($1) AND from_bank IS NOT NULL |
| 212 | UNION |
| 213 | SELECT banked_as FROM exam_question |
| 214 | WHERE exam = ANY($1) AND banked_as IS NOT NULL) |
| 215 | SELECT q.id AS "id: ExamQuestionId" FROM exam_question q |
| 216 | WHERE q.exam = $2 |
| 217 | AND (q.from_bank IN (SELECT t FROM shared) |
| 218 | OR q.banked_as IN (SELECT t FROM shared))"#, |
| 219 | &live, |
| 220 | exam.uuid(), |
| 221 | ) |
| 222 | .fetch_all(db) |
| 223 | .await?; |
| 224 | Ok(rows.into_iter().map(|row| row.id.key()).collect()) |
| 225 | } |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | pub async fn update( |
| 238 | db: &Database, |
| 239 | question: ExamQuestion, |
| 240 | subject: SubjectId, |
| 241 | text: QuestionText, |
| 242 | points: QuestionPoints, |
| 243 | spec: QuestionSpec, |
| 244 | ) -> Result<ExamQuestion, AppError> { |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | let retag = (subject != question.subject).then_some((subject, question.subject)); |
| 262 | tx_with_retry(db, false, async move |conn| { |
| 263 | |
| 264 | |
| 265 | freeze_gate(conn, &question.exam).await?; |
| 266 | if let Some((next, previous)) = &retag { |
| 267 | sqlx::query!( |
| 268 | r#"UPDATE subject SET exam_question_count = |
| 269 | GREATEST(exam_question_count - 1, 0) |
| 270 | WHERE id = $1"#, |
| 271 | previous.uuid(), |
| 272 | ) |
| 273 | .execute(&mut *conn) |
| 274 | .await?; |
| 275 | let claimed = sqlx::query!( |
| 276 | r#"UPDATE subject SET exam_question_count = subject.exam_question_count + 1 |
| 277 | WHERE id = $1 RETURNING 1 AS n"#, |
| 278 | next.uuid(), |
| 279 | ) |
| 280 | .fetch_optional(&mut *conn) |
| 281 | .await?; |
| 282 | if claimed.is_none() { |
| 283 | return Err(dead_subject()); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | let written = sqlx::query_as!( |
| 292 | ExamQuestion, |
| 293 | r#"UPDATE exam_question |
| 294 | SET subject = $2, text = $3, points = $4, kind = $5, |
| 295 | choices = $6, correct = $7 |
| 296 | WHERE id = $1 AND subject = $8 |
| 297 | RETURNING id AS "id: ExamQuestionId", exam AS "exam: ExamId", |
| 298 | subject AS "subject: SubjectId", text AS "text: QuestionText", |
| 299 | kind AS "kind: QuestionKind", points AS "points: QuestionPoints", |
| 300 | choices AS "choices: Json<Vec<Choice>>", |
| 301 | correct AS "correct: ChoiceId", |
| 302 | from_bank AS "from_bank: BankQuestionId", |
| 303 | banked_as AS "banked_as: BankQuestionId""#, |
| 304 | question.id.uuid(), |
| 305 | subject.uuid(), |
| 306 | text.as_str(), |
| 307 | points.as_i64(), |
| 308 | spec.kind.as_str(), |
| 309 | spec.choices.as_deref().map(choices_as_value), |
| 310 | spec.correct.as_ref().map(ChoiceId::as_str), |
| 311 | question.subject.uuid(), |
| 312 | ) |
| 313 | .fetch_optional(&mut *conn) |
| 314 | .await?; |
| 315 | let Some(written) = written else { |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | let live = sqlx::query!( |
| 321 | r#"SELECT EXISTS(SELECT 1 FROM exam_question WHERE id = $1) AS live"#, |
| 322 | question.id.uuid(), |
| 323 | ) |
| 324 | .fetch_one(&mut *conn) |
| 325 | .await?; |
| 326 | if !live.live.unwrap_or(false) { |
| 327 | return Err(AppError::NotFound); |
| 328 | } |
| 329 | return Err(AppError::Conflict( |
| 330 | "the subject this question was read on changed since; re-read and retry", |
| 331 | )); |
| 332 | }; |
| 333 | Ok(written) |
| 334 | }) |
| 335 | .await |
| 336 | } |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | pub async fn link_banked_as( |
| 351 | db: &Database, |
| 352 | question: ExamQuestion, |
| 353 | template: BankQuestionId, |
| 354 | ) -> Result<ExamQuestion, AppError> { |
| 355 | let linked = sqlx::query_as!( |
| 356 | ExamQuestion, |
| 357 | r#"UPDATE exam_question SET banked_as = $2 WHERE id = $1 |
| 358 | RETURNING id AS "id: ExamQuestionId", exam AS "exam: ExamId", |
| 359 | subject AS "subject: SubjectId", text AS "text: QuestionText", |
| 360 | kind AS "kind: QuestionKind", points AS "points: QuestionPoints", |
| 361 | choices AS "choices: Json<Vec<Choice>>", |
| 362 | correct AS "correct: ChoiceId", |
| 363 | from_bank AS "from_bank: BankQuestionId", |
| 364 | banked_as AS "banked_as: BankQuestionId""#, |
| 365 | question.id.uuid(), |
| 366 | template.uuid(), |
| 367 | ) |
| 368 | .fetch_optional(db) |
| 369 | .await?; |
| 370 | linked.ok_or(AppError::NotFound) |
| 371 | } |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | pub async fn delete(db: &Database, question: ExamQuestion) -> Result<ExamQuestion, AppError> { |
| 380 | tx_with_retry(db, false, async move |conn| { |
| 381 | freeze_gate(conn, &question.exam).await?; |
| 382 | sqlx::query!( |
| 383 | r#"DELETE FROM exam_answer WHERE question = $1"#, |
| 384 | question.id.uuid(), |
| 385 | ) |
| 386 | .execute(&mut *conn) |
| 387 | .await?; |
| 388 | sqlx::query!( |
| 389 | r#"DELETE FROM question_image WHERE question = $1"#, |
| 390 | question.id.uuid(), |
| 391 | ) |
| 392 | .execute(&mut *conn) |
| 393 | .await?; |
| 394 | let deleted = sqlx::query_as!( |
| 395 | ExamQuestion, |
| 396 | r#"DELETE FROM exam_question WHERE id = $1 |
| 397 | RETURNING id AS "id: ExamQuestionId", exam AS "exam: ExamId", |
| 398 | subject AS "subject: SubjectId", text AS "text: QuestionText", |
| 399 | kind AS "kind: QuestionKind", points AS "points: QuestionPoints", |
| 400 | choices AS "choices: Json<Vec<Choice>>", |
| 401 | correct AS "correct: ChoiceId", |
| 402 | from_bank AS "from_bank: BankQuestionId", |
| 403 | banked_as AS "banked_as: BankQuestionId""#, |
| 404 | question.id.uuid(), |
| 405 | ) |
| 406 | .fetch_optional(&mut *conn) |
| 407 | .await?; |
| 408 | |
| 409 | |
| 410 | |
| 411 | if let Some(deleted) = &deleted { |
| 412 | sqlx::query!( |
| 413 | r#"UPDATE subject SET exam_question_count = |
| 414 | GREATEST(exam_question_count - 1, 0) |
| 415 | WHERE id = $1"#, |
| 416 | deleted.subject.uuid(), |
| 417 | ) |
| 418 | .execute(&mut *conn) |
| 419 | .await?; |
| 420 | } |
| 421 | deleted.ok_or(AppError::NotFound) |
| 422 | }) |
| 423 | .await |
| 424 | } |
| 425 | |
| 426 | #[cfg(test)] |
| 427 | mod tests { |
| 428 | use super::*; |
| 429 | use crate::domain::exam_question::QuestionKind; |
| 430 | |
| 431 | fn kind(value: &str) -> QuestionKind { |
| 432 | QuestionKind::try_new(value).unwrap() |
| 433 | } |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | mod counters { |
| 439 | use super::*; |
| 440 | use crate::database::init_test_db; |
| 441 | use crate::domain::exam::{ |
| 442 | Exam, ExamAttemptLimit, ExamDescription, ExamKind, ExamMode, ExamSchedule, ExamTitle, |
| 443 | }; |
| 444 | use crate::domain::settings::Settings; |
| 445 | use crate::domain::subject::{Subject, SubjectDescription, SubjectName}; |
| 446 | use crate::domain::user::UserId; |
| 447 | |
| 448 | async fn an_exam(db: &Database) -> Exam { |
| 449 | |
| 450 | |
| 451 | let creator = UserId::from_key("019732e3-7b00-7000-8000-00000000acdc"); |
| 452 | sqlx::query( |
| 453 | "INSERT INTO app_user (id, username, created_at) \ |
| 454 | VALUES ($1, 'acdc-fixture', 0)", |
| 455 | ) |
| 456 | .bind(creator.uuid()) |
| 457 | .execute(db) |
| 458 | .await |
| 459 | .unwrap(); |
| 460 | let kinds = Settings::defaults().get_exam_kinds().to_vec(); |
| 461 | |
| 462 | |
| 463 | let (instance, _course) = crate::db::course::a_test_instance(db).await; |
| 464 | let term = crate::db::term::a_test_term(db).await; |
| 465 | crate::db::exam::create( |
| 466 | db, |
| 467 | &creator, |
| 468 | &instance, |
| 469 | &term, |
| 470 | ExamTitle::try_new("practice").unwrap(), |
| 471 | ExamDescription::try_new("").unwrap(), |
| 472 | ExamKind::try_new("yazili", &kinds).unwrap(), |
| 473 | ExamSchedule::try_new(Some(ExamMode::try_new("open").unwrap()), None, None, None) |
| 474 | .unwrap(), |
| 475 | ExamAttemptLimit::try_new(1).unwrap(), |
| 476 | true, |
| 477 | false, |
| 478 | false, |
| 479 | ) |
| 480 | .await |
| 481 | .unwrap() |
| 482 | } |
| 483 | |
| 484 | async fn a_subject(db: &Database) -> Subject { |
| 485 | crate::db::subject::create( |
| 486 | db, |
| 487 | &crate::db::course::a_test_course(db).await, |
| 488 | SubjectName::try_new("topic").unwrap(), |
| 489 | SubjectDescription::try_new("").unwrap(), |
| 490 | ) |
| 491 | .await |
| 492 | .unwrap() |
| 493 | } |
| 494 | |
| 495 | |
| 496 | fn body() -> (QuestionText, QuestionPoints, QuestionSpec) { |
| 497 | ( |
| 498 | QuestionText::try_new("3 + 3?").unwrap(), |
| 499 | QuestionPoints::try_new(5).unwrap(), |
| 500 | QuestionSpec::try_new(kind("text"), None, None, &[]).unwrap(), |
| 501 | ) |
| 502 | } |
| 503 | |
| 504 | async fn a_question(exam: &Exam, on: &SubjectId, db: &Database) -> ExamQuestion { |
| 505 | let (text, points, spec) = body(); |
| 506 | create(db, exam.get_id(), *on, text, points, spec) |
| 507 | .await |
| 508 | .unwrap() |
| 509 | } |
| 510 | |
| 511 | async fn moved( |
| 512 | question: ExamQuestion, |
| 513 | to: &SubjectId, |
| 514 | db: &Database, |
| 515 | ) -> Result<ExamQuestion, AppError> { |
| 516 | let (text, points, spec) = body(); |
| 517 | update(db, question, *to, text, points, spec).await |
| 518 | } |
| 519 | |
| 520 | |
| 521 | async fn count_on(subject: &SubjectId, db: &Database) -> i64 { |
| 522 | sqlx::query_scalar::<_, i64>("SELECT exam_question_count FROM subject WHERE id = $1") |
| 523 | .bind(subject.uuid()) |
| 524 | .fetch_one(db) |
| 525 | .await |
| 526 | .unwrap() |
| 527 | } |
| 528 | |
| 529 | async fn rows(sql: &str, db: &Database) -> usize { |
| 530 | sqlx::query_scalar::<_, i64>(sqlx::AssertSqlSafe(format!( |
| 531 | "SELECT count(*) FROM ({sql}) AS t" |
| 532 | ))) |
| 533 | .fetch_one(db) |
| 534 | .await |
| 535 | .unwrap() as usize |
| 536 | } |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | #[tokio::test] |
| 542 | async fn a_frozen_exam_leaves_the_subject_counters_untouched() { |
| 543 | let (db, _leases) = init_test_db().await; |
| 544 | let exam = an_exam(&db).await; |
| 545 | let (from, to) = (a_subject(&db).await, a_subject(&db).await); |
| 546 | let (from, to) = (*from.get_id(), *to.get_id()); |
| 547 | let question = a_question(&exam, &from, &db).await; |
| 548 | assert_eq!(count_on(&from, &db).await, 1); |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | let student = crate::db::user::create( |
| 554 | &db, |
| 555 | crate::domain::user::Username::try_new("ogrenci").unwrap(), |
| 556 | None, |
| 557 | ) |
| 558 | .await |
| 559 | .unwrap(); |
| 560 | crate::service::exam_attempt::start(&db, &exam, student.get_id()) |
| 561 | .await |
| 562 | .unwrap(); |
| 563 | |
| 564 | let (text, points, spec) = body(); |
| 565 | let error = create(&db, exam.get_id(), to, text, points, spec) |
| 566 | .await |
| 567 | .expect_err("a started exam takes no new questions"); |
| 568 | assert!(matches!(error, AppError::Conflict(_)), "{error:?}"); |
| 569 | assert_eq!(count_on(&to, &db).await, 0, "the refused claim rolled back"); |
| 570 | |
| 571 | let error = moved(question, &to, &db) |
| 572 | .await |
| 573 | .expect_err("a started exam takes no re-tag either"); |
| 574 | assert!(matches!(error, AppError::Conflict(_)), "{error:?}"); |
| 575 | assert_eq!(count_on(&from, &db).await, 1, "the release rolled back too"); |
| 576 | assert_eq!(count_on(&to, &db).await, 0); |
| 577 | } |
| 578 | |
| 579 | |
| 580 | |
| 581 | #[tokio::test] |
| 582 | async fn a_create_on_a_dead_subject_writes_neither_row_nor_count() { |
| 583 | let (db, _leases) = init_test_db().await; |
| 584 | let exam = an_exam(&db).await; |
| 585 | let subject = a_subject(&db).await; |
| 586 | let id = *subject.get_id(); |
| 587 | crate::db::subject::delete(&db, subject).await.unwrap(); |
| 588 | |
| 589 | let (text, points, spec) = body(); |
| 590 | let error = create(&db, exam.get_id(), id, text, points, spec) |
| 591 | .await |
| 592 | .expect_err("a subject that is gone must not be taggable"); |
| 593 | assert!(error.to_string().contains("subject does not exist")); |
| 594 | assert_eq!( |
| 595 | rows("SELECT id FROM exam_question", &db).await, |
| 596 | 0, |
| 597 | "a refused create may write no row" |
| 598 | ); |
| 599 | assert_eq!( |
| 600 | rows("SELECT id FROM subject", &db).await, |
| 601 | 0, |
| 602 | "…and least of all a count on a subject it just brought back" |
| 603 | ); |
| 604 | } |
| 605 | |
| 606 | #[tokio::test] |
| 607 | async fn a_subject_move_moves_the_count() { |
| 608 | let (db, _leases) = init_test_db().await; |
| 609 | let exam = an_exam(&db).await; |
| 610 | let (from, to) = (a_subject(&db).await, a_subject(&db).await); |
| 611 | let (from, to) = (*from.get_id(), *to.get_id()); |
| 612 | let question = a_question(&exam, &from, &db).await; |
| 613 | |
| 614 | let after = moved(question, &to, &db).await.unwrap(); |
| 615 | assert_eq!(after.get_subject(), &to); |
| 616 | assert_eq!(count_on(&from, &db).await, 0, "the old subject is free"); |
| 617 | assert_eq!(count_on(&to, &db).await, 1, "the new one is not"); |
| 618 | } |
| 619 | |
| 620 | #[tokio::test] |
| 621 | async fn a_move_to_a_dead_subject_leaves_everything_untouched() { |
| 622 | let (db, _leases) = init_test_db().await; |
| 623 | let exam = an_exam(&db).await; |
| 624 | let from = *a_subject(&db).await.get_id(); |
| 625 | let dead = a_subject(&db).await; |
| 626 | let gone = *dead.get_id(); |
| 627 | crate::db::subject::delete(&db, dead).await.unwrap(); |
| 628 | let question = a_question(&exam, &from, &db).await; |
| 629 | |
| 630 | let error = moved(question.clone(), &gone, &db) |
| 631 | .await |
| 632 | .expect_err("a subject that is gone must not be taggable"); |
| 633 | assert!(error.to_string().contains("subject does not exist")); |
| 634 | let stored = read(&db, question.get_id()).await.unwrap().unwrap(); |
| 635 | assert_eq!(stored.get_subject(), &from, "the link never moved"); |
| 636 | assert_eq!( |
| 637 | count_on(&from, &db).await, |
| 638 | 1, |
| 639 | "the release rolled back with the claim" |
| 640 | ); |
| 641 | assert_eq!(rows("SELECT id FROM subject", &db).await, 1); |
| 642 | } |
| 643 | |
| 644 | |
| 645 | |
| 646 | |
| 647 | |
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | |
| 653 | |
| 654 | |
| 655 | #[tokio::test] |
| 656 | async fn a_stale_mover_is_refused_and_claims_nothing() { |
| 657 | let (db, _leases) = init_test_db().await; |
| 658 | let exam = an_exam(&db).await; |
| 659 | let from = *a_subject(&db).await.get_id(); |
| 660 | let to = *a_subject(&db).await.get_id(); |
| 661 | let other = *a_subject(&db).await.get_id(); |
| 662 | let question = a_question(&exam, &from, &db).await; |
| 663 | let stale = question.clone(); |
| 664 | |
| 665 | moved(question, &to, &db).await.unwrap(); |
| 666 | let error = moved(stale.clone(), &other, &db) |
| 667 | .await |
| 668 | .expect_err("a mover that read a subject it no longer holds must be refused"); |
| 669 | assert!( |
| 670 | matches!(error, AppError::Conflict(_)), |
| 671 | "a lost CAS is a conflict, not a 404 or a 500: {error:?}" |
| 672 | ); |
| 673 | |
| 674 | let stored = read(&db, stale.get_id()).await.unwrap().unwrap(); |
| 675 | assert_eq!(stored.get_subject(), &to, "the winner's link"); |
| 676 | assert_eq!(count_on(&from, &db).await, 0, "released once, not twice"); |
| 677 | assert_eq!(count_on(&to, &db).await, 1, "claimed once"); |
| 678 | assert_eq!(count_on(&other, &db).await, 0, "never claimed"); |
| 679 | } |
| 680 | |
| 681 | |
| 682 | |
| 683 | |
| 684 | |
| 685 | |
| 686 | |
| 687 | |
| 688 | |
| 689 | |
| 690 | |
| 691 | #[tokio::test] |
| 692 | async fn a_stale_re_stater_is_refused_and_reverts_nothing() { |
| 693 | let (db, _leases) = init_test_db().await; |
| 694 | let exam = an_exam(&db).await; |
| 695 | let from = *a_subject(&db).await.get_id(); |
| 696 | let to = *a_subject(&db).await.get_id(); |
| 697 | let question = a_question(&exam, &from, &db).await; |
| 698 | let stale = question.clone(); |
| 699 | |
| 700 | moved(question, &to, &db).await.unwrap(); |
| 701 | let error = moved(stale.clone(), &from, &db) |
| 702 | .await |
| 703 | .expect_err("re-stating a subject a rival moved off must be refused"); |
| 704 | assert!( |
| 705 | matches!(error, AppError::Conflict(_)), |
| 706 | "a lost CAS is a conflict, not a silent revert: {error:?}" |
| 707 | ); |
| 708 | |
| 709 | let stored = read(&db, stale.get_id()).await.unwrap().unwrap(); |
| 710 | assert_eq!(stored.get_subject(), &to, "the winner's link stands"); |
| 711 | assert_eq!( |
| 712 | count_on(&from, &db).await, |
| 713 | 0, |
| 714 | "released once, and stayed so" |
| 715 | ); |
| 716 | assert_eq!(count_on(&to, &db).await, 1, "claimed once, and stayed so"); |
| 717 | } |
| 718 | |
| 719 | |
| 720 | |
| 721 | #[tokio::test] |
| 722 | async fn a_no_op_re_state_still_lands() { |
| 723 | let (db, _leases) = init_test_db().await; |
| 724 | let exam = an_exam(&db).await; |
| 725 | let on = *a_subject(&db).await.get_id(); |
| 726 | let question = a_question(&exam, &on, &db).await; |
| 727 | |
| 728 | let (_, points, spec) = body(); |
| 729 | let after = update( |
| 730 | &db, |
| 731 | question, |
| 732 | on, |
| 733 | QuestionText::try_new("4 + 4?").unwrap(), |
| 734 | points, |
| 735 | spec, |
| 736 | ) |
| 737 | .await |
| 738 | .expect("re-stating the subject the row holds is not a conflict"); |
| 739 | assert_eq!(after.get_text().as_str(), "4 + 4?"); |
| 740 | assert_eq!(after.get_subject(), &on); |
| 741 | assert_eq!(count_on(&on, &db).await, 1, "no counter moved"); |
| 742 | } |
| 743 | |
| 744 | |
| 745 | |
| 746 | #[tokio::test] |
| 747 | async fn a_move_of_a_deleted_question_is_still_a_404() { |
| 748 | let (db, _leases) = init_test_db().await; |
| 749 | let exam = an_exam(&db).await; |
| 750 | let from = *a_subject(&db).await.get_id(); |
| 751 | let to = *a_subject(&db).await.get_id(); |
| 752 | let question = a_question(&exam, &from, &db).await; |
| 753 | delete(&db, question.clone()).await.unwrap(); |
| 754 | |
| 755 | let error = moved(question, &to, &db) |
| 756 | .await |
| 757 | .expect_err("a deleted question cannot be re-tagged"); |
| 758 | assert!(matches!(error, AppError::NotFound), "{error:?}"); |
| 759 | assert_eq!(count_on(&to, &db).await, 0, "and nothing was claimed"); |
| 760 | } |
| 761 | } |
| 762 | } |