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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
use toasty::schema::mapping::{self, FieldPrimitive, FieldStruct};

#[allow(unused_imports)]
use crate::prelude::*;

/// Tests that a newtype embedded struct (`struct Email(String)`) is registered
/// in the app schema with `app: None` on its inner field.
#[driver_test]
pub async fn basic_newtype_embed(test: &mut Test) {
    #[derive(Debug, toasty::Embed)]
    struct Email(String);

    let db = test.setup_db(models!(Email)).await;
    let schema = db.schema();

    assert_struct!(schema.app.models, #{
        Email::id(): toasty::schema::app::Model::EmbeddedStruct({
            name.upper_camel_case(): "Email",
            fields: [
                { name.app: None },
            ],
        }),
    });

    assert!(schema.db.tables.is_empty());
}

/// Tests that a newtype field produces a single column whose name matches the
/// parent field — `email: Email` where `struct Email(String)` produces column
/// `email`, not `email_0`.
#[driver_test(requires(sql))]
pub async fn newtype_column_name(test: &mut Test) {
    #[derive(Debug, toasty::Embed)]
    struct Email(String);

    #[derive(Debug, toasty::Model)]
    struct User {
        #[key]
        id: String,
        #[allow(dead_code)]
        email: Email,
    }

    let db = test.setup_db(models!(User, Email)).await;
    let schema = db.schema();

    assert_struct!(schema.db.tables, [
        {
            name: =~ r"users$",
            columns: [
                { name: "id" },
                { name: "email" },
            ],
        },
    ]);

    let user = &schema.app.models[&User::id()];
    let user_mapping = &schema.mapping.models[&User::id()];
    let user_table = schema.table_for(user);

    assert_struct!(user_mapping, {
        columns.len(): 2,
        fields: [
            mapping::Field::Primitive(FieldPrimitive {
                column: == user_table.columns[0].id,
                ..
            }),
            mapping::Field::Struct(FieldStruct {
                fields: [
                    mapping::Field::Primitive(FieldPrimitive {
                        column: == user_table.columns[1].id,
                        ..
                    }),
                ],
                ..
            }),
        ],
    });
}

/// Tests create, read-back, eq filter, update, delete-by-filter, and batch
/// create — all with the same `Email(String)` newtype model.
#[driver_test(id(ID), requires(sql))]
pub async fn crud_newtype_embed(t: &mut Test) -> Result<()> {
    #[derive(Debug, toasty::Embed)]
    struct Email(String);

    #[derive(Debug, toasty::Model)]
    struct User {
        #[key]
        #[auto]
        id: ID,
        name: String,
        email: Email,
    }

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

    // Create + read back
    let mut user = toasty::create!(User {
        name: "Alice",
        email: Email("alice@example.com".into()),
    })
    .exec(&mut db)
    .await?;

    assert_eq!(user.name, "Alice");
    assert_eq!(user.email.0, "alice@example.com");

    let found = User::get_by_id(&mut db, &user.id).await?;
    assert_eq!(found.email.0, "alice@example.com");

    // Update
    user.update()
        .email(Email("new@example.com".into()))
        .exec(&mut db)
        .await?;

    let found = User::get_by_id(&mut db, &user.id).await?;
    assert_eq!(found.email.0, "new@example.com");

    // Eq filter
    toasty::create!(User {
        name: "Bob",
        email: Email("bob@example.com".into()),
    })
    .exec(&mut db)
    .await?;

    let users = User::filter(User::fields().email().eq(Email("bob@example.com".into())))
        .exec(&mut db)
        .await?;
    assert_eq!(users.len(), 1);
    assert_eq!(users[0].name, "Bob");

    // Delete by filter
    User::filter(User::fields().email().eq(Email("bob@example.com".into())))
        .delete()
        .exec(&mut db)
        .await?;

    let all = User::all().exec(&mut db).await?;
    assert_eq!(all.len(), 1);
    assert_eq!(all[0].name, "Alice");

    // Batch create
    User::create_many()
        .item(
            User::create()
                .name("Carol")
                .email(Email("carol@example.com".into())),
        )
        .item(
            User::create()
                .name("Dave")
                .email(Email("dave@example.com".into())),
        )
        .exec(&mut db)
        .await?;

    let all = User::all().exec(&mut db).await?;
    assert_eq!(all.len(), 3);

    Ok(())
}

// TODO: ne(), gt(), lt(), ge(), le() are not yet generated for newtype field
// structs. Add comparison operator tests once codegen is extended.

/// Tests `#[unique]` on a newtype field generates `get_by_*` and enforces
/// uniqueness.
#[driver_test(id(ID), requires(sql))]
pub async fn newtype_unique_constraint(t: &mut Test) -> Result<()> {
    #[derive(Debug, toasty::Embed)]
    struct Email(String);

    #[derive(Debug, toasty::Model)]
    struct User {
        #[key]
        #[auto]
        id: ID,
        name: String,
        #[unique]
        email: Email,
    }

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

    toasty::create!(User {
        name: "Alice",
        email: Email("alice@example.com".into()),
    })
    .exec(&mut db)
    .await?;

    // Duplicate email should fail
    assert_err!(
        toasty::create!(User {
            name: "Bob",
            email: Email("alice@example.com".into()),
        })
        .exec(&mut db)
        .await
    );

    // get_by_email should work
    let found = User::get_by_email(&mut db, Email("alice@example.com".into())).await?;
    assert_eq!(found.name, "Alice");

    Ok(())
}

