toasty-driver-integration-suite 0.5.0

Integration test suite for Toasty database drivers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
use crate::prelude::*;

/// Tests basic CRUD with a unit enum using explicit string discriminants.
#[driver_test(id(ID))]
pub async fn string_discriminant_unit_enum(t: &mut Test) -> Result<()> {
    #[derive(Debug, PartialEq, toasty::Embed)]
    enum Status {
        #[column(variant = "pending")]
        Pending,
        #[column(variant = "active")]
        Active,
        #[column(variant = "done")]
        Done,
    }

    #[derive(Debug, toasty::Model)]
    struct Task {
        #[key]
        #[auto]
        id: ID,
        title: String,
        status: Status,
    }

    let mut db = t.setup_db(models!(Task, Status)).await;

    let task = toasty::create!(Task {
        title: "Ship it",
        status: Status::Pending,
    })
    .exec(&mut db)
    .await?;
    assert_eq!(task.status, Status::Pending);

    let found = Task::get_by_id(&mut db, &task.id).await?;
    assert_eq!(found.status, Status::Pending);

    // Update and re-read
    let mut task = found;
    task.update().status(Status::Active).exec(&mut db).await?;
    let found = Task::get_by_id(&mut db, &task.id).await?;
    assert_eq!(found.status, Status::Active);

    Ok(())
}

/// Tests unit enum with default labels (variant ident used as string label).
#[driver_test(id(ID))]
pub async fn default_string_labels(t: &mut Test) -> Result<()> {
    #[derive(Debug, PartialEq, toasty::Embed)]
    enum Priority {
        Low,
        Medium,
        High,
    }

    #[derive(Debug, toasty::Model)]
    struct Task {
        #[key]
        #[auto]
        id: ID,
        title: String,
        priority: Priority,
    }

    let mut db = t.setup_db(models!(Task, Priority)).await;

    let task = toasty::create!(Task {
        title: "Fix bug",
        priority: Priority::High,
    })
    .exec(&mut db)
    .await?;
    assert_eq!(task.priority, Priority::High);

    let found = Task::get_by_id(&mut db, &task.id).await?;
    assert_eq!(found.priority, Priority::High);

    Ok(())
}

/// Tests mixing explicit string labels with default labels.
#[driver_test(id(ID))]
pub async fn mixed_explicit_and_default_labels(t: &mut Test) -> Result<()> {
    #[derive(Debug, PartialEq, toasty::Embed)]
    enum Status {
        #[column(variant = "waiting")]
        Pending,
        Active,
        Done,
    }

    #[derive(Debug, toasty::Model)]
    struct Task {
        #[key]
        #[auto]
        id: ID,
        status: Status,
    }

    let mut db = t.setup_db(models!(Task, Status)).await;

    // "waiting" is the explicit label for Pending
    let t1 = toasty::create!(Task {
        status: Status::Pending
    })
    .exec(&mut db)
    .await?;
    assert_eq!(t1.status, Status::Pending);

    // "Active" is the default label
    let t2 = toasty::create!(Task {
        status: Status::Active
    })
    .exec(&mut db)
    .await?;

    let found1 = Task::get_by_id(&mut db, &t1.id).await?;
    let found2 = Task::get_by_id(&mut db, &t2.id).await?;
    assert_eq!(found1.status, Status::Pending);
    assert_eq!(found2.status, Status::Active);

    Ok(())
}

/// Tests data-carrying enum with string discriminants.
#[driver_test(id(ID))]
pub async fn string_discriminant_data_enum(t: &mut Test) -> Result<()> {
    #[derive(Debug, PartialEq, toasty::Embed)]
    enum ContactMethod {
        #[column(variant = "email")]
        Email { address: String },
        #[column(variant = "phone")]
        Phone { number: String },
    }

    #[derive(Debug, toasty::Model)]
    #[allow(dead_code)]
    struct User {
        #[key]
        #[auto]
        id: ID,
        name: String,
        contact: ContactMethod,
    }

    let mut db = t.setup_db(models!(User, ContactMethod)).await;

    let user = toasty::create!(User {
        name: "Alice",
        contact: ContactMethod::Email {
            address: "alice@example.com".into(),
        },
    })
    .exec(&mut db)
    .await?;

    let found = User::get_by_id(&mut db, &user.id).await?;
    assert_eq!(
        found.contact,
        ContactMethod::Email {
            address: "alice@example.com".into()
        }
    );

    // Update to a different variant
    let mut user = found;
    user.update()
        .contact(ContactMethod::Phone {
            number: "555-0100".into(),
        })
        .exec(&mut db)
        .await?;

    let found = User::get_by_id(&mut db, &user.id).await?;
    assert_eq!(
        found.contact,
        ContactMethod::Phone {
            number: "555-0100".into()
        }
    );

    Ok(())
}

