summaryrefslogtreecommitdiff
path: root/src/models.rs
blob: b3f23d1abfdbc96c52db455982919eadc67c6b9c (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
use chrono::{DateTime, NaiveDate, Utc};
use serde::{Deserialize, Serialize};
use strum::EnumIter;
use uuid::Uuid;

#[derive(sqlx::Type, Serialize, Deserialize, EnumIter)]
#[sqlx(type_name = "metric_type")]
pub enum MetricType {
    BodyWeight,
    Hip,
    Waist,
}

#[derive(sqlx::Type, Serialize, Deserialize, EnumIter)]
#[sqlx(type_name = "unit")]
pub enum Unit {
    Kilogram,
    Centimeter,
}

#[derive(Serialize)]
pub struct MeasurementView {
    pub id: Uuid,
    pub measured_at: DateTime<Utc>,
    pub metric_type: MetricType,
    pub value: f64,
    pub unit: Unit,
}

#[derive(Deserialize)]
pub struct MeasurementData {
    pub measured_at: DateTime<Utc>,
    pub metric_type: MetricType,
    pub value: f64,
    pub unit: Unit,
}

#[derive(Deserialize)]
pub struct DateQuery {
    pub date: Option<NaiveDate>,
    pub from: Option<NaiveDate>,
    pub to: Option<NaiveDate>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use sqlx::PgPool;
    use strum::IntoEnumIterator;

    #[sqlx::test]
    async fn unit_enums_match(pool: PgPool) {
        let values: Vec<Unit> = sqlx::query_scalar("SELECT enum_range(NULL::unit)")
            .fetch_one(&pool)
            .await
            .expect("Database has a variant that Rust doesn't");
        assert_eq!(
            values.len(),
            Unit::iter().count(),
            "Rust has a variant that the database doesn't"
        );
    }

    #[sqlx::test]
    async fn metric_type_enums_match(pool: PgPool) {
        let values: Vec<MetricType> = sqlx::query_scalar("SELECT enum_range(NULL::metric_type)")
            .fetch_one(&pool)
            .await
            .expect("Database has a variant that Rust doesn't");
        assert_eq!(
            values.len(),
            MetricType::iter().count(),
            "Rust has a variant that the database doesn't"
        );
    }
}