Rework the tests to use a macro and DB seeding
This commit is contained in:
parent
329a92d593
commit
8bac9ec027
|
@ -56,61 +56,86 @@ pub fn destroy(id: i32, conn: DbConn) -> NoContentResponse {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use rocket::http::{Accept, ContentType, Status};
|
use rocket::http::{Accept, ContentType, Status};
|
||||||
use rocket::local::Client;
|
|
||||||
use serde_json::{self, json};
|
use serde_json::{self, json};
|
||||||
|
|
||||||
use super::*;
|
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]
|
#[test]
|
||||||
fn index() {
|
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();
|
let body = res.body_string().unwrap();
|
||||||
assert_eq!(res.status(), Status::Ok);
|
let customers = serde_json::from_str::<Vec<Customer>>(body.as_str()).unwrap();
|
||||||
|
assert_eq!(customers.len(), 2);
|
||||||
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!
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn create() {
|
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!({
|
let customers: Vec<Customer> = all!(Customer, *conn).expect("Customers");
|
||||||
"name": "Test Company"
|
let new_customer_json = json!({
|
||||||
});
|
"address_city": "Some City",
|
||||||
let res = client
|
"address_postal_code": "123456",
|
||||||
.post("/customers")
|
"address_street": "Somestreet",
|
||||||
.header(ContentType::JSON)
|
"email": "info@testcompany.tld",
|
||||||
.header(Accept::JSON)
|
"financial_contact": "Financial Office",
|
||||||
.body(new_customer_json.to_string())
|
"hourly_rate": 100.0,
|
||||||
.dispatch();
|
"name": "New Test Company",
|
||||||
assert_eq!(res.status(), Status::UnprocessableEntity);
|
"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!({
|
let customers_after: Vec<Customer> = all!(Customer, *conn).expect("Customers");
|
||||||
"address_city": "Some City",
|
assert_eq!(customers_after.len(), customers.len() + 1);
|
||||||
"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");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn new() {}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn show() {}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn update() {}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn destroy() {}
|
||||||
}
|
}
|
||||||
|
|
|
@ -116,9 +116,11 @@ macro_rules! get {
|
||||||
macro_rules! create {
|
macro_rules! create {
|
||||||
($model_name:ident, $object:expr, $conn:expr) => {{
|
($model_name:ident, $object:expr, $conn:expr) => {{
|
||||||
use diesel::associations::HasTable;
|
use diesel::associations::HasTable;
|
||||||
|
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl, Table};
|
||||||
|
|
||||||
let table = $crate::models::$model_name::table();
|
let table = $crate::models::$model_name::table();
|
||||||
let primary_key = table.primary_key();
|
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)?;
|
diesel::insert_into(table).values($object).execute(&$conn)?;
|
||||||
table.order(primary_key.desc()).first(&$conn)
|
table.order(primary_key.desc()).first(&$conn)
|
||||||
})
|
})
|
||||||
|
@ -128,6 +130,7 @@ macro_rules! create {
|
||||||
macro_rules! update {
|
macro_rules! update {
|
||||||
($model_name:ident, $primary_key:expr, $object:expr, $conn:expr) => {{
|
($model_name:ident, $primary_key:expr, $object:expr, $conn:expr) => {{
|
||||||
use diesel::associations::HasTable;
|
use diesel::associations::HasTable;
|
||||||
|
|
||||||
let table = $crate::models::$model_name::table();
|
let table = $crate::models::$model_name::table();
|
||||||
$conn.transaction::<_, diesel::result::Error, _>(|| {
|
$conn.transaction::<_, diesel::result::Error, _>(|| {
|
||||||
diesel::update(table.find($primary_key))
|
diesel::update(table.find($primary_key))
|
||||||
|
@ -141,6 +144,7 @@ macro_rules! update {
|
||||||
macro_rules! destroy {
|
macro_rules! destroy {
|
||||||
($model_name:ident, $primary_key:expr, $conn:expr) => {{
|
($model_name:ident, $primary_key:expr, $conn:expr) => {{
|
||||||
use diesel::associations::HasTable;
|
use diesel::associations::HasTable;
|
||||||
|
|
||||||
let table = $crate::models::$model_name::table();
|
let table = $crate::models::$model_name::table();
|
||||||
let result: Result<$model_name, _> = get!($model_name, $primary_key, $conn);
|
let result: Result<$model_name, _> = get!($model_name, $primary_key, $conn);
|
||||||
match result {
|
match result {
|
||||||
|
|
Loading…
Reference in a new issue