summaryrefslogtreecommitdiff
path: root/src/tests.rs
blob: e97065aa6f183f64b6584a78a69bb3e7053ac93a (plain)
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
use axum::{
    Router,
    body::Body,
    http::{
        HeaderMap, Method, Request, StatusCode,
        header::{CONTENT_TYPE, LOCATION},
    },
};
use chrono::{TimeDelta, Utc};
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, "2025-10-14T12:00:00Z", 1.0).await;
    add_waist(app, "2025-10-15T00:00:00Z", 2.0).await;
    add_waist(app, "2025-10-15T23:59:59.500Z", 3.0).await;
    add_waist(app, "2025-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("2025-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": "2025-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("2025-10-14T12:00:00Z", "Nope", 1.0, "Kilogram"),
            StatusCode::UNPROCESSABLE_ENTITY,
        ),
        (
            measurement("2025-10-14T12:00:00Z", "Waist", -1.0, "Centimeter"),
            StatusCode::UNPROCESSABLE_ENTITY,
        ),
        (
            measurement("2025-10-14T12:00:00", "Waist", 80.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("2025-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, "2025-10-16T08:00:00Z", 4.0).await;
    add_waist(&app, "2025-10-14T12:00:00Z", 1.0).await;
    add_waist(&app, "2025-10-15T23:59:59.500Z", 3.0).await;
    add_waist(&app, "2025-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_bounds_are_half_open(pool: PgPool) {
    let app = router(pool);
    seed(&app).await;
    let uri = "/measurements?from=2025-10-15T00:00:00Z&to=2025-10-16T08:00:00Z";
    let (status, rows) = get_json(&app, uri).await;
    assert_eq!(status, StatusCode::OK);
    assert_eq!(values(&rows), [2.0, 3.0]);
}

#[sqlx::test]
async fn list_filters_by_utc_timestamps(pool: PgPool) {
    let app = router(pool);
    seed(&app).await;
    let cases = [
        (
            "/measurements?from=2025-10-15T00:00:00Z",
            vec![2.0, 3.0, 4.0],
        ),
        ("/measurements?to=2025-10-15T00:00:00Z", vec![1.0]),
        (
            "/measurements?from=2025-10-14T15:00:00Z&to=2025-10-15T15:00:00Z",
            vec![2.0],
        ),
        (
            "/measurements?from=2025-10-15T09:00:00%2B09:00",
            vec![2.0, 3.0, 4.0],
        ),
        (
            "/measurements?from=2025-10-15T00:00:00Z&to=2025-10-15T00:00:00Z",
            vec![],
        ),
        ("/measurements?from=2030-01-01T00:00:00Z", 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=2025-10-15T00:00:00Z&limit=1", vec![2.0]),
        (
            "/measurements?from=2025-10-15T00:00:00Z&order=desc&limit=2",
            vec![4.0, 3.0],
        ),
        (
            "/measurements?from=2025-10-15T00:00:00Z&to=2025-10-16T00:00:00Z&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_filters_by_metric_type(pool: PgPool) {
    let app = router(pool);
    let rows = [
        ("2025-10-14T12:00:00Z", "BodyWeight", 70.0, "Kilogram"),
        ("2025-10-15T12:00:00Z", "Waist", 80.0, "Centimeter"),
        ("2025-10-16T12:00:00Z", "BodyWeight", 71.0, "Kilogram"),
        ("2025-10-17T12:00:00Z", "Hip", 95.0, "Centimeter"),
        ("2025-10-18T12:00:00Z", "BodyWeight", 72.0, "Kilogram"),
    ];
    for (measured_at, metric_type, value, unit) in rows {
        let body = measurement(measured_at, metric_type, value, unit);
        let (status, _, _) = send(&app, Method::POST, "/measurements", Some(body)).await;
        assert_eq!(status, StatusCode::CREATED);
    }
    let cases = [
        ("/measurements", vec![70.0, 80.0, 71.0, 95.0, 72.0]),
        (
            "/measurements?metric_type=BodyWeight",
            vec![70.0, 71.0, 72.0],
        ),
        ("/measurements?metric_type=Waist", vec![80.0]),
        ("/measurements?metric_type=Hip", vec![95.0]),
        (
            "/measurements?metric_type=BodyWeight&order=desc&limit=2",
            vec![72.0, 71.0],
        ),
        (
            "/measurements?metric_type=BodyWeight&from=2025-10-15T00:00:00Z&to=2025-10-18T00:00:00Z",
            vec![71.0],
        ),
        (
            "/measurements?metric_type=Hip&to=2025-10-17T00:00:00Z",
            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_rejects_bad_queries(pool: PgPool) {
    let app = router(pool);
    let uris = [
        "/measurements?limit=0",
        "/measurements?limit=-1",
        "/measurements?order=sideways",
        "/measurements?metric_type=Nope",
        "/measurements?metric_type=bodyweight",
        "/measurements?from=bad",
        "/measurements?from=2025-10-15",
        "/measurements?to=2025-10-15T00:00:00",
        "/measurements?from=2025-10-15T09:00:00+09:00",
        "/measurements?date=2025-10-15",
        "/measurements?from=2025-10-16T00:00:00Z&to=2025-10-14T00:00:00Z",
    ];
    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, "2025-10-14T12:00:00Z", 80.0).await;
    let uri = format!("/measurements/{id}");
    let body = measurement("2025-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": "2025-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("2025-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, "2025-10-14T12:00:00Z", 80.0).await;
    let uri = format!("/measurements/{id}");
    let bodies = [
        json!({"x": 1}).to_string(),
        measurement("2025-10-20T09:30:00Z", "Waist", 82.0, "Kilogram"),
        measurement("2025-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, "2025-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, "2025-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=2025-10-14",
        "/units",
        "/metric_types",
    ] {
        let (status, _) = get_json(&app, uri).await;
        assert_eq!(status, StatusCode::NOT_FOUND, "{uri}");
    }
}

#[sqlx::test]
async fn errors_have_a_json_body(pool: PgPool) {
    let app = router(pool);
    let missing = format!("/measurements/{}", Uuid::now_v7());
    let valid = measurement("2025-10-14T12:00:00Z", "Waist", 80.0, "Centimeter");
    let invalid_pair = measurement("2025-10-14T12:00:00Z", "Waist", 80.0, "Kilogram");
    let cases = [
        (
            Method::GET,
            "/measurements?limit=0".to_owned(),
            None,
            StatusCode::BAD_REQUEST,
        ),
        (
            Method::GET,
            "/measurements?order=sideways".to_owned(),
            None,
            StatusCode::BAD_REQUEST,
        ),
        (
            Method::GET,
            "/measurements?from=bad".to_owned(),
            None,
            StatusCode::BAD_REQUEST,
        ),
        (
            Method::GET,
            "/measurements/not-a-uuid".to_owned(),
            None,
            StatusCode::BAD_REQUEST,
        ),
        (Method::GET, missing.clone(), None, StatusCode::NOT_FOUND),
        (Method::PUT, missing, Some(valid), StatusCode::NOT_FOUND),
        (
            Method::POST,
            "/measurements".to_owned(),
            Some("not json".to_owned()),
            StatusCode::BAD_REQUEST,
        ),
        (
            Method::POST,
            "/measurements".to_owned(),
            Some(invalid_pair),
            StatusCode::UNPROCESSABLE_ENTITY,
        ),
        (Method::GET, "/nope".to_owned(), None, StatusCode::NOT_FOUND),
        (
            Method::PATCH,
            "/measurements".to_owned(),
            None,
            StatusCode::METHOD_NOT_ALLOWED,
        ),
    ];
    for (method, uri, body, expected) in cases {
        let (status, _, body) = send(&app, method.clone(), &uri, body).await;
        assert_eq!(status, expected, "{method} {uri}");
        assert!(body["error"].is_string(), "{method} {uri}: {body}");
    }
}

#[sqlx::test]
async fn error_messages_explain_the_problem(pool: PgPool) {
    let app = router(pool);
    let cases = [
        ("/measurements?limit=0", "limit must be positive"),
        (
            "/measurements?from=2025-10-16T00:00:00Z&to=2025-10-14T00:00:00Z",
            "from must not be after to",
        ),
    ];
    for (uri, message) in cases {
        let (_, body) = get_json(&app, uri).await;
        assert_eq!(body["error"], message, "{uri}");
    }
    let (_, body) = get_json(&app, "/measurements?date=2025-10-15").await;
    let message = body["error"].as_str().unwrap();
    assert!(message.contains("unknown field `date`"), "{message}");
    let body = measurement("2025-10-14T12:00:00Z", "Waist", 80.0, "Kilogram");
    let (_, _, body) = send(&app, Method::POST, "/measurements", Some(body)).await;
    let message = body["error"].as_str().unwrap();
    assert!(
        message.contains("Invalid pair Waist, Kilogram"),
        "{message}"
    );
}

#[sqlx::test]
async fn create_rejects_future_measured_at_beyond_tolerance(pool: PgPool) {
    let app = router(pool);
    let cases = [
        (Utc::now() - TimeDelta::days(1), StatusCode::CREATED),
        (Utc::now() + TimeDelta::minutes(1), StatusCode::CREATED),
        (
            Utc::now() + TimeDelta::hours(1),
            StatusCode::UNPROCESSABLE_ENTITY,
        ),
        (
            Utc::now() + TimeDelta::hours(9),
            StatusCode::UNPROCESSABLE_ENTITY,
        ),
    ];
    for (measured_at, expected) in cases {
        let measured_at = measured_at.to_rfc3339();
        let body = measurement(&measured_at, "Waist", 80.0, "Centimeter");
        let (status, _, body) = send(&app, Method::POST, "/measurements", Some(body)).await;
        assert_eq!(status, expected, "{measured_at}");
        if expected == StatusCode::UNPROCESSABLE_ENTITY {
            let message = body["error"].as_str().unwrap();
            assert!(
                message.contains("measured_at is in the future"),
                "{message}"
            );
        }
    }
}

#[sqlx::test]
async fn update_rejects_future_measured_at(pool: PgPool) {
    let app = router(pool);
    let id = add_waist(&app, "2025-10-14T12:00:00Z", 80.0).await;
    let uri = format!("/measurements/{id}");
    let future = (Utc::now() + TimeDelta::hours(1)).to_rfc3339();
    let body = measurement(&future, "Waist", 82.0, "Centimeter");
    let (status, _, _) = send(&app, Method::PUT, &uri, Some(body)).await;
    assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY);
    let (_, row) = get_json(&app, &uri).await;
    assert_eq!(row["value"], json!(80.0));
}