Skip to main content

toasty_driver_integration_suite/scenarios/
user_org_project_todo.rs

1use crate::prelude::*;
2
3scenario! {
4    //! A 3-step has_many via chain: `User` → `Organization` → `Project` → `Todo`.
5    //!
6    //! Used by tests that need a `via` path longer than the 2-step
7    //! `user_comment_article` scenario.
8    //!
9    //! `User::todos` is the flat 3-step via (`organizations.projects.todos`).
10    //! `User::nested_todos` reaches the same todos through `organizations.todos`,
11    //! where `Organization::todos` is itself a via — a via-of-via, used to test
12    //! recursive flattening of a via path whose step names another via.
13
14    #[derive(Debug, toasty::Model)]
15    struct User {
16        #[key]
17        #[auto]
18        id: uuid::Uuid,
19
20        name: String,
21
22        #[has_many]
23        organizations: toasty::Deferred<Vec<Organization>>,
24
25        // User → organizations → projects → todos
26        #[has_many(via = organizations.projects.todos)]
27        todos: toasty::Deferred<Vec<Todo>>,
28
29        // User → organizations → Organization::todos, which is itself a via.
30        // The second step expands into another via path (via-of-via).
31        #[has_many(via = organizations.todos)]
32        nested_todos: toasty::Deferred<Vec<Todo>>,
33
34        // A via-of-via with a *scalar* terminal: the chain routes through
35        // `Organization::todos` (itself a via), then projects `Todo::title`.
36        // Exercises the nested-via splice on the scalar-terminal code paths.
37        #[has_many(via = organizations.todos.title)]
38        nested_todo_titles: toasty::Deferred<Vec<String>>,
39    }
40
41    #[derive(Debug, toasty::Model)]
42    struct Organization {
43        #[key]
44        #[auto]
45        id: uuid::Uuid,
46
47        name: String,
48
49        #[index]
50        user_id: uuid::Uuid,
51
52        #[belongs_to(key = user_id, references = id)]
53        user: toasty::Deferred<User>,
54
55        #[has_many]
56        projects: toasty::Deferred<Vec<Project>>,
57
58        // Organization → projects → todos. The via that `User::nested_todos`
59        // routes through, making that relation a via-of-via.
60        #[has_many(via = projects.todos)]
61        todos: toasty::Deferred<Vec<Todo>>,
62    }
63
64    #[derive(Debug, toasty::Model)]
65    struct Project {
66        #[key]
67        #[auto]
68        id: uuid::Uuid,
69
70        name: String,
71
72        #[index]
73        organization_id: uuid::Uuid,
74
75        #[belongs_to(key = organization_id, references = id)]
76        organization: toasty::Deferred<Organization>,
77
78        #[has_many]
79        todos: toasty::Deferred<Vec<Todo>>,
80    }
81
82    #[derive(Debug, toasty::Model)]
83    struct Todo {
84        #[key]
85        #[auto]
86        id: uuid::Uuid,
87
88        title: String,
89
90        #[index]
91        project_id: uuid::Uuid,
92
93        #[belongs_to(key = project_id, references = id)]
94        project: toasty::Deferred<Project>,
95    }
96
97    async fn setup(test: &mut Test) -> toasty::Db {
98        test.setup_db(models!(User, Organization, Project, Todo)).await
99    }
100}