use glacier::db::{self, Repo, Timestamp};
use glacier::prelude::*;
use glacier::testing::{TestClient, queries};
#[derive(Debug, Clone, Model)]
struct Note {
id: i64,
body: String,
inserted_at: Timestamp,
updated_at: Timestamp,
}
async fn repo() -> Repo {
let repo = Repo::connect("sqlite::memory:").await.unwrap();
db::with(&repo, async {
db::sql(
"CREATE TABLE notes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
body TEXT NOT NULL,
inserted_at TEXT NOT NULL,
updated_at TEXT NOT NULL
)",
)
.execute()
.await
.unwrap();
db::sql("INSERT INTO notes (body, inserted_at, updated_at) VALUES ('first', '2026-01-01 00:00:00', '2026-01-01 00:00:00')")
.execute()
.await
.unwrap();
})
.await;
repo
}
async fn bodies() -> glacier::Result<String> {
let notes = Note::all().all().await?;
Ok(notes
.iter()
.map(|n| n.body.as_str())
.collect::<Vec<_>>()
.join(","))
}
async fn client() -> (TestClient, Repo) {
let repo = repo().await;
let app = App::new().database(repo.clone()).routes(|r| {
r.get("/twice", |_ctx| async {
let (a, b) = (bodies().await?, bodies().await?);
Ok::<_, Error>(format!("{a} {b}"))
});
r.get("/write_between", |_ctx| async {
let before = bodies().await?;
db::sql("INSERT INTO notes (body, inserted_at, updated_at) VALUES ('second', '2026-01-01 00:00:00', '2026-01-01 00:00:00')")
.execute()
.await?;
Ok::<_, Error>(format!("{before} {}", bodies().await?))
});
r.get("/uncached", |_ctx| async {
bodies().await?;
db::query_cache::uncached(bodies()).await?;
Ok::<_, Error>("ok")
});
r.get("/transaction", |_ctx| async {
bodies().await?;
db::transaction(bodies()).await?;
bodies().await?;
Ok::<_, Error>("ok")
});
r.get("/raw", |_ctx| async {
for _ in 0..2 {
db::sql("SELECT body FROM notes").fetch_all().await?;
}
Ok::<_, Error>("ok")
});
});
(TestClient::new(app), repo)
}
#[tokio::test]
async fn a_read_run_twice_in_a_request_reaches_the_database_once() {
let (c, _) = client().await;
let (res, sql) = queries(c.get("/twice")).await;
assert_eq!(res.body_text(), "first first");
assert_eq!(sql.len(), 1, "{sql:#?}");
let (_, sql) = queries(c.get("/twice")).await;
assert_eq!(
sql.len(),
1,
"each request starts with an empty cache: {sql:#?}"
);
}
#[tokio::test]
async fn a_write_empties_the_cache() {
let (c, _) = client().await;
let (res, sql) = queries(c.get("/write_between")).await;
assert_eq!(
res.body_text(),
"first first,second",
"the read after the write sees it"
);
assert_eq!(sql.len(), 3, "read, insert, read: {sql:#?}");
}
#[tokio::test]
async fn uncached_reads_go_to_the_database() {
let (c, _) = client().await;
let (_, sql) = queries(c.get("/uncached")).await;
assert_eq!(sql.len(), 2, "{sql:#?}");
}
#[tokio::test]
async fn a_transaction_s_end_empties_the_cache() {
let (c, _) = client().await;
let (res, sql) = queries(c.get("/transaction")).await;
assert_eq!(res.body_text(), "ok");
let reads = sql.iter().filter(|q| q.starts_with("SELECT")).count();
assert_eq!(reads, 2, "{sql:#?}");
}
#[tokio::test]
async fn raw_sql_and_work_outside_a_request_are_not_cached() {
let (c, repo) = client().await;
let (_, sql) = queries(c.get("/raw")).await;
assert_eq!(sql.len(), 2, "{sql:#?}");
let (_, sql) = queries(db::with(&repo, async {
bodies().await.unwrap();
bodies().await.unwrap();
}))
.await;
assert_eq!(sql.len(), 2, "a job or a script reads afresh: {sql:#?}");
}