Rework the tests to use a macro and DB seeding

This commit is contained in:
Paul van Tilburg 2019-07-08 20:14:21 +02:00
parent 329a92d593
commit 8bac9ec027
2 changed files with 73 additions and 44 deletions

View File

@ -56,61 +56,86 @@ pub fn destroy(id: i32, conn: DbConn) -> NoContentResponse {
#[cfg(test)]
mod tests {
use rocket::http::{Accept, ContentType, Status};
use rocket::local::Client;
use serde_json::{self, json};
use super::*;
use crate::rocket;
macro_rules! run_test {
(|$client:ident, $conn:ident| $block:expr) => {{
let rocket = crate::rocket();
let db = crate::DbConn::get_one(&rocket);
let $conn = db.expect("Failed to get database connection for testing");
let $client =
rocket::local::Client::new(rocket).expect("Failed to set up Rocket client");
$block
}};
}
#[test]
fn index() {
let client = Client::new(rocket()).unwrap();
run_test!(|client, _conn| {
let mut res = client.get("/customers").header(Accept::JSON).dispatch();
assert_eq!(res.status(), Status::Ok);
let mut res = client.get("/customers").header(Accept::JSON).dispatch();
assert_eq!(res.status(), Status::Ok);
let body = res.body_string().unwrap();
let customers = serde_json::from_str::<Vec<Customer>>(body.as_str()).unwrap();
assert_eq!(customers, vec![]);
// FIXME: Actually have something in de the index!
let body = res.body_string().unwrap();
let customers = serde_json::from_str::<Vec<Customer>>(body.as_str()).unwrap();
assert_eq!(customers.len(), 2);
});
}
#[test]
fn create() {
let client = Client::new(rocket()).unwrap();
run_test!(|client, conn| {
let new_customer_json = json!({
"name": "Test Company"
});
let res = client
.post("/customers")
.header(ContentType::JSON)
.header(Accept::JSON)
.body(new_customer_json.to_string())
.dispatch();
assert_eq!(res.status(), Status::UnprocessableEntity);
let new_customer_json = json!({
"name": "Test Company"
});
let res = client
.post("/customers")
.header(ContentType::JSON)
.header(Accept::JSON)
.body(new_customer_json.to_string())
.dispatch();
assert_eq!(res.status(), Status::UnprocessableEntity);
let customers: Vec<Customer> = all!(Customer, *conn).expect("Customers");
let new_customer_json = json!({
"address_city": "Some City",
"address_postal_code": "123456",
"address_street": "Somestreet",
"email": "info@testcompany.tld",
"financial_contact": "Financial Office",
"hourly_rate": 100.0,
"name": "New Test Company",
"phone": "0123456789",
"short_name": "Test Co.",
"time_specification": true,
});
let mut res = client
.post("/customers")
.header(ContentType::JSON)
.header(Accept::JSON)
.body(new_customer_json.to_string())
.dispatch();
assert_eq!(res.status(), Status::Created);
let body = res.body_string().unwrap();
let customer = serde_json::from_str::<Customer>(body.as_str()).unwrap();
assert_eq!(customer.name, "New Test Company");
let new_customer_json = json!({
"address_city": "Some City",
"address_postal_code": "123456",
"address_street": "Somestreet",
"email": "info@testcompany.tld",
"financial_contact": "Financial Office",
"hourly_rate": 100.0,
"name": "Test Company",
"phone": "0123456789",
"short_name": "Test Co.",
"time_specification": true
});
let mut res = client
.post("/customers")
.header(ContentType::JSON)
.header(Accept::JSON)
.body(new_customer_json.to_string())
.dispatch();
assert_eq!(res.status(), Status::Created);
let body = res.body_string().unwrap();
let new_customer = serde_json::from_str::<Customer>(body.as_str()).unwrap();
assert_eq!(new_customer.name, "Test Company");
let customers_after: Vec<Customer> = all!(Customer, *conn).expect("Customers");
assert_eq!(customers_after.len(), customers.len() + 1);
})
}
#[test]
fn new() {}
#[test]
fn show() {}
#[test]
fn update() {}
#[test]
fn destroy() {}
}

View File

@ -116,9 +116,11 @@ macro_rules! get {
macro_rules! create {
($model_name:ident, $object:expr, $conn:expr) => {{
use diesel::associations::HasTable;
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl, Table};
let table = $crate::models::$model_name::table();
let primary_key = table.primary_key();
$conn.transaction::<_, diesel::result::Error, _>(|| {
$conn.transaction::<$model_name, diesel::result::Error, _>(|| {
diesel::insert_into(table).values($object).execute(&$conn)?;
table.order(primary_key.desc()).first(&$conn)
})
@ -128,6 +130,7 @@ macro_rules! create {
macro_rules! update {
($model_name:ident, $primary_key:expr, $object:expr, $conn:expr) => {{
use diesel::associations::HasTable;
let table = $crate::models::$model_name::table();
$conn.transaction::<_, diesel::result::Error, _>(|| {
diesel::update(table.find($primary_key))
@ -141,6 +144,7 @@ macro_rules! update {
macro_rules! destroy {
($model_name:ident, $primary_key:expr, $conn:expr) => {{
use diesel::associations::HasTable;
let table = $crate::models::$model_name::table();
let result: Result<$model_name, _> = get!($model_name, $primary_key, $conn);
match result {