Skip to main content

toasty_driver_integration_suite/tests/
paginate_include.rs

1use crate::prelude::*;
2use toasty::stmt::Page;
3
4#[driver_test(requires(sql))]
5pub async fn include_preserves_pagination_cursor(test: &mut Test) -> Result<()> {
6    #[derive(Debug, toasty::Model)]
7    struct Author {
8        #[key]
9        id: i64,
10    }
11
12    #[derive(Debug, toasty::Model)]
13    struct Book {
14        #[key]
15        id: i64,
16        shelf: i64,
17
18        author_id: Option<i64>,
19        #[belongs_to]
20        author: toasty::Deferred<Option<Author>>,
21    }
22
23    let mut db = test.setup_db(models!(Author, Book)).await;
24    let author = toasty::create!(Author { id: 1 }).exec(&mut db).await?;
25    toasty::create!(Book::[
26        { id: 1, shelf: 1, author: &author },
27        { id: 2, shelf: 1, author: &author },
28        { id: 3, shelf: 1, author: &author },
29    ])
30    .exec(&mut db)
31    .await?;
32
33    let first: Page<Book> = Book::all()
34        .include(Book::fields().author())
35        .order_by((Book::fields().shelf().asc(), Book::fields().id().asc()))
36        .paginate(2)
37        .exec(&mut db)
38        .await?;
39
40    assert_struct!(first.items, [{ id: 1 }, { id: 2 }]);
41    assert_struct!(first.items[0].author.get(), Some({ id: 1 }));
42    assert!(first.has_next());
43
44    let second = first.next(&mut db).await?.unwrap();
45    assert_struct!(second.items, [{ id: 3 }]);
46
47    let first_again = second.prev(&mut db).await?.unwrap();
48    assert_struct!(first_again.items, [{ id: 1 }, { id: 2 }]);
49
50    Ok(())
51}