Skip to content

Instantly share code, notes, and snippets.

@longfellowone
Last active February 21, 2022 18:59
Show Gist options
  • Save longfellowone/9ec300a7e50267a8f411b24f5b06bd6f to your computer and use it in GitHub Desktop.
Save longfellowone/9ec300a7e50267a8f411b24f5b06bd6f to your computer and use it in GitHub Desktop.
use async_graphql::ID;
use serde::Serialize;
use server::config::{Configuration, Http, Postgres};
use server::http::App;
use sqlx::{Executor, PgPool};
use uuid::Uuid;
pub struct TestApp {
pub addr: String,
pub pool: PgPool,
}
impl TestApp {
pub async fn new() -> Self {
let test_database = format!("test_{}", Uuid::new_v4().to_string().replace('-', ""));
let config = Configuration {
http: Http {
host: "localhost".to_string(),
port: 0,
},
postgres: Postgres {
host: "localhost".to_string(),
port: 5432,
user: "postgres".to_string(),
password: "postgres".to_string(),
database: test_database.clone(),
sslmode: false,
},
};
let mut pg_connection = config.postgres.connection().await;
pg_connection
.execute(format!("CREATE DATABASE {};", &config.postgres.database).as_str())
.await
.ok();
let pool = config.postgres.pool().await;
sqlx::migrate!().run(&pool).await.unwrap();
let app = App::new(config, pool.clone());
let addr = format!("http://{}", app.addr());
tokio::spawn(async move { app.run().await });
TestApp { addr, pool }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment