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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
use crate::prelude::*;

#[driver_test(id(ID), scenario(crate::scenarios::has_one_optional_belongs_to))]
pub async fn crud_has_one_bi_direction_optional(test: &mut Test) -> Result<()> {
    let mut db = setup(test).await;

    // Create a user without a profile
    let user = User::create().name("Jane Doe").exec(&mut db).await?;

    // No profile
    assert_none!(user.profile().exec(&mut db).await?);

    // Create a profile for the user
    let profile = user
        .profile()
        .create()
        .bio("a person")
        .exec(&mut db)
        .await?;

    // Load the profile
    let profile_reload = user.profile().exec(&mut db).await?.unwrap();
    assert_eq!(profile.id, profile_reload.id);

    // Load the user via the profile
    let user_reload = profile.user().exec(&mut db).await?.unwrap();
    assert_eq!(user.id, user_reload.id);

    // Create a new user with a profile
    let mut user = User::create()
        .name("Tim Apple")
        .profile(Profile::create().bio("an apple a day"))
        .exec(&mut db)
        .await?;

    let profile = user.profile().exec(&mut db).await?.unwrap();
    assert_eq!(profile.bio, "an apple a day");

    // The new profile is associated with the user
    assert_eq!(user.id, profile.user().exec(&mut db).await?.unwrap().id);

    // Update a user, creating a new profile.
    user.update()
        .profile(Profile::create().bio("keeps the doctor away"))
        .exec(&mut db)
        .await?;

    // The user's profile is updated
    let profile = user.profile().exec(&mut db).await?.unwrap();
    assert_eq!(profile.bio, "keeps the doctor away");
    assert_eq!(user.id, profile.user().exec(&mut db).await?.unwrap().id);

    // Unset the profile via an update. This will nullify user on the profile.
    user.update().profile(None).exec(&mut db).await?;

    // The profile is none
    assert!(user.profile().exec(&mut db).await?.is_none());

    let profile_reloaded = Profile::filter_by_id(profile.id).get(&mut db).await?;
    assert_none!(profile_reloaded.user_id);

    user.update()
        .profile(&profile_reloaded)
        .exec(&mut db)
        .await?;

    let profile_reloaded = Profile::get_by_id(&mut db, &profile.id).await?;
    assert_eq!(&user.id, profile_reloaded.user_id.as_ref().unwrap());

    // Deleting the profile will nullify the profile field for the user
    profile_reloaded.delete().exec(&mut db).await?;

    let mut user_reloaded = User::get_by_id(&mut db, &user.id).await?;
    assert_none!(user_reloaded.profile().exec(&mut db).await?);

    // Create a new profile for the user
    user_reloaded
        .update()
        .profile(Profile::create().bio("hello"))
        .exec(&mut db)
        .await?;

    let profile_id = user_reloaded.profile().exec(&mut db).await?.unwrap().id;

    // Delete the user
    user_reloaded.delete().exec(&mut db).await?;

    let profile_reloaded = Profile::get_by_id(&mut db, &profile_id).await?;
    assert_none!(profile_reloaded.user_id);
    Ok(())
}

#[driver_test(id(ID))]
#[should_panic]
pub async fn crud_has_one_required_belongs_to_optional(test: &mut Test) -> Result<()> {
    #[derive(Debug, toasty::Model)]
    struct User {
        #[key]
        #[auto]
        id: ID,

        #[has_one]
        profile: toasty::HasOne<Profile>,
    }

    #[derive(Debug, toasty::Model)]
    struct Profile {
        #[key]
        #[auto]
        id: ID,

        #[unique]
        user_id: Option<ID>,

        #[belongs_to(key = user_id, references = id)]
        user: toasty::BelongsTo<Option<User>>,

        bio: String,
    }

    let mut db = test.setup_db(models!(User, Profile)).await;

    // Create a new user with a profile
    let user = User::create()
        .profile(Profile::create().bio("an apple a day"))
        .exec(&mut db)
        .await?;

    let profile = user.profile().exec(&mut db).await?;
    assert_eq!(profile.bio, "an apple a day");

    // The new profile is associated with the user
    assert_eq!(user.id, profile.user().exec(&mut db).await?.unwrap().id);

    // Deleting the user leaves the profile in place.
    user.delete().exec(&mut db).await?;
    let profile_reloaded = Profile::get_by_id(&mut db, &profile.id).await?;
    assert_none!(profile_reloaded.user_id);

    // Try creating a user **without** a user: error
    assert_err!(User::create().exec(&mut db).await);
    Ok(())
}

