Set up tests for database queries
This commit is contained in:
parent
040934da2d
commit
011951c129
6 changed files with 98 additions and 0 deletions
23
Cargo.lock
generated
23
Cargo.lock
generated
|
@ -1718,6 +1718,7 @@ dependencies = [
|
|||
"serde",
|
||||
"serde_json",
|
||||
"serde_yaml",
|
||||
"serial_test",
|
||||
"sha2",
|
||||
"thiserror",
|
||||
"tokio",
|
||||
|
@ -2746,6 +2747,28 @@ dependencies = [
|
|||
"yaml-rust",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serial_test"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e0bccbcf40c8938196944a3da0e133e031a33f4d6b72db3bda3cc556e361905d"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
"parking_lot",
|
||||
"serial_test_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serial_test_derive"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b2acd6defeddb41eb60bb468f8825d0cfd0c2a76bc03bfd235b6a1dc4f6a1ad5"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sha-1"
|
||||
version = "0.9.4"
|
||||
|
|
|
@ -79,3 +79,6 @@ ulid = { version = "0.4.1", features = ["uuid"] }
|
|||
uuid = { version = "0.8.2", features = ["serde", "v4"] }
|
||||
# Used to query ethereum node
|
||||
web3 = { version = "0.15.0", default-features = false, features = ["http", "http-tls", "signing"] }
|
||||
|
||||
[dev-dependencies]
|
||||
serial_test = "0.5.1"
|
||||
|
|
|
@ -3,6 +3,9 @@ use tokio_postgres::config::{Config as DbConfig};
|
|||
pub mod int_enum;
|
||||
pub mod migrate;
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod test_utils;
|
||||
|
||||
pub type Pool = deadpool_postgres::Pool;
|
||||
|
||||
pub async fn create_database_client(db_config: &DbConfig) -> tokio_postgres::Client {
|
||||
|
|
37
src/database/test_utils.rs
Normal file
37
src/database/test_utils.rs
Normal file
|
@ -0,0 +1,37 @@
|
|||
use tokio_postgres::Client;
|
||||
use tokio_postgres::config::Config;
|
||||
use super::create_database_client;
|
||||
use super::migrate::apply_migrations;
|
||||
|
||||
const DEFAULT_CONNECTION_URL: &str = "postgres://mitra:mitra@127.0.0.1:5432/mitra-test";
|
||||
|
||||
pub async fn create_test_database() -> Client {
|
||||
let connection_url = std::env::var("TEST_DATABASE_URL")
|
||||
.unwrap_or(DEFAULT_CONNECTION_URL.to_string());
|
||||
let mut db_config: Config = connection_url.parse()
|
||||
.expect("invalid database connection URL");
|
||||
let db_name = db_config.get_dbname()
|
||||
.expect("database name not specified")
|
||||
.to_string();
|
||||
|
||||
// Create connection without database name
|
||||
db_config.dbname("");
|
||||
let db_client = create_database_client(&db_config).await;
|
||||
let drop_db_statement = format!(
|
||||
"DROP DATABASE IF EXISTS {db_name:?}",
|
||||
db_name=db_name,
|
||||
);
|
||||
db_client.execute(drop_db_statement.as_str(), &[]).await.unwrap();
|
||||
let create_db_statement = format!(
|
||||
"CREATE DATABASE {db_name:?} WITH OWNER={owner:?};",
|
||||
db_name=db_name,
|
||||
owner=db_config.get_user().unwrap(),
|
||||
);
|
||||
db_client.execute(create_db_statement.as_str(), &[]).await.unwrap();
|
||||
|
||||
// Create new connection to database
|
||||
db_config.dbname(&db_name);
|
||||
let mut db_client = create_database_client(&db_config).await;
|
||||
apply_migrations(&mut db_client).await;
|
||||
db_client
|
||||
}
|
|
@ -382,3 +382,34 @@ pub async fn update_post_count(
|
|||
let profile = row.try_get("actor_profile")?;
|
||||
Ok(profile)
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serial_test::serial;
|
||||
use crate::database::test_utils::create_test_database;
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn test_create_profile() {
|
||||
let profile_data = ProfileCreateData {
|
||||
username: "test".to_string(),
|
||||
..Default::default()
|
||||
};
|
||||
let db_client = create_test_database().await;
|
||||
let profile = create_profile(&db_client, &profile_data).await.unwrap();
|
||||
assert_eq!(profile.username, profile_data.username);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn test_delete_profile() {
|
||||
let profile_data = ProfileCreateData::default();
|
||||
let mut db_client = create_test_database().await;
|
||||
let profile = create_profile(&db_client, &profile_data).await.unwrap();
|
||||
let deletion_queue = delete_profile(&mut db_client, &profile.id).await.unwrap();
|
||||
assert_eq!(deletion_queue.files.len(), 0);
|
||||
assert_eq!(deletion_queue.ipfs_objects.len(), 0);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -135,6 +135,7 @@ impl Default for DbActorProfile {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(test, derive(Default))]
|
||||
pub struct ProfileCreateData {
|
||||
pub username: String,
|
||||
pub display_name: Option<String>,
|
||||
|
|
Loading…
Reference in a new issue