From 75c19eb5c9d6ed0e421137e81df963a16900a031 Mon Sep 17 00:00:00 2001 From: "Daniel T. Rodrigues" <45438149+danitrod@users.noreply.github.com> Date: Mon, 1 Mar 2021 15:35:02 -0300 Subject: [PATCH] Add tests to diesel example (#414) * add tests to diesel example * add diesel migration to gh workflow * update diesel gh action * update diesel gh action * review #414 diesel tests --- .github/workflows/linux.yml | 7 +++ Cargo.lock | 5 +- database_interactions/diesel/Cargo.toml | 3 ++ database_interactions/diesel/src/main.rs | 55 ++++++++++++++++++++++ database_interactions/diesel/src/models.rs | 2 +- 5 files changed, 69 insertions(+), 3 deletions(-) diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index 9a70a97..c23b66f 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -49,6 +49,13 @@ jobs: with: redis-version: 6 + - name: run diesel migrations + run: | + cargo install diesel_cli + cd database_interactions/diesel + diesel migration run + chmod a+rwx test.db + - name: cargo test uses: actions-rs/cargo@v1 timeout-minutes: 30 diff --git a/Cargo.lock b/Cargo.lock index afebc17..e2a031d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1874,6 +1874,7 @@ dependencies = [ name = "diesel-example" version = "1.0.0" dependencies = [ + "actix-rt", "actix-web", "diesel", "dotenv", @@ -3037,9 +3038,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.80" +version = "0.2.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d58d1b70b004888f764dfbf6a26a3b0342a1632d33968e4a179d8011c760614" +checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c" [[package]] name = "libgit2-sys" diff --git a/database_interactions/diesel/Cargo.toml b/database_interactions/diesel/Cargo.toml index bf978e6..11324cd 100644 --- a/database_interactions/diesel/Cargo.toml +++ b/database_interactions/diesel/Cargo.toml @@ -18,3 +18,6 @@ r2d2 = "0.8" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" uuid = { version = "0.8", features = ["serde", "v4"] } + +[dev-dependencies] +actix-rt = "1" diff --git a/database_interactions/diesel/src/main.rs b/database_interactions/diesel/src/main.rs index e4f81ae..42ceb2c 100644 --- a/database_interactions/diesel/src/main.rs +++ b/database_interactions/diesel/src/main.rs @@ -92,3 +92,58 @@ async fn main() -> std::io::Result<()> { .run() .await } + +#[cfg(test)] +mod tests { + use super::*; + use actix_web::test; + + #[actix_rt::test] + async fn user_routes() { + std::env::set_var("RUST_LOG", "actix_web=debug"); + env_logger::init(); + dotenv::dotenv().ok(); + + let connspec = std::env::var("DATABASE_URL").expect("DATABASE_URL"); + let manager = ConnectionManager::::new(connspec); + let pool = r2d2::Pool::builder() + .build(manager) + .expect("Failed to create pool."); + + let mut app = test::init_service( + App::new() + .data(pool.clone()) + .wrap(middleware::Logger::default()) + .service(get_user) + .service(add_user), + ) + .await; + + // Insert a user + let req = test::TestRequest::post() + .uri("/user") + .set_json(&models::NewUser { + name: "Test user".to_owned(), + }) + .to_request(); + + let resp: models::User = test::read_response_json(&mut app, req).await; + + assert_eq!(resp.name, "Test user"); + + // Get a user + let req = test::TestRequest::get() + .uri(&format!("/user/{}", resp.id)) + .to_request(); + + let resp: models::User = test::read_response_json(&mut app, req).await; + + assert_eq!(resp.name, "Test user"); + + // Delete new user from table + use crate::schema::users::dsl::*; + diesel::delete(users.filter(id.eq(resp.id))) + .execute(&pool.get().expect("couldn't get db connection from pool")) + .expect("couldn't delete test user from table"); + } +} diff --git a/database_interactions/diesel/src/models.rs b/database_interactions/diesel/src/models.rs index 1409be1..0187631 100644 --- a/database_interactions/diesel/src/models.rs +++ b/database_interactions/diesel/src/models.rs @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize}; use crate::schema::users; -#[derive(Debug, Clone, Serialize, Queryable, Insertable)] +#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Insertable)] pub struct User { pub id: String, pub name: String,