1
0
mirror of https://github.com/actix/examples synced 2024-11-23 14:31:07 +01:00

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
This commit is contained in:
Daniel T. Rodrigues 2021-03-01 15:35:02 -03:00 committed by GitHub
parent c3407627d0
commit 75c19eb5c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 69 additions and 3 deletions

View File

@ -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

5
Cargo.lock generated
View File

@ -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"

View File

@ -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"

View File

@ -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::<SqliteConnection>::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");
}
}

View File

@ -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,