Files
georm/tests/o2o_relationship.rs
Claude 4c314c2f0b feat: add SQLite support behind a sqlite Cargo feature
Adds a SqliteDialect alongside PostgresDialect, gated by mutually exclusive
postgres (default) / sqlite Cargo features, plus a feature-selected
ActiveDatabase type alias used throughout the public Georm/Defaultable
traits. Basic CRUD and Defaultable are fully covered by a new SQLite test
suite (tests/sqlite/*) running against a real SQLite database, alongside
split migrations/sqlite/*.

Two real dialect differences surfaced beyond placeholder syntax:
- SQLite's INTEGER columns type-infer as i64 in sqlx's compile-time query
  macros (unlike Postgres SERIAL -> i32), so the SQLite test models use i64
  for integer PK/FK fields.
- sqlx's SQLite query macros need bind arguments bound to a place rather
  than passed as an inline expression (fixed once, centrally, in
  SqlDialect::generate_relation_lookup).

Composite-key entities with chrono::DateTime columns (tests/composite_key.rs)
are not yet ported to SQLite: SQLite has no TIMESTAMPTZ equivalent, so sqlx
infers TEXT date/time columns as String rather than DateTime<Utc> unless the
query explicitly overrides the column type. That needs per-field type
overrides threaded through the generated queries, left as follow-up.

CI now matrixes over both backends; formatting/audit only run once since
they don't vary by backend. examples/postgres/* stays out of
--no-default-features builds by scoping sqlite lint/test just recipes to
`-p georm` (it depends on georm's default postgres feature via Cargo
feature unification, so building it under --features sqlite at the
workspace level double-activates both dialects).
2026-07-21 19:06:35 +00:00

79 lines
2.7 KiB
Rust

#![cfg(feature = "postgres")]
use georm::Georm;
mod models;
use models::*;
#[sqlx::test(fixtures("simple_struct", "o2o"))]
async fn book_should_have_working_get_author_method(pool: sqlx::PgPool) -> sqlx::Result<()> {
let book = Book::find(&pool, &1).await?;
assert!(book.is_some());
let book = book.unwrap();
let author = book.get_author(&pool).await?;
let expected_author = Author {
id: 1,
name: "J.R.R. Tolkien".into(),
biography_id: Some(2),
};
assert_eq!(expected_author, author);
Ok(())
}
#[sqlx::test(fixtures("simple_struct"))]
async fn author_should_have_working_get_biography_method(pool: sqlx::PgPool) -> sqlx::Result<()> {
let author = Author::find(&pool, &1).await?;
assert!(author.is_some());
let author = author.unwrap();
let biography = author.get_biography(&pool).await?;
assert!(biography.is_some());
Ok(())
}
#[sqlx::test(fixtures("simple_struct"))]
async fn author_should_have_optional_biographies(pool: sqlx::PgPool) -> sqlx::Result<()> {
let tolkien = Author::find(&pool, &1).await?;
assert!(tolkien.is_some());
let tolkien_biography = tolkien.unwrap().get_biography(&pool).await?;
assert!(tolkien_biography.is_some());
let biography = Biography {
id: 2,
content: "Some other text".into(),
};
assert_eq!(biography, tolkien_biography.unwrap());
let orwell = Author::find(&pool, &2).await?;
assert!(orwell.is_some());
assert!(orwell.unwrap().get_biography(&pool).await?.is_none());
Ok(())
}
#[sqlx::test(fixtures("simple_struct", "o2o"))]
async fn books_are_found_despite_nonstandard_id_name(pool: sqlx::PgPool) -> sqlx::Result<()> {
let review = Review::find(&pool, &1).await?.unwrap();
let book = review.get_book(&pool).await?;
let tolkien = Author::find(&pool, &1).await?.unwrap();
assert_eq!(tolkien, book.get_author(&pool).await?);
Ok(())
}
#[sqlx::test(fixtures("simple_struct"))]
async fn biographies_should_find_remote_o2o_author(pool: sqlx::PgPool) -> sqlx::Result<()> {
let london = Author::find(&pool, &3).await?.unwrap();
let london_biography = Biography::find(&pool, &1).await?.unwrap();
let result = london_biography.get_author(&pool).await;
assert!(result.is_ok());
let result = result.unwrap();
assert!(result.is_some());
let result = result.unwrap();
assert_eq!(london, result);
Ok(())
}
#[sqlx::test(fixtures("simple_struct"))]
async fn biographies_may_not_have_corresponding_author(pool: sqlx::PgPool) -> sqlx::Result<()> {
let biography = Biography::find(&pool, &3).await?.unwrap();
let result = biography.get_author(&pool).await?;
assert!(result.is_none());
Ok(())
}