Skip to content

Commit

Permalink
Drop the use of rust_decimal_macros
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 committed Jan 24, 2024
1 parent ce7e420 commit 427d265
Show file tree
Hide file tree
Showing 16 changed files with 67 additions and 73 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ futures = { version = "0.3" }
tokio = { version = "1.6", features = ["full"] }
actix-rt = { version = "2.2.0" }
maplit = { version = "1" }
rust_decimal_macros = { version = "1" }
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
sea-orm = { path = ".", features = ["mock", "debug-print", "tests-cfg", "postgres-array", "sea-orm-internal"] }
pretty_assertions = { version = "0.7" }
Expand Down
7 changes: 3 additions & 4 deletions tests/common/bakery_chain/cake.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::common::setup::rust_dec;
use sea_orm::{entity::prelude::*, ConnectionTrait};

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
Expand Down Expand Up @@ -63,8 +64,7 @@ impl ActiveModelBehavior for ActiveModel {
where
C: ConnectionTrait,
{
use rust_decimal_macros::dec;
if self.price.as_ref() == &dec!(0) {
if self.price.as_ref() == &rust_dec(0) {
Err(DbErr::Custom(format!(
"[before_save] Invalid Price, insert: {insert}"
)))
Expand All @@ -77,8 +77,7 @@ impl ActiveModelBehavior for ActiveModel {
where
C: ConnectionTrait,
{
use rust_decimal_macros::dec;
if model.price < dec!(0) {
if model.price < rust_dec(0) {
Err(DbErr::Custom(format!(
"[after_save] Invalid Price, insert: {insert}"
)))
Expand Down
5 changes: 5 additions & 0 deletions tests/common/setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,8 @@ pub async fn create_table_without_asserts(
}
db.execute(builder.build(create)).await
}

pub fn rust_dec<T: ToString>(v: T) -> rust_decimal::Decimal {
use std::str::FromStr;
rust_decimal::Decimal::from_str(&v.to_string()).unwrap()
}
5 changes: 2 additions & 3 deletions tests/crud/create_cake.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub use super::*;
use rust_decimal_macros::dec;
use uuid::Uuid;

pub async fn test_create_cake(db: &DbConn) {
Expand Down Expand Up @@ -31,7 +30,7 @@ pub async fn test_create_cake(db: &DbConn) {

let mud_cake = cake::ActiveModel {
name: Set("Mud Cake".to_owned()),
price: Set(dec!(-10.25)),
price: Set(rust_dec(-10.25)),
gluten_free: Set(false),
serial: Set(uuid),
bakery_id: Set(Some(bakery_insert_res.last_insert_id)),
Expand Down Expand Up @@ -64,7 +63,7 @@ pub async fn test_create_cake(db: &DbConn) {
assert!(cake.is_some());
let cake_model = cake.unwrap();
assert_eq!(cake_model.name, "Mud Cake");
assert_eq!(cake_model.price, dec!(-10.25));
assert_eq!(cake_model.price, rust_dec(-10.25));
assert!(!cake_model.gluten_free);
assert_eq!(
cake_model
Expand Down
9 changes: 4 additions & 5 deletions tests/crud/create_lineitem.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub use super::*;
use chrono::offset::Utc;
use rust_decimal_macros::dec;
use uuid::Uuid;

pub async fn test_create_lineitem(db: &DbConn) {
Expand Down Expand Up @@ -34,7 +33,7 @@ pub async fn test_create_lineitem(db: &DbConn) {
// Cake
let mud_cake = cake::ActiveModel {
name: Set("Mud Cake".to_owned()),
price: Set(dec!(10.25)),
price: Set(rust_dec(10.25)),
gluten_free: Set(false),
serial: Set(Uuid::new_v4()),
bakery_id: Set(Some(bakery_insert_res.last_insert_id)),
Expand Down Expand Up @@ -75,7 +74,7 @@ pub async fn test_create_lineitem(db: &DbConn) {
let order_1 = order::ActiveModel {
bakery_id: Set(bakery_insert_res.last_insert_id),
customer_id: Set(customer_insert_res.last_insert_id),
total: Set(dec!(7.55)),
total: Set(rust_dec(7.55)),
placed_at: Set(Utc::now().naive_utc()),
..Default::default()
};
Expand All @@ -88,7 +87,7 @@ pub async fn test_create_lineitem(db: &DbConn) {
let lineitem_1 = lineitem::ActiveModel {
cake_id: Set(cake_insert_res.last_insert_id),
order_id: Set(order_insert_res.last_insert_id),
price: Set(dec!(7.55)),
price: Set(rust_dec(7.55)),
quantity: Set(1),
..Default::default()
};
Expand All @@ -106,7 +105,7 @@ pub async fn test_create_lineitem(db: &DbConn) {
assert!(lineitem.is_some());
let lineitem_model = lineitem.unwrap();

assert_eq!(lineitem_model.price, dec!(7.55));
assert_eq!(lineitem_model.price, rust_dec(7.55));

let cake: Option<cake::Model> = Cake::find_by_id(lineitem_model.cake_id)
.one(db)
Expand Down
11 changes: 5 additions & 6 deletions tests/crud/create_order.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub use super::*;
use chrono::offset::Utc;
use rust_decimal_macros::dec;
use uuid::Uuid;

pub async fn test_create_order(db: &DbConn) {
Expand Down Expand Up @@ -34,7 +33,7 @@ pub async fn test_create_order(db: &DbConn) {
// Cake
let mud_cake = cake::ActiveModel {
name: Set("Mud Cake".to_owned()),
price: Set(dec!(10.25)),
price: Set(rust_dec(10.25)),
gluten_free: Set(false),
serial: Set(Uuid::new_v4()),
bakery_id: Set(Some(bakery_insert_res.last_insert_id)),
Expand Down Expand Up @@ -75,7 +74,7 @@ pub async fn test_create_order(db: &DbConn) {
let order_1 = order::ActiveModel {
bakery_id: Set(bakery_insert_res.last_insert_id),
customer_id: Set(customer_insert_res.last_insert_id),
total: Set(dec!(15.10)),
total: Set(rust_dec(15.10)),
placed_at: Set(Utc::now().naive_utc()),
..Default::default()
};
Expand All @@ -88,7 +87,7 @@ pub async fn test_create_order(db: &DbConn) {
let lineitem_1 = lineitem::ActiveModel {
cake_id: Set(cake_insert_res.last_insert_id),
order_id: Set(order_insert_res.last_insert_id),
price: Set(dec!(7.55)),
price: Set(rust_dec(7.55)),
quantity: Set(2),
..Default::default()
};
Expand All @@ -104,7 +103,7 @@ pub async fn test_create_order(db: &DbConn) {

assert!(order.is_some());
let order_model = order.unwrap();
assert_eq!(order_model.total, dec!(15.10));
assert_eq!(order_model.total, rust_dec(15.10));

let customer: Option<customer::Model> = Customer::find_by_id(order_model.customer_id)
.one(db)
Expand All @@ -128,6 +127,6 @@ pub async fn test_create_order(db: &DbConn) {
.await
.expect("could not find related lineitems");
assert_eq!(related_lineitems.len(), 1);
assert_eq!(related_lineitems[0].price, dec!(7.55));
assert_eq!(related_lineitems[0].price, rust_dec(7.55));
assert_eq!(related_lineitems[0].quantity, 2);
}
3 changes: 1 addition & 2 deletions tests/crud/deletes.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub use super::*;
use rust_decimal_macros::dec;
use uuid::Uuid;

pub async fn test_delete_cake(db: &DbConn) {
Expand All @@ -17,7 +16,7 @@ pub async fn test_delete_cake(db: &DbConn) {

let mud_cake = cake::ActiveModel {
name: Set("Mud Cake".to_owned()),
price: Set(dec!(10.25)),
price: Set(rust_dec(10.25)),
gluten_free: Set(false),
serial: Set(Uuid::new_v4()),
bakery_id: Set(Some(bakery_insert_res.last_insert_id)),
Expand Down
3 changes: 1 addition & 2 deletions tests/crud/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub use super::*;
use rust_decimal_macros::dec;
use sea_orm::error::*;
#[cfg(any(
feature = "sqlx-mysql",
Expand All @@ -12,7 +11,7 @@ use uuid::Uuid;
pub async fn test_cake_error_sqlx(db: &DbConn) {
let mud_cake = cake::ActiveModel {
name: Set("Moldy Cake".to_owned()),
price: Set(dec!(10.25)),
price: Set(rust_dec(10.25)),
gluten_free: Set(false),
serial: Set(Uuid::new_v4()),
bakery_id: Set(None),
Expand Down
1 change: 1 addition & 0 deletions tests/crud/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub use error::*;
pub use updates::*;

pub use super::common::bakery_chain::*;
pub use crate::common::setup::rust_dec;
use sea_orm::{entity::*, DbConn};

pub async fn test_create_bakery(db: &DbConn) {
Expand Down
5 changes: 2 additions & 3 deletions tests/crud/updates.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub use super::*;
use rust_decimal_macros::dec;
use sea_orm::{query::*, DbErr};
use uuid::Uuid;

Expand All @@ -16,7 +15,7 @@ pub async fn test_update_cake(db: &DbConn) {

let mud_cake = cake::ActiveModel {
name: Set("Mud Cake".to_owned()),
price: Set(dec!(10.25)),
price: Set(rust_dec(10.25)),
gluten_free: Set(false),
serial: Set(Uuid::new_v4()),
bakery_id: Set(Some(bakery_insert_res.last_insert_id)),
Expand All @@ -36,7 +35,7 @@ pub async fn test_update_cake(db: &DbConn) {
assert!(cake.is_some());
let cake_model = cake.unwrap();
assert_eq!(cake_model.name, "Mud Cake");
assert_eq!(cake_model.price, dec!(10.25));
assert_eq!(cake_model.price, rust_dec(10.25));
assert!(!cake_model.gluten_free);

let large_number = (u64::MAX as u128 + 1).to_string().parse().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion tests/cursor_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ pub async fn create_baker_cake(db: &DatabaseConnection) -> Result<(), DbErr> {
});
cakes.push(cake::ActiveModel {
name: Set(c.to_ascii_lowercase().to_string()),
price: Set(rust_decimal_macros::dec!(10.25)),
price: Set(rust_dec(10.25)),
gluten_free: Set(false),
serial: Set(Uuid::new_v4()),
bakery_id: Set(Some((c as i32 - 65) % 10 + 1)),
Expand Down
11 changes: 5 additions & 6 deletions tests/pi_tests.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
pub mod common;

use common::{features::*, TestContext};
use common::{features::*, setup::*, TestContext};
use pretty_assertions::assert_eq;
use rust_decimal_macros::dec;
use sea_orm::{entity::prelude::*, entity::*, DatabaseConnection};
use std::str::FromStr;

Expand All @@ -24,7 +23,7 @@ async fn main() -> Result<(), DbErr> {
pub async fn create_and_update_pi(db: &DatabaseConnection) -> Result<(), DbErr> {
let pi = pi::Model {
id: 1,
decimal: dec!(3.1415926536),
decimal: rust_dec(3.1415926536),
big_decimal: BigDecimal::from_str("3.1415926536").unwrap(),
decimal_opt: None,
big_decimal_opt: None,
Expand All @@ -37,7 +36,7 @@ pub async fn create_and_update_pi(db: &DatabaseConnection) -> Result<(), DbErr>
assert_eq!(model, Some(pi.clone()));

let res = pi::ActiveModel {
decimal_opt: Set(Some(dec!(3.1415926536))),
decimal_opt: Set(Some(rust_dec(3.1415926536))),
big_decimal_opt: Set(Some(BigDecimal::from_str("3.1415926536").unwrap())),
..pi.clone().into_active_model()
}
Expand All @@ -50,9 +49,9 @@ pub async fn create_and_update_pi(db: &DatabaseConnection) -> Result<(), DbErr>
model,
Some(pi::Model {
id: 1,
decimal: dec!(3.1415926536),
decimal: rust_dec(3.1415926536),
big_decimal: BigDecimal::from_str("3.1415926536").unwrap(),
decimal_opt: Some(dec!(3.1415926536)),
decimal_opt: Some(rust_dec(3.1415926536)),
big_decimal_opt: Some(BigDecimal::from_str("3.1415926536").unwrap()),
})
);
Expand Down
Loading

0 comments on commit 427d265

Please sign in to comment.