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            }),
41            source.body: ExprSet::Values(_ {
42                rows: [=~ (Any, "foo")],
43                ..
44            }),
45            ..
46        }),
47        ..
48    }));
49    Ok(())
50}
51
52#[driver_test(id(ID), requires(native_varchar))]
53pub async fn specify_custom_column_name_with_type(test: &mut Test) -> Result<()> {
54    #[derive(toasty::Model)]
55    struct User {
56        #[key]
57        #[auto]
58        id: ID,
59
60        #[column("my_name", type = varchar(5))]
61        name: String,
62    }
63
64    let mut db = test.setup_db(models!(User)).await;
65
66    let u = User::create().name("foo").exec(&mut db).await?;
67    assert_eq!(u.name, "foo");
68
69    // Verify that the INSERT operation used the correct column name "my_name"
70    // and sent the value as a string
71    let (op, _resp) = test.log().pop();
72
73    // Get the expected column IDs for the users table
74    let user_table_id = table_id(&db, "users");
75    let expected_columns = columns(&db, "users", &["id", "my_name"]);
76
77    // Verify the operation uses the correct table and column names
78    assert_struct!(op, Operation::QuerySql(_ {
79        stmt: Statement::Insert(_ {
80            target: InsertTarget::Table(_ {
81                table: == user_table_id,
82                columns: == expected_columns,
83                ..
84            }),
85            ..
86        }),
87        ..
88    }));
89
90    // Verify the value "foo" is sent as a string
91    if let Operation::QuerySql(query) = op {
92        if let Statement::Insert(insert) = query.stmt {
93            if let ExprSet::Values(values) = insert.source.body {
94                assert_struct!(values.rows, [=~ (Any, "foo")]);
95            } else {
96                panic!("Expected Values in INSERT source");
97            }
98        }
99    }
100
101    // Creating a user with a name larger than 5 characters should fail.
102    let res = User::create().name("foo bar").exec(&mut db).await;
103    assert!(res.is_err());
104    Ok(())
105}