#[driver_test(id(ID))]
pub async fn update_belongs_to_with_required_has_one_pair(test: &mut Test) -> Result<()> {
    #[derive(Debug, toasty::Model)]
    struct User {
        #[key]
        #[auto]
        id: ID,

        #[has_one]
        profile: toasty::HasOne<Profile>,
    }

    #[derive(Debug, toasty::Model)]
    struct Profile {
        #[key]
        #[auto]
        id: ID,

        #[unique]
        user_id: Option<ID>,

        #[belongs_to(key = user_id, references = id)]
        user: toasty::BelongsTo<Option<User>>,

        bio: String,
    }

    let mut db = test.setup_db(models!(User, Profile)).await;

    // Create a user with a profile
    let u1 = User::create()
        .profile(Profile::create().bio("an apple a day"))
        .exec(&mut db)
        .await?;

    let mut p1 = u1.profile().exec(&mut db).await?;
    assert_eq!(p1.bio, "an apple a day");

    // Associate the profile with a new user by value
    let u2 = User::create()
        .profile(Profile::create().bio("I plant trees"))
        .exec(&mut db)
        .await?;

    let p2 = u2.profile().exec(&mut db).await?;
    assert_eq!(p2.bio, "I plant trees");

    // Associate the original profile w/ the new user by value
    p1.update().user(&u2).exec(&mut db).await?;

    // assert_eq!(u2.id, p1.user().find(&mut db).await.unwrap().unwrap().id);
    // u1 is deleted
    assert_err!(User::get_by_id(&mut db, &u1.id).await);
    // p2 ID is null
    let p2_reloaded = Profile::get_by_id(&mut db, &p2.id).await?;
    assert_none!(p2_reloaded.user_id);

    /*
    // Associate the profile with a new user by statement
    let u1 = db::User::create()
        .name("Tim Apple")
        .profile(db::Profile::create().bio("an apple a day"))
        .exec(&mut db)
        .await
        .unwrap();

    let mut p1 = u1.profile().exec(&mut db).await.unwrap();
    assert_eq!(p1.bio, "an apple a day");

    /*
    // Associate the profile with a new user by value
    let u2 = db::User::create()
        .name("Johnny Appleseed")
        .profile(db::Profile::create().bio("I plant trees"))
        .exec(&mut db)
        .await
        .unwrap();

    let p2 = u2.profile().exec(&mut db).await.unwrap();
    assert_eq!(p2.bio, "I plant trees");
    */

    // Associate the original profile w/ the new user by value
    p1.update()
        .user(db::User::create().name("Johnny Appleseed"))
        .exec(&mut db)
        .await
        .unwrap();

    assert_eq!(
        u2.id,
        p1.user
            .as_ref()
            .unwrap()
            .get(&mut db)
            .await
            .unwrap()
            .unwrap()
            .id
    );
    // u1 is deleted
    assert_err!(db::User::find_by_id(&u1.id).get(&mut db).await);
    */
    Ok(())
}

#[driver_test(id(ID))]
pub async fn crud_has_one_optional_belongs_to_required(test: &mut Test) -> Result<()> {
    #[derive(Debug, toasty::Model)]
    struct User {
        #[key]
        #[auto]
        id: ID,

        #[has_one]
        profile: toasty::HasOne<Option<Profile>>,
    }

    #[derive(Debug, toasty::Model)]
    struct Profile {
        #[key]
        #[auto]
        id: ID,

        #[unique]
        user_id: ID,

        #[belongs_to(key = user_id, references = id)]
        user: toasty::BelongsTo<User>,

        bio: String,
    }

    let mut db = test.setup_db(models!(User, Profile)).await;

    // Create a new user with a profile
    let user = User::create()
        .profile(Profile::create().bio("an apple a day"))
        .exec(&mut db)
        .await?;

    let profile = user.profile().exec(&mut db).await?.unwrap();
    assert_eq!(profile.bio, "an apple a day");

    // The new profile is associated with the user
    assert_eq!(user.id, profile.user().exec(&mut db).await?.id);

    // Deleting the user also deletes the profile
    user.delete().exec(&mut db).await?;
    assert_err!(Profile::get_by_id(&mut db, &profile.id).await);
    Ok(())
}

