toasty_driver_integration_suite/tests/
field_auto.rs1use crate::prelude::*;
2
3#[driver_test]
4pub async fn auto_uuid_v4(test: &mut Test) -> Result<()> {
5 #[derive(toasty::Model)]
6 struct Item {
7 #[key]
8 #[auto]
9 id: uuid::Uuid,
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 assert!(uuid::Uuid::parse_str(&u.auto_field.to_string()).is_ok());
20 Ok(())
21}
22
23#[driver_test]
24pub async fn auto_uuid_v7(test: &mut Test) -> Result<()> {
25 #[derive(toasty::Model)]
26 struct Item {
27 #[key]
28 #[auto]
29 id: uuid::Uuid,
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 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(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#[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 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#[driver_test(requires(and(auto_increment, returning_from_mutation)))]
134pub async fn auto_increment_with_associations(test: &mut Test) -> Result<()> {
135 #[derive(Debug, 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(Debug, 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}
175
176#[driver_test(requires(auto_increment))]
177pub async fn auto_increment_with_single_association(test: &mut Test) -> Result<()> {
178 #[derive(toasty::Model)]
179 struct Parent {
180 #[key]
181 #[auto]
182 id: u32,
183
184 #[has_many]
185 children: toasty::Deferred<Vec<Child>>,
186 }
187
188 #[derive(toasty::Model)]
189 struct Child {
190 #[key]
191 #[auto]
192 id: u32,
193
194 #[index]
195 parent_id: u32,
196
197 #[belongs_to(key = parent_id, references = id)]
198 #[allow(dead_code)]
199 parent: toasty::Deferred<Parent>,
200 }
201
202 let mut db = test.setup_db(models!(Parent, Child)).await;
203
204 for id in 1..4 {
205 let parent = Parent::create()
206 .children([Child::create()])
207 .exec(&mut db)
208 .await?;
209
210 assert_eq!(parent.id, id);
211 assert_eq!(parent.children.get()[0].id, id);
212 assert_eq!(parent.children.get()[0].parent_id, id);
213 }
214
215 Ok(())
216}
217
218#[driver_test(requires(and(auto_increment, not(returning_from_mutation))))]
219pub async fn auto_increment_parent_with_batch_associations(test: &mut Test) -> Result<()> {
220 #[derive(Debug, toasty::Model)]
221 struct Parent {
222 #[key]
223 #[auto]
224 id: u32,
225
226 #[has_many]
227 children: toasty::Deferred<Vec<Child>>,
228 }
229
230 #[derive(Debug, toasty::Model)]
231 struct Child {
232 #[key]
233 #[auto]
234 id: uuid::Uuid,
235
236 #[index]
237 parent_id: u32,
238
239 #[belongs_to(key = parent_id, references = id)]
240 #[allow(dead_code)]
241 parent: toasty::Deferred<Parent>,
242 }
243
244 let mut db = test.setup_db(models!(Parent, Child)).await;
245 let parent = Parent::create()
246 .children([Child::create(), Child::create()])
247 .exec(&mut db)
248 .await?;
249
250 assert_eq!(parent.id, 1);
251 assert_struct!(parent.children.get(), [
252 { parent_id: 1 },
253 { parent_id: 1 },
254 ]);
255 let ids = parent
256 .children
257 .get()
258 .iter()
259 .map(|child| child.id)
260 .collect::<Vec<_>>();
261 assert_unique!(ids);
262
263 Ok(())
264}