Skip to content

Commit

Permalink
Deal with subsecond precision differents in db tests.
Browse files Browse the repository at this point in the history
Apparently some environments roundtrip datetimes in Postgres
with different precision.
  • Loading branch information
ehuss committed Feb 21, 2023
1 parent e862a48 commit 05c928d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
5 changes: 3 additions & 2 deletions tests/db/jobs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::run_test;
use crate::assert_datetime_approx_equal;
use serde_json::json;

#[test]
Expand All @@ -22,13 +23,13 @@ fn jobs() {
let jobs = connection.get_jobs_to_execute().await.unwrap();
assert_eq!(jobs.len(), 2);
assert_eq!(jobs[0].name, "sample_job1");
assert_eq!(jobs[0].scheduled_at, past);
assert_datetime_approx_equal(&jobs[0].scheduled_at, &past);
assert_eq!(jobs[0].metadata, json! {{"foo": 123}});
assert_eq!(jobs[0].executed_at, None);
assert_eq!(jobs[0].error_message, None);

assert_eq!(jobs[1].name, "sample_job2");
assert_eq!(jobs[1].scheduled_at, past);
assert_datetime_approx_equal(&jobs[1].scheduled_at, &past);
assert_eq!(jobs[1].metadata, json! {{}});
assert_eq!(jobs[1].executed_at, None);
assert_eq!(jobs[1].error_message, None);
Expand Down
7 changes: 4 additions & 3 deletions tests/db/notification.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::run_test;
use crate::assert_datetime_approx_equal;
use std::num::NonZeroUsize;
use triagebot::db::notifications::{Identifier, Notification};

Expand Down Expand Up @@ -63,7 +64,7 @@ fn notification() {
notifications[0].short_description.as_deref(),
Some("Comment on some issue")
);
assert_eq!(notifications[0].time, now);
assert_datetime_approx_equal(&notifications[0].time, &now);
assert_eq!(notifications[0].metadata, None);

assert_eq!(
Expand All @@ -78,7 +79,7 @@ fn notification() {
notifications[1].short_description.as_deref(),
Some("Comment on some issue")
);
assert_eq!(notifications[1].time, now);
assert_datetime_approx_equal(&notifications[1].time, &now);
assert_eq!(notifications[1].metadata, None);

let notifications = connection.get_notifications("weihanglo").await.unwrap();
Expand All @@ -95,7 +96,7 @@ fn notification() {
notifications[0].short_description.as_deref(),
Some("Comment on some issue")
);
assert_eq!(notifications[0].time, now);
assert_datetime_approx_equal(&notifications[0].time, &now);
assert_eq!(notifications[0].metadata, None);

let notifications = connection.get_notifications("octocat").await.unwrap();
Expand Down
5 changes: 3 additions & 2 deletions tests/db/rustc_commits.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::run_test;
use crate::assert_datetime_approx_equal;
use triagebot::db::Commit;

#[test]
Expand Down Expand Up @@ -72,15 +73,15 @@ fn rustc_commits() {
commits[0].parent_sha,
"73f40197ecabf77ed59028af61739404eb60dd2e"
);
assert_eq!(commits[0].time, now);
assert_datetime_approx_equal(&commits[0].time, &now);
assert_eq!(commits[0].pr, Some(108228));

assert_eq!(commits[1].sha, "73f40197ecabf77ed59028af61739404eb60dd2e");
assert_eq!(
commits[1].parent_sha,
"fcdbd1c07f0b6c8e7d8bbd727c6ca69a1af8c7e9"
);
assert_eq!(commits[1].time, now3);
assert_datetime_approx_equal(&commits[1].time, &now3);
assert_eq!(commits[1].pr, Some(107772));
});
}
17 changes: 17 additions & 0 deletions tests/testsuite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod db;
mod github_client;
mod server_test;

use chrono::DateTime;
use std::collections::HashMap;
use std::io::{BufRead, BufReader, Read, Write};
use std::net::TcpStream;
Expand Down Expand Up @@ -378,3 +379,19 @@ impl HttpServer {
}
}
}

/// Helper for comparing datetimes.
///
/// This helps ignore sub-second differences (which can happen when
/// round-tripping through a database).
pub fn assert_datetime_approx_equal<T, U>(a: &DateTime<T>, b: &DateTime<U>)
where
T: chrono::TimeZone,
U: chrono::TimeZone,
<T as chrono::TimeZone>::Offset: std::fmt::Display,
<U as chrono::TimeZone>::Offset: std::fmt::Display,
{
if a.timestamp() != b.timestamp() {
panic!("datetime differs {a} != {b}");
}
}

0 comments on commit 05c928d

Please sign in to comment.