luxid-orm 0.2.0

The data layer for the Luxid web framework
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
//! The data layer exercised against a real SQLite database.
//!
//! SQLite is used so the suite runs on any machine with no provisioning.
//!
//! Running the same tests against Postgres to catch dialect differences is not
//! wired up yet: the schema here uses SQLite's `INTEGER PRIMARY KEY
//! AUTOINCREMENT`, so it needs dialect-aware DDL first.

use luxid_orm::model::{Record, delete_by_id, insert, update};
use luxid_orm::{Db, sea_orm};
use sea_orm::{ActiveValue::Set, ConnectionTrait, IntoActiveModel};

mod users {
    use sea_orm::entity::prelude::*;

    // `crate = ::luxid_orm` because this crate is below the `luxid` facade;
    // applications use the default and never write this.
    #[derive(Clone, Debug, PartialEq, DeriveEntityModel, serde::Serialize, luxid_macros::Model)]
    #[luxid(crate = ::luxid_orm)]
    #[sea_orm(table_name = "users")]
    pub struct Model {
        #[sea_orm(primary_key)]
        pub id: i64,
        pub name: String,
        pub email: String,
        pub team_id: i64,
        pub deleted_at: Option<String>,
    }

    #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
    pub enum Relation {}

    impl ActiveModelBehavior for ActiveModel {}
}

use users::Entity as Users;
use users::Model as User;

const SCHEMA: &str = "
    CREATE TABLE users (
        id          INTEGER PRIMARY KEY AUTOINCREMENT,
        name        TEXT    NOT NULL,
        email       TEXT    NOT NULL,
        team_id     INTEGER NOT NULL,
        deleted_at  TEXT
    );
";

async fn database() -> Db {
    let db = Db::in_memory().await.expect("opens");
    db.connection()
        .execute_unprepared(SCHEMA)
        .await
        .expect("creates the schema");
    db
}

fn new_user(name: &str, email: &str, team_id: i64) -> users::ActiveModel {
    users::ActiveModel {
        name: Set(name.to_owned()),
        email: Set(email.to_owned()),
        team_id: Set(team_id),
        deleted_at: Set(None),
        ..Default::default()
    }
}

async fn seed() -> Db {
    let db = database().await;

    db.scope(async {
        insert(new_user("Ada", "ada@example.com", 1))
            .await
            .expect("inserts");
        insert(new_user("Alan", "alan@example.com", 1))
            .await
            .expect("inserts");
        insert(new_user("Grace", "grace@example.com", 2))
            .await
            .expect("inserts");
    })
    .await;

    db
}

#[tokio::test]
async fn inserting_and_finding_a_row() {
    let db = database().await;

    db.scope(async {
        let created = insert(new_user("Ada", "ada@example.com", 1))
            .await
            .expect("inserts");
        assert_eq!(created.name, "Ada");

        let found = User::find(created.id)
            .await
            .expect("queries")
            .expect("exists");
        assert_eq!(found.email, "ada@example.com");
    })
    .await;
}

#[tokio::test]
async fn find_returns_none_for_a_missing_row() {
    let db = database().await;

    db.scope(async {
        assert!(User::find(999).await.expect("queries").is_none());
    })
    .await;
}

#[tokio::test]
async fn find_or_fail_produces_a_404_naming_the_model_and_id() {
    let db = database().await;

    db.scope(async {
        let err = User::find_or_fail(404).await.unwrap_err();

        assert_eq!(err.status_code().as_u16(), 404);

        let problem = err.problem();
        assert_eq!(problem["resource"], "User");
        assert_eq!(problem["id"], "404");
    })
    .await;
}

#[tokio::test]
async fn filtering_and_ordering() {
    let db = seed().await;

    db.scope(async {
        let team_one = User::query()
            .where_eq(users::Column::TeamId, 1)
            .order_by_desc(users::Column::Name)
            .all()
            .await
            .expect("queries");

        let names: Vec<_> = team_one.iter().map(|user| user.name.as_str()).collect();
        assert_eq!(names, vec!["Alan", "Ada"]);
    })
    .await;
}

#[tokio::test]
async fn counting_and_existence() {
    let db = seed().await;

    db.scope(async {
        assert_eq!(User::count_all().await.expect("counts"), 3);
        assert_eq!(
            User::query()
                .where_eq(users::Column::TeamId, 2)
                .count()
                .await
                .expect("counts"),
            1
        );
        assert!(
            User::query()
                .where_eq(users::Column::Email, "ada@example.com")
                .exists()
                .await
                .expect("queries")
        );
        assert!(
            !User::query()
                .where_eq(users::Column::Email, "nobody@example.com")
                .exists()
                .await
                .expect("queries")
        );
    })
    .await;
}

#[tokio::test]
async fn null_filters() {
    let db = seed().await;

    db.scope(async {
        assert_eq!(
            User::query()
                .where_null(users::Column::DeletedAt)
                .count()
                .await
                .expect("counts"),
            3
        );
        assert_eq!(
            User::query()
                .where_not_null(users::Column::DeletedAt)
                .count()
                .await
                .expect("counts"),
            0
        );
    })
    .await;
}

