From 78810d883256083bd4ddbf939e817155ba59cb5d Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Thu, 8 Oct 2026 15:17:30 -0400 Subject: [PATCH] DRY pass #3: core tests open their store and seed a label one way (#5373) Five test modules wrote the same db() over memory_conn, and portable.rs unwrapped it inline; local::test_db() is that. Four pull/push tests wrote the same label INSERT; local::seed_label(conn, id, name, dirty) is it. Both are cfg(test), beside memory_conn, as wire::sample_note is beside wire::Note. Co-Authored-By: Claude Opus 5.5 --- core/src/local/mod.rs | 17 +++++++++++++++++ core/src/local/portable.rs | 2 +- core/src/local/retention.rs | 4 +--- core/src/local/store.rs | 4 +--- core/src/sync/pull.rs | 18 +++--------------- core/src/sync/push.rs | 18 +++--------------- core/src/sync/state.rs | 4 +--- 7 files changed, 27 insertions(+), 40 deletions(-) diff --git a/core/src/local/mod.rs b/core/src/local/mod.rs index 2b4e2a4..87f04eb 100644 --- a/core/src/local/mod.rs +++ b/core/src/local/mod.rs @@ -88,6 +88,23 @@ pub(crate) fn memory_conn() -> rusqlite::Result { Ok(conn) } +/// A test's store: [`memory_conn`], which can't fail on a fresh in-memory database. +#[cfg(test)] +pub(crate) fn test_db() -> Connection { + memory_conn().expect("in-memory db") +} + +/// A label this device holds, by id and name, for tests that need one in place. +#[cfg(test)] +pub(crate) fn seed_label(conn: &Connection, id: &str, name: &str, dirty: i64) { + conn.execute( + "INSERT INTO labels (id, name, color, created_at, updated_at, dirty) + VALUES (?1, ?2, 'default', '2026-01-01', '2026-01-01', ?3)", + rusqlite::params![id, name, dirty], + ) + .expect("seed label"); +} + /// A one-line count summary of the store, for the startup log. pub fn summary(db: &Db) -> String { let conn = match db.conn() { diff --git a/core/src/local/portable.rs b/core/src/local/portable.rs index a770cbe..17c643a 100644 --- a/core/src/local/portable.rs +++ b/core/src/local/portable.rs @@ -595,7 +595,7 @@ mod tests { use super::*; fn store() -> (Connection, BlobStore, std::path::PathBuf) { - let conn = crate::local::memory_conn().unwrap(); + let conn = crate::local::test_db(); let blobs = crate::sync::blobs::scratch("portable"); let dir = blobs.root().to_path_buf(); (conn, blobs, dir) diff --git a/core/src/local/retention.rs b/core/src/local/retention.rs index 95dfd9f..9dd979e 100644 --- a/core/src/local/retention.rs +++ b/core/src/local/retention.rs @@ -81,9 +81,7 @@ pub fn sweep_if_unlinked(conn: &Connection) -> rusqlite::Result> { mod tests { use super::*; - fn db() -> Connection { - crate::local::memory_conn().expect("in-memory db") - } + use crate::local::test_db as db; /// A trashed note of a given age, stamped in the format the CLIENT writes /// (`...Z`, millisecond precision — see `store::now`). diff --git a/core/src/local/store.rs b/core/src/local/store.rs index d425d0f..77e8068 100644 --- a/core/src/local/store.rs +++ b/core/src/local/store.rs @@ -1123,9 +1123,7 @@ mod tests { use super::*; - fn db() -> Connection { - crate::local::memory_conn().expect("in-memory db") - } + use crate::local::test_db as db; fn note(conn: &Connection, body: &str) -> Note { create_note( diff --git a/core/src/sync/pull.rs b/core/src/sync/pull.rs index 478f23f..c4557b5 100644 --- a/core/src/sync/pull.rs +++ b/core/src/sync/pull.rs @@ -508,9 +508,7 @@ pub async fn run( mod tests { use super::*; - fn db() -> Connection { - crate::local::memory_conn().expect("in-memory db") - } + use crate::local::test_db as db; fn note(id: &str, revision: i64) -> wire::Note { wire::sample_note(id, revision) @@ -738,12 +736,7 @@ mod tests { #[test] fn server_label_replaces_a_local_duplicate_by_name() { let conn = db(); - conn.execute( - "INSERT INTO labels (id, name, color, created_at, updated_at, dirty) - VALUES ('local-id', 'Work', 'default', '2026-01-01', '2026-01-01', 1)", - [], - ) - .expect("seed local label"); + crate::local::seed_label(&conn, "local-id", "Work", 1); let server = wire::Label { id: "server-id".into(), @@ -768,12 +761,7 @@ mod tests { // plain delete would strip the label off them with nothing to repair it. let conn = db(); apply_page(&conn, &page(vec![note("n1", 1)], vec![], 1)).expect("seed note"); - conn.execute( - "INSERT INTO labels (id, name, color, created_at, updated_at, dirty) - VALUES ('local-id', 'Work', 'default', '2026-01-01', '2026-01-01', 1)", - [], - ) - .expect("seed local label"); + crate::local::seed_label(&conn, "local-id", "Work", 1); conn.execute( "INSERT INTO note_labels (note_id, label_id, via_tag) VALUES ('n1', 'local-id', 0)", diff --git a/core/src/sync/push.rs b/core/src/sync/push.rs index 2248bb3..d2c6380 100644 --- a/core/src/sync/push.rs +++ b/core/src/sync/push.rs @@ -704,9 +704,7 @@ mod tests { use super::*; use crate::local::store; - fn db() -> Connection { - crate::local::memory_conn().expect("in-memory db") - } + use crate::local::test_db as db; fn seed_note(conn: &Connection, id: &str, dirty: i64) { conn.execute( @@ -754,12 +752,7 @@ mod tests { let conn = db(); seed_note(&conn, "n1", 1); for (id, name, via_tag) in [("manual", "Manual", 0), ("tagged", "Tagged", 1)] { - conn.execute( - "INSERT INTO labels (id, name, color, created_at, updated_at, dirty) - VALUES (?1, ?2, 'default', '2026-01-01', '2026-01-01', 0)", - params![id, name], - ) - .expect("seed label"); + crate::local::seed_label(&conn, id, name, 0); conn.execute( "INSERT INTO note_labels (note_id, label_id, via_tag) VALUES ('n1', ?1, ?2)", params![id, via_tag], @@ -877,12 +870,7 @@ mod tests { let conn = db(); seed_note(&conn, "n1", 0); for (id, name) in [("src", "Source"), ("dst", "Target")] { - conn.execute( - "INSERT INTO labels (id, name, color, created_at, updated_at, dirty) - VALUES (?1, ?2, 'default', '2026-01-01', '2026-01-01', 0)", - params![id, name], - ) - .expect("seed label"); + crate::local::seed_label(&conn, id, name, 0); } conn.execute( "INSERT INTO note_labels (note_id, label_id, via_tag) VALUES ('n1', 'src', 0)", diff --git a/core/src/sync/state.rs b/core/src/sync/state.rs index 01a6281..ef81880 100644 --- a/core/src/sync/state.rs +++ b/core/src/sync/state.rs @@ -309,9 +309,7 @@ pub fn status(conn: &Connection) -> rusqlite::Result { mod tests { use super::*; - fn db() -> Connection { - crate::local::memory_conn().expect("in-memory db") - } + use crate::local::test_db as db; #[test] fn fresh_store_is_unlinked() {