keelson-gen 0.1.0

keelson's code generator: introspect a live schema, emit readable model .rs files against keelson-models.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! The factory acceptance: the checked-in **generated** SQLite factories
//! (`tests/generated/sqlite_factories`, pinned byte-for-byte by
//! `generate_factories.rs`) are compiled here and run through the **same
//! assertions the hand-written factory spec runs**
//! (`keelson-factory/tests/spec_sqlite.rs`) — build without a database,
//! seeded reproduction, mods as values, hooks firing through the model path,
//! parent chains, `create_many` uniqueness at n=100, existing and shaped
//! parents, has-many children.
//!
//! On top of the spec's set: a **non-key unique column** (`tags.name`) is
//! sequence-backed, which the hand-written spec's three tables never
//! exercise.

// `pub` throughout the generated files because that is what the generator
// emits into an application's crates; this test binary has no external
// readers, and not every generated item is exercised.
#[allow(unreachable_pub, dead_code)]
// The fixture is prettyplease-formatted by the generator; rustfmt must not
// rewrite it, or the byte-identical freshness test would fight `cargo fmt`.
#[rustfmt::skip]
#[path = "generated/sqlite_factories/mod.rs"]
mod models;

use keelson_exec::{Executor, Statement};
use keelson_factory::Faker;
use keelson_models::Set;
use keelson_sqlite::{quote, select};
use keelson_sqlx::sqlite::Pool;

use models::factories::{comments, posts, tags, users};

/// The application's hand-written hooks, outside the generated tree — the
/// same pair the model spec has: normalise the email before insert, write an
/// audit tag on the caller's executor after insert.
#[allow(unreachable_pub)]
mod hooks {
    pub mod users {
        use keelson_exec::{ExecError, ExecFuture, Execute as _, Executor};
        use keelson_models::Set;
        use keelson_sqlite::{arg, insert, quote};

        use crate::models::users::{Setter, User};

        pub fn before_insert<'a>(
            _db: &'a dyn Executor,
            setter: &'a mut Setter,
        ) -> ExecFuture<'a, Result<(), ExecError>> {
            Box::pin(async move {
                if let Set::Value(email) = &mut setter.email {
                    *email = email.to_lowercase();
                }
                Ok(())
            })
        }

        pub fn after_insert<'a>(
            db: &'a dyn Executor,
            rows: &'a [User],
        ) -> ExecFuture<'a, Result<(), ExecError>> {
            Box::pin(async move {
                for u in rows {
                    keelson_sqlite::insert((
                        insert::into(quote("tags")).columns(["id", "name"]),
                        insert::values((arg(u.id), arg(format!("audit-user-{}", u.id)))),
                    ))
                    .execute(db)
                    .await?;
                }
                Ok(())
            })
        }
    }
}

// ────────────────────────── build(): no database ──────────────────────────

#[test]
fn build_produces_a_setter_with_no_executor_in_sight() {
    let mut f = Faker::seeded(1);
    let s = users::factory(()).build(&mut f);
    assert!(matches!(s.id, Set::Value(_)), "sequence-based id");
    match &s.name {
        Set::Value(n) => assert!(n.starts_with("user-"), "random default name, got {n}"),
        other => panic!("expected a generated name, got {other:?}"),
    }
    assert!(matches!(s.email, Set::Value(_)));
    assert!(matches!(s.age, Set::Value(_)));
    // Schema-defaulted columns stay out of the statement entirely.
    assert!(s.is_active.is_unset());
    assert!(s.created_at.is_unset());

    // A required FK without an existing parent stays unset: build alone
    // cannot create the chain — that is create()'s job.
    let s = posts::factory(()).build(&mut f);
    assert!(s.user_id.is_unset());
    // With an existing key, build fills it.
    let s = posts::factory(posts::user_id(77)).build(&mut f);
    assert_eq!(s.user_id, Set::Value(77));
}

