toasty_driver_integration_suite/tests/
field_column_name.rs

1use crate::prelude::*;
2
3use toasty_core::{
4    driver::Operation,
5    stmt::{ExprSet, InsertTarget, Statement},
6};
7
8#[driver_test(id(ID))]
9pub async fn specify_custom_column_name(test: &mut Test) -> Result<()> {
10    #[derive(toasty::Model)]
11    struct User {
12        #[key]
13        #[auto]
14        id: ID,
15
16        #[column("my_name")]
17        name: String,
18    }
19
20    let mut db = test.setup_db(models!(User)).await;
21
22    let u = User::create().name("foo").exec(&mut db).await?;
23    assert_eq!(u.name, "foo");
24
25    // Verify that the INSERT operation used the correct column name "my_name"
26    // and sent the value as a string
27    let (op, _resp) = test.log().pop();
28
29    // Get the expected column IDs for the users table
30    let user_table_id = table_id(&db, "users");
31    let expected_columns = columns(&db, "users", &["id", "my_name"]);
32
33    // Verify the operation uses the correct table and column names
34    assert_struct!(op, Operation::QuerySql({
35        stmt: Statement::Insert({
36            target: InsertTarget::Table({
37                table: == user_table_id,
38                columns: == expected_columns,
39            }),
40            source.body: ExprSet::Values({
41                rows: [=~ (Any, "foo")],
42            }),
43        }),
44    }));
45    Ok(())
46}
47
48#[driver_test(id(ID), requires(native_varchar))]
49pub async fn specify_custom_column_name_with_type(test: &mut Test) -> Result<()> {
50    #[derive(toasty::Model)]
51    struct User {
52        #[key]
53        #[auto]
54        id: ID,
55
56        #[column("my_name", type = varchar(5))]
57        name: String,
58    }
59
60    let mut db = test.setup_db(models!(User)).await;
61
62    let u = User::create().name("foo").exec(&mut db).await?;
63    assert_eq!(u.name, "foo");
64
65    // Verify that the INSERT operation used the correct column name "my_name"
66    // and sent the value as a string
67    let (op, _resp) = test.log().pop();
68
69    // Get the expected column IDs for the users table
70    let user_table_id = table_id(&db, "users");
71    let expected_columns = columns(&db, "users", &["id", "my_name"]);
72
73    // Verify the operation uses the correct table and column names
74    assert_struct!(op, Operation::QuerySql({
75        stmt: Statement::Insert({
76            target: InsertTarget::Table({
77                table: == user_table_id,
78                columns: == expected_columns,
79            }),
80        }),
81    }));
82
83    // Verify the value "foo" is sent as a string
84    if let Operation::QuerySql(query) = op
85        && let Statement::Insert(insert) = query.stmt
86    {
87        if let ExprSet::Values(values) = insert.source.body {
88            assert_struct!(values.rows, [=~ (Any, "foo")]);
89        } else {
90            panic!("Expected Values in INSERT source");
91        }
92    }
93
94    // Creating a user with a name larger than 5 characters should fail.
95    let res = User::create().name("foo bar").exec(&mut db).await;
96    assert!(res.is_err());
97    Ok(())
98}