Merge pull request #457 from AppFlowy-IO/select_user_workspace

chore: sql tests
This commit is contained in:
Zack 2024-04-09 09:57:32 +08:00 committed by GitHub
commit cf5183d827
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 99 additions and 90 deletions

View File

@ -1,70 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "\n SELECT\n workspace_id,\n database_storage_id,\n owner_uid,\n (SELECT name FROM public.af_user WHERE uid = owner_uid) AS owner_name,\n created_at,\n workspace_type,\n deleted_at,\n workspace_name,\n icon\n FROM public.af_workspace\n WHERE workspace_id IN (\n SELECT workspace_id FROM public.af_workspace_member\n WHERE af_workspace_member.uid = (SELECT uid FROM public.af_user WHERE uuid = $1)\n );\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "workspace_id",
"type_info": "Uuid"
},
{
"ordinal": 1,
"name": "database_storage_id",
"type_info": "Uuid"
},
{
"ordinal": 2,
"name": "owner_uid",
"type_info": "Int8"
},
{
"ordinal": 3,
"name": "owner_name",
"type_info": "Text"
},
{
"ordinal": 4,
"name": "created_at",
"type_info": "Timestamptz"
},
{
"ordinal": 5,
"name": "workspace_type",
"type_info": "Int4"
},
{
"ordinal": 6,
"name": "deleted_at",
"type_info": "Timestamptz"
},
{
"ordinal": 7,
"name": "workspace_name",
"type_info": "Text"
},
{
"ordinal": 8,
"name": "icon",
"type_info": "Text"
}
],
"parameters": {
"Left": [
"Uuid"
]
},
"nullable": [
false,
false,
false,
null,
true,
false,
true,
true,
false
]
},
"hash": "71093ffc23294c01b80af7b1c43fab3a5a67eef820f9885c72117364c683fb4a"
}

12
Cargo.lock generated
View File

@ -86,7 +86,7 @@ dependencies = [
"encoding_rs",
"flate2",
"futures-core",
"h2 0.3.24",
"h2 0.3.26",
"http 0.2.11",
"httparse",
"httpdate",
@ -875,7 +875,7 @@ dependencies = [
"derive_more",
"futures-core",
"futures-util",
"h2 0.3.24",
"h2 0.3.26",
"http 0.2.11",
"itoa",
"log",
@ -2669,9 +2669,9 @@ dependencies = [
[[package]]
name = "h2"
version = "0.3.24"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb2c4422095b67ee78da96fbb51a4cc413b3b25883c7717ff7ca1ab31022c9c9"
checksum = "81fe527a889e1532da5c525686d96d4c2e74cdd345badf8dfef9f6b39dd5f5e8"
dependencies = [
"bytes",
"fnv",
@ -2913,7 +2913,7 @@ dependencies = [
"futures-channel",
"futures-core",
"futures-util",
"h2 0.3.24",
"h2 0.3.26",
"http 0.2.11",
"http-body 0.4.6",
"httparse",
@ -4590,7 +4590,7 @@ dependencies = [
"encoding_rs",
"futures-core",
"futures-util",
"h2 0.3.24",
"h2 0.3.26",
"http 0.2.11",
"http-body 0.4.6",
"hyper 0.14.28",

View File

@ -588,19 +588,19 @@ pub async fn select_all_user_workspaces<'a, E: Executor<'a, Database = Postgres>
AFWorkspaceRow,
r#"
SELECT
workspace_id,
database_storage_id,
owner_uid,
(SELECT name FROM public.af_user WHERE uid = owner_uid) AS owner_name,
created_at,
workspace_type,
deleted_at,
workspace_name,
icon
FROM public.af_workspace
WHERE workspace_id IN (
SELECT workspace_id FROM public.af_workspace_member
WHERE af_workspace_member.uid = (SELECT uid FROM public.af_user WHERE uuid = $1)
w.workspace_id,
w.database_storage_id,
w.owner_uid,
(SELECT name FROM public.af_user WHERE uid = w.owner_uid) AS owner_name,
w.created_at,
w.workspace_type,
w.deleted_at,
w.workspace_name,
w.icon
FROM af_workspace w
JOIN af_workspace_member wm ON w.workspace_id = wm.workspace_id
WHERE wm.uid = (
SELECT uid FROM public.af_user WHERE uuid = $1
);
"#,
user_uuid

View File

@ -1,7 +1,7 @@
mod collab;
mod collab_snapshot;
// mod collab_stream;
mod gotrue;
mod pg_sql;
mod user;
mod websocket;
mod workspace;

2
tests/pg_sql/mod.rs Normal file
View File

@ -0,0 +1,2 @@
mod util;
mod workspace_test;

59
tests/pg_sql/util.rs Normal file
View File

@ -0,0 +1,59 @@
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(())
}
pub async fn insert_auth_user(pool: &PgPool, user_uuid: Uuid) -> anyhow::Result<()> {
sqlx::query(
r#"
INSERT INTO auth.users (id)
VALUES ($1)
"#,
)
.bind(user_uuid)
.execute(pool)
.await?;
Ok(())
}
lazy_static! {
pub static ref ID_GEN: RwLock<Snowflake> = 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(())
}

View File

@ -0,0 +1,18 @@
use crate::pg_sql::util::{insert_auth_user, 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);
insert_auth_user(&pool, user_uuid).await.unwrap();
test_create_user(&pool, user_uuid, &email, &name)
.await
.unwrap();
Ok(())
}