Skip to main content

tank_tests/
shopping.rs

1#![allow(unused_imports)]
2use rust_decimal::Decimal;
3use std::collections::HashMap;
4use std::pin::pin;
5use std::{str::FromStr, sync::Arc, sync::LazyLock};
6use tank::QueryBuilder;
7use tank::{
8    AsValue, Dataset, Entity, Executor, FixedDecimal, cols, expr, join,
9    stream::{StreamExt, TryStreamExt},
10};
11use time::{Date, Month, PrimitiveDateTime, Time};
12use tokio::sync::Mutex;
13use uuid::Uuid;
14
15static MUTEX: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(()));
16
17#[derive(Default, Debug, Entity)]
18#[tank(schema = "shopping", primary_key = Self::id)]
19struct Product {
20    id: usize,
21    name: String,
22    price: FixedDecimal<8, 2>,
23    desc: Option<String>,
24    stock: Option<isize>,
25    #[cfg(not(feature = "disable-lists"))]
26    tags: Vec<String>,
27}
28
29#[derive(Debug, Entity)]
30#[tank(schema = "shopping", primary_key = Self::id)]
31struct User {
32    id: Uuid,
33    name: String,
34    email: String,
35    birthday: Date,
36    #[cfg(not(feature = "disable-lists"))]
37    preferences: Option<Arc<Vec<String>>>,
38    registered: PrimitiveDateTime,
39}
40
41#[derive(Debug, Entity)]
42#[tank(schema = "shopping", primary_key = (user, product))]
43struct Cart {
44    #[tank(references = User::id)]
45    user: Uuid,
46    #[tank(references = Product::id)]
47    product: usize,
48    /// The price can stay locked once added to the shopping cart
49    price: FixedDecimal<8, 2>,
50    timestamp: PrimitiveDateTime,
51}
52
53pub async fn shopping<E: Executor>(executor: &mut E) {
54    let _lock = MUTEX.lock().await;
55
56    // Product
57    Product::drop_table(executor, true, false)
58        .await
59        .expect("Failed to drop product table");
60    Product::create_table(executor, false, true)
61        .await
62        .expect("Failed to create the product table");
63    let products = [
64        Product {
65            id: 1,
66            name: "Rust-Proof Coffee Mug".into(),
67            price: Decimal::new(12_99, 2).into(),
68            desc: Some("Keeps your coffee warm and your compiler calm.".into()),
69            stock: 42.into(),
70            #[cfg(not(feature = "disable-lists"))]
71            tags: vec!["kitchen".into(), "coffee".into(), "metal".into()].into(),
72        },
73        Product {
74            id: 2,
75            name: "Zero-Cost Abstraction Hoodie".into(),
76            price: Decimal::new(49_95, 2).into(),
77            desc: Some("For developers who think runtime overhead is a moral failure.".into()),
78            stock: 10.into(),
79            #[cfg(not(feature = "disable-lists"))]
80            tags: vec!["clothing".into(), "nerdwear".into()].into(),
81        },
82        Product {
83            id: 3,
84            name: "Thread-Safe Notebook".into(),
85            price: Decimal::new(7_50, 2).into(),
86            desc: None,
87            stock: 0.into(),
88            #[cfg(not(feature = "disable-lists"))]
89            tags: vec!["stationery".into()].into(),
90        },
91        Product {
92            id: 4,
93            name: "Async Teapot".into(),
94            price: Decimal::new(25_00, 2).into(),
95            desc: Some("Returns 418 on brew() call.".into()),
96            stock: 3.into(),
97            #[cfg(not(feature = "disable-lists"))]
98            tags: vec!["kitchen".into(), "humor".into()].into(),
99        },
100    ];
101    Product::insert_many(executor, &products)
102        .await
103        .expect("Could not insert the products");
104    let total_products = Product::find_many(executor, true, None).count().await;
105    assert_eq!(total_products, 4);
106    let ordered_products = executor
107        .fetch(
108            QueryBuilder::new()
109                .select([Product::id, Product::name, Product::price])
110                .from(Product::table())
111                .where_expr(expr!(Product::stock > 0))
112                .order_by(cols!(Product::price ASC))
113                .build(&executor.driver()),
114        )
115        .map(|r| r.and_then(Product::from_row))
116        .try_collect::<Vec<Product>>()
117        .await
118        .expect("Could not get the products ordered by increasing price");
119    assert!(
120        ordered_products.iter().map(|v| &v.name).eq([
121            "Rust-Proof Coffee Mug",
122            "Async Teapot",
123            "Zero-Cost Abstraction Hoodie"
124        ]
125        .into_iter())
126    );
127    let zero_stock = Product::find_one(executor, expr!(Product::stock == 0))
128        .await
129        .expect("Failed to query product with zero stock")
130        .expect("Expected a product with zero stock");
131    assert_eq!(zero_stock.id, 3);
132
133    // Decrease stock for product id 4 by 1 and verify save works
134    let mut prod4 = Product::find_one(executor, expr!(Product::id == 4))
135        .await
136        .expect("Failed to query product 4")
137        .expect("Product 4 expected");
138    let old_stock = prod4.stock.unwrap_or(0);
139    prod4.stock = Some(old_stock - 1);
140    prod4
141        .save(executor)
142        .await
143        .expect("Failed to save updated product 4");
144    let prod4_after = Product::find_one(executor, expr!(Product::id == 4))
145        .await
146        .expect("Failed to query product 4 after update")
147        .expect("Product 4 expected after update");
148    assert_eq!(prod4_after.stock, Some(old_stock - 1));
149
150    // User
151    User::drop_table(executor, true, false)
152        .await
153        .expect("Failed to drop user table");
154    User::create_table(executor, false, false)
155        .await
156        .expect("Failed to create the user table");
157    let users = vec![
158        User {
159            id: Uuid::new_v4(),
160            name: "Alice Compiler".into(),
161            email: "alice@example.com".into(),
162            birthday: Date::from_calendar_date(1995, Month::May, 17).unwrap(),
163            #[cfg(not(feature = "disable-lists"))]
164            preferences: Some(vec!["dark_mode".into(), "express_shipping".into()].into()),
165            registered: PrimitiveDateTime::new(
166                Date::from_calendar_date(2023, Month::January, 2).unwrap(),
167                Time::from_hms(10, 30, 0).unwrap(),
168            ),
169        },
170        User {
171            id: Uuid::new_v4(),
172            name: "Bob Segfault".into(),
173            email: "bob@crashmail.net".into(),
174            birthday: Date::from_calendar_date(1988, Month::March, 12).unwrap(),
175            #[cfg(not(feature = "disable-lists"))]
176            preferences: None,
177            registered: PrimitiveDateTime::new(
178                Date::from_calendar_date(2024, Month::June, 8).unwrap(),
179                Time::from_hms(22, 15, 0).unwrap(),
180            ),
181        },
182    ];
183    User::insert_many(executor, &users)
184        .await
185        .expect("Could not insert the users");
186    let row = pin!(
187        executor.fetch(
188            QueryBuilder::new()
189                .select(cols!(COUNT(*)))
190                .from(User::table())
191                .where_expr(true)
192                .limit(Some(1))
193                .build(&executor.driver())
194        )
195    )
196    .try_next()
197    .await
198    .expect("Failed to query for count")
199    .expect("Did not return some value");
200    assert_eq!(i64::try_from_value(row.values[0].clone()).unwrap(), 2);
201
202    // Cart
203    Cart::drop_table(executor, true, false)
204        .await
205        .expect("Failed to drop cart table");
206    Cart::create_table(executor, false, false)
207        .await
208        .expect("Failed to create the cart table");
209    let carts = vec![
210        Cart {
211            user: users[0].id,
212            product: 1,
213            price: Decimal::new(12_99, 2).into(),
214            timestamp: PrimitiveDateTime::new(
215                Date::from_calendar_date(2025, Month::March, 1).unwrap(),
216                Time::from_hms(9, 0, 0).unwrap(),
217            ),
218        },
219        Cart {
220            user: users[0].id,
221            product: 2,
222            price: Decimal::new(49_95, 2).into(),
223            timestamp: PrimitiveDateTime::new(
224                Date::from_calendar_date(2025, Month::March, 1).unwrap(),
225                Time::from_hms(9, 5, 0).unwrap(),
226            ),
227        },
228        Cart {
229            user: users[1].id,
230            product: 4,
231            price: Decimal::new(23_50, 2).into(),
232            timestamp: PrimitiveDateTime::new(
233                Date::from_calendar_date(2025, Month::March, 3).unwrap(),
234                Time::from_hms(14, 12, 0).unwrap(),
235            ),
236        },
237    ];
238    Cart::insert_many(executor, &carts)
239        .await
240        .expect("Could not insert the carts");
241    let cart_count = Cart::find_many(executor, true, None).count().await;
242    assert_eq!(cart_count, 3);
243
244    // Product 4 in cart has different price than current product price
245    let cart_for_4 = Cart::find_one(executor, expr!(Cart::product == 4))
246        .await
247        .expect("Failed to query cart for product 4");
248    let cart_for_4 = cart_for_4.expect("Expected a cart for product 4");
249    let product4 = Product::find_one(executor, expr!(Product::id == 4))
250        .await
251        .expect("Failed to query product 4 for price check")
252        .expect("Expected product 4");
253    assert_eq!(cart_for_4.price.0, Decimal::new(23_50, 2));
254    assert_eq!(product4.price.0, Decimal::new(25_00, 2));
255
256    // Delete the cart containing product 2
257    let cart_for_2 = Cart::find_one(executor, expr!(Cart::product == 2))
258        .await
259        .expect("Failed to query cart for product 2")
260        .expect("Expected a cart for product 2");
261    cart_for_2
262        .delete(executor)
263        .await
264        .expect("Failed to delete cart for product 2");
265    let cart_count_after = Cart::find_many(executor, true, None).count().await;
266    assert_eq!(cart_count_after, 2);
267
268    #[cfg(not(feature = "disable-joins"))]
269    {
270        #[derive(Debug, Entity, PartialEq)]
271        struct Carts {
272            user: String,
273            product: String,
274            price: Decimal,
275        }
276        let carts: Vec<Carts> = executor
277            .fetch(
278                QueryBuilder::new()
279                    .select(cols!(
280                        Product::name as product,
281                        User::name as user,
282                        Cart::price
283                    ))
284                    .from(join!(
285                        User INNER JOIN Cart ON User::id == Cart::user
286                            JOIN Product ON Cart::product == Product::id
287                    ))
288                    .where_expr(true)
289                    .order_by(cols!(Product::name ASC, User::name ASC))
290                    .build(&executor.driver()),
291            )
292            .map_ok(Carts::from_row)
293            .map(Result::flatten)
294            .try_collect::<Vec<_>>()
295            .await
296            .expect("Could not get the products ordered by increasing price");
297        assert_eq!(
298            carts,
299            &[
300                Carts {
301                    user: "Bob Segfault".into(),
302                    product: "Async Teapot".into(),
303                    price: Decimal::new(23_50, 2),
304                },
305                Carts {
306                    user: "Alice Compiler".into(),
307                    product: "Rust-Proof Coffee Mug".into(),
308                    price: Decimal::new(12_99, 2),
309                },
310            ]
311        )
312    }
313}