gen isolated tables
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
// src/table_definition/handlers/post_table_definition.rs
|
||||||
|
|
||||||
use tonic::Status;
|
use tonic::Status;
|
||||||
use sqlx::{PgPool, Transaction, Postgres};
|
use sqlx::{PgPool, Transaction, Postgres};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
|||||||
@@ -1,24 +1,81 @@
|
|||||||
// tests/table_definition/post_table_definition_test.rs
|
// tests/table_definition/post_table_definition_test.rs
|
||||||
|
|
||||||
use crate::common::setup_isolated_db;
|
// Keep all your normal use statements
|
||||||
|
|
||||||
use common::proto::multieko2::table_definition::{
|
use common::proto::multieko2::table_definition::{
|
||||||
ColumnDefinition, PostTableDefinitionRequest, TableLink,
|
ColumnDefinition, PostTableDefinitionRequest, TableLink,
|
||||||
};
|
};
|
||||||
use rstest::{fixture, rstest};
|
use rstest::{fixture, rstest};
|
||||||
use server::table_definition::handlers::post_table_definition;
|
use server::table_definition::handlers::post_table_definition;
|
||||||
use sqlx::{PgPool, Row};
|
use sqlx::{postgres::PgPoolOptions, Connection, Executor, PgConnection, PgPool, Row}; // Add PgConnection etc.
|
||||||
use tonic::Code;
|
use tonic::Code;
|
||||||
|
// Add these two new use statements for the isolation logic
|
||||||
|
use rand::distr::Alphanumeric;
|
||||||
|
use rand::Rng;
|
||||||
|
use std::env;
|
||||||
|
use dotenvy;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
// ========= Fixtures =========
|
// ===================================================================
|
||||||
|
// SPECIALIZED SETUP FOR `table_definition` TESTS
|
||||||
|
// This setup logic is now local to this file and will not affect other tests.
|
||||||
|
// ===================================================================
|
||||||
|
async fn setup_isolated_gen_schema_db() -> PgPool {
|
||||||
|
// ---- ADD THIS BLOCK TO LOAD THE .env_test FILE ----
|
||||||
|
let manifest_dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR must be set");
|
||||||
|
let env_path = Path::new(&manifest_dir).join(".env_test");
|
||||||
|
dotenvy::from_path(env_path).ok();
|
||||||
|
// ----------------------------------------------------
|
||||||
|
|
||||||
|
let database_url = env::var("TEST_DATABASE_URL").expect("TEST_DATABASE_URL must be set");
|
||||||
|
|
||||||
|
let unique_schema_name = format!(
|
||||||
|
"test_{}",
|
||||||
|
rand::thread_rng()
|
||||||
|
.sample_iter(&Alphanumeric)
|
||||||
|
.take(12)
|
||||||
|
.map(char::from)
|
||||||
|
.collect::<String>()
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut root_conn = PgConnection::connect(&database_url).await.unwrap();
|
||||||
|
root_conn
|
||||||
|
.execute(format!("CREATE SCHEMA \"{}\"", unique_schema_name).as_str())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let pool = PgPoolOptions::new()
|
||||||
|
.max_connections(5)
|
||||||
|
.after_connect(move |conn, _meta| {
|
||||||
|
let schema = unique_schema_name.clone();
|
||||||
|
Box::pin(async move {
|
||||||
|
conn.execute(format!("SET search_path = '{}'", schema).as_str())
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.connect(&database_url)
|
||||||
|
.await
|
||||||
|
.expect("Failed to create isolated pool");
|
||||||
|
|
||||||
|
sqlx::migrate!()
|
||||||
|
.run(&pool)
|
||||||
|
.await
|
||||||
|
.expect("Migrations failed in isolated schema");
|
||||||
|
|
||||||
|
sqlx::query!("INSERT INTO profiles (name) VALUES ('default') ON CONFLICT (name) DO NOTHING")
|
||||||
|
.execute(&pool)
|
||||||
|
.await
|
||||||
|
.expect("Failed to insert test profile in isolated schema");
|
||||||
|
|
||||||
|
pool
|
||||||
|
}
|
||||||
|
|
||||||
|
// ========= Fixtures for THIS FILE ONLY =========
|
||||||
|
|
||||||
/// THIS IS THE KEY CHANGE.
|
|
||||||
/// The `pool` fixture, which most of your tests already use,
|
|
||||||
/// will now provide a completely isolated database schema for every test.
|
|
||||||
#[fixture]
|
#[fixture]
|
||||||
async fn pool() -> PgPool {
|
async fn pool() -> PgPool {
|
||||||
// Instead of the old setup, we call the new isolated setup.
|
// This fixture now calls the LOCAL, SPECIALIZED setup function.
|
||||||
setup_isolated_db().await
|
setup_isolated_gen_schema_db().await
|
||||||
}
|
}
|
||||||
|
|
||||||
#[fixture]
|
#[fixture]
|
||||||
|
|||||||
Reference in New Issue
Block a user