Skip to content

Commit 4d49443

Browse files
committed
Cache DB connection in tests
To avoid creating a new DB connection on each call to the `db_client` method
1 parent 3933437 commit 4d49443

File tree

4 files changed

+26
-21
lines changed

4 files changed

+26
-21
lines changed

src/db/review_prefs.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,9 @@ mod tests {
117117
#[tokio::test]
118118
async fn insert_prefs_create_user() {
119119
run_db_test(|ctx| async {
120-
let db = ctx.db_client().await;
121-
122120
let user = user("Martin", 1);
123-
upsert_review_prefs(&db, user.clone(), Some(1)).await?;
124-
125-
assert_eq!(get_user(&db, user.id).await?.unwrap(), user);
121+
upsert_review_prefs(&ctx.db_client(), user.clone(), Some(1)).await?;
122+
assert_eq!(get_user(&ctx.db_client(), user.id).await?.unwrap(), user);
126123

127124
Ok(ctx)
128125
})
@@ -132,11 +129,12 @@ mod tests {
132129
#[tokio::test]
133130
async fn insert_max_assigned_prs() {
134131
run_db_test(|ctx| async {
135-
let db = ctx.db_client().await;
136-
137-
upsert_review_prefs(&db, user("Martin", 1), Some(5)).await?;
132+
upsert_review_prefs(&ctx.db_client(), user("Martin", 1), Some(5)).await?;
138133
assert_eq!(
139-
get_review_prefs(&db, 1).await?.unwrap().max_assigned_prs,
134+
get_review_prefs(&ctx.db_client(), 1)
135+
.await?
136+
.unwrap()
137+
.max_assigned_prs,
140138
Some(5)
141139
);
142140

@@ -148,7 +146,7 @@ mod tests {
148146
#[tokio::test]
149147
async fn update_max_assigned_prs() {
150148
run_db_test(|ctx| async {
151-
let db = ctx.db_client().await;
149+
let db = ctx.db_client();
152150

153151
upsert_review_prefs(&db, user("Martin", 1), Some(5)).await?;
154152
assert_eq!(

src/db/users.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ mod tests {
4646
#[tokio::test]
4747
async fn update_username_on_conflict() {
4848
run_db_test(|ctx| async {
49-
let db = ctx.db_client().await;
49+
let db = ctx.db_client();
5050

5151
record_username(&db, 1, "Foo").await?;
5252
record_username(&db, 1, "Bar").await?;

src/handlers/pr_tracking.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ impl ReviewerWorkqueue {
4343
.map(|prs| prs.len() as u64)
4444
.unwrap_or(0)
4545
}
46+
47+
#[cfg(test)]
48+
pub fn set_user_prs(&mut self, user_id: UserId, prs: HashSet<PullRequestNumber>) {
49+
self.reviewers.insert(user_id, prs);
50+
}
4651
}
4752

4853
pub(super) enum ReviewPrefsInput {

src/tests/mod.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@ use std::future::Future;
88
use std::sync::Arc;
99
use tokio::sync::RwLock;
1010
use tokio_postgres::config::Host;
11-
use tokio_postgres::{Config, GenericClient};
11+
use tokio_postgres::Config;
1212

1313
pub(crate) mod github;
1414

1515
/// Represents a connection to a Postgres database that can be
1616
/// used in integration tests to test logic that interacts with
1717
/// a database.
1818
pub(crate) struct TestContext {
19-
pool: ClientPool,
2019
ctx: Context,
2120
db_name: String,
2221
original_db_url: String,
22+
// Pre-cached client to avoid creating unnecessary connections in tests
23+
client: PooledClient,
2324
}
2425

2526
impl TestContext {
@@ -53,7 +54,8 @@ impl TestContext {
5354
db_name
5455
);
5556
let pool = ClientPool::new(test_db_url.clone());
56-
db::run_migrations(&mut *pool.get().await)
57+
let mut client = pool.get().await;
58+
db::run_migrations(&mut client)
5759
.await
5860
.expect("Cannot run database migrations");
5961

@@ -66,17 +68,17 @@ impl TestContext {
6668
);
6769
let ctx = Context {
6870
github,
69-
db: ClientPool::new(test_db_url),
71+
db: pool,
7072
username: "triagebot-test".to_string(),
7173
octocrab,
7274
workqueue: Arc::new(RwLock::new(Default::default())),
7375
};
7476

7577
Self {
76-
pool,
7778
db_name,
7879
original_db_url: db_url.to_string(),
7980
ctx,
81+
client,
8082
}
8183
}
8284

@@ -87,21 +89,21 @@ impl TestContext {
8789
&self.ctx
8890
}
8991

90-
pub(crate) async fn db_client(&self) -> PooledClient {
91-
self.pool.get().await
92+
pub(crate) fn db_client(&self) -> &PooledClient {
93+
&self.client
9294
}
9395

94-
#[allow(dead_code)]
9596
pub(crate) async fn add_user(&self, name: &str, id: u64) {
96-
record_username(self.db_client().await.client(), id, name)
97+
record_username(self.db_client(), id, name)
9798
.await
9899
.expect("Cannot create user");
99100
}
100101

101102
async fn finish(self) {
102103
// Cleanup the test database
103104
// First, we need to stop using the database
104-
drop(self.pool);
105+
drop(self.client);
106+
drop(self.ctx);
105107

106108
// Then we need to connect to the default database and drop our test DB
107109
let client = make_client(&self.original_db_url)

0 commit comments

Comments
 (0)