| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | mod common; |
| 7 | |
| 8 | use axum::http::StatusCode; |
| 9 | use common::*; |
| 10 | use serde_json::json; |
| 11 | |
| 12 | #[tokio::test] |
| 13 | async fn concurrent_partial_patches_keep_both_fields() { |
| 14 | let (app, db) = app_and_db().await; |
| 15 | let who = login_as(&app, &db, "ali", "teacher").await; |
| 16 | |
| 17 | for round in 0..20 { |
| 18 | let created = send( |
| 19 | &app, |
| 20 | "POST", |
| 21 | "/events", |
| 22 | Some(&who), |
| 23 | Some(json!({ "title": "old title", "description": "old description" })), |
| 24 | ) |
| 25 | .await; |
| 26 | assert_eq!(created.status, StatusCode::CREATED, "round {round} create"); |
| 27 | let id = id_of(&created.body); |
| 28 | let uri = format!("/events/{id}"); |
| 29 | |
| 30 | let a_patch = json!({ "title": "new title" }); |
| 31 | let b_patch = json!({ "description": "new description" }); |
| 32 | let (a, b) = tokio::join!( |
| 33 | send(&app, "PATCH", &uri, Some(&who), Some(a_patch)), |
| 34 | send(&app, "PATCH", &uri, Some(&who), Some(b_patch)), |
| 35 | ); |
| 36 | assert_eq!(a.status, StatusCode::OK, "round {round} title patch"); |
| 37 | assert_eq!(b.status, StatusCode::OK, "round {round} description patch"); |
| 38 | |
| 39 | let after = send(&app, "GET", &uri, Some(&who), None).await.body; |
| 40 | assert_eq!(after["title"], "new title", "round {round}: title reverted"); |
| 41 | assert_eq!( |
| 42 | after["description"], "new description", |
| 43 | "round {round}: description reverted" |
| 44 | ); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | |
| 49 | |
| 50 | #[tokio::test] |
| 51 | async fn schedule_stays_clearable_and_keepable() { |
| 52 | let (app, db) = app_and_db().await; |
| 53 | let who = login_as(&app, &db, "veli", "teacher").await; |
| 54 | let future = 1_900_000_000_000_i64; |
| 55 | |
| 56 | let created = send( |
| 57 | &app, |
| 58 | "POST", |
| 59 | "/events", |
| 60 | Some(&who), |
| 61 | Some(json!({ "title": "timed", "starts_at": future, "ends_at": future + 3_600_000 })), |
| 62 | ) |
| 63 | .await; |
| 64 | assert_eq!(created.status, StatusCode::CREATED); |
| 65 | let uri = format!("/events/{}", id_of(&created.body)); |
| 66 | |
| 67 | |
| 68 | let kept = send( |
| 69 | &app, |
| 70 | "PATCH", |
| 71 | &uri, |
| 72 | Some(&who), |
| 73 | Some(json!({ "title": "t" })), |
| 74 | ) |
| 75 | .await; |
| 76 | assert_eq!(kept.status, StatusCode::OK); |
| 77 | assert_eq!(kept.body["starts_at"], future); |
| 78 | assert_eq!(kept.body["ends_at"], future + 3_600_000); |
| 79 | |
| 80 | |
| 81 | let cleared = send( |
| 82 | &app, |
| 83 | "PATCH", |
| 84 | &uri, |
| 85 | Some(&who), |
| 86 | Some(json!({ "starts_at": null, "ends_at": null })), |
| 87 | ) |
| 88 | .await; |
| 89 | assert_eq!(cleared.status, StatusCode::OK); |
| 90 | assert_eq!(cleared.body["starts_at"], json!(null)); |
| 91 | assert_eq!(cleared.body["ends_at"], json!(null)); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | #[tokio::test] |
| 98 | async fn range_check_still_sees_the_stored_other_side() { |
| 99 | let (app, db) = app_and_db().await; |
| 100 | let who = login_as(&app, &db, "ayse", "teacher").await; |
| 101 | let future = 1_900_000_000_000_i64; |
| 102 | |
| 103 | let created = send( |
| 104 | &app, |
| 105 | "POST", |
| 106 | "/events", |
| 107 | Some(&who), |
| 108 | Some(json!({ "title": "timed", "starts_at": future })), |
| 109 | ) |
| 110 | .await; |
| 111 | assert_eq!(created.status, StatusCode::CREATED); |
| 112 | let uri = format!("/events/{}", id_of(&created.body)); |
| 113 | |
| 114 | let bad = send( |
| 115 | &app, |
| 116 | "PATCH", |
| 117 | &uri, |
| 118 | Some(&who), |
| 119 | Some(json!({ "ends_at": future - 1000 })), |
| 120 | ) |
| 121 | .await; |
| 122 | assert_eq!( |
| 123 | bad.status, |
| 124 | StatusCode::BAD_REQUEST, |
| 125 | "ends_at before stored starts_at" |
| 126 | ); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | #[tokio::test] |
| 134 | async fn concurrent_range_patches_never_invert_the_stored_row() { |
| 135 | let (app, db) = app_and_db().await; |
| 136 | let who = login_as(&app, &db, "range", "teacher").await; |
| 137 | let starts = 1_900_000_000_000_i64; |
| 138 | let ends = starts + 3_600_000; |
| 139 | |
| 140 | for round in 0..20 { |
| 141 | let created = send( |
| 142 | &app, |
| 143 | "POST", |
| 144 | "/events", |
| 145 | Some(&who), |
| 146 | Some(json!({ "title": "timed", "starts_at": starts, "ends_at": ends })), |
| 147 | ) |
| 148 | .await; |
| 149 | assert_eq!(created.status, StatusCode::CREATED, "round {round} create"); |
| 150 | let uri = format!("/events/{}", id_of(&created.body)); |
| 151 | |
| 152 | |
| 153 | |
| 154 | let (a, b) = tokio::join!( |
| 155 | send( |
| 156 | &app, |
| 157 | "PATCH", |
| 158 | &uri, |
| 159 | Some(&who), |
| 160 | Some(json!({ "starts_at": ends - 1000 })), |
| 161 | ), |
| 162 | send( |
| 163 | &app, |
| 164 | "PATCH", |
| 165 | &uri, |
| 166 | Some(&who), |
| 167 | Some(json!({ "ends_at": starts + 1000 })), |
| 168 | ), |
| 169 | ); |
| 170 | |
| 171 | let after = send(&app, "GET", &uri, Some(&who), None).await.body; |
| 172 | let stored_starts = after["starts_at"].as_i64().expect("starts_at"); |
| 173 | let stored_ends = after["ends_at"].as_i64().expect("ends_at"); |
| 174 | assert!( |
| 175 | stored_starts <= stored_ends, |
| 176 | "round {round}: stored inverted range starts_at={stored_starts} \ |
| 177 | ends_at={stored_ends} (statuses {} {})", |
| 178 | a.status, |
| 179 | b.status |
| 180 | ); |
| 181 | } |
| 182 | } |