Skip to content

Commit 05c928d

Browse files
committed
Deal with subsecond precision differents in db tests.
Apparently some environments roundtrip datetimes in Postgres with different precision.
1 parent e862a48 commit 05c928d

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

tests/db/jobs.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::run_test;
2+
use crate::assert_datetime_approx_equal;
23
use serde_json::json;
34

45
#[test]
@@ -22,13 +23,13 @@ fn jobs() {
2223
let jobs = connection.get_jobs_to_execute().await.unwrap();
2324
assert_eq!(jobs.len(), 2);
2425
assert_eq!(jobs[0].name, "sample_job1");
25-
assert_eq!(jobs[0].scheduled_at, past);
26+
assert_datetime_approx_equal(&jobs[0].scheduled_at, &past);
2627
assert_eq!(jobs[0].metadata, json! {{"foo": 123}});
2728
assert_eq!(jobs[0].executed_at, None);
2829
assert_eq!(jobs[0].error_message, None);
2930

3031
assert_eq!(jobs[1].name, "sample_job2");
31-
assert_eq!(jobs[1].scheduled_at, past);
32+
assert_datetime_approx_equal(&jobs[1].scheduled_at, &past);
3233
assert_eq!(jobs[1].metadata, json! {{}});
3334
assert_eq!(jobs[1].executed_at, None);
3435
assert_eq!(jobs[1].error_message, None);

tests/db/notification.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::run_test;
2+
use crate::assert_datetime_approx_equal;
23
use std::num::NonZeroUsize;
34
use triagebot::db::notifications::{Identifier, Notification};
45

@@ -63,7 +64,7 @@ fn notification() {
6364
notifications[0].short_description.as_deref(),
6465
Some("Comment on some issue")
6566
);
66-
assert_eq!(notifications[0].time, now);
67+
assert_datetime_approx_equal(&notifications[0].time, &now);
6768
assert_eq!(notifications[0].metadata, None);
6869

6970
assert_eq!(
@@ -78,7 +79,7 @@ fn notification() {
7879
notifications[1].short_description.as_deref(),
7980
Some("Comment on some issue")
8081
);
81-
assert_eq!(notifications[1].time, now);
82+
assert_datetime_approx_equal(&notifications[1].time, &now);
8283
assert_eq!(notifications[1].metadata, None);
8384

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

101102
let notifications = connection.get_notifications("octocat").await.unwrap();

tests/db/rustc_commits.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::run_test;
2+
use crate::assert_datetime_approx_equal;
23
use triagebot::db::Commit;
34

45
#[test]
@@ -72,15 +73,15 @@ fn rustc_commits() {
7273
commits[0].parent_sha,
7374
"73f40197ecabf77ed59028af61739404eb60dd2e"
7475
);
75-
assert_eq!(commits[0].time, now);
76+
assert_datetime_approx_equal(&commits[0].time, &now);
7677
assert_eq!(commits[0].pr, Some(108228));
7778

7879
assert_eq!(commits[1].sha, "73f40197ecabf77ed59028af61739404eb60dd2e");
7980
assert_eq!(
8081
commits[1].parent_sha,
8182
"fcdbd1c07f0b6c8e7d8bbd727c6ca69a1af8c7e9"
8283
);
83-
assert_eq!(commits[1].time, now3);
84+
assert_datetime_approx_equal(&commits[1].time, &now3);
8485
assert_eq!(commits[1].pr, Some(107772));
8586
});
8687
}

tests/testsuite.rs

+17
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ mod db;
1616
mod github_client;
1717
mod server_test;
1818

19+
use chrono::DateTime;
1920
use std::collections::HashMap;
2021
use std::io::{BufRead, BufReader, Read, Write};
2122
use std::net::TcpStream;
@@ -378,3 +379,19 @@ impl HttpServer {
378379
}
379380
}
380381
}
382+
383+
/// Helper for comparing datetimes.
384+
///
385+
/// This helps ignore sub-second differences (which can happen when
386+
/// round-tripping through a database).
387+
pub fn assert_datetime_approx_equal<T, U>(a: &DateTime<T>, b: &DateTime<U>)
388+
where
389+
T: chrono::TimeZone,
390+
U: chrono::TimeZone,
391+
<T as chrono::TimeZone>::Offset: std::fmt::Display,
392+
<U as chrono::TimeZone>::Offset: std::fmt::Display,
393+
{
394+
if a.timestamp() != b.timestamp() {
395+
panic!("datetime differs {a} != {b}");
396+
}
397+
}

0 commit comments

Comments
 (0)