#[test]
fn seeded_fakers_reproduce_random_sources_while_sequences_stay_unique() {
    let a = users::factory(()).build(&mut Faker::seeded(7));
    let b = users::factory(()).build(&mut Faker::seeded(7));
    assert_eq!(a.name, b.name);
    assert_eq!(a.email, b.email);
    assert_eq!(a.age, b.age);
    // The sequence-backed unique column deliberately does not reproduce: it
    // is uniqueness machinery, outside the seed.
    assert_ne!(a.id, b.id);

    // A Gen source (random_id) is inside the seed.
    let a = users::factory(users::random_id()).build(&mut Faker::seeded(7));
    let b = users::factory(users::random_id()).build(&mut Faker::seeded(7));
    assert_eq!(a.id, b.id);
}

#[test]
fn mods_are_values_and_override_the_default_sources() {
    let s = users::factory((
        users::id(42),
        users::name("Ada"),
        users::email_null(),
        users::age(36),
    ))
    .build(&mut Faker::seeded(0));
    assert_eq!(s.id, Set::Value(42));
    assert_eq!(s.name, Set::Value("Ada".to_owned()));
    assert_eq!(s.email, Set::Null, "the generated NULL mod");
    assert_eq!(s.age, Set::Value(36));
}

/// The non-key unique column the spec's tables do not have: `tags.name` is
/// `UNIQUE`, so its default is sequence-backed rather than random.
#[test]
fn a_non_key_unique_column_is_sequence_backed() {
    let mut names = std::collections::HashSet::new();
    for _ in 0..50 {
        let s = tags::factory(()).build(&mut Faker::seeded(3));
        match &s.name {
            Set::Value(n) => {
                assert!(n.starts_with("tag-"), "{n}");
                assert!(names.insert(n.clone()), "unique column repeated {n}");
            }
            other => panic!("expected a generated name, got {other:?}"),
        }
    }
}

#[test]
fn the_built_insert_is_judged_sql() {
    use keelson_core::Query as _;
    use keelson_models::Table as _;

    let s = users::factory((users::id(1), users::name("Ada"), users::email_null()))
        .build(&mut Faker::seeded(0));
    let q = models::users::Users::insert_query(models::users::Setter {
        age: Set::Unset, // pin: the unseeded age came from the faker
        ..s
    });
    let (sql, _args) = q.build().expect("build");
    keelson_sqlcheck::assert_sql(
        keelson_sqlcheck::Dialect::Sqlite,
        &sql,
        concat!(
            r#"INSERT INTO "users" ("id", "name", "email") VALUES (?1, ?2, ?3) "#,
            r#"RETURNING "users"."id", "users"."name", "users"."email", "users"."age", "#,
            r#""users"."is_active", "users"."created_at""#
        ),
    );
}

// ─────────────────────── end-to-end (real SQLite) ───────────────────────

async fn db() -> Pool {
    static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
    let path = std::env::temp_dir().join(format!(
        "keelson-gen-factories-{}-{}.db",
        std::process::id(),
        NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
    ));
    let _ = std::fs::remove_file(&path);
    let pool = Pool::connect(&format!("sqlite://{}", path.display()))
        .await
        .expect("opening the SQLite database");
    for ddl in ddl_statements(include_str!("fixtures/sqlite_schema.sql")) {
        pool.execute(Statement::new(&ddl, vec![])).await.unwrap();
    }
    pool
}