#[driver_test(id(ID))]
pub async fn set_has_one_by_value_in_update_query(test: &mut Test) -> Result<()> {
    #[derive(Debug, toasty::Model)]
    struct User {
        #[key]
        #[auto]
        id: ID,

        #[has_one]
        profile: toasty::HasOne<Option<Profile>>,
    }

    #[derive(Debug, toasty::Model)]
    struct Profile {
        #[key]
        #[auto]
        id: ID,

        #[unique]
        user_id: Option<ID>,

        #[belongs_to(key = user_id, references = id)]
        user: toasty::BelongsTo<Option<User>>,
    }

    let mut db = test.setup_db(models!(User, Profile)).await;

    let user = User::create().exec(&mut db).await?;
    let profile = Profile::create().exec(&mut db).await?;

    User::filter_by_id(user.id)
        .update()
        .profile(&profile)
        .exec(&mut db)
        .await?;

    let profile_reload = user.profile().exec(&mut db).await?.unwrap();
    assert_eq!(profile_reload.id, profile.id);

    assert_eq!(profile_reload.user_id.as_ref().unwrap(), &user.id);
    Ok(())
}

#[driver_test(id(ID))]
pub async fn unset_has_one_in_batch_update(test: &mut Test) -> Result<()> {
    #[derive(Debug, toasty::Model)]
    struct User {
        #[key]
        #[auto]
        id: ID,

        #[index]
        name: String,

        #[has_one]
        profile: toasty::HasOne<Option<Profile>>,
    }

    #[derive(Debug, toasty::Model)]
    struct Profile {
        #[key]
        #[auto]
        id: ID,

        #[unique]
        user_id: ID,

        #[belongs_to(key = user_id, references = id)]
        user: toasty::BelongsTo<User>,
    }

    let mut db = test.setup_db(models!(User, Profile)).await;

    // Create two users with the same name, each with a profile
    let u1 = User::create()
        .name("alice")
        .profile(Profile::create())
        .exec(&mut db)
        .await?;
    let p1 = u1.profile().exec(&mut db).await?.unwrap();

    let u2 = User::create()
        .name("alice")
        .profile(Profile::create())
        .exec(&mut db)
        .await?;
    let p2 = u2.profile().exec(&mut db).await?.unwrap();

    // A third user with a different name (should not be affected)
    let u3 = User::create()
        .name("bob")
        .profile(Profile::create())
        .exec(&mut db)
        .await?;

    // Batch update: unset profiles for all users named "alice"
    User::filter_by_name("alice")
        .update()
        .profile(None)
        .exec(&mut db)
        .await?;

    // Both profiles should be deleted (required belongs_to)
    assert_err!(Profile::get_by_id(&mut db, &p1.id).await);
    assert_err!(Profile::get_by_id(&mut db, &p2.id).await);

    // Bob's profile should still exist
    let p3 = u3.profile().exec(&mut db).await?.unwrap();
    assert_eq!(p3.user_id, u3.id);

    Ok(())
}

#[driver_test(id(ID))]
pub async fn unset_has_one_with_required_pair_in_pk_query_update(test: &mut Test) -> Result<()> {
    #[derive(Debug, toasty::Model)]
    struct User {
        #[key]
        #[auto]
        id: ID,

        #[has_one]
        profile: toasty::HasOne<Option<Profile>>,
    }

    #[derive(Debug, toasty::Model)]
    struct Profile {
        #[key]
        #[auto]
        id: ID,

        #[unique]
        user_id: ID,

        #[belongs_to(key = user_id, references = id)]
        user: toasty::BelongsTo<User>,
    }

    let mut db = test.setup_db(models!(User, Profile)).await;

    let user = User::create()
        .profile(Profile::create())
        .exec(&mut db)
        .await?;
    let profile = user.profile().exec(&mut db).await?.unwrap();

    assert_eq!(user.id, profile.user_id);

    User::filter_by_id(user.id)
        .update()
        .profile(None)
        .exec(&mut db)
        .await?;

    // Profile is deleted
    assert_err!(Profile::get_by_id(&mut db, &profile.id).await);
    Ok(())
}

