Skip to main content

toasty_driver_integration_suite/scenarios/
user_account_subscription.rs

1use crate::prelude::*;
2
3scenario! {
4    //! A 2-step `has_one` via chain: `User` → `Account` → `Subscription`.
5    //!
6    //! `User::subscription` is the via (`account.subscription`). Every step is
7    //! a `has_one`, so the via target is a single record — used to test the
8    //! single-result (`query.single`) path of via-include lowering, which the
9    //! all-`has_many` `user_org_project_todo` scenario never exercises.
10
11    #[derive(Debug, toasty::Model)]
12    struct User {
13        #[key]
14        #[auto]
15        id: uuid::Uuid,
16
17        name: String,
18
19        #[has_one]
20        account: toasty::Deferred<Option<Account>>,
21
22        // User → account → subscription, all single (`has_one`) steps.
23        #[has_one(via = account.subscription)]
24        subscription: toasty::Deferred<Option<Subscription>>,
25    }
26
27    #[derive(Debug, toasty::Model)]
28    struct Account {
29        #[key]
30        #[auto]
31        id: uuid::Uuid,
32
33        #[unique]
34        user_id: Option<uuid::Uuid>,
35
36        #[belongs_to(key = user_id, references = id)]
37        user: toasty::Deferred<Option<User>>,
38
39        #[has_one]
40        subscription: toasty::Deferred<Option<Subscription>>,
41    }
42
43    #[derive(Debug, toasty::Model)]
44    struct Subscription {
45        #[key]
46        #[auto]
47        id: uuid::Uuid,
48
49        #[unique]
50        account_id: Option<uuid::Uuid>,
51
52        #[belongs_to(key = account_id, references = id)]
53        account: toasty::Deferred<Option<Account>>,
54
55        plan: String,
56    }
57
58    async fn setup(test: &mut Test) -> toasty::Db {
59        test.setup_db(models!(User, Account, Subscription)).await
60    }
61}