Skip to content

Commit

Permalink
added tests for PgInterval
Browse files Browse the repository at this point in the history
  • Loading branch information
yasamoka committed Oct 10, 2023
1 parent e9898ec commit c9c53dc
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/backend/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,7 @@ pub trait QueryBuilder:
write!(s, "'").unwrap();

if v.months > 0 {
write!(s, "{} MONTHS", v.days).unwrap();
write!(s, "{} MONTHS", v.months).unwrap();
space = true;
}

Expand Down
122 changes: 116 additions & 6 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,12 +509,12 @@ type_to_box_value!(Vec<u8>, Bytes, Binary(BlobSize::Blob(None)));
type_to_box_value!(String, String, String(None));

#[cfg(feature = "postgres-interval")]
#[derive(Debug, Eq, PartialEq, Clone, Hash)]
pub struct PgIntervalValue {
pub months: i32,
pub days: i32,
pub microseconds: i64,
}
#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
pub struct PgIntervalValue {
pub months: i32,
pub days: i32,
pub microseconds: i64,
}

#[cfg(feature = "with-json")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-json")))]
Expand Down Expand Up @@ -1920,6 +1920,116 @@ mod tests {
);
}

#[test]
#[cfg(feature = "postgres-interval")]
fn test_pginterval_value() {
let interval = PgIntervalValue {
months: 1,
days: 2,
microseconds: 300,
};
let value: Value = interval.into();
let out: PgIntervalValue = value.unwrap();
assert_eq!(out, interval);
}

#[test]
#[cfg(feature = "postgres-interval")]
fn test_pginterval_query() {
use crate::*;

const VALUES: [(PgIntervalValue, &str); 10] = [
(
PgIntervalValue {
months: 0,
days: 0,
microseconds: 1,
},
"1 MICROSECONDS",
),
(
PgIntervalValue {
months: 0,
days: 0,
microseconds: 100,
},
"100 MICROSECONDS",
),
(
PgIntervalValue {
months: 0,
days: 1,
microseconds: 0,
},
"1 DAYS",
),
(
PgIntervalValue {
months: 0,
days: 2,
microseconds: 0,
},
"2 DAYS",
),
(
PgIntervalValue {
months: 0,
days: 2,
microseconds: 100,
},
"2 DAYS 100 MICROSECONDS",
),
(
PgIntervalValue {
months: 1,
days: 0,
microseconds: 0,
},
"1 MONTHS",
),
(
PgIntervalValue {
months: 2,
days: 0,
microseconds: 0,
},
"2 MONTHS",
),
(
PgIntervalValue {
months: 2,
days: 0,
microseconds: 100,
},
"2 MONTHS 100 MICROSECONDS",
),
(
PgIntervalValue {
months: 2,
days: 2,
microseconds: 0,
},
"2 MONTHS 2 DAYS",
),
(
PgIntervalValue {
months: 2,
days: 2,
microseconds: 100,
},
"2 MONTHS 2 DAYS 100 MICROSECONDS",
),
];

for (interval, formatted) in VALUES {
let query = Query::select().expr(interval).to_owned();
assert_eq!(
query.to_string(PostgresQueryBuilder),
format!("SELECT '{formatted}'::interval")
);
}
}

#[test]
#[cfg(feature = "with-uuid")]
fn test_uuid_value() {
Expand Down

0 comments on commit c9c53dc

Please sign in to comment.