test: added tests for M2M relationships, it works

This commit is contained in:
2025-01-31 22:52:28 +01:00
parent 86e29fa2dc
commit b70b4b7a81
5 changed files with 93 additions and 5 deletions

33
tests/o2m_relationship.rs Normal file
View File

@@ -0,0 +1,33 @@
use georm::Georm;
mod models;
use models::*;
#[sqlx::test(fixtures("simple_struct", "o2o"))]
async fn books_access_one_review(pool: sqlx::PgPool) -> sqlx::Result<()> {
let book = Book::find(&pool, &1).await?.unwrap();
let reviews = book.get_reviews(&pool).await?;
let review = Review {
id: 1,
book_id: 1,
review: "Great book".into(),
};
assert_eq!(vec![review], reviews);
Ok(())
}
#[sqlx::test(fixtures("simple_struct", "o2o"))]
async fn books_should_access_their_multiple_reviews(pool: sqlx::PgPool) -> sqlx::Result<()> {
let book = Book::find(&pool, &2).await?.unwrap();
let reviews = book.get_reviews(&pool).await?;
assert_eq!(2, reviews.len());
Ok(())
}
#[sqlx::test(fixtures("simple_struct", "o2o"))]
async fn books_can_have_no_reviews(pool: sqlx::PgPool) -> sqlx::Result<()> {
let book = Book::find(&pool, &4).await?.unwrap();
let reviews = book.get_reviews(&pool).await?;
assert_eq!(0, reviews.len());
Ok(())
}