From 05c928d94e422e093b4d3315360df4c904fa9883 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 20 Feb 2023 15:01:35 -0800 Subject: [PATCH] Deal with subsecond precision differents in db tests. Apparently some environments roundtrip datetimes in Postgres with different precision. --- tests/db/jobs.rs | 5 +++-- tests/db/notification.rs | 7 ++++--- tests/db/rustc_commits.rs | 5 +++-- tests/testsuite.rs | 17 +++++++++++++++++ 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/tests/db/jobs.rs b/tests/db/jobs.rs index 824b1212..a2941063 100644 --- a/tests/db/jobs.rs +++ b/tests/db/jobs.rs @@ -1,4 +1,5 @@ use super::run_test; +use crate::assert_datetime_approx_equal; use serde_json::json; #[test] @@ -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); diff --git a/tests/db/notification.rs b/tests/db/notification.rs index b54e39ae..8edd986b 100644 --- a/tests/db/notification.rs +++ b/tests/db/notification.rs @@ -1,4 +1,5 @@ use super::run_test; +use crate::assert_datetime_approx_equal; use std::num::NonZeroUsize; use triagebot::db::notifications::{Identifier, Notification}; @@ -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(¬ifications[0].time, &now); assert_eq!(notifications[0].metadata, None); assert_eq!( @@ -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(¬ifications[1].time, &now); assert_eq!(notifications[1].metadata, None); let notifications = connection.get_notifications("weihanglo").await.unwrap(); @@ -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(¬ifications[0].time, &now); assert_eq!(notifications[0].metadata, None); let notifications = connection.get_notifications("octocat").await.unwrap(); diff --git a/tests/db/rustc_commits.rs b/tests/db/rustc_commits.rs index b1b307ab..8c36c6fb 100644 --- a/tests/db/rustc_commits.rs +++ b/tests/db/rustc_commits.rs @@ -1,4 +1,5 @@ use super::run_test; +use crate::assert_datetime_approx_equal; use triagebot::db::Commit; #[test] @@ -72,7 +73,7 @@ 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"); @@ -80,7 +81,7 @@ fn rustc_commits() { 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)); }); } diff --git a/tests/testsuite.rs b/tests/testsuite.rs index 0e7d38f1..bfd29f70 100644 --- a/tests/testsuite.rs +++ b/tests/testsuite.rs @@ -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; @@ -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(a: &DateTime, b: &DateTime) +where + T: chrono::TimeZone, + U: chrono::TimeZone, + ::Offset: std::fmt::Display, + ::Offset: std::fmt::Display, +{ + if a.timestamp() != b.timestamp() { + panic!("datetime differs {a} != {b}"); + } +}