/// Tests data-carrying enum with default string labels (variant ident as discriminant).
#[driver_test(id(ID))]
pub async fn default_string_labels_data_enum(t: &mut Test) -> Result<()> {
    #[derive(Debug, PartialEq, toasty::Embed)]
    enum ContactMethod {
        Email { address: String },
        Phone { number: String },
    }

    #[derive(Debug, toasty::Model)]
    #[allow(dead_code)]
    struct User {
        #[key]
        #[auto]
        id: ID,
        name: String,
        contact: ContactMethod,
    }

    let mut db = t.setup_db(models!(User, ContactMethod)).await;

    let user = toasty::create!(User {
        name: "Alice",
        contact: ContactMethod::Email {
            address: "alice@example.com".into(),
        },
    })
    .exec(&mut db)
    .await?;

    let found = User::get_by_id(&mut db, &user.id).await?;
    assert_eq!(
        found.contact,
        ContactMethod::Email {
            address: "alice@example.com".into()
        }
    );

    // Update to a different variant
    let mut user = found;
    user.update()
        .contact(ContactMethod::Phone {
            number: "555-0100".into(),
        })
        .exec(&mut db)
        .await?;

    let found = User::get_by_id(&mut db, &user.id).await?;
    assert_eq!(
        found.contact,
        ContactMethod::Phone {
            number: "555-0100".into()
        }
    );

    Ok(())
}

/// Tests data-carrying enum mixing explicit string labels with defaults.
#[driver_test(id(ID))]
pub async fn mixed_string_labels_data_enum(t: &mut Test) -> Result<()> {
    #[derive(Debug, PartialEq, toasty::Embed)]
    enum ContactMethod {
        #[column(variant = "mail")]
        Email {
            address: String,
        },
        Phone {
            number: String,
        },
    }

    #[derive(Debug, toasty::Model)]
    #[allow(dead_code)]
    struct User {
        #[key]
        #[auto]
        id: ID,
        name: String,
        contact: ContactMethod,
    }

    let mut db = t.setup_db(models!(User, ContactMethod)).await;

    // Create with the explicit-label variant
    let u1 = toasty::create!(User {
        name: "Alice",
        contact: ContactMethod::Email {
            address: "alice@example.com".into(),
        },
    })
    .exec(&mut db)
    .await?;

    // Create with the default-label variant
    let u2 = toasty::create!(User {
        name: "Bob",
        contact: ContactMethod::Phone {
            number: "555-0200".into(),
        },
    })
    .exec(&mut db)
    .await?;

    let found1 = User::get_by_id(&mut db, &u1.id).await?;
    assert_eq!(
        found1.contact,
        ContactMethod::Email {
            address: "alice@example.com".into()
        }
    );

    let found2 = User::get_by_id(&mut db, &u2.id).await?;
    assert_eq!(
        found2.contact,
        ContactMethod::Phone {
            number: "555-0200".into()
        }
    );

    // Update from explicit-label variant to default-label variant
    let mut user = found1;
    user.update()
        .contact(ContactMethod::Phone {
            number: "555-0300".into(),
        })
        .exec(&mut db)
        .await?;

    let found = User::get_by_id(&mut db, &user.id).await?;
    assert_eq!(
        found.contact,
        ContactMethod::Phone {
            number: "555-0300".into()
        }
    );

    Ok(())
}

/// Tests filtering by variant with string discriminants.
#[driver_test(requires(sql))]
pub async fn filter_by_string_variant(t: &mut Test) -> Result<()> {
    #[derive(Debug, PartialEq, toasty::Embed)]
    enum Status {
        #[column(variant = "pending")]
        Pending,
        #[column(variant = "active")]
        Active,
    }

    #[derive(Debug, toasty::Model)]
    #[allow(dead_code)]
    struct Task {
        #[key]
        #[auto]
        id: uuid::Uuid,
        title: String,
        status: Status,
    }

    let mut db = t.setup_db(models!(Task, Status)).await;

    toasty::create!(Task {
        title: "A",
        status: Status::Pending
    })
    .exec(&mut db)
    .await?;

    toasty::create!(Task {
        title: "B",
        status: Status::Active
    })
    .exec(&mut db)
    .await?;

    let pending = Task::filter(Task::fields().status().is_pending())
        .exec(&mut db)
        .await?;
    assert_eq!(pending.len(), 1);
    assert_eq!(pending[0].title, "A");

    Ok(())
}

/// Verifies the schema registers string discriminants with the correct type.
#[driver_test]
pub async fn string_discriminant_schema_registration(t: &mut Test) {
    #[derive(Debug, PartialEq, toasty::Embed)]
    enum Color {
        #[column(variant = "red")]
        Red,
        #[column(variant = "green")]
        Green,
        #[column(variant = "blue")]
        Blue,
    }

    let db = t.setup_db(models!(Color)).await;
    let schema = db.schema();

    let color_model = schema.app.model(Color::id()).as_embedded_enum_unwrap();
    assert_eq!(color_model.discriminant.ty, toasty_core::stmt::Type::String);
    assert_eq!(color_model.variants.len(), 3);
    assert_eq!(
        color_model.variants[0].discriminant,
        toasty_core::stmt::Value::String("red".to_string())
    );
    assert_eq!(
        color_model.variants[1].discriminant,
        toasty_core::stmt::Value::String("green".to_string())
    );
    assert_eq!(
        color_model.variants[2].discriminant,
        toasty_core::stmt::Value::String("blue".to_string())
    );
}