#![cfg(feature = "sqlite")]
use autumn_web::config::DatabaseConfig;
use autumn_web::db::{RuntimeConnection, create_pool};
use autumn_web::reexports::{diesel, diesel_async};
use diesel_async::RunQueryDsl as _;
use diesel_async::pooled_connection::deadpool::Pool;
type SqlitePool = Pool<RuntimeConnection>;
mod schema {
autumn_web::reexports::diesel::table! {
crud_notes (id) {
id -> Int8,
content -> Text,
views -> Int8,
}
}
}
use schema::crud_notes;
#[autumn_web::model]
pub struct CrudNote {
#[id]
pub id: i64,
pub content: String,
pub views: i64,
}
#[autumn_web::repository(CrudNote)]
pub trait CrudNoteRepository {}
async fn boot_pool() -> SqlitePool {
let config = DatabaseConfig {
url: Some("sqlite://file:crud_basic?mode=memory&cache=shared".to_string()),
primary_pool_size: Some(1),
..Default::default()
};
let pool: SqlitePool = create_pool(&config)
.expect("sqlite pool builds via build_sqlite_pool")
.expect("a url is configured");
{
let mut conn = pool.get().await.expect("checkout a sqlite connection");
diesel::sql_query(
"CREATE TABLE crud_notes (\
id INTEGER PRIMARY KEY AUTOINCREMENT, \
content TEXT NOT NULL, \
views BIGINT NOT NULL DEFAULT 0\
)",
)
.execute(&mut *conn)
.await
.expect("create crud_notes table");
}
pool
}
#[tokio::test]
async fn generated_crud_compiles_and_runs_on_sqlite() {
let pool = boot_pool().await;
let repo = PgCrudNoteRepository::with_pool_untracked(pool);
let created = repo
.save(&NewCrudNote {
content: "first".to_string(),
views: 0,
})
.await
.expect("save inserts a row and returns it");
assert_eq!(created.content, "first");
assert!(created.id > 0, "autoincrement id assigned");
let found = repo
.find_by_id(created.id)
.await
.expect("find_by_id query")
.expect("row exists");
assert_eq!(found.content, "first");
assert_eq!(found.views, 0);
let updated = repo
.update(
created.id,
&UpdateCrudNote {
content: autumn_web::hooks::Patch::Set("first-edited".to_string()),
views: autumn_web::hooks::Patch::Set(7),
},
)
.await
.expect("update mutates and returns the row");
assert_eq!(updated.content, "first-edited");
assert_eq!(updated.views, 7);
let many = repo
.save_many(&[
NewCrudNote {
content: "second".to_string(),
views: 1,
},
NewCrudNote {
content: "third".to_string(),
views: 2,
},
])
.await
.expect("save_many inserts a batch and returns the rows");
assert_eq!(many.len(), 2);
let mut contents: Vec<&str> = many.iter().map(|m| m.content.as_str()).collect();
contents.sort_unstable();
assert_eq!(contents, ["second", "third"]);
let total = repo.count().await.expect("count query");
assert_eq!(total, 3);
repo.delete_by_id(created.id)
.await
.expect("delete_by_id removes the row");
assert!(
repo.find_by_id(created.id)
.await
.expect("find_by_id after delete")
.is_none(),
"deleted row is gone"
);
assert_eq!(repo.count().await.expect("count after delete"), 2);
}