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    #![id(ID)]
12
13    #[derive(Debug, toasty::Model)]
14    struct User {
15        #[key]
16        #[auto]
17        id: ID,
18
19        name: String,
20
21        #[has_one]
22        account: toasty::Deferred<Option<Account>>,
23
24        // User → account → subscription, all single (`has_one`) steps.
25        #[has_one(via = account.subscription)]
26        subscription: toasty::Deferred<Option<Subscription>>,
27    }
28
29    #[derive(Debug, toasty::Model)]
30    struct Account {
31        #[key]
32        #[auto]
33        id: ID,
34
35        #[unique]
36        user_id: Option<ID>,
37
38        #[belongs_to(key = user_id, references = id)]
39        user: toasty::Deferred<Option<User>>,
40
41        #[has_one]
42        subscription: toasty::Deferred<Option<Subscription>>,
43    }
44
45    #[derive(Debug, toasty::Model)]
46    struct Subscription {
47        #[key]
48        #[auto]
49        id: ID,
50
51        #[unique]
52        account_id: Option<ID>,
53
54        #[belongs_to(key = account_id, references = id)]
55        account: toasty::Deferred<Option<Account>>,
56
57        plan: String,
58    }
59
60    async fn setup(test: &mut Test) -> toasty::Db {
61        test.setup_db(models!(User, Account, Subscription)).await
62    }
63}