Skip to main content

toasty_driver_integration_suite/tests/
field_auto.rs

1use crate::prelude::*;
2
3#[driver_test(id(ID))]
4pub async fn auto_uuid_v4(test: &mut Test) -> Result<()> {
5    #[derive(toasty::Model)]
6    struct Item {
7        #[key]
8        #[auto]
9        id: ID,
10
11        #[auto(uuid(v4))]
12        auto_field: uuid::Uuid,
13    }
14
15    let mut db = test.setup_db(models!(Item)).await;
16
17    let u = Item::create().exec(&mut db).await?;
18    // Sanity check that it actually generated a UUID
19    assert!(uuid::Uuid::parse_str(&u.auto_field.to_string()).is_ok());
20    Ok(())
21}
22
23#[driver_test(id(ID))]
24pub async fn auto_uuid_v7(test: &mut Test) -> Result<()> {
25    #[derive(toasty::Model)]
26    struct Item {
27        #[key]
28        #[auto]
29        id: ID,
30
31        #[auto(uuid(v7))]
32        auto_field: uuid::Uuid,
33    }
34
35    let mut db = test.setup_db(models!(Item)).await;
36
37    let u = Item::create().exec(&mut db).await?;
38    // Sanity check that it actually generated a UUID
39    assert!(uuid::Uuid::parse_str(&u.auto_field.to_string()).is_ok());
40    Ok(())
41}
42
43#[driver_test(requires(auto_increment))]
44pub async fn auto_increment_explicit(test: &mut Test) -> Result<()> {
45    #[derive(toasty::Model)]
46    struct Item {
47        #[key]
48        #[auto(increment)]
49        auto_field: u32,
50    }
51
52    let mut db = test.setup_db(models!(Item)).await;
53
54    for i in 1..10 {
55        let u = Item::create().exec(&mut db).await?;
56        assert_eq!(u.auto_field, i);
57    }
58    Ok(())
59}
60
61#[driver_test(requires(auto_increment))]
62pub async fn auto_increment_i64_key(test: &mut Test) -> Result<()> {
63    #[derive(toasty::Model)]
64    struct Item {
65        #[key]
66        #[auto]
67        id: i64,
68
69        name: String,
70    }
71
72    let mut db = test.setup_db(models!(Item)).await;
73
74    let first = toasty::create!(Item { name: "first" })
75        .exec(&mut db)
76        .await?;
77    let second = toasty::create!(Item { name: "second" })
78        .exec(&mut db)
79        .await?;
80
81    assert_eq!(first.id, 1);
82    assert_eq!(second.id, 2);
83
84    Ok(())
85}
86
87#[driver_test(id(ID), requires(auto_increment))]
88pub async fn auto_increment_implicit(test: &mut Test) -> Result<()> {
89    #[derive(toasty::Model)]
90    struct Item {
91        #[key]
92        #[auto]
93        auto_field: u32,
94    }
95
96    let mut db = test.setup_db(models!(Item)).await;
97
98    for i in 1..10 {
99        let u = Item::create().exec(&mut db).await?;
100        assert_eq!(u.auto_field, i);
101    }
102    Ok(())
103}
104
105// Test that auto-increment with composite primary keys is rejected
106// This only applies to numeric types since UUID uses a different auto strategy
107#[driver_test(requires(auto_increment))]
108pub async fn auto_increment_with_composite_key_errors(test: &mut Test) {
109    #[derive(toasty::Model)]
110    #[key(partition = user_id, local = id)]
111    struct InvalidModel {
112        #[auto(increment)]
113        id: u64,
114
115        user_id: u64,
116    }
117
118    // This should fail during schema setup
119    let result = test.try_setup_db(models!(InvalidModel)).await;
120
121    assert!(result.is_err(), "Expected schema setup to fail");
122    let err = result.unwrap_err();
123    assert!(
124        err.to_string()
125            .contains("cannot be used with composite primary keys"),
126        "Expected error message about composite keys, got: {}",
127        err
128    );
129}
130
131// Foreign key IDs passed to assocations depend on the auto-increment ID generated by the database,
132// we want to make sure this works.
133#[driver_test(id(ID), requires(auto_increment))]
134pub async fn auto_increment_with_associations(test: &mut Test) -> Result<()> {
135    #[derive(toasty::Model)]
136    struct Parent {
137        #[key]
138        #[auto(increment)]
139        id: u32,
140
141        #[has_many]
142        children: toasty::Deferred<Vec<Child>>,
143    }
144
145    #[derive(toasty::Model)]
146    struct Child {
147        #[key]
148        #[auto(increment)]
149        id: u32,
150
151        #[index]
152        parent_id: u32,
153
154        #[belongs_to(key = parent_id, references = id)]
155        #[allow(dead_code)]
156        parent: toasty::Deferred<Parent>,
157    }
158
159    let mut db = test.setup_db(models!(Parent, Child)).await;
160
161    for i in 1..10 {
162        let u = Parent::create()
163            .children([Child::create()])
164            .children([Child::create()])
165            .exec(&mut db)
166            .await?;
167        assert_eq!(u.id, i);
168        assert_eq!(u.children.get()[0].parent_id, i);
169        assert_eq!(u.children.get()[1].parent_id, i);
170        assert_eq!(u.children.get()[0].id, i * 2 - 1);
171        assert_eq!(u.children.get()[1].id, i * 2);
172    }
173    Ok(())
174}