From 6e9f0069f5d1e4cfbf7f9feb8536795a78c818f6 Mon Sep 17 00:00:00 2001 From: Joe Carstairs Date: Mon, 28 Oct 2024 07:52:51 +0000 Subject: Integration tests for basic CRD queries --- backend/core/src/models/date_time_utc.rs | 106 ++++++++++++++++++++++--------- 1 file changed, 76 insertions(+), 30 deletions(-) (limited to 'backend/core/src/models/date_time_utc.rs') diff --git a/backend/core/src/models/date_time_utc.rs b/backend/core/src/models/date_time_utc.rs index 52c0cc6..d59d8f4 100644 --- a/backend/core/src/models/date_time_utc.rs +++ b/backend/core/src/models/date_time_utc.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use anyhow::{Error, Result}; use chrono::{DateTime, Datelike, Utc}; use diesel::{ backend::Backend, @@ -17,40 +17,88 @@ use serde::{Deserialize, Serialize}; PartialOrd, Serialize, Deserialize, - Copy, Clone, Debug, AsExpression, FromSqlRow, )] #[diesel(sql_type=Text)] -pub struct DateTimeUtc(DateTime); +pub struct DateTimeUtc { + chrono_datetime_utc: DateTime, +} impl DateTimeUtc { pub fn day_diff(&self, other: &DateTimeUtc) -> u32 { - self.0 + self.chrono_datetime_utc .num_days_from_ce() - .abs_diff(other.0.num_days_from_ce()) + .abs_diff(other.chrono_datetime_utc.num_days_from_ce()) } - pub fn to_day_str(&self) -> String { - self.0 - .to_rfc3339() - .split_at_checked(10) - .map(|(day_str, _)| String::from(day_str)) - .unwrap_or_else(|| String::from("")) + pub fn new(chrono_datetime_utc: &DateTime) -> Self { + Self { + chrono_datetime_utc: chrono_datetime_utc.clone(), + } } - pub fn new(date_time_utc: DateTime) -> Self { - Self(date_time_utc) + pub fn now() -> Self { + Self::new(&Utc::now()) } + /// Constructs DateTimeUtc from a day string, YYYY-MM-DD, in UTC + /// Example: "2024-10-24" -> Midnight 24 Oct 2024 UTC + pub fn from_day_str(day_str: &str) -> Result { + if !Self::is_day_str(day_str) { + Err(Error::msg(format!("{} is not a day string in YYYY-MM-DD format", day_str))) + } else { + let mut date_str = String::from(day_str); + date_str.push_str("T00:00:00.000Z"); + Ok(Self::from_date_str(&date_str)?) + } + } + + /// Constructs DateTimeUtc from a date string in ISO format, in UTC + /// Example: "2024-10-24T10:30:00.000" -> 10.30am 24 Oct 2024 UTC pub fn from_date_str(date_time_utc: &str) -> Result { - Ok(Self(date_time_utc.parse::>()?)) + let chrono_datetime_utc = date_time_utc.parse::>()?; + Ok(Self { + chrono_datetime_utc, + }) } - pub fn now() -> Self { - Self(Utc::now()) + /// Converts to a day string, YYYY-MM-DD, in UTC + /// Example: 10.30am 24 Oct 2024 UTC -> "2024-10-24" + pub fn to_day_str(&self) -> String { + let date_str = self.to_date_str(); + let day_str = date_str + .split_at_checked(10) + .unwrap() + .0; + String::from(day_str) + } + + /// Converts to a date string in ISO format, in UTC + /// Example: 10.30am 24 Oct 2024 UTC -> "2024-10-24T10:30:00.000Z" + pub fn to_date_str(&self) -> String { + self.chrono_datetime_utc.to_rfc3339() + } + + + fn is_day_str(str: &str) -> bool { + if str.len() != 10 { + return false; + } + for (ix, ch) in str.chars().enumerate() { + if ix == 4 || ix == 7 { + if ch != '-' { + return false; + } + } else { + if ch < '0' || ch > '9' { + return false; + } + } + } + return true; } } @@ -59,16 +107,15 @@ impl ToSql for DateTimeUtc { &'b self, out: &mut diesel::serialize::Output<'b, '_, Sqlite>, ) -> serialize::Result { - out.set_value(self.to_day_str()); - Ok(diesel::serialize::IsNull::No) + out.set_value(self.to_date_str()); + Ok(serialize::IsNull::No) } } impl FromSql for DateTimeUtc { fn from_sql(bytes: ::RawValue<'_>) -> deserialize::Result { - let str: String = FromSql::::from_sql(bytes)?; - let date_time = str.as_str().parse::>()?; - Ok(DateTimeUtc(date_time)) + let str = >::from_sql(bytes)?; + Ok(Self::from_date_str(&str)?) } } @@ -80,9 +127,9 @@ mod test { #[test] fn given_midnight_when_day_diff_next_midnight_less_1s_then_return_0() { - let start = DateTimeUtc::new(Utc.with_ymd_and_hms(2000, 1, 1, 0, 0, 0).unwrap().to_utc()); + let start = DateTimeUtc::new(&Utc.with_ymd_and_hms(2000, 1, 1, 0, 0, 0).unwrap().to_utc()); let end = DateTimeUtc::new( - Utc.with_ymd_and_hms(2000, 1, 1, 23, 59, 59) + &Utc.with_ymd_and_hms(2000, 1, 1, 23, 59, 59) .unwrap() .to_utc(), ); @@ -96,8 +143,8 @@ mod test { #[test] fn given_midnight_when_day_diff_next_midnight_then_return_1() { - let start = DateTimeUtc::new(Utc.with_ymd_and_hms(2000, 1, 1, 0, 0, 0).unwrap().to_utc()); - let end = DateTimeUtc::new(Utc.with_ymd_and_hms(2000, 1, 2, 0, 0, 0).unwrap().to_utc()); + let start = DateTimeUtc::new(&Utc.with_ymd_and_hms(2000, 1, 1, 0, 0, 0).unwrap().to_utc()); + let end = DateTimeUtc::new(&Utc.with_ymd_and_hms(2000, 1, 2, 0, 0, 0).unwrap().to_utc()); let day_diff = start.day_diff(&end); let neg_day_diff = end.day_diff(&start); @@ -109,11 +156,11 @@ mod test { #[test] fn given_midnight_less_1s_when_day_diff_midnight_then_return_1() { let start = DateTimeUtc::new( - Utc.with_ymd_and_hms(2000, 1, 1, 23, 59, 59) + &Utc.with_ymd_and_hms(2000, 1, 1, 23, 59, 59) .unwrap() .to_utc(), ); - let end = DateTimeUtc::new(Utc.with_ymd_and_hms(2000, 1, 2, 0, 0, 0).unwrap().to_utc()); + let end = DateTimeUtc::new(&Utc.with_ymd_and_hms(2000, 1, 2, 0, 0, 0).unwrap().to_utc()); let day_diff = start.day_diff(&end); let neg_day_diff = end.day_diff(&start); @@ -125,12 +172,11 @@ mod test { #[test] fn given_23_nov_1970_when_to_day_str_then_return_iso_day_str() { let date_time_utc = DateTimeUtc::new( - Utc.with_ymd_and_hms(1970, 11, 23, 0, 0, 0) + &Utc.with_ymd_and_hms(1970, 11, 23, 0, 0, 0) .unwrap() .to_utc(), ); - let day_str = date_time_utc.to_day_str(); - assert_eq!(day_str, "1970-11-23"); + assert_eq!(date_time_utc.to_day_str(), "1970-11-23"); } } -- cgit v1.2.3