mirror of
https://github.com/Phundrak/georm.git
synced 2026-07-28 20:09:18 +02:00
4c314c2f0b
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).
170 lines
5.1 KiB
Rust
170 lines
5.1 KiB
Rust
#![cfg(feature = "postgres")]
|
|
|
|
use georm::Georm;
|
|
use rand::seq::SliceRandom;
|
|
|
|
use models::Author;
|
|
mod models;
|
|
|
|
#[sqlx::test(fixtures("simple_struct"))]
|
|
async fn find_all_query_works(pool: sqlx::PgPool) -> sqlx::Result<()> {
|
|
let result = Author::find_all(&pool).await?;
|
|
assert_eq!(3, result.len());
|
|
Ok(())
|
|
}
|
|
|
|
#[sqlx::test]
|
|
async fn find_all_returns_empty_vec_on_empty_table(pool: sqlx::PgPool) -> sqlx::Result<()> {
|
|
let result = Author::find_all(&pool).await?;
|
|
assert_eq!(0, result.len());
|
|
Ok(())
|
|
}
|
|
|
|
#[sqlx::test(fixtures("simple_struct"))]
|
|
async fn find_query_works(pool: sqlx::PgPool) -> sqlx::Result<()> {
|
|
let id = 1;
|
|
let res = Author::find(&pool, &id).await?;
|
|
assert!(res.is_some());
|
|
let res = res.unwrap();
|
|
assert_eq!(String::from("J.R.R. Tolkien"), res.name);
|
|
assert_eq!(1, res.id);
|
|
Ok(())
|
|
}
|
|
|
|
#[sqlx::test]
|
|
async fn find_returns_none_if_not_found(pool: sqlx::PgPool) -> sqlx::Result<()> {
|
|
let res = Author::find(&pool, &420).await?;
|
|
assert!(res.is_none());
|
|
Ok(())
|
|
}
|
|
|
|
#[sqlx::test]
|
|
async fn create_works(pool: sqlx::PgPool) -> sqlx::Result<()> {
|
|
let author = Author {
|
|
id: 1,
|
|
name: "J.R.R. Tolkien".into(),
|
|
..Default::default()
|
|
};
|
|
author.create(&pool).await?;
|
|
let all_authors = Author::find_all(&pool).await?;
|
|
assert_eq!(1, all_authors.len());
|
|
assert_eq!(vec![author], all_authors);
|
|
Ok(())
|
|
}
|
|
|
|
#[sqlx::test(fixtures("simple_struct"))]
|
|
async fn create_fails_if_already_exists(pool: sqlx::PgPool) -> sqlx::Result<()> {
|
|
let author = Author {
|
|
id: 2,
|
|
name: "Miura Kentaro".into(),
|
|
..Default::default()
|
|
};
|
|
let result = author.create(&pool).await;
|
|
assert!(result.is_err());
|
|
let error = result.err().unwrap();
|
|
assert_eq!(
|
|
"error returned from database: duplicate key value violates unique constraint \"authors_pkey\"",
|
|
error.to_string()
|
|
);
|
|
Ok(())
|
|
}
|
|
|
|
#[sqlx::test(fixtures("simple_struct"))]
|
|
async fn update_works(pool: sqlx::PgPool) -> sqlx::Result<()> {
|
|
let expected_initial = Author {
|
|
name: "J.R.R. Tolkien".into(),
|
|
id: 1,
|
|
biography_id: Some(2),
|
|
};
|
|
let expected_final = Author {
|
|
name: "Jolkien Rolkien Rolkien Tolkien".into(),
|
|
id: 1,
|
|
biography_id: Some(2),
|
|
};
|
|
let tolkien = Author::find(&pool, &1).await?;
|
|
assert!(tolkien.is_some());
|
|
let mut tolkien = tolkien.unwrap();
|
|
assert_eq!(expected_initial, tolkien);
|
|
tolkien.name = expected_final.name.clone();
|
|
let updated = tolkien.update(&pool).await?;
|
|
assert_eq!(expected_final, updated);
|
|
Ok(())
|
|
}
|
|
|
|
#[sqlx::test]
|
|
async fn update_fails_if_not_already_exists(pool: sqlx::PgPool) -> sqlx::Result<()> {
|
|
let author = Author {
|
|
id: 2,
|
|
name: "Miura Kentaro".into(),
|
|
..Default::default()
|
|
};
|
|
let result = author.update(&pool).await;
|
|
assert!(result.is_err());
|
|
let error = result.err().unwrap();
|
|
assert_eq!(
|
|
"no rows returned by a query that expected to return at least one row",
|
|
error.to_string()
|
|
);
|
|
Ok(())
|
|
}
|
|
|
|
#[sqlx::test]
|
|
async fn should_create_if_does_not_exist(pool: sqlx::PgPool) -> sqlx::Result<()> {
|
|
let all_authors = Author::find_all(&pool).await?;
|
|
assert_eq!(0, all_authors.len());
|
|
let author = Author {
|
|
id: 4,
|
|
name: "Miura Kentaro".into(),
|
|
..Default::default()
|
|
};
|
|
author.upsert(&pool).await?;
|
|
let all_authors = Author::find_all(&pool).await?;
|
|
assert_eq!(1, all_authors.len());
|
|
Ok(())
|
|
}
|
|
|
|
#[sqlx::test(fixtures("simple_struct"))]
|
|
async fn should_update_if_exist(pool: sqlx::PgPool) -> sqlx::Result<()> {
|
|
let all_authors = Author::find_all(&pool).await?;
|
|
assert_eq!(3, all_authors.len());
|
|
let author = Author {
|
|
id: 2,
|
|
name: "Miura Kentaro".into(),
|
|
..Default::default()
|
|
};
|
|
author.upsert(&pool).await?;
|
|
let mut all_authors = Author::find_all(&pool).await?;
|
|
all_authors.sort();
|
|
assert_eq!(3, all_authors.len());
|
|
assert_eq!(author, all_authors[1]);
|
|
Ok(())
|
|
}
|
|
|
|
#[sqlx::test(fixtures("simple_struct"))]
|
|
async fn delete_by_id_should_delete_only_one_entry(pool: sqlx::PgPool) -> sqlx::Result<()> {
|
|
let id = 2;
|
|
let all_authors = Author::find_all(&pool).await?;
|
|
assert_eq!(3, all_authors.len());
|
|
assert!(all_authors.iter().any(|author| author.get_id() == id));
|
|
let result = Author::delete_by_id(&pool, &id).await?;
|
|
assert_eq!(1, result);
|
|
let all_authors = Author::find_all(&pool).await?;
|
|
assert_eq!(2, all_authors.len());
|
|
assert!(all_authors.iter().all(|author| author.get_id() != id));
|
|
Ok(())
|
|
}
|
|
|
|
#[sqlx::test(fixtures("simple_struct"))]
|
|
async fn delete_should_delete_current_entity_from_db(pool: sqlx::PgPool) -> sqlx::Result<()> {
|
|
let mut all_authors = Author::find_all(&pool).await?;
|
|
assert_eq!(3, all_authors.len());
|
|
all_authors.shuffle(&mut rand::rng());
|
|
let author = all_authors.first().unwrap();
|
|
let result = author.delete(&pool).await?;
|
|
assert_eq!(1, result);
|
|
let all_authors = Author::find_all(&pool).await?;
|
|
assert_eq!(2, all_authors.len());
|
|
assert!(all_authors.iter().all(|a| a.get_id() != author.get_id()));
|
|
Ok(())
|
|
}
|