/// Split the fixture DDL into statements.
///
/// A plain `split(';')` cannot do it: a `CREATE TRIGGER` body is itself a
/// sequence of `;`-terminated statements between `BEGIN` and `END`, and the
/// fixture has three of them (the `INSTEAD OF` triggers that make
/// `editable_users` writable). The other SQLite tests hand the whole file to
/// rusqlite's `execute_batch`, which does this for itself; this one goes
/// through the pool, one statement at a time. `BEGIN`/`END` inside a comment
/// would fool it, so the fixture keeps those words out of its comments.
fn ddl_statements(sql: &str) -> Vec<String> {
    let mut out = Vec::new();
    let mut current = String::new();
    let mut depth = 0usize;
    for part in sql.split_inclusive(';') {
        current.push_str(part);
        for word in part.split(|c: char| !c.is_alphanumeric() && c != '_') {
            match word.to_ascii_uppercase().as_str() {
                "BEGIN" => depth += 1,
                "END" => depth = depth.saturating_sub(1),
                _ => {}
            }
        }
        if depth == 0 {
            let stmt = current.trim().trim_end_matches(';').trim().to_owned();
            if !stmt.is_empty() {
                out.push(stmt);
            }
            current.clear();
        }
    }
    let tail = current.trim().trim_end_matches(';').trim().to_owned();
    if !tail.is_empty() {
        out.push(tail);
    }
    out
}

async fn count(db: &dyn Executor, table: &'static str) -> i64 {
    use keelson_exec::Execute as _;
    keelson_sqlite::select((select::columns("count(*)"), select::from(quote(table))))
        .fetch_scalar(db)
        .await
        .unwrap()
}

#[tokio::test]
async fn build_alone_leaves_the_database_untouched() {
    let db = db().await;
    let _ = users::factory(()).build(&mut Faker::from_entropy());
    let _ = comments::factory(()).build(&mut Faker::from_entropy());
    for t in ["users", "posts", "comments", "tags"] {
        assert_eq!(count(&db, t).await, 0, "{t} rows appeared from build()");
    }
}

/// Factories fire model hooks — through the *generated* model's insert path,
/// which delegates to the application's hooks module.
#[tokio::test]
async fn create_goes_through_the_model_path_so_hooks_fire() {
    let db = db().await;
    let u = users::factory(users::email("ADA@Example.COM"))
        .create(&db)
        .await
        .unwrap();
    assert_eq!(
        u.email.as_deref(),
        Some("ada@example.com"),
        "before_insert normalised the factory's setter"
    );
    assert_eq!(count(&db, "users").await, 1);
    let audit: i64 = {
        use keelson_exec::Execute as _;
        use keelson_sqlite::{Chain as _, arg};
        keelson_sqlite::select((
            select::columns("count(*)"),
            select::from(quote("tags")),
            select::where_(quote("name").eq(arg(format!("audit-user-{}", u.id)))),
        ))
        .fetch_scalar(&db)
        .await
        .unwrap()
    };
    assert_eq!(audit, 1, "after_insert ran on the caller's executor");
}

/// The schema-aware win: a comment needs a post needs a user, and one create
/// makes the chain exist. The nullable FK stays NULL.
#[tokio::test]
async fn a_comment_auto_creates_its_post_and_user_chain() {
    let db = db().await;
    let c = comments::factory(()).create(&db).await.unwrap();

    let p = models::posts::table()
        .query(models::posts::id().eq(c.post_id))
        .one(&db)
        .await
        .unwrap();
    let owner = models::users::table()
        .query(models::users::id().eq(p.user_id))
        .one(&db)
        .await
        .unwrap();
    assert!(owner.is_active, "the chained user is a real defaulted row");
    assert_eq!(
        c.user_id, None,
        "the nullable FK stayed NULL — factories do not invent optional rows"
    );
    assert_eq!(count(&db, "users").await, 1);
    assert_eq!(count(&db, "posts").await, 1);
    assert_eq!(count(&db, "comments").await, 1);
}

/// FactoryBot's association semantics: each created row gets its own chain.
#[tokio::test]
async fn create_many_on_comments_builds_a_chain_per_row() {
    let db = db().await;
    let cs = comments::factory(()).create_many(&db, 10).await.unwrap();
    assert_eq!(cs.len(), 10);
    assert_eq!(count(&db, "comments").await, 10);
    assert_eq!(count(&db, "posts").await, 10, "one post per comment");
    assert_eq!(count(&db, "users").await, 10, "one user per post");
    let mut post_ids: Vec<i64> = cs.iter().map(|c| c.post_id).collect();
    post_ids.sort_unstable();
    post_ids.dedup();
    assert_eq!(post_ids.len(), 10);
    let found = models::posts::table()
        .query(models::posts::id().in_(post_ids))
        .all(&db)
        .await
        .unwrap();
    assert_eq!(found.len(), 10);
}

