mirror of
https://github.com/Phundrak/georm.git
synced 2026-07-29 04:19: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).
115 lines
2.9 KiB
Rust
115 lines
2.9 KiB
Rust
#![cfg(feature = "postgres")]
|
|
|
|
use georm::Georm;
|
|
|
|
mod models;
|
|
use models::{UserRole, UserRoleId};
|
|
|
|
#[sqlx::test(fixtures("composite_key"))]
|
|
async fn composite_key_find(pool: sqlx::PgPool) -> sqlx::Result<()> {
|
|
// This will test the find query generation bug
|
|
let id = models::UserRoleId {
|
|
user_id: 1,
|
|
role_id: 1,
|
|
};
|
|
|
|
let result = UserRole::find(&pool, &id).await?;
|
|
assert!(result.is_some());
|
|
|
|
let user_role = result.unwrap();
|
|
assert_eq!(1, user_role.user_id);
|
|
assert_eq!(1, user_role.role_id);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn composite_key_get_id() {
|
|
let user_role = UserRole {
|
|
user_id: 1,
|
|
role_id: 1,
|
|
assigned_at: chrono::Local::now().into(),
|
|
};
|
|
|
|
// This will test the get_id implementation bug
|
|
let id = user_role.get_id();
|
|
assert_eq!(1, id.user_id);
|
|
assert_eq!(1, id.role_id);
|
|
}
|
|
|
|
#[sqlx::test(fixtures("composite_key"))]
|
|
async fn composite_key_upsert(pool: sqlx::PgPool) -> sqlx::Result<()> {
|
|
let new_user_role = UserRole {
|
|
user_id: 5,
|
|
role_id: 2,
|
|
assigned_at: chrono::Local::now().into(),
|
|
};
|
|
|
|
// This will test the upsert query generation bug
|
|
let result = new_user_role.upsert(&pool).await?;
|
|
assert_eq!(5, result.user_id);
|
|
assert_eq!(2, result.role_id);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[sqlx::test(fixtures("composite_key"))]
|
|
async fn composite_key_delete(pool: sqlx::PgPool) -> sqlx::Result<()> {
|
|
let id = models::UserRoleId {
|
|
user_id: 1,
|
|
role_id: 1,
|
|
};
|
|
|
|
let rows_affected = UserRole::delete_by_id(&pool, &id).await?;
|
|
assert_eq!(1, rows_affected);
|
|
|
|
// Verify it's deleted
|
|
let result = UserRole::find(&pool, &id).await?;
|
|
assert!(result.is_none());
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[sqlx::test(fixtures("composite_key"))]
|
|
async fn composite_key_find_all(pool: sqlx::PgPool) -> sqlx::Result<()> {
|
|
let all_user_roles = UserRole::find_all(&pool).await?;
|
|
assert_eq!(4, all_user_roles.len());
|
|
Ok(())
|
|
}
|
|
|
|
#[sqlx::test(fixtures("composite_key"))]
|
|
async fn composite_key_create(pool: sqlx::PgPool) -> sqlx::Result<()> {
|
|
let new_user_role = UserRole {
|
|
user_id: 10,
|
|
role_id: 5,
|
|
assigned_at: chrono::Local::now().into(),
|
|
};
|
|
let result = new_user_role.create(&pool).await?;
|
|
assert_eq!(new_user_role.user_id, result.user_id);
|
|
assert_eq!(new_user_role.role_id, result.role_id);
|
|
Ok(())
|
|
}
|
|
|
|
#[sqlx::test(fixtures("composite_key"))]
|
|
async fn composite_key_update(pool: sqlx::PgPool) -> sqlx::Result<()> {
|
|
let mut user_role = UserRole::find(
|
|
&pool,
|
|
&UserRoleId {
|
|
user_id: 1,
|
|
role_id: 1,
|
|
},
|
|
)
|
|
.await?
|
|
.unwrap();
|
|
let now: chrono::DateTime<chrono::Utc> = chrono::Local::now().into();
|
|
user_role.assigned_at = now;
|
|
let updated = user_role.update(&pool).await?;
|
|
assert_eq!(
|
|
now.timestamp_millis(),
|
|
updated.assigned_at.timestamp_millis()
|
|
);
|
|
assert_eq!(1, updated.user_id);
|
|
assert_eq!(1, updated.role_id);
|
|
Ok(())
|
|
}
|