From c4c8911af2fa4b3a70a82a714691ae31b169c838 Mon Sep 17 00:00:00 2001 From: nathan Date: Mon, 8 Apr 2024 18:23:25 +0800 Subject: [PATCH] chore: add test --- libs/workspace-template/Cargo.toml | 10 +++---- tests/main.rs | 1 + tests/pg_sql/mod.rs | 2 ++ tests/pg_sql/util.rs | 44 ++++++++++++++++++++++++++++++ tests/pg_sql/workspace_test.rs | 17 ++++++++++++ 5 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 tests/pg_sql/mod.rs create mode 100644 tests/pg_sql/util.rs create mode 100644 tests/pg_sql/workspace_test.rs diff --git a/libs/workspace-template/Cargo.toml b/libs/workspace-template/Cargo.toml index 13e5634d..9f3f1f89 100644 --- a/libs/workspace-template/Cargo.toml +++ b/libs/workspace-template/Cargo.toml @@ -9,10 +9,10 @@ crate-type = ["cdylib", "rlib"] [dependencies] bytes.workspace = true -collab = { version = "0.1.0"} -collab-folder = { version = "0.1.0"} -collab-document = { version = "0.1.0"} -collab-entity = { version = "0.1.0"} +collab = { version = "0.1.0" } +collab-folder = { version = "0.1.0" } +collab-document = { version = "0.1.0" } +collab-entity = { version = "0.1.0" } collab-database = { version = "0.1.0" } async-trait = "0.1.77" anyhow.workspace = true @@ -24,4 +24,4 @@ nanoid = "0.4.0" serde = { version = "1.0.195", features = ["derive"] } [target.'cfg(target_arch = "wasm32")'.dependencies] -getrandom = { version = "0.2", features = ["js"]} +getrandom = { version = "0.2", features = ["js"] } diff --git a/tests/main.rs b/tests/main.rs index 7b2a6aff..8980c7a0 100644 --- a/tests/main.rs +++ b/tests/main.rs @@ -1,6 +1,7 @@ mod collab; mod collab_snapshot; mod gotrue; +mod pg_sql; mod user; mod websocket; mod workspace; diff --git a/tests/pg_sql/mod.rs b/tests/pg_sql/mod.rs new file mode 100644 index 00000000..7cac0f51 --- /dev/null +++ b/tests/pg_sql/mod.rs @@ -0,0 +1,2 @@ +mod util; +mod workspace_test; diff --git a/tests/pg_sql/util.rs b/tests/pg_sql/util.rs new file mode 100644 index 00000000..72d244c7 --- /dev/null +++ b/tests/pg_sql/util.rs @@ -0,0 +1,44 @@ +use lazy_static::lazy_static; +use snowflake::Snowflake; +use sqlx::PgPool; +use tokio::sync::RwLock; +use uuid::Uuid; + +pub async fn setup_db(pool: &PgPool) -> anyhow::Result<()> { + // Have to manually create schema and tables managed by gotrue but referenced by our + // migration scripts. + sqlx::query(r#"create schema auth"#).execute(pool).await?; + sqlx::query( + r#"create table auth.users( +id uuid NOT NULL UNIQUE, +deleted_at timestamptz null, +CONSTRAINT users_pkey PRIMARY KEY (id) +)"#, + ) + .execute(pool) + .await?; + + sqlx::migrate!("./migrations") + .set_ignore_missing(true) + .run(pool) + .await + .unwrap(); + Ok(()) +} + +lazy_static! { + pub static ref ID_GEN: RwLock = RwLock::new(Snowflake::new(1)); +} + +pub async fn test_create_user( + pool: &PgPool, + user_uuid: Uuid, + email: &str, + name: &str, +) -> anyhow::Result<()> { + let uid = ID_GEN.write().await.next_id(); + database::user::create_user(pool, uid, &user_uuid, &email, &name) + .await + .unwrap(); + Ok(()) +} diff --git a/tests/pg_sql/workspace_test.rs b/tests/pg_sql/workspace_test.rs new file mode 100644 index 00000000..e56143de --- /dev/null +++ b/tests/pg_sql/workspace_test.rs @@ -0,0 +1,17 @@ +use crate::pg_sql::util::{setup_db, test_create_user}; +use sqlx::PgPool; + +#[sqlx::test(migrations = false)] +async fn basic_test(pool: PgPool) -> sqlx::Result<()> { + setup_db(&pool).await.unwrap(); + + let user_uuid = uuid::Uuid::new_v4(); + let name = user_uuid.to_string(); + let email = format!("{}@appflowy.io", name); + + test_create_user(&pool, user_uuid, &email, &name) + .await + .unwrap(); + + Ok(()) +}