From 8e6946bfce9dc36cbd41967e2259389146cd0024 Mon Sep 17 00:00:00 2001 From: Asone Date: Fri, 28 Oct 2022 13:32:36 +0200 Subject: [PATCH 01/14] feat(test): move Rocket build to external file so it can be loaded for testing --- src/db/mod.rs | 5 +++++ src/main.rs | 22 ++++++++++++++-------- src/rocket.rs | 21 +++++++++++++++++++++ 3 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 src/rocket.rs diff --git a/src/db/mod.rs b/src/db/mod.rs index 21905c8..e347da8 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -6,9 +6,14 @@ pub mod igniter; pub mod models; pub mod schema; +#[cfg(not(test))] #[database("main_db")] pub struct PostgresConn(pub diesel::PgConnection); +#[cfg(test)] +#[database("test_db")] +pub struct PostgresConn(pub diesel::PgConnection); + impl PostgresConn { pub async fn find_api_payment(self, payment_request: String) -> Option { self.run(|c| ApiPayment::find_one_by_request(payment_request, c)) diff --git a/src/main.rs b/src/main.rs index cd83c2f..0802363 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ #![recursion_limit = "1024"] - +#[cfg(test)] +mod tests; #[macro_use] extern crate juniper; @@ -33,7 +34,7 @@ use app::Schema; use db::igniter::run_db_migrations; use dotenv::dotenv; use juniper::EmptySubscription; -use rocket::Rocket; +use rocket::{Rocket, Build}; use rocket::{fairing::AdHoc, Route}; use routes::{auth::login, file::get_file, utils::graphiql, utils::static_index}; use std::env; @@ -60,7 +61,14 @@ async fn main() -> Result<(), rocket::Error> { dotenv().ok(); config::init(); - let _rocket = Rocket::build() + let rocket = app_build().launch() + .await + .expect("server to launch"); + Ok(()) +} + +fn app_build() -> Rocket { + let rocket = Rocket::build() .attach(PostgresConn::fairing()) .attach(Cors) .attach(AdHoc::try_on_ignite( @@ -75,12 +83,10 @@ async fn main() -> Result<(), rocket::Error> { Mutation, EmptySubscription::::new(), )) - .mount("/", routes_builder()) - .launch() - .await - .expect("server to launch"); + .mount("/", routes_builder()); + + return rocket; - Ok(()) } fn routes_builder() -> Vec { diff --git a/src/rocket.rs b/src/rocket.rs new file mode 100644 index 0000000..1d95743 --- /dev/null +++ b/src/rocket.rs @@ -0,0 +1,21 @@ +pub fn rocket_instance() -> Rocket { + Rocket::build() + .attach(PostgresConn::fairing()) + .attach(Cors) + .attach(AdHoc::try_on_ignite( + "Database Migrations", + run_db_migrations, + )) + .manage(Cors) + // .configure(figment) + .register("/", catchers![payment_required]) + .manage(Schema::new( + Query, + Mutation, + EmptySubscription::::new(), + )) + .mount("/", routes_builder()) + .launch() + .await + .expect("server to launch") +} \ No newline at end of file From c85e062e7fde0226a8125867cfb2bd99efc74bc2 Mon Sep 17 00:00:00 2001 From: Asone Date: Mon, 31 Oct 2022 08:25:49 +0100 Subject: [PATCH 02/14] feat(test): try implementation to run tests with db --- .github/workflows/rust.yml | 18 ++++++- src/db/mod.rs | 7 ++- src/main.rs | 2 +- src/tests/database.rs | 96 ++++++++++++++++++++++++++++++++++++ src/tests/db/media.rs | 40 +++++++++++++++ src/tests/db/mod.rs | 1 + src/tests/fixtures/medias.rs | 5 ++ src/tests/fixtures/mod.rs | 1 + src/tests/mod.rs | 5 ++ src/tests/rocket.rs | 49 ++++++++++++++++++ 10 files changed, 218 insertions(+), 6 deletions(-) create mode 100644 src/tests/database.rs create mode 100644 src/tests/db/media.rs create mode 100644 src/tests/db/mod.rs create mode 100644 src/tests/fixtures/medias.rs create mode 100644 src/tests/fixtures/mod.rs create mode 100644 src/tests/mod.rs create mode 100644 src/tests/rocket.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 0c65b14..7c250d8 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -11,8 +11,24 @@ env: jobs: build: - runs-on: ubuntu-latest + + services: + postgres: + image: postgres + + env: + POSTGRES_PASSWORD: postgres + POSTGRES_USER: postgres + POSTGRES_DB: postgres + + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + ports: + - 5432:5432 steps: - uses: actions/checkout@v3 diff --git a/src/db/mod.rs b/src/db/mod.rs index e347da8..62b4adf 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -6,13 +6,12 @@ pub mod igniter; pub mod models; pub mod schema; -#[cfg(not(test))] #[database("main_db")] pub struct PostgresConn(pub diesel::PgConnection); -#[cfg(test)] -#[database("test_db")] -pub struct PostgresConn(pub diesel::PgConnection); +// #[cfg(test)] +// #[database("test_db")] +// pub struct PostgresConn(pub diesel::PgConnection); impl PostgresConn { pub async fn find_api_payment(self, payment_request: String) -> Option { diff --git a/src/main.rs b/src/main.rs index 0802363..71e7171 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,7 +61,7 @@ async fn main() -> Result<(), rocket::Error> { dotenv().ok(); config::init(); - let rocket = app_build().launch() + let _rocket = app_build().launch() .await .expect("server to launch"); Ok(()) diff --git a/src/tests/database.rs b/src/tests/database.rs new file mode 100644 index 0000000..ab094e1 --- /dev/null +++ b/src/tests/database.rs @@ -0,0 +1,96 @@ +#[cfg(test)] +pub mod tests { + + + use diesel::{PgConnection, Connection, sql_query, RunQueryDsl}; + use diesel_migrations::RunMigrationsError; + + pub struct TestDb { + default_db_url: String, + url: String, + name: String, + delete_on_drop: bool, + } + impl TestDb { + pub fn new() -> Self { + + let db_name = format!( + "test_{}", + env!("CARGO_PKG_NAME") + ); + let default_db_url = std::env::var("TEST_DATABASE_URL").expect("TEST_DATABASE_URL"); + let conn = PgConnection::establish(&default_db_url).unwrap(); + + sql_query(format!("DROP DATABASE IF EXISTS {};", &db_name)) + .execute(&conn) + .unwrap(); + + sql_query(format!("CREATE DATABASE {};", db_name)) + .execute(&conn) + .unwrap(); + + let url = format!("{}/{}",&default_db_url,&db_name); + + let _migrations = Self::run_migrations(&url); + + Self { + default_db_url, + url, + name: db_name, + delete_on_drop: true, + } + } + + + pub fn url(&self) -> &str { + &self.url + } + + pub fn run_migrations(db_url: &String) -> Result<(), RunMigrationsError> { + let conn = PgConnection::establish(db_url).unwrap(); + + diesel_migrations::run_pending_migrations(&conn) + } + + // For further implementation of fixtures loading in test DB. + // pub fn load_fixtures(&self) -> () { + // let connection = &self.conn(); + + + // } + + pub fn conn(&self) -> PgConnection { + PgConnection::establish(&self.url.as_str()).unwrap() + } + + pub fn leak(&mut self) { + self.delete_on_drop = false; + } + } + + impl Drop for TestDb { + fn drop(&mut self) { + if !self.delete_on_drop { + warn!("TestDb leaking database {}", self.name); + return; + } + let conn = + PgConnection::establish(&self.default_db_url).unwrap(); + sql_query(format!( + "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '{}'", + self.name + )) + .execute(&conn) + .unwrap(); + sql_query(format!("DROP DATABASE {}", self.name)) + .execute(&conn) + .unwrap(); + } + } + + + #[test] + fn test_create() { + + } +} diff --git a/src/tests/db/media.rs b/src/tests/db/media.rs new file mode 100644 index 0000000..6430f0c --- /dev/null +++ b/src/tests/db/media.rs @@ -0,0 +1,40 @@ + +#[cfg(test)] +mod tests { + use diesel::{Connection, QueryResult}; +use lazy_static::lazy_static; + +use crate::tests::database::{ tests::TestDb}; + +lazy_static! { + static ref TEST_DB : TestDb = TestDb::new(); +} + +#[test] + pub fn test_media_insert() -> () { + use crate::db::models::media::*; + use dotenv::dotenv; + + dotenv().ok(); + + // let test_db = TestDb::new(); + + let conn = &TEST_DB.conn(); + + + let new_media = NewMedia { + uuid: uuid::Uuid::new_v4(), + title: "test".to_string(), + description: Some("description test".to_string()), + absolute_path: "nowhere".to_string(), + published: true, + price: 100, + }; + + let result = &conn.test_transaction(|| Media::create(new_media,&conn)); + + assert_eq!("test".to_string(),result.title); + +} + +} \ No newline at end of file diff --git a/src/tests/db/mod.rs b/src/tests/db/mod.rs new file mode 100644 index 0000000..aaf258f --- /dev/null +++ b/src/tests/db/mod.rs @@ -0,0 +1 @@ +mod media; \ No newline at end of file diff --git a/src/tests/fixtures/medias.rs b/src/tests/fixtures/medias.rs new file mode 100644 index 0000000..d80a4b9 --- /dev/null +++ b/src/tests/fixtures/medias.rs @@ -0,0 +1,5 @@ +use crate::db::models::media::{Media, NewMedia}; + +pub static Medias: Vec = [ + +].to_vec(); \ No newline at end of file diff --git a/src/tests/fixtures/mod.rs b/src/tests/fixtures/mod.rs new file mode 100644 index 0000000..e125da8 --- /dev/null +++ b/src/tests/fixtures/mod.rs @@ -0,0 +1 @@ +// mod medias; \ No newline at end of file diff --git a/src/tests/mod.rs b/src/tests/mod.rs new file mode 100644 index 0000000..caccf5c --- /dev/null +++ b/src/tests/mod.rs @@ -0,0 +1,5 @@ +#[cfg(test)] +mod rocket; +mod database; +mod db; +mod fixtures; \ No newline at end of file diff --git a/src/tests/rocket.rs b/src/tests/rocket.rs new file mode 100644 index 0000000..41bdaf6 --- /dev/null +++ b/src/tests/rocket.rs @@ -0,0 +1,49 @@ + +#[cfg(test)] +mod tests { + + use rocket::http::ContentType; + use crate::app_build; + + // Sets environment variables for testing as .env file is not loaded + fn set_test_env_variables() -> () { + + std::env::set_var("LND_ADDRESS", "https://umbrel.local:10009"); + std::env::set_var("LND_CERTFILE_PATH", "src/lnd/config/lnd.cert"); + std::env::set_var("LND_MACAROON_PATH", "src/lnd/config/admin.macaroon"); + } + + // Tests rocket ignition + #[rocket::async_test] + async fn test_rocket() { + + use rocket::local::asynchronous::Client; + let _client = Client::tracked(app_build()).await.unwrap(); + } + + + // This test ensures graphql gets provided through the expected endpoint + #[ignore] + #[rocket::async_test] + async fn test_graphql_http_endpoint() { + use rocket::local::asynchronous::Client; + + set_test_env_variables(); + + let client = Client::tracked(app_build()).await.unwrap(); + + let request = client + .post(uri!(crate::app::post_graphql_handler)) + .header(ContentType::JSON) + .body(r#"{"query":"query IntrospectionQuery {__schema {queryType { name }mutationType { name } subscriptionType { name }}}","operationName":"IntrospectionQuery"}"#); + let response = request.dispatch().await; + + let content = response.into_string().await; + + assert!(content.is_some()); + + + } + + // async fn test_graphql_ +} \ No newline at end of file From 1522e6a01ab0d119c5d5a2d9d0d1d34b2fe902ad Mon Sep 17 00:00:00 2001 From: Asone Date: Mon, 31 Oct 2022 08:30:07 +0100 Subject: [PATCH 03/14] feat(test): try fixing CI conf for tests --- .github/workflows/rust.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 7c250d8..c0d9c9f 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -18,10 +18,7 @@ jobs: image: postgres env: - POSTGRES_PASSWORD: postgres - POSTGRES_USER: postgres - POSTGRES_DB: postgres - + TEST_DATABASE_URL: postgres://postgres:postgres@postgres:5432 options: >- --health-cmd pg_isready --health-interval 10s From 7fd75296657650d33e70da8b6972a7412d464e4b Mon Sep 17 00:00:00 2001 From: Asone Date: Mon, 31 Oct 2022 08:31:52 +0100 Subject: [PATCH 04/14] feat(test): try fixing CI conf for tests --- .github/workflows/rust.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index c0d9c9f..c0b0288 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -18,6 +18,9 @@ jobs: image: postgres env: + POSTGRES_PASSWORD: postgres + POSTGRES_USER: postgres + POSTGRES_DB: postgres TEST_DATABASE_URL: postgres://postgres:postgres@postgres:5432 options: >- --health-cmd pg_isready From 7bea6527a04d074f8110c0d657965bcde3392e76 Mon Sep 17 00:00:00 2001 From: Asone Date: Mon, 31 Oct 2022 08:56:46 +0100 Subject: [PATCH 05/14] feat(test): try fixing CI conf for tests --- .github/workflows/rust.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index c0b0288..6fffda9 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -50,4 +50,6 @@ jobs: - name: Build run: cargo build --verbose - name: Run tests - run: cargo test --verbose \ No newline at end of file + run: cargo test --verbose + env: + TEST_DATABASE_URL: postgres://postgres:postgres@postgres:5432 \ No newline at end of file From d47521bfc993188a1e6d3d03184a24348dfb2c91 Mon Sep 17 00:00:00 2001 From: Asone Date: Mon, 31 Oct 2022 14:18:20 +0100 Subject: [PATCH 06/14] feat(test): try fixing CI conf for tests --- .github/workflows/rust.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 6fffda9..d7fff6d 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -21,7 +21,7 @@ jobs: POSTGRES_PASSWORD: postgres POSTGRES_USER: postgres POSTGRES_DB: postgres - TEST_DATABASE_URL: postgres://postgres:postgres@postgres:5432 + TEST_DATABASE_URL: postgres://postgres:postgres@localhost:5432 options: >- --health-cmd pg_isready --health-interval 10s From a28b66974aff55de4c8cf77014f28005ca4d4506 Mon Sep 17 00:00:00 2001 From: Asone Date: Mon, 31 Oct 2022 14:21:44 +0100 Subject: [PATCH 07/14] feat(test): try fixing CI conf for tests --- .github/workflows/rust.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index d7fff6d..d6c80a3 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -52,4 +52,4 @@ jobs: - name: Run tests run: cargo test --verbose env: - TEST_DATABASE_URL: postgres://postgres:postgres@postgres:5432 \ No newline at end of file + TEST_DATABASE_URL: postgres://postgres:postgres@localhost:5432 \ No newline at end of file From 6d366e4608d04b610655cf6c7e8b33aec68b4723 Mon Sep 17 00:00:00 2001 From: Asone Date: Mon, 31 Oct 2022 17:06:03 +0100 Subject: [PATCH 08/14] feat(test): try fixing CI conf for tests --- .github/workflows/rust.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index d6c80a3..37e827e 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -52,4 +52,5 @@ jobs: - name: Run tests run: cargo test --verbose env: + DATABASE_URL: postgres://postgres:postgres@localhost:5432 TEST_DATABASE_URL: postgres://postgres:postgres@localhost:5432 \ No newline at end of file From 54bf938b8f00859f7bf4c227a7e6532cab86a591 Mon Sep 17 00:00:00 2001 From: Asone Date: Mon, 31 Oct 2022 17:33:30 +0100 Subject: [PATCH 09/14] feat(test): try fixing CI conf for tests --- .github/workflows/rust.yml | 3 +++ rocket.ci.toml | 15 +++++++++++++++ src/db/mod.rs | 7 ++++--- 3 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 rocket.ci.toml diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 37e827e..b610280 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -47,6 +47,9 @@ jobs: ~/.cargo/git target key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} + - name: Build env variables + run: | + mv rocket.ci.toml rocket.toml - name: Build run: cargo build --verbose - name: Run tests diff --git a/rocket.ci.toml b/rocket.ci.toml new file mode 100644 index 0000000..c2ca50a --- /dev/null +++ b/rocket.ci.toml @@ -0,0 +1,15 @@ +[default.databases] +test_db = { url = "postgres://postgres:postgres@localhost:5432/postgres"} + +[default.limits] +data-form = "32 MiB" + +#[default.tls] +#certs = "ssl/localhost.pem" +#key = "ssl/localhost-key.pem" + +[release] +main_db = { url = "postgres://rustuser:rustuser@db:5433/test"} +log_level="normal" +address = "0.0.0.0" +port = 8000 \ No newline at end of file diff --git a/src/db/mod.rs b/src/db/mod.rs index 62b4adf..e347da8 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -6,12 +6,13 @@ pub mod igniter; pub mod models; pub mod schema; +#[cfg(not(test))] #[database("main_db")] pub struct PostgresConn(pub diesel::PgConnection); -// #[cfg(test)] -// #[database("test_db")] -// pub struct PostgresConn(pub diesel::PgConnection); +#[cfg(test)] +#[database("test_db")] +pub struct PostgresConn(pub diesel::PgConnection); impl PostgresConn { pub async fn find_api_payment(self, payment_request: String) -> Option { From cf6a49bca9b071df01b67c159283f3530f8a141b Mon Sep 17 00:00:00 2001 From: Asone Date: Mon, 31 Oct 2022 17:36:38 +0100 Subject: [PATCH 10/14] feat(test): try fixing CI conf for tests --- rocket.ci.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rocket.ci.toml b/rocket.ci.toml index c2ca50a..4c5bb9d 100644 --- a/rocket.ci.toml +++ b/rocket.ci.toml @@ -9,7 +9,7 @@ data-form = "32 MiB" #key = "ssl/localhost-key.pem" [release] -main_db = { url = "postgres://rustuser:rustuser@db:5433/test"} +test_db = { url = "postgres://postgres:postgres@localhost:5432/postgres"} log_level="normal" address = "0.0.0.0" port = 8000 \ No newline at end of file From 2f8bb35c11db9e7219dcd889987d0a3e21b7fdce Mon Sep 17 00:00:00 2001 From: Asone Date: Mon, 31 Oct 2022 17:45:24 +0100 Subject: [PATCH 11/14] feat(test): try fixing CI conf for tests --- .github/workflows/rust.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index b610280..ddad7b6 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -50,6 +50,7 @@ jobs: - name: Build env variables run: | mv rocket.ci.toml rocket.toml + cat rocket.toml - name: Build run: cargo build --verbose - name: Run tests From 7dcc6c74fe2e3e68f89334e0b13d0bb0bd783206 Mon Sep 17 00:00:00 2001 From: Asone Date: Mon, 31 Oct 2022 18:00:02 +0100 Subject: [PATCH 12/14] feat(test): try fixing CI conf for tests --- .github/workflows/rust.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index ddad7b6..fe938b1 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -56,5 +56,7 @@ jobs: - name: Run tests run: cargo test --verbose env: + ROCKET_DATABASES: | + { test_db : { url: postgres://postgres:postgres@localhost:5432 } } DATABASE_URL: postgres://postgres:postgres@localhost:5432 TEST_DATABASE_URL: postgres://postgres:postgres@localhost:5432 \ No newline at end of file From 1e7d71c8ca641035690125fc592dcd3db8e3d1fd Mon Sep 17 00:00:00 2001 From: Asone Date: Mon, 31 Oct 2022 18:05:32 +0100 Subject: [PATCH 13/14] feat(test): try fixing CI conf for tests --- .github/workflows/rust.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index fe938b1..aadee75 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -57,6 +57,6 @@ jobs: run: cargo test --verbose env: ROCKET_DATABASES: | - { test_db : { url: postgres://postgres:postgres@localhost:5432 } } + { test_db = { url = postgres://postgres:postgres@localhost:5432 } } DATABASE_URL: postgres://postgres:postgres@localhost:5432 TEST_DATABASE_URL: postgres://postgres:postgres@localhost:5432 \ No newline at end of file From 1b131e1416dd35036d6f1916286e3c2d39fb7d67 Mon Sep 17 00:00:00 2001 From: Asone Date: Mon, 31 Oct 2022 19:35:28 +0100 Subject: [PATCH 14/14] feat(test): format code --- src/main.rs | 7 ++---- src/tests/database.rs | 30 ++++++---------------- src/tests/db/media.rs | 52 ++++++++++++++++++--------------------- src/tests/db/mod.rs | 2 +- src/tests/fixtures/mod.rs | 2 +- src/tests/mod.rs | 6 ++--- src/tests/rocket.rs | 40 +++++++++++++----------------- 7 files changed, 56 insertions(+), 83 deletions(-) diff --git a/src/main.rs b/src/main.rs index 71e7171..381c8a7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,8 +34,8 @@ use app::Schema; use db::igniter::run_db_migrations; use dotenv::dotenv; use juniper::EmptySubscription; -use rocket::{Rocket, Build}; use rocket::{fairing::AdHoc, Route}; +use rocket::{Build, Rocket}; use routes::{auth::login, file::get_file, utils::graphiql, utils::static_index}; use std::env; @@ -61,9 +61,7 @@ async fn main() -> Result<(), rocket::Error> { dotenv().ok(); config::init(); - let _rocket = app_build().launch() - .await - .expect("server to launch"); + let _rocket = app_build().launch().await.expect("server to launch"); Ok(()) } @@ -86,7 +84,6 @@ fn app_build() -> Rocket { .mount("/", routes_builder()); return rocket; - } fn routes_builder() -> Vec { diff --git a/src/tests/database.rs b/src/tests/database.rs index ab094e1..ece80d3 100644 --- a/src/tests/database.rs +++ b/src/tests/database.rs @@ -1,8 +1,7 @@ #[cfg(test)] pub mod tests { - - use diesel::{PgConnection, Connection, sql_query, RunQueryDsl}; + use diesel::{sql_query, Connection, PgConnection, RunQueryDsl}; use diesel_migrations::RunMigrationsError; pub struct TestDb { @@ -13,14 +12,10 @@ pub mod tests { } impl TestDb { pub fn new() -> Self { - - let db_name = format!( - "test_{}", - env!("CARGO_PKG_NAME") - ); + let db_name = format!("test_{}", env!("CARGO_PKG_NAME")); let default_db_url = std::env::var("TEST_DATABASE_URL").expect("TEST_DATABASE_URL"); let conn = PgConnection::establish(&default_db_url).unwrap(); - + sql_query(format!("DROP DATABASE IF EXISTS {};", &db_name)) .execute(&conn) .unwrap(); @@ -29,7 +24,7 @@ pub mod tests { .execute(&conn) .unwrap(); - let url = format!("{}/{}",&default_db_url,&db_name); + let url = format!("{}/{}", &default_db_url, &db_name); let _migrations = Self::run_migrations(&url); @@ -41,14 +36,13 @@ pub mod tests { } } - - pub fn url(&self) -> &str { + pub fn _url(&self) -> &str { &self.url } pub fn run_migrations(db_url: &String) -> Result<(), RunMigrationsError> { let conn = PgConnection::establish(db_url).unwrap(); - + diesel_migrations::run_pending_migrations(&conn) } @@ -56,14 +50,13 @@ pub mod tests { // pub fn load_fixtures(&self) -> () { // let connection = &self.conn(); - // } pub fn conn(&self) -> PgConnection { PgConnection::establish(&self.url.as_str()).unwrap() } - pub fn leak(&mut self) { + pub fn _leak(&mut self) { self.delete_on_drop = false; } } @@ -74,8 +67,7 @@ pub mod tests { warn!("TestDb leaking database {}", self.name); return; } - let conn = - PgConnection::establish(&self.default_db_url).unwrap(); + let conn = PgConnection::establish(&self.default_db_url).unwrap(); sql_query(format!( "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '{}'", self.name @@ -87,10 +79,4 @@ pub mod tests { .unwrap(); } } - - - #[test] - fn test_create() { - - } } diff --git a/src/tests/db/media.rs b/src/tests/db/media.rs index 6430f0c..4eeb8d6 100644 --- a/src/tests/db/media.rs +++ b/src/tests/db/media.rs @@ -1,40 +1,36 @@ - #[cfg(test)] mod tests { - use diesel::{Connection, QueryResult}; -use lazy_static::lazy_static; + use diesel::Connection; + use lazy_static::lazy_static; -use crate::tests::database::{ tests::TestDb}; + use crate::tests::database::tests::TestDb; -lazy_static! { - static ref TEST_DB : TestDb = TestDb::new(); -} + lazy_static! { + static ref TEST_DB: TestDb = TestDb::new(); + } -#[test] - pub fn test_media_insert() -> () { - use crate::db::models::media::*; - use dotenv::dotenv; + #[test] + pub fn test_media_insert() -> () { + use crate::db::models::media::*; + use dotenv::dotenv; - dotenv().ok(); + dotenv().ok(); - // let test_db = TestDb::new(); + // let test_db = TestDb::new(); - let conn = &TEST_DB.conn(); + let conn = &TEST_DB.conn(); - - let new_media = NewMedia { - uuid: uuid::Uuid::new_v4(), - title: "test".to_string(), - description: Some("description test".to_string()), - absolute_path: "nowhere".to_string(), - published: true, - price: 100, - }; + let new_media = NewMedia { + uuid: uuid::Uuid::new_v4(), + title: "test".to_string(), + description: Some("description test".to_string()), + absolute_path: "nowhere".to_string(), + published: true, + price: 100, + }; - let result = &conn.test_transaction(|| Media::create(new_media,&conn)); + let result = &conn.test_transaction(|| Media::create(new_media, &conn)); - assert_eq!("test".to_string(),result.title); - + assert_eq!("test".to_string(), result.title); + } } - -} \ No newline at end of file diff --git a/src/tests/db/mod.rs b/src/tests/db/mod.rs index aaf258f..514fafc 100644 --- a/src/tests/db/mod.rs +++ b/src/tests/db/mod.rs @@ -1 +1 @@ -mod media; \ No newline at end of file +mod media; diff --git a/src/tests/fixtures/mod.rs b/src/tests/fixtures/mod.rs index e125da8..38c54de 100644 --- a/src/tests/fixtures/mod.rs +++ b/src/tests/fixtures/mod.rs @@ -1 +1 @@ -// mod medias; \ No newline at end of file +// mod medias; diff --git a/src/tests/mod.rs b/src/tests/mod.rs index caccf5c..1429707 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -1,5 +1,5 @@ -#[cfg(test)] -mod rocket; mod database; mod db; -mod fixtures; \ No newline at end of file +mod fixtures; +#[cfg(test)] +mod rocket; diff --git a/src/tests/rocket.rs b/src/tests/rocket.rs index 41bdaf6..e9970da 100644 --- a/src/tests/rocket.rs +++ b/src/tests/rocket.rs @@ -1,49 +1,43 @@ - #[cfg(test)] mod tests { - - use rocket::http::ContentType; + use crate::app_build; - + use rocket::http::ContentType; + // Sets environment variables for testing as .env file is not loaded fn set_test_env_variables() -> () { - - std::env::set_var("LND_ADDRESS", "https://umbrel.local:10009"); - std::env::set_var("LND_CERTFILE_PATH", "src/lnd/config/lnd.cert"); - std::env::set_var("LND_MACAROON_PATH", "src/lnd/config/admin.macaroon"); + std::env::set_var("LND_ADDRESS", "https://umbrel.local:10009"); + std::env::set_var("LND_CERTFILE_PATH", "src/lnd/config/lnd.cert"); + std::env::set_var("LND_MACAROON_PATH", "src/lnd/config/admin.macaroon"); } // Tests rocket ignition #[rocket::async_test] async fn test_rocket() { - - use rocket::local::asynchronous::Client; - let _client = Client::tracked(app_build()).await.unwrap(); + use rocket::local::asynchronous::Client; + let _client = Client::tracked(app_build()).await.unwrap(); } - // This test ensures graphql gets provided through the expected endpoint #[ignore] #[rocket::async_test] async fn test_graphql_http_endpoint() { - use rocket::local::asynchronous::Client; + use rocket::local::asynchronous::Client; - set_test_env_variables(); - - let client = Client::tracked(app_build()).await.unwrap(); + set_test_env_variables(); - let request = client + let client = Client::tracked(app_build()).await.unwrap(); + + let request = client .post(uri!(crate::app::post_graphql_handler)) .header(ContentType::JSON) .body(r#"{"query":"query IntrospectionQuery {__schema {queryType { name }mutationType { name } subscriptionType { name }}}","operationName":"IntrospectionQuery"}"#); - let response = request.dispatch().await; - - let content = response.into_string().await; + let response = request.dispatch().await; - assert!(content.is_some()); - + let content = response.into_string().await; + assert!(content.is_some()); } // async fn test_graphql_ -} \ No newline at end of file +}