Skip to content

Commit

Permalink
added order update test
Browse files Browse the repository at this point in the history
  • Loading branch information
FayCarsons committed Feb 12, 2024
1 parent 3154e17 commit 252f6be
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion backend/src/tests/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
mod tests {
use std::fs::{self};

use diesel::{dsl::count, query_dsl::methods::SelectDsl, QueryDsl, RunQueryDsl};
use diesel::{dsl::count, query_dsl::methods::SelectDsl, ExpressionMethods, QueryDsl, RunQueryDsl, SelectableHelper};

use crate::{
model::{
Expand Down Expand Up @@ -63,6 +63,35 @@ mod tests {
assert!(insert.is_ok());
assert_eq!(crate::schema::carts::table.count().first::<i64>(&mut conn), Ok(cart.len() as i64));
}

#[test]
fn test_update_order() {
let JsonOrder { name, street, zipcode, .. } = serde_json::from_str(include_str!("./mock_order.json")).expect("Cannot open mock order");

let db = test_db::TestDb::new();
let mut conn = db.connection();

use crate::schema::orders;

let inserted_id = diesel::insert_into(orders::table)
.values(&NewOrder {
name: &name,
street: &street,
zipcode: &zipcode,
fulfilled: false,
})
.returning(orders::dsl::id)
.get_result::<i32>(&mut conn);

assert!(inserted_id.is_ok());
assert_eq!(inserted_id, Ok(1));
assert_eq!(crate::schema::orders::table.count().first(&mut conn), Ok(1));

let res = diesel::update(crate::schema::orders::table.filter(crate::schema::orders::id.eq(inserted_id.unwrap()))).set(crate::schema::orders::fulfilled.eq(true)).execute(&mut conn);
assert!(res.is_ok());

assert_eq!(crate::schema::orders::table.filter(crate::schema::orders::fulfilled.eq(true)).count().first(&mut conn), Ok(1));
}

#[test]
fn insert_stock() {
Expand Down

0 comments on commit 252f6be

Please sign in to comment.