Simplify macro for running a test with both databases

This commit is contained in:
Max Brunsfeld 2023-08-20 14:25:19 -07:00 committed by Mikayla
parent 26c3312049
commit 29e43384f0
No known key found for this signature in database
2 changed files with 558 additions and 537 deletions

View file

@ -1,32 +1,17 @@
use super::*;
use crate::test_both_dbs;
use gpui::executor::{Background, Deterministic};
use pretty_assertions::{assert_eq, assert_ne};
use std::sync::Arc;
use test_db::TestDb;
macro_rules! test_both_dbs {
($postgres_test_name:ident, $sqlite_test_name:ident, $db:ident, $body:block) => {
#[gpui::test]
async fn $postgres_test_name() {
let test_db = TestDb::postgres(Deterministic::new(0).build_background());
let $db = test_db.db();
$body
}
#[gpui::test]
async fn $sqlite_test_name() {
let test_db = TestDb::sqlite(Deterministic::new(0).build_background());
let $db = test_db.db();
$body
}
};
}
test_both_dbs!(
test_get_users,
test_get_users_by_ids_postgres,
test_get_users_by_ids_sqlite,
db,
{
test_get_users_by_ids_sqlite
);
async fn test_get_users(db: &Arc<Database>) {
let mut user_ids = Vec::new();
let mut user_metric_ids = Vec::new();
for i in 1..=4 {
@ -88,13 +73,14 @@ test_both_dbs!(
]
);
}
);
test_both_dbs!(
test_get_or_create_user_by_github_account,
test_get_or_create_user_by_github_account_postgres,
test_get_or_create_user_by_github_account_sqlite,
db,
{
test_get_or_create_user_by_github_account_sqlite
);
async fn test_get_or_create_user_by_github_account(db: &Arc<Database>) {
let user_id1 = db
.create_user(
"user1@example.com",
@ -155,13 +141,14 @@ test_both_dbs!(
assert_eq!(user.github_user_id, Some(103));
assert_eq!(user.email_address, Some("user3@example.com".into()));
}
);
test_both_dbs!(
test_create_access_tokens,
test_create_access_tokens_postgres,
test_create_access_tokens_sqlite,
db,
{
test_create_access_tokens_sqlite
);
async fn test_create_access_tokens(db: &Arc<Database>) {
let user = db
.create_user(
"u1@example.com",
@ -234,9 +221,14 @@ test_both_dbs!(
assert!(db.get_access_token(token_2).await.is_err());
assert!(db.get_access_token(token_1).await.is_err());
}
test_both_dbs!(
test_add_contacts,
test_add_contacts_postgres,
test_add_contacts_sqlite
);
test_both_dbs!(test_add_contacts_postgres, test_add_contacts_sqlite, db, {
async fn test_add_contacts(db: &Arc<Database>) {
let mut user_ids = Vec::new();
for i in 0..3 {
user_ids.push(
@ -403,9 +395,15 @@ test_both_dbs!(test_add_contacts_postgres, test_add_contacts_sqlite, db, {
busy: false,
}],
);
});
}
test_both_dbs!(test_metrics_id_postgres, test_metrics_id_sqlite, db, {
test_both_dbs!(
test_metrics_id,
test_metrics_id_postgres,
test_metrics_id_sqlite
);
async fn test_metrics_id(db: &Arc<Database>) {
let NewUserResult {
user_id: user1,
metrics_id: metrics_id1,
@ -444,13 +442,15 @@ test_both_dbs!(test_metrics_id_postgres, test_metrics_id_sqlite, db, {
assert_eq!(metrics_id1.len(), 36);
assert_eq!(metrics_id2.len(), 36);
assert_ne!(metrics_id1, metrics_id2);
});
}
test_both_dbs!(
test_project_count,
test_project_count_postgres,
test_project_count_sqlite,
db,
{
test_project_count_sqlite
);
async fn test_project_count(db: &Arc<Database>) {
let owner_id = db.create_server("test").await.unwrap().0 as u32;
let user1 = db
@ -519,7 +519,6 @@ test_both_dbs!(
.unwrap();
assert_eq!(db.project_count_excluding_admins().await.unwrap(), 0);
}
);
#[test]
fn test_fuzzy_like_string() {
@ -878,7 +877,9 @@ async fn test_invite_codes() {
assert!(db.has_contact(user5, user1).await.unwrap());
}
test_both_dbs!(test_channels_postgres, test_channels_sqlite, db, {
test_both_dbs!(test_channels, test_channels_postgres, test_channels_sqlite);
async fn test_channels(db: &Arc<Database>) {
let a_id = db
.create_user(
"user1@example.com",
@ -1063,13 +1064,15 @@ test_both_dbs!(test_channels_postgres, test_channels_sqlite, db, {
assert!(db.get_channel(rust_id, a_id).await.unwrap().is_none());
assert!(db.get_channel(cargo_id, a_id).await.unwrap().is_none());
assert!(db.get_channel(cargo_ra_id, a_id).await.unwrap().is_none());
});
}
test_both_dbs!(
test_joining_channels,
test_joining_channels_postgres,
test_joining_channels_sqlite,
db,
{
test_joining_channels_sqlite
);
async fn test_joining_channels(db: &Arc<Database>) {
let owner_id = db.create_server("test").await.unwrap().0 as u32;
let user_1 = db
@ -1119,13 +1122,14 @@ test_both_dbs!(
.await
.is_err());
}
);
test_both_dbs!(
test_channel_invites,
test_channel_invites_postgres,
test_channel_invites_sqlite,
db,
{
test_channel_invites_sqlite
);
async fn test_channel_invites(db: &Arc<Database>) {
db.create_server("test").await.unwrap();
let user_1 = db
@ -1263,13 +1267,14 @@ test_both_dbs!(
]
);
}
);
test_both_dbs!(
test_channel_renames,
test_channel_renames_postgres,
test_channel_renames_sqlite,
db,
{
test_channel_renames_sqlite
);
async fn test_channel_renames(db: &Arc<Database>) {
db.create_server("test").await.unwrap();
let user_1 = db
@ -1323,7 +1328,6 @@ test_both_dbs!(
let bad_name_rename = db.rename_channel(zed_id, user_1, "#").await;
assert!(bad_name_rename.is_err())
}
);
#[gpui::test]
async fn test_multiple_signup_overwrite() {

View file

@ -91,6 +91,23 @@ impl TestDb {
}
}
#[macro_export]
macro_rules! test_both_dbs {
($test_name:ident, $postgres_test_name:ident, $sqlite_test_name:ident) => {
#[gpui::test]
async fn $postgres_test_name() {
let test_db = TestDb::postgres(Deterministic::new(0).build_background());
$test_name(test_db.db()).await;
}
#[gpui::test]
async fn $sqlite_test_name() {
let test_db = TestDb::sqlite(Deterministic::new(0).build_background());
$test_name(test_db.db()).await;
}
};
}
impl Drop for TestDb {
fn drop(&mut self) {
let db = self.db.take().unwrap();