#[tokio::test]
async fn pagination_reports_totals_and_pages() {
    let db = seed().await;

    db.scope(async {
        let first = User::query()
            .order_by_asc(users::Column::Id)
            .paginate(1, 2)
            .await
            .expect("paginates");

        assert_eq!(first.data.len(), 2);
        assert_eq!(first.total, 3);
        assert_eq!(first.last_page, 2);
        assert_eq!(first.page, 1);
        assert!(first.has_more());

        let second = User::query()
            .order_by_asc(users::Column::Id)
            .paginate(2, 2)
            .await
            .expect("paginates");

        assert_eq!(second.data.len(), 1);
        assert_eq!(second.data[0].name, "Grace");
        assert!(!second.has_more());
    })
    .await;
}

#[tokio::test]
async fn pagination_is_one_based_and_clamps_nonsense() {
    let db = seed().await;

    db.scope(async {
        // Page 0 and per_page 0 are user input, not a reason to panic or to
        // silently return nothing.
        let page = User::query().paginate(0, 0).await.expect("paginates");

        assert_eq!(page.page, 1);
        assert_eq!(page.per_page, 1);
        assert_eq!(page.total, 3);
        assert_eq!(page.last_page, 3);
    })
    .await;
}

#[tokio::test]
async fn pagination_past_the_end_is_empty_rather_than_an_error() {
    let db = seed().await;

    db.scope(async {
        let page = User::query().paginate(99, 10).await.expect("paginates");

        assert!(page.is_empty());
        assert_eq!(page.total, 3);
    })
    .await;
}

#[tokio::test]
async fn updating_a_row() {
    let db = seed().await;

    db.scope(async {
        let user = User::query()
            .where_eq(users::Column::Email, "ada@example.com")
            .first()
            .await
            .expect("queries")
            .expect("exists");

        let mut active = user.into_active_model();
        active.name = Set("Ada Lovelace".to_owned());

        let saved = update(active).await.expect("updates");
        assert_eq!(saved.name, "Ada Lovelace");

        let reloaded = User::find_or_fail(saved.id).await.expect("exists");
        assert_eq!(reloaded.name, "Ada Lovelace");
    })
    .await;
}

#[tokio::test]
async fn deleting_a_row() {
    let db = seed().await;

    db.scope(async {
        let user = User::query()
            .first()
            .await
            .expect("queries")
            .expect("exists");

        assert!(delete_by_id::<Users>(user.id).await.expect("deletes"));
        assert!(User::find(user.id).await.expect("queries").is_none());

        // Deleting again removes nothing, and says so rather than erroring.
        assert!(!delete_by_id::<Users>(user.id).await.expect("deletes"));
    })
    .await;
}

#[tokio::test]
async fn a_rollback_scope_leaves_nothing_behind() {
    let db = database().await;

    db.rollback_scope(async || {
        insert(new_user("Temporary", "temp@example.com", 1))
            .await
            .expect("inserts");
        assert_eq!(User::count_all().await.expect("counts"), 1);
    })
    .await
    .expect("rolls back");

    db.scope(async {
        assert_eq!(
            User::count_all().await.expect("counts"),
            0,
            "the row was rolled back"
        );
    })
    .await;
}

#[tokio::test]
async fn a_transaction_commits_on_success() {
    let db = database().await;

    db.transaction(async || {
        insert(new_user("Kept", "kept@example.com", 1)).await?;
        Ok(())
    })
    .await
    .expect("commits");

    db.scope(async {
        assert_eq!(User::count_all().await.expect("counts"), 1);
    })
    .await;
}

#[tokio::test]
async fn a_transaction_rolls_back_on_error() {
    let db = database().await;

    let outcome: luxid_core::Result<()> = db
        .transaction(async || {
            insert(new_user("Doomed", "doomed@example.com", 1)).await?;
            Err(luxid_core::Error::Conflict("changed my mind".into()))
        })
        .await;

    assert!(outcome.is_err());

    db.scope(async {
        assert_eq!(
            User::count_all().await.expect("counts"),
            0,
            "the insert was undone"
        );
    })
    .await;
}

#[tokio::test]
async fn querying_outside_a_scope_explains_itself() {
    let err = User::count_all().await.unwrap_err();
    let message = format!("{err}");

    assert!(
        message.contains("no database connection is in scope"),
        "{message}"
    );
    assert!(message.contains("tokio::spawn"), "{message}");
}

#[tokio::test]
async fn a_detached_task_does_not_inherit_the_scope() {
    let db = database().await;

    let outcome = db
        .scope(async {
            tokio::spawn(async { User::count_all().await })
                .await
                .expect("joins")
        })
        .await;

    // Reported, not silently routed to a different connection.
    assert!(outcome.is_err());
}

/// Regression: a scope nested inside a transaction must join it rather than
/// reach for the pool. With a single-connection pool, reaching for the pool
/// deadlocks against the connection the transaction holds.
#[tokio::test]
async fn a_nested_scope_joins_the_open_transaction() {
    let db = database().await;

    db.rollback_scope(async || {
        insert(new_user("Inside", "inside@example.com", 1))
            .await
            .expect("inserts");

        // Stands in for what the request middleware does per request.
        let seen = if luxid_orm::db::current().is_ok() {
            User::count_all().await.expect("counts")
        } else {
            db.scope(async { User::count_all().await.expect("counts") })
                .await
        };

        assert_eq!(seen, 1, "the nested scope sees the transaction's write");
    })
    .await
    .expect("rolls back");

    db.scope(async {
        assert_eq!(User::count_all().await.expect("counts"), 0);
    })
    .await;
}