chore: add sql test (#461)

* chore: add sql test

* chore: clippy
This commit is contained in:
Nathan.fooo 2024-04-09 23:16:13 +08:00 committed by GitHub
parent c1bbcbfdd5
commit e0122d106e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 51 additions and 8 deletions

View File

@ -1,4 +1,6 @@
use lazy_static::lazy_static;
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use snowflake::Snowflake;
use sqlx::PgPool;
use tokio::sync::RwLock;
@ -50,10 +52,29 @@ pub async fn test_create_user(
user_uuid: Uuid,
email: &str,
name: &str,
) -> anyhow::Result<()> {
) -> anyhow::Result<TestUser> {
insert_auth_user(pool, user_uuid).await.unwrap();
let uid = ID_GEN.write().await.next_id();
database::user::create_user(pool, uid, &user_uuid, email, name)
let workspace_id = database::user::create_user(pool, uid, &user_uuid, email, name)
.await
.unwrap();
Ok(())
Ok(TestUser {
uid,
workspace_id: workspace_id.to_string(),
})
}
pub struct TestUser {
pub uid: i64,
pub workspace_id: String,
}
pub fn generate_random_bytes(size: usize) -> Vec<u8> {
let s: String = thread_rng()
.sample_iter(&Alphanumeric)
.take(size)
.map(char::from)
.collect();
s.into_bytes()
}

View File

@ -1,18 +1,40 @@
use crate::pg_sql::util::{insert_auth_user, setup_db, test_create_user};
use crate::pg_sql::util::{generate_random_bytes, setup_db, test_create_user};
use collab_entity::CollabType;
use database::collab::insert_into_af_collab;
use database_entity::dto::CollabParams;
use sqlx::PgPool;
#[sqlx::test(migrations = false)]
async fn basic_test(pool: PgPool) -> sqlx::Result<()> {
async fn insert_collab_sql_test(pool: PgPool) {
setup_db(&pool).await.unwrap();
let user_uuid = uuid::Uuid::new_v4();
let name = user_uuid.to_string();
let email = format!("{}@appflowy.io", name);
insert_auth_user(&pool, user_uuid).await.unwrap();
test_create_user(&pool, user_uuid, &email, &name)
let user = test_create_user(&pool, user_uuid, &email, &name)
.await
.unwrap();
Ok(())
let data_sizes = vec![1024, 10240, 102400, 1024000]; // Example sizes: 1KB, 10KB, 100KB, 1MB
for &data_size in &data_sizes {
let encoded_collab_v1 = generate_random_bytes(data_size);
let object_id = uuid::Uuid::new_v4().to_string();
let start_time = std::time::Instant::now(); // Start timing
let mut txn = pool.begin().await.unwrap();
let params = CollabParams {
object_id,
collab_type: CollabType::Empty,
encoded_collab_v1,
};
insert_into_af_collab(&mut txn, &user.uid, &user.workspace_id, &params)
.await
.unwrap();
txn.commit().await.unwrap();
let duration = start_time.elapsed(); // End timing
println!(
"Data size: {} bytes, Insert time: {:?}",
data_size, duration
);
}
}