/// Uniqueness at n=100, and the hook fired for every row.
#[tokio::test]
async fn create_many_at_one_hundred_holds_uniqueness_and_fires_every_hook() {
    let db = db().await;
    let us = users::factory(()).create_many(&db, 100).await.unwrap();
    assert_eq!(us.len(), 100);
    let mut ids: Vec<i64> = us.iter().map(|u| u.id).collect();
    ids.sort_unstable();
    ids.dedup();
    assert_eq!(ids.len(), 100, "sequence ids collided");
    assert_eq!(count(&db, "users").await, 100);
    assert_eq!(count(&db, "tags").await, 100, "one audit tag per hook run");
}

/// The override matrix: an existing row, and a shaped parent template.
#[tokio::test]
async fn existing_and_shaped_parents_override_auto_creation() {
    let db = db().await;

    let u = users::factory(users::name("owner"))
        .create(&db)
        .await
        .unwrap();
    let p = posts::factory(posts::user(&u)).create(&db).await.unwrap();
    assert_eq!(p.user_id, u.id);
    assert_eq!(count(&db, "users").await, 1);

    let c = comments::factory(comments::for_post(posts::factory((
        posts::title("shaped"),
        posts::user(&u),
    ))))
    .create(&db)
    .await
    .unwrap();
    let shaped = models::posts::table()
        .query(models::posts::id().eq(c.post_id))
        .one(&db)
        .await
        .unwrap();
    assert_eq!(shaped.title, "shaped");
    assert_eq!(shaped.user_id, u.id);
    assert_eq!(count(&db, "users").await, 1, "still no invented users");

    let c = comments::factory((comments::post(&p), comments::user(&u)))
        .create(&db)
        .await
        .unwrap();
    assert_eq!(c.post_id, p.id);
    assert_eq!(c.user_id, Some(u.id));

    let c = comments::factory((
        comments::post(&p),
        comments::for_user(users::factory(users::name("commenter"))),
    ))
    .create(&db)
    .await
    .unwrap();
    let commenter = models::users::table()
        .query(models::users::id().eq(c.user_id.unwrap()))
        .one(&db)
        .await
        .unwrap();
    assert_eq!(commenter.name, "commenter");
    assert_eq!(count(&db, "users").await, 2);
}

/// Has-many children: `with_new_post` creates the child after the user, with
/// the FK forced to the created row.
#[tokio::test]
async fn with_new_post_creates_children_bound_to_the_new_user() {
    let db = db().await;
    let u = users::factory((
        users::with_new_post(posts::factory(posts::title("first"))),
        users::with_new_post(posts::factory(posts::title("second"))),
    ))
    .create(&db)
    .await
    .unwrap();

    let ps = models::posts::table()
        .query(models::posts::user_id().eq(u.id))
        .all(&db)
        .await
        .unwrap();
    let mut titles: Vec<&str> = ps.iter().map(|p| p.title.as_str()).collect();
    titles.sort_unstable();
    assert_eq!(titles, vec!["first", "second"]);
    assert_eq!(count(&db, "users").await, 1, "children reuse their creator");
}

/// Seeded creates reproduce the random columns across two databases.
#[tokio::test]
async fn seeded_creates_reproduce_across_databases() {
    let db_a = db().await;
    let db_b = db().await;
    let mut fa = Faker::seeded(99);
    let mut fb = Faker::seeded(99);
    let a = users::factory(())
        .create_with(&db_a, &mut fa)
        .await
        .unwrap();
    let b = users::factory(())
        .create_with(&db_b, &mut fb)
        .await
        .unwrap();
    assert_eq!(a.name, b.name);
    assert_eq!(a.email, b.email);
    assert_eq!(a.age, b.age);
}