ORM-17: Port test fixtures and tests to the ambient API and migrations #17

Open
opened 2026-10-13 12:49:39 +01:00 by NoctisWolfshadow · 0 comments
Owner

Depends on: #11, #16

Context

The fixtures still build their schema with a raw CREATE TABLE and pass &conn around. Once migrations and the ambient API exist, the tests should use them: it proves both work and removes the last direct Connection handling from tests. (This was originally planned for the migrations milestone, but it needs the ambient all().)

Tasks

  • tests/common::setup() becomes Database::connect_thread(Config::memory()) plus Migrator::new(migrations()).run(), seeding with User::create.
  • Replace every raw CREATE TABLE in tests with a migration, and every .all_on(&conn) with .all().
  • Replace the assertion-free it_works test with real tests.

Example

// tests/common/mod.rs
pub fn migrations() -> MigrationList {
    MigrationList::new(vec![Migration::new(1, "create_users", "
        CREATE TABLE users (
            id     INTEGER PRIMARY KEY,
            name   TEXT NOT NULL,
            email  TEXT,
            age    INTEGER NOT NULL,
            active INTEGER NOT NULL DEFAULT 1
        );
    ")])
    .unwrap()
}

/// Five users (ids 1..=5): alice 31, bob 17, carol 45 (inactive), alan 22, dave 19.
pub fn setup() {
    Database::connect_thread(Config::memory()).unwrap();
    Migrator::new(migrations()).run().unwrap();

    for user in [
        User::new("alice", Some("alice@example.com"), 31, true),
        User::new("bob", None, 17, true),
        User::new("carol", Some("carol@example.com"), 45, false),
        User::new("alan", None, 22, true),
        User::new("dave", Some("dave@example.com"), 19, true),
    ] {
        User::create(user).unwrap();
    }
}
// tests/query.rs
#[test]
fn filter_order_limit() {
    setup();
    let users = User::query()
        .filter(User::AGE.greater_equals(18))
        .filter(User::ACTIVE.equals(true))
        .order_by(User::NAME.asc())
        .limit(10)
        .all()
        .unwrap();
    assert_eq!(names(&users), ["alan", "alice", "dave"]);
}

Acceptance criteria

  • No test constructs a Connection directly.
  • No test contains a raw CREATE TABLE.
  • The suite is green and passes when run in parallel.
**Depends on:** #11, #16 ## Context The fixtures still build their schema with a raw `CREATE TABLE` and pass `&conn` around. Once migrations and the ambient API exist, the tests should use them: it proves both work and removes the last direct `Connection` handling from tests. (This was originally planned for the migrations milestone, but it needs the ambient `all()`.) ## Tasks - [ ] `tests/common::setup()` becomes `Database::connect_thread(Config::memory())` plus `Migrator::new(migrations()).run()`, seeding with `User::create`. - [ ] Replace every raw `CREATE TABLE` in tests with a migration, and every `.all_on(&conn)` with `.all()`. - [ ] Replace the assertion-free `it_works` test with real tests. ## Example ```rust // tests/common/mod.rs pub fn migrations() -> MigrationList { MigrationList::new(vec![Migration::new(1, "create_users", " CREATE TABLE users ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, email TEXT, age INTEGER NOT NULL, active INTEGER NOT NULL DEFAULT 1 ); ")]) .unwrap() } /// Five users (ids 1..=5): alice 31, bob 17, carol 45 (inactive), alan 22, dave 19. pub fn setup() { Database::connect_thread(Config::memory()).unwrap(); Migrator::new(migrations()).run().unwrap(); for user in [ User::new("alice", Some("alice@example.com"), 31, true), User::new("bob", None, 17, true), User::new("carol", Some("carol@example.com"), 45, false), User::new("alan", None, 22, true), User::new("dave", Some("dave@example.com"), 19, true), ] { User::create(user).unwrap(); } } ``` ```rust // tests/query.rs #[test] fn filter_order_limit() { setup(); let users = User::query() .filter(User::AGE.greater_equals(18)) .filter(User::ACTIVE.equals(true)) .order_by(User::NAME.asc()) .limit(10) .all() .unwrap(); assert_eq!(names(&users), ["alan", "alice", "dave"]); } ``` ## Acceptance criteria - [ ] No test constructs a `Connection` directly. - [ ] No test contains a raw `CREATE TABLE`. - [ ] The suite is green and passes when run in parallel.
Sign in to join this conversation.
No description provided.