/// Tests `#[index]` on a newtype field generates `filter_by_*`.
#[driver_test(id(ID), requires(sql))]
pub async fn newtype_index(t: &mut Test) -> Result<()> {
    #[derive(Debug, toasty::Embed)]
    struct Email(String);

    #[derive(Debug, toasty::Model)]
    struct User {
        #[key]
        #[auto]
        id: ID,
        name: String,
        #[index]
        email: Email,
    }

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

    toasty::create!(User {
        name: "Alice",
        email: Email("alice@example.com".into()),
    })
    .exec(&mut db)
    .await?;

    toasty::create!(User {
        name: "Bob",
        email: Email("bob@example.com".into()),
    })
    .exec(&mut db)
    .await?;

    let users = User::filter_by_email(Email("alice@example.com".into()))
        .exec(&mut db)
        .await?;

    assert_eq!(users.len(), 1);
    assert_eq!(users[0].name, "Alice");

    Ok(())
}

/// Tests a newtype wrapping a numeric type with CRUD and eq filter.
#[driver_test(id(ID), requires(sql))]
pub async fn newtype_numeric(t: &mut Test) -> Result<()> {
    #[derive(Debug, toasty::Embed)]
    struct Score(i64);

    #[derive(Debug, toasty::Model)]
    struct Player {
        #[key]
        #[auto]
        id: ID,
        name: String,
        score: Score,
    }

    let mut db = t.setup_db(models!(Player, Score)).await;

    toasty::create!(Player {
        name: "Alice",
        score: Score(100)
    })
    .exec(&mut db)
    .await?;
    toasty::create!(Player {
        name: "Bob",
        score: Score(200)
    })
    .exec(&mut db)
    .await?;

    let found = Player::filter(Player::fields().score().eq(Score(200)))
        .exec(&mut db)
        .await?;
    assert_eq!(found.len(), 1);
    assert_eq!(found[0].name, "Bob");

    Ok(())
}

/// Tests using a newtype as the primary key field.
#[driver_test(requires(sql))]
pub async fn newtype_as_primary_key(t: &mut Test) -> Result<()> {
    #[derive(Debug, toasty::Embed)]
    struct UserId(String);

    #[derive(Debug, toasty::Model)]
    struct User {
        #[key]
        id: UserId,
        name: String,
    }

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

    let user = toasty::create!(User {
        id: UserId("user-1".into()),
        name: "Alice",
    })
    .exec(&mut db)
    .await?;

    assert_eq!(user.id.0, "user-1");

    let found = User::get_by_id(&mut db, &UserId("user-1".into())).await?;
    assert_eq!(found.name, "Alice");

    Ok(())
}

/// Tests newtype nested inside an embedded struct: create, read-back, and
/// filter by the nested newtype field.
#[driver_test(id(ID), requires(sql))]
pub async fn nested_newtype(t: &mut Test) -> Result<()> {
    #[derive(Debug, toasty::Embed)]
    struct ZipCode(String);

    #[derive(Debug, toasty::Embed)]
    struct Address {
        city: String,
        zip: ZipCode,
    }

    #[derive(Debug, toasty::Model)]
    struct User {
        #[key]
        #[auto]
        id: ID,
        name: String,
        address: Address,
    }

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

    // Create + read back
    let user = toasty::create!(User {
        name: "Alice",
        address: Address {
            city: "Seattle".into(),
            zip: ZipCode("98101".into()),
        },
    })
    .exec(&mut db)
    .await?;

    let found = User::get_by_id(&mut db, &user.id).await?;
    assert_eq!(found.address.city, "Seattle");
    assert_eq!(found.address.zip.0, "98101");

    // Filter by nested newtype
    toasty::create!(User {
        name: "Bob",
        address: Address {
            city: "Portland".into(),
            zip: ZipCode("97201".into()),
        },
    })
    .exec(&mut db)
    .await?;

    let users = User::filter(User::fields().address().zip().eq(ZipCode("98101".into())))
        .exec(&mut db)
        .await?;

    assert_eq!(users.len(), 1);
    assert_eq!(users[0].name, "Alice");

    Ok(())
}

/// Tests a newtype used as the partition key and in a filter predicate.
/// No `requires(sql)` — this must work on all drivers including DynamoDB.
#[driver_test]
pub async fn newtype_key_and_predicate(t: &mut Test) -> Result<()> {
    #[derive(Debug, toasty::Embed)]
    struct Region(String);

    #[derive(Debug, toasty::Model)]
    #[key(partition = region, local = id)]
    struct Item {
        #[auto]
        id: uuid::Uuid,
        region: Region,
        name: String,
    }

    let mut db = t.setup_db(models!(Item, Region)).await;

    toasty::create!(Item {
        region: Region("us-east".into()),
        name: "Alpha",
    })
    .exec(&mut db)
    .await?;

    toasty::create!(Item {
        region: Region("us-east".into()),
        name: "Beta",
    })
    .exec(&mut db)
    .await?;

    toasty::create!(Item {
        region: Region("eu-west".into()),
        name: "Gamma",
    })
    .exec(&mut db)
    .await?;

    // Filter by newtype partition key
    let items = Item::filter(Item::fields().region().eq(Region("us-east".into())))
        .exec(&mut db)
        .await?;

    assert_eq!(items.len(), 2);
    let mut names: Vec<_> = items.iter().map(|i| i.name.as_str()).collect();
    names.sort();
    assert_eq!(names, ["Alpha", "Beta"]);

    Ok(())
}