1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
use axum::{
Router,
body::Body,
http::{
HeaderMap, Method, Request, StatusCode,
header::{CONTENT_TYPE, LOCATION},
},
};
use http_body_util::BodyExt;
use serde_json::{Value, json};
use sqlx::PgPool;
use tower::ServiceExt;
use uuid::Uuid;
use crate::router;
async fn send(
app: &Router,
method: Method,
uri: &str,
body: Option<String>,
) -> (StatusCode, HeaderMap, Value) {
let mut request = Request::builder().method(method).uri(uri);
let body = match body {
Some(body) => {
request = request.header(CONTENT_TYPE, "application/json");
Body::from(body)
}
None => Body::empty(),
};
let response = app
.clone()
.oneshot(request.body(body).unwrap())
.await
.unwrap();
let status = response.status();
let headers = response.headers().clone();
let bytes = response.into_body().collect().await.unwrap().to_bytes();
(
status,
headers,
serde_json::from_slice(&bytes).unwrap_or(Value::Null),
)
}
async fn get_json(app: &Router, uri: &str) -> (StatusCode, Value) {
let (status, _, body) = send(app, Method::GET, uri, None).await;
(status, body)
}
fn measurement(measured_at: &str, metric_type: &str, value: f64, unit: &str) -> String {
json!({
"measured_at": measured_at,
"metric_type": metric_type,
"value": value,
"unit": unit,
})
.to_string()
}
async fn add_waist(app: &Router, measured_at: &str, value: f64) -> String {
let body = measurement(measured_at, "Waist", value, "Centimeter");
let (status, _, created) = send(app, Method::POST, "/measurements", Some(body)).await;
assert_eq!(status, StatusCode::CREATED);
created["id"].as_str().unwrap().to_owned()
}
async fn seed(app: &Router) {
add_waist(app, "2026-10-14T12:00:00Z", 1.0).await;
add_waist(app, "2026-10-15T00:00:00Z", 2.0).await;
add_waist(app, "2026-10-15T23:59:59.500Z", 3.0).await;
add_waist(app, "2026-10-16T08:00:00Z", 4.0).await;
}
fn values(rows: &Value) -> Vec<f64> {
rows.as_array()
.unwrap()
.iter()
.map(|row| row["value"].as_f64().unwrap())
.collect()
}
#[sqlx::test]
async fn health_is_ok(pool: PgPool) {
let app = router(pool);
let (status, _) = get_json(&app, "/health").await;
assert_eq!(status, StatusCode::OK);
}
#[sqlx::test]
async fn create_returns_the_measurement_and_location(pool: PgPool) {
let app = router(pool);
let body = measurement("2026-10-14T12:00:00Z", "Waist", 80.5, "Centimeter");
let (status, headers, created) = send(&app, Method::POST, "/measurements", Some(body)).await;
assert_eq!(status, StatusCode::CREATED);
let id = created["id"].as_str().unwrap();
Uuid::parse_str(id).unwrap();
assert_eq!(
created,
json!({
"id": id,
"measured_at": "2026-10-14T12:00:00Z",
"metric_type": "Waist",
"value": 80.5,
"unit": "Centimeter",
})
);
let location = headers[LOCATION].to_str().unwrap();
assert_eq!(location, format!("/measurements/{id}"));
let (status, fetched) = get_json(&app, location).await;
assert_eq!(status, StatusCode::OK);
assert_eq!(fetched, created);
}
#[sqlx::test]
async fn create_rejects_bad_bodies(pool: PgPool) {
let app = router(pool);
let cases = [
(json!({"metric_type": "Hip"}).to_string(), StatusCode::UNPROCESSABLE_ENTITY),
(
measurement("2026-10-14T12:00:00Z", "Nope", 1.0, "Kilogram"),
StatusCode::UNPROCESSABLE_ENTITY,
),
(
measurement("2026-10-14T12:00:00Z", "Waist", -1.0, "Centimeter"),
StatusCode::UNPROCESSABLE_ENTITY,
),
("not json".to_owned(), StatusCode::BAD_REQUEST),
];
for (body, expected) in cases {
let (status, _, _) = send(&app, Method::POST, "/measurements", Some(body.clone())).await;
assert_eq!(status, expected, "body: {body}");
}
let (_, rows) = get_json(&app, "/measurements").await;
assert!(values(&rows).is_empty());
}
#[sqlx::test]
async fn metric_types_lists_the_valid_pairs(pool: PgPool) {
let app = router(pool);
let (status, pairs) = get_json(&app, "/metric-types").await;
assert_eq!(status, StatusCode::OK);
assert_eq!(
pairs,
json!([
{"metric_type": "BodyWeight", "unit": "Kilogram"},
{"metric_type": "Hip", "unit": "Centimeter"},
{"metric_type": "Waist", "unit": "Centimeter"},
])
);
}
#[sqlx::test]
async fn create_accepts_exactly_the_advertised_pairs(pool: PgPool) {
let app = router(pool);
let (_, pairs) = get_json(&app, "/metric-types").await;
let pairs = pairs.as_array().unwrap();
for metric_type in ["BodyWeight", "Hip", "Waist"] {
for unit in ["Kilogram", "Centimeter"] {
let advertised = pairs.contains(&json!({"metric_type": metric_type, "unit": unit}));
let expected = if advertised {
StatusCode::CREATED
} else {
StatusCode::UNPROCESSABLE_ENTITY
};
let body = measurement("2026-10-14T12:00:00Z", metric_type, 70.0, unit);
let (status, _, _) = send(&app, Method::POST, "/measurements", Some(body)).await;
assert_eq!(status, expected, "{metric_type} {unit}");
}
}
}
#[sqlx::test]
async fn list_is_empty_array_without_data(pool: PgPool) {
let app = router(pool);
let (status, rows) = get_json(&app, "/measurements").await;
assert_eq!(status, StatusCode::OK);
assert_eq!(rows, json!([]));
}
#[sqlx::test]
async fn list_orders_by_measured_at(pool: PgPool) {
let app = router(pool);
add_waist(&app, "2026-10-16T08:00:00Z", 4.0).await;
add_waist(&app, "2026-10-14T12:00:00Z", 1.0).await;
add_waist(&app, "2026-10-15T23:59:59.500Z", 3.0).await;
add_waist(&app, "2026-10-15T00:00:00Z", 2.0).await;
let (_, rows) = get_json(&app, "/measurements").await;
assert_eq!(values(&rows), [1.0, 2.0, 3.0, 4.0]);
let (_, rows) = get_json(&app, "/measurements?order=asc").await;
assert_eq!(values(&rows), [1.0, 2.0, 3.0, 4.0]);
let (_, rows) = get_json(&app, "/measurements?order=desc").await;
assert_eq!(values(&rows), [4.0, 3.0, 2.0, 1.0]);
}
#[sqlx::test]
async fn list_limit_applies_after_ordering(pool: PgPool) {
let app = router(pool);
seed(&app).await;
let (_, rows) = get_json(&app, "/measurements?limit=2").await;
assert_eq!(values(&rows), [1.0, 2.0]);
let (_, rows) = get_json(&app, "/measurements?limit=2&order=desc").await;
assert_eq!(values(&rows), [4.0, 3.0]);
}
#[sqlx::test]
async fn list_single_date_includes_the_whole_day(pool: PgPool) {
let app = router(pool);
seed(&app).await;
let (status, rows) = get_json(&app, "/measurements?date=2026-10-15").await;
assert_eq!(status, StatusCode::OK);
assert_eq!(values(&rows), [2.0, 3.0]);
}
#[sqlx::test]
async fn list_ranges_are_inclusive_of_both_days(pool: PgPool) {
let app = router(pool);
seed(&app).await;
let cases = [
("/measurements?from=2026-10-15", vec![2.0, 3.0, 4.0]),
("/measurements?to=2026-10-14", vec![1.0]),
("/measurements?from=2026-10-14&to=2026-10-15", vec![1.0, 2.0, 3.0]),
("/measurements?from=2026-10-15&to=2026-10-15", vec![2.0, 3.0]),
("/measurements?from=2030-01-01", vec![]),
];
for (uri, expected) in cases {
let (status, rows) = get_json(&app, uri).await;
assert_eq!(status, StatusCode::OK, "{uri}");
assert_eq!(values(&rows), expected, "{uri}");
}
}
#[sqlx::test]
async fn list_combines_filters_with_order_and_limit(pool: PgPool) {
let app = router(pool);
seed(&app).await;
let cases = [
("/measurements?from=2026-10-15&limit=1", vec![2.0]),
("/measurements?from=2026-10-15&order=desc&limit=2", vec![4.0, 3.0]),
("/measurements?date=2026-10-15&order=desc", vec![3.0, 2.0]),
];
for (uri, expected) in cases {
let (status, rows) = get_json(&app, uri).await;
assert_eq!(status, StatusCode::OK, "{uri}");
assert_eq!(values(&rows), expected, "{uri}");
}
}
#[sqlx::test]
async fn list_rejects_bad_queries(pool: PgPool) {
let app = router(pool);
let uris = [
"/measurements?limit=0",
"/measurements?limit=-1",
"/measurements?order=sideways",
"/measurements?date=bad",
"/measurements?from=bad",
"/measurements?date=2026-10-15&from=2026-10-14",
"/measurements?date=2026-10-15&to=2026-10-16",
"/measurements?from=2026-10-16&to=2026-10-14",
];
for uri in uris {
let (status, _) = get_json(&app, uri).await;
assert_eq!(status, StatusCode::BAD_REQUEST, "{uri}");
}
}
#[sqlx::test]
async fn get_not_found_and_bad_id(pool: PgPool) {
let app = router(pool);
let (status, _) = get_json(&app, &format!("/measurements/{}", Uuid::now_v7())).await;
assert_eq!(status, StatusCode::NOT_FOUND);
let (status, _) = get_json(&app, "/measurements/not-a-uuid").await;
assert_eq!(status, StatusCode::BAD_REQUEST);
}
#[sqlx::test]
async fn update_replaces_the_row(pool: PgPool) {
let app = router(pool);
let id = add_waist(&app, "2026-10-14T12:00:00Z", 80.0).await;
let uri = format!("/measurements/{id}");
let body = measurement("2026-10-20T09:30:00Z", "Hip", 95.0, "Centimeter");
let (status, _, _) = send(&app, Method::PUT, &uri, Some(body)).await;
assert_eq!(status, StatusCode::NO_CONTENT);
let (_, row) = get_json(&app, &uri).await;
assert_eq!(
row,
json!({
"id": id,
"measured_at": "2026-10-20T09:30:00Z",
"metric_type": "Hip",
"value": 95.0,
"unit": "Centimeter",
})
);
}
#[sqlx::test]
async fn update_missing_id_is_not_found(pool: PgPool) {
let app = router(pool);
let body = measurement("2026-10-20T09:30:00Z", "Hip", 95.0, "Centimeter");
let uri = format!("/measurements/{}", Uuid::now_v7());
let (status, _, _) = send(&app, Method::PUT, &uri, Some(body)).await;
assert_eq!(status, StatusCode::NOT_FOUND);
}
#[sqlx::test]
async fn update_rejects_invalid_body_and_leaves_row_alone(pool: PgPool) {
let app = router(pool);
let id = add_waist(&app, "2026-10-14T12:00:00Z", 80.0).await;
let uri = format!("/measurements/{id}");
let bodies = [
json!({"x": 1}).to_string(),
measurement("2026-10-20T09:30:00Z", "Waist", 82.0, "Kilogram"),
measurement("2026-10-20T09:30:00Z", "Waist", -1.0, "Centimeter"),
];
for body in bodies {
let (status, _, _) = send(&app, Method::PUT, &uri, Some(body.clone())).await;
assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY, "body: {body}");
}
let (_, row) = get_json(&app, &uri).await;
assert_eq!(row["value"], json!(80.0));
}
#[sqlx::test]
async fn delete_removes_the_row(pool: PgPool) {
let app = router(pool);
let id = add_waist(&app, "2026-10-14T12:00:00Z", 80.0).await;
let uri = format!("/measurements/{id}");
let (status, _, _) = send(&app, Method::DELETE, &uri, None).await;
assert_eq!(status, StatusCode::NO_CONTENT);
let (status, _) = get_json(&app, &uri).await;
assert_eq!(status, StatusCode::NOT_FOUND);
}
#[sqlx::test]
async fn delete_is_idempotent(pool: PgPool) {
let app = router(pool);
let id = add_waist(&app, "2026-10-14T12:00:00Z", 80.0).await;
let uri = format!("/measurements/{id}");
for _ in 0..2 {
let (status, _, _) = send(&app, Method::DELETE, &uri, None).await;
assert_eq!(status, StatusCode::NO_CONTENT);
}
let missing = format!("/measurements/{}", Uuid::now_v7());
let (status, _, _) = send(&app, Method::DELETE, &missing, None).await;
assert_eq!(status, StatusCode::NO_CONTENT);
}
#[sqlx::test]
async fn old_paths_are_gone(pool: PgPool) {
let app = router(pool);
for uri in ["/", "/all", "/search?from=2026-10-14", "/units", "/metric_types"] {
let (status, _) = get_json(&app, uri).await;
assert_eq!(status, StatusCode::NOT_FOUND, "{uri}");
}
}
|