Skip to content

Commit

Permalink
make sure test timestamps and DB timestamps have the same granularity…
Browse files Browse the repository at this point in the history
… for comparing

truncate to nanoseconds, some postgres DB version only store seconds up
to 5 decimal places.
  • Loading branch information
michaeldjeffrey committed Feb 7, 2025
1 parent 38a529d commit 3f79329
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions mobile_verifier/src/heartbeats/last_location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,20 @@ mod tests {
use sqlx::PgPool;
use uuid::Uuid;

// Make sure test timestamps and DB timestamps have the same granularity
fn nanos_trunc(ts: DateTime<Utc>) -> DateTime<Utc> {
ts.duration_trunc(Duration::nanoseconds(10)).unwrap()
}
fn hour_trunc(ts: DateTime<Utc>) -> DateTime<Utc> {
ts.duration_trunc(Duration::hours(1)).unwrap()
}

async fn insert_heartbeat(
pool: &PgPool,
hotspot: &PublicKeyBinary,
received_timestamp: DateTime<Utc>,
validation_timestamp: DateTime<Utc>,
) -> anyhow::Result<()> {
let truncated = received_timestamp
.duration_trunc(Duration::hours(1))
.unwrap();
sqlx::query(
r#"
INSERT INTO wifi_heartbeats
Expand All @@ -203,9 +208,9 @@ mod tests {
"#,
)
.bind(hotspot)
.bind(validation_timestamp)
.bind(received_timestamp)
.bind(truncated)
.bind(nanos_trunc(validation_timestamp))
.bind(nanos_trunc(received_timestamp))
.bind(hour_trunc(received_timestamp))
.bind(Uuid::new_v4())
.execute(pool)
.await?;
Expand All @@ -218,8 +223,8 @@ mod tests {
location_validation_timestamp: DateTime<Utc>,
) -> LastLocation {
LastLocation {
location_validation_timestamp,
latest_timestamp,
location_validation_timestamp: nanos_trunc(location_validation_timestamp),
latest_timestamp: nanos_trunc(latest_timestamp),
lat: 0.0,
lon: 0.0,
}
Expand Down

0 comments on commit 3f79329

Please sign in to comment.