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(id(ID), requires(auto_increment))]
62pub async fn auto_increment_implicit(test: &mut Test) -> Result<()> {
63    #[derive(toasty::Model)]
64    struct Item {
65        #[key]
66        #[auto]
67        auto_field: u32,
68    }
69
70    let mut db = test.setup_db(models!(Item)).await;
71
72    for i in 1..10 {
73        let u = Item::create().exec(&mut db).await?;
74        assert_eq!(u.auto_field, i);
75    }
76    Ok(())
77}
78
79// Test that auto-increment with composite primary keys is rejected
80// This only applies to numeric types since UUID uses a different auto strategy
81#[driver_test(requires(auto_increment))]
82pub async fn auto_increment_with_composite_key_errors(test: &mut Test) {
83    #[derive(toasty::Model)]
84    #[key(partition = user_id, local = id)]
85    struct InvalidModel {
86        #[auto(increment)]
87        id: u64,
88
89        user_id: u64,
90    }
91
92    // This should fail during schema setup
93    let result = test.try_setup_db(models!(InvalidModel)).await;
94
95    assert!(result.is_err(), "Expected schema setup to fail");
96    let err = result.unwrap_err();
97    assert!(
98        err.to_string()
99            .contains("cannot be used with composite primary keys"),
100        "Expected error message about composite keys, got: {}",
101        err
102    );
103}
104
105// Foreign key IDs passed to assocations depend on the auto-increment ID generated by the database,
106// we want to make sure this works.
107#[driver_test(id(ID), requires(auto_increment))]
108pub async fn auto_increment_with_associations(test: &mut Test) -> Result<()> {
109    #[derive(toasty::Model)]
110    struct Parent {
111        #[key]
112        #[auto(increment)]
113        id: u32,
114
115        #[has_many]
116        children: toasty::HasMany<Child>,
117    }
118
119    #[derive(toasty::Model)]
120    struct Child {
121        #[key]
122        #[auto(increment)]
123        id: u32,
124
125        #[index]
126        parent_id: u32,
127
128        #[belongs_to(key = parent_id, references = id)]
129        #[allow(dead_code)]
130        parent: toasty::BelongsTo<Parent>,
131    }
132
133    let mut db = test.setup_db(models!(Parent, Child)).await;
134
135    for i in 1..10 {
136        let u = Parent::create()
137            .child(Child::create())
138            .child(Child::create())
139            .exec(&mut db)
140            .await?;
141        assert_eq!(u.id, i);
142        assert_eq!(u.children.get()[0].parent_id, i);
143        assert_eq!(u.children.get()[1].parent_id, i);
144        assert_eq!(u.children.get()[0].id, i * 2 - 1);
145        assert_eq!(u.children.get()[1].id, i * 2);
146    }
147    Ok(())
148}