Skip to main content

toasty_driver_integration_suite/tests/
field_column_name.rs

1use crate::prelude::*;
2
3use toasty_core::{
4    driver::Operation,
5    stmt::{Expr, 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, and that
34    // the value is transmitted either as a bind parameter (SQL) or inline (DDB).
35    //
36    // Position: id_u64 uses Expr::Default for auto-increment (no param), so "foo"
37    // is at params[0]. id_uuid generates the uuid client-side, so "foo" is at
38    // params[1].
39    let sql = test.capability().sql;
40    let val_pos = if driver_test_cfg!(id_u64) { 0 } else { 1 };
41    let val = if sql {
42        ArgOr::Arg(val_pos)
43    } else {
44        ArgOr::Value("foo")
45    };
46    assert_struct!(op, Operation::QuerySql({
47        stmt: Statement::Insert({
48            target: InsertTarget::Table({
49                table: == user_table_id,
50                columns: == expected_columns,
51            }),
52            source.body: ExprSet::Values({
53                rows: [=~ (Any, val)],
54            }),
55        }),
56    }));
57    if sql {
58        assert_struct!(op, Operation::QuerySql({
59            params[val_pos].value: == "foo",
60        }));
61    }
62    Ok(())
63}
64
65#[driver_test(id(ID), requires(native_varchar))]
66pub async fn specify_custom_column_name_with_type(test: &mut Test) -> Result<()> {
67    #[derive(toasty::Model)]
68    struct User {
69        #[key]
70        #[auto]
71        id: ID,
72
73        #[column("my_name", type = varchar(5))]
74        name: String,
75    }
76
77    let mut db = test.setup_db(models!(User)).await;
78
79    let u = User::create().name("foo").exec(&mut db).await?;
80    assert_eq!(u.name, "foo");
81
82    // Verify that the INSERT operation used the correct column name "my_name"
83    // and sent the value as a string
84    let (op, _resp) = test.log().pop();
85
86    // Get the expected column IDs for the users table
87    let user_table_id = table_id(&db, "users");
88    let expected_columns = columns(&db, "users", &["id", "my_name"]);
89
90    // Verify the operation uses the correct table and column names, and that the
91    // value "foo" is sent as a string bind parameter. This test is SQL-only
92    // (requires native_varchar), so the value always becomes an Arg placeholder.
93    assert_struct!(op, Operation::QuerySql({
94        stmt: Statement::Insert({
95            target: InsertTarget::Table({
96                table: == user_table_id,
97                columns: == expected_columns,
98            }),
99            source.body: ExprSet::Values({
100                rows: [Expr::Record({ fields: [_, Expr::Arg(_)] })],
101            }),
102        }),
103        params: [.., { value: == "foo" }],
104    }));
105
106    // Creating a user with a name larger than 5 characters should fail.
107    let res = User::create().name("foo bar").exec(&mut db).await;
108    assert!(res.is_err());
109    Ok(())
110}