forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits

partial_patch_bank_questions.rs166 lines, 5.5 KiBHistoryRawDownload
1//! A bank-template PATCH is a read-modify-write: the kind/choices/correct trio
2//! has to be re-submitted as a unit (choices carry their ids, and their
3//! pictures hang off those ids), so the row read, the merge and the write are
4//! a compare-and-set on the stored snapshot. Without that, two concurrent
5//! partial PATCHes revert each other.
6
7mod common;
8
9use axum::http::StatusCode;
10use common::*;
11use serde_json::json;
12
13async fn template(app: &axum::Router, who: &str, subject: &str) -> String {
14 let created = send(
15 app,
16 "POST",
17 "/bank-questions",
18 Some(who),
19 Some(json!({ "subject_id": subject, "text": "old text",
20 "kind": "text", "points": 1 })),
21 )
22 .await;
23 assert_eq!(created.status, StatusCode::CREATED, "{}", created.body);
24 id_of(&created.body)
25}
26
27#[tokio::test]
28async fn concurrent_partial_patches_keep_both_fields() {
29 let (app, db) = app_and_db().await;
30 let who = login_as(&app, &db, "ali", "teacher").await;
31 let course = create_course(&app, &who, "math").await;
32 let subject = create_subject(&app, &who, &course, "algebra").await;
33
34 for round in 0..20 {
35 let uri = format!("/bank-questions/{}", template(&app, &who, &subject).await);
36
37 let (a, b) = tokio::join!(
38 send(
39 &app,
40 "PATCH",
41 &uri,
42 Some(&who),
43 Some(json!({ "text": "new text" })),
44 ),
45 send(
46 &app,
47 "PATCH",
48 &uri,
49 Some(&who),
50 Some(json!({ "points": 7 })),
51 ),
52 );
53 assert_eq!(a.status, StatusCode::OK, "round {round} text patch");
54 assert_eq!(b.status, StatusCode::OK, "round {round} points patch");
55
56 let after = send(&app, "GET", &uri, Some(&who), None).await.body;
57 assert_eq!(after["text"], "new text", "round {round}: text reverted");
58 assert_eq!(after["points"], 7, "round {round}: points reverted");
59 }
60}
61
62/// The visibility switch races a text edit: publishing must not be undone by an
63/// edit that never mentioned `visibility`, and vice versa.
64#[tokio::test]
65async fn publishing_survives_a_concurrent_text_edit() {
66 let (app, db) = app_and_db().await;
67 let who = login_as(&app, &db, "veli", "teacher").await;
68 let course = create_course(&app, &who, "physics").await;
69 let subject = create_subject(&app, &who, &course, "optics").await;
70
71 for round in 0..20 {
72 let uri = format!("/bank-questions/{}", template(&app, &who, &subject).await);
73
74 let (a, b) = tokio::join!(
75 send(
76 &app,
77 "PATCH",
78 &uri,
79 Some(&who),
80 Some(json!({ "visibility": "school" })),
81 ),
82 send(
83 &app,
84 "PATCH",
85 &uri,
86 Some(&who),
87 Some(json!({ "text": "edited" })),
88 ),
89 );
90 assert_eq!(a.status, StatusCode::OK, "round {round} publish");
91 assert_eq!(b.status, StatusCode::OK, "round {round} text patch");
92
93 let after = send(&app, "GET", &uri, Some(&who), None).await.body;
94 assert_eq!(
95 after["visibility"], "school",
96 "round {round}: publish reverted"
97 );
98 assert_eq!(after["text"], "edited", "round {round}: text reverted");
99 }
100}
101
102/// The kind-dependent trio is still merged and re-validated as a unit, and the
103/// choice ids (which the pictures hang off) survive a text-only edit.
104#[tokio::test]
105async fn choice_identity_and_unit_validation_survive() {
106 let (app, db) = app_and_db().await;
107 let who = login_as(&app, &db, "ayse", "teacher").await;
108 let course = create_course(&app, &who, "math").await;
109 let subject = create_subject(&app, &who, &course, "algebra").await;
110
111 let created = send(
112 &app,
113 "POST",
114 "/bank-questions",
115 Some(&who),
116 Some(
117 json!({ "subject_id": subject, "text": "2 + 2?", "kind": "choice",
118 "points": 10,
119 "choices": [{"id": "c0", "text": "3"}, {"id": "c1", "text": "4"}],
120 "correct": "c1" }),
121 ),
122 )
123 .await;
124 assert_eq!(created.status, StatusCode::CREATED, "{}", created.body);
125 let ids: Vec<String> = created.body["choices"]
126 .as_array()
127 .expect("choices")
128 .iter()
129 .map(|c| c["id"].as_str().expect("choice id").to_string())
130 .collect();
131 let uri = format!("/bank-questions/{}", id_of(&created.body));
132
133 let edited = send(
134 &app,
135 "PATCH",
136 &uri,
137 Some(&who),
138 Some(json!({ "text": "two plus two?" })),
139 )
140 .await;
141 assert_eq!(edited.status, StatusCode::OK);
142 let after_ids: Vec<String> = edited.body["choices"]
143 .as_array()
144 .expect("choices")
145 .iter()
146 .map(|c| c["id"].as_str().expect("choice id").to_string())
147 .collect();
148 assert_eq!(after_ids, ids, "a text edit kept every choice id");
149 assert_eq!(edited.body["correct"], ids[1], "correct kept");
150
151 // Switching to a text template while keeping the stored choices is still a
152 // unit-validation failure — a PATCH cannot land a half-question.
153 let bad = send(
154 &app,
155 "PATCH",
156 &uri,
157 Some(&who),
158 Some(json!({ "kind": "text" })),
159 )
160 .await;
161 assert_eq!(
162 bad.status,
163 StatusCode::BAD_REQUEST,
164 "text kind with stored choices refused"
165 );
166}