Skip to content

Commit

Permalink
Integro-test
Browse files Browse the repository at this point in the history
  • Loading branch information
squadgazzz committed Nov 27, 2023
1 parent c98939a commit 9c3aa44
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions crates/autopilot/src/periodic_db_cleanup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,70 @@ impl Metrics {
Metrics::instance(observe::metrics::get_storage_registry()).unwrap()
}
}

#[cfg(test)]
mod tests {
use {
super::*,
database::{
byte_array::ByteArray,
order_events::{OrderEvent, OrderEventLabel},
},
sqlx::PgConnection,
};

#[tokio::test]
#[ignore]
async fn postgres_count_rows_in_table_() {
let db = Postgres::new("postgresql://").await.unwrap();
let mut ex = db.0.begin().await.unwrap();
database::clear_DANGER_(&mut ex).await.unwrap();

let event_a = OrderEvent {
order_uid: ByteArray([1; 56]),
timestamp: Utc::now() - chrono::Duration::days(31),
label: OrderEventLabel::Created,
};
let latest_timestamp = Utc::now() - chrono::Duration::days(29);
let event_b = OrderEvent {
order_uid: ByteArray([2; 56]),
timestamp: latest_timestamp,
label: OrderEventLabel::Created,
};

database::order_events::insert_order_event(&mut ex, &event_a)
.await
.unwrap();
database::order_events::insert_order_event(&mut ex, &event_b)
.await
.unwrap();

let count = order_events_before_timestamp_count(
&mut ex,
latest_timestamp + chrono::Duration::days(1),
)
.await;
assert_eq!(count, 2u64);

let count = order_events_before_timestamp_count(&mut ex, latest_timestamp).await;
assert_eq!(count, 1u64);

async fn order_events_before_timestamp_count(
ex: &mut PgConnection,
timestamp: DateTime<Utc>,
) -> u64 {
const QUERY: &str = r#"
SELECT COUNT(1)
FROM order_events
WHERE timestamp < $1
"#;
let count: i64 = sqlx::query_scalar(QUERY)
.bind(timestamp)
.fetch_one(ex)
.await
.unwrap();

count as u64
}
}
}

0 comments on commit 9c3aa44

Please sign in to comment.