-
-
Save longfellowone/9ec300a7e50267a8f411b24f5b06bd6f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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