#[driver_test(id(ID))]
pub async fn unset_has_one_with_required_pair_in_non_pk_query_update(
    test: &mut Test,
) -> Result<()> {
    #[derive(Debug, toasty::Model)]
    struct User {
        #[key]
        #[auto]
        id: ID,

        #[unique]
        email: String,

        #[has_one]
        profile: toasty::HasOne<Option<Profile>>,
    }

    #[derive(Debug, toasty::Model)]
    struct Profile {
        #[key]
        #[auto]
        id: ID,

        #[unique]
        user_id: ID,

        #[belongs_to(key = user_id, references = id)]
        user: toasty::BelongsTo<User>,
    }

    let mut db = test.setup_db(models!(User, Profile)).await;

    let user = User::create()
        .email("foo@example.com")
        .profile(Profile::create())
        .exec(&mut db)
        .await?;
    let profile = user.profile().exec(&mut db).await?.unwrap();
    assert_eq!(profile.user_id, user.id);

    User::filter_by_email(&user.email)
        .update()
        .profile(None)
        .exec(&mut db)
        .await?;

    // Profile is deleted
    assert_err!(Profile::get_by_id(&mut db, &profile.id).await);
    Ok(())
}

#[driver_test(id(ID))]
pub async fn associate_has_one_by_val_on_insert(test: &mut Test) -> Result<()> {
    #[derive(Debug, toasty::Model)]
    struct User {
        #[key]
        #[auto]
        id: ID,

        #[has_one]
        profile: toasty::HasOne<Profile>,
    }

    #[derive(Debug, toasty::Model)]
    struct Profile {
        #[key]
        #[auto]
        id: ID,

        #[unique]
        user_id: Option<ID>,

        #[belongs_to(key = user_id, references = id)]
        user: toasty::BelongsTo<Option<User>>,

        bio: String,
    }

    let mut db = test.setup_db(models!(User, Profile)).await;

    // Create a profile
    let profile = Profile::create().bio("hello world").exec(&mut db).await?;

    // Create a user and associate the profile with it, by value
    let u1 = User::create().profile(&profile).exec(&mut db).await?;

    let profile_reloaded = u1.profile().exec(&mut db).await?;
    assert_eq!(profile.id, profile_reloaded.id);
    assert_eq!(Some(&u1.id), profile_reloaded.user_id.as_ref());
    assert_eq!(profile.bio, profile_reloaded.bio);
    Ok(())
}

#[driver_test(id(ID), scenario(crate::scenarios::has_one_optional_belongs_to))]
pub async fn associate_has_one_by_val_on_update_query_with_filter_1(test: &mut Test) -> Result<()> {
    let mut db = setup(test).await;

    let u1 = User::create().name("user 1").exec(&mut db).await?;
    let p1 = Profile::create().bio("hello world").exec(&mut db).await?;

    // Associate profile via filter_by_id update — should work
    User::filter_by_id(u1.id)
        .update()
        .profile(&p1)
        .exec(&mut db)
        .await?;

    let u1_reloaded = User::get_by_id(&mut db, &u1.id).await?;
    let p1_reloaded = u1_reloaded.profile().exec(&mut db).await?.unwrap();
    assert_eq!(p1.id, p1_reloaded.id);
    assert_eq!(p1.bio, p1_reloaded.bio);
    assert_eq!(p1_reloaded.user_id.as_ref(), Some(&u1.id));

    // Unset
    User::filter_by_id(u1.id)
        .update()
        .profile(None)
        .exec(&mut db)
        .await?;

    User::filter_by_id(u1.id)
        .filter(User::fields().name().eq("anon"))
        .update()
        .profile(&p1)
        .exec(&mut db)
        .await?;

    // Verify profile's user_id is still None (update was a no-op)
    let p1_reloaded = Profile::get_by_id(&mut db, &p1.id).await?;
    assert!(p1_reloaded.user_id.is_none());

    Ok(())
}

#[driver_test(id(ID), scenario(crate::scenarios::has_one_optional_belongs_to))]
pub async fn associate_has_one_by_val_on_update_query_with_filter_2(test: &mut Test) -> Result<()> {
    let mut db = setup(test).await;

    let u1 = toasty::create!(User {
        name: "User 1",
        profile: {
            bio: "hello world"
        }
    })
    .exec(&mut db)
    .await?;

    let u2 = toasty::create!(User { name: "User 2" })
        .exec(&mut db)
        .await?;

    let p1 = u1.profile().exec(&mut db).await?.unwrap();
    assert_eq!(p1.user_id.as_ref(), Some(&u1.id));

    User::filter_by_id(u2.id)
        .filter(User::fields().name().eq("anon"))
        .update()
        .profile(&p1)
        .exec(&mut db)
        .await?;

    // Verify profile's user_id still points to u1 (not changed)
    let p1_reloaded = Profile::get_by_id(&mut db, &p1.id).await?;
    assert_eq!(p1_reloaded.user_id.as_ref(), Some(&u1.id));

    Ok(())
}