test(db): implement database layer tests.

-set up test structure
-implement item lifetime tests
This commit is contained in:
ryan 2025-03-04 18:05:18 +03:00
parent 1b99027dbf
commit 505647b432
3 changed files with 403 additions and 11 deletions

338
Cargo.lock generated
View file

@ -81,6 +81,31 @@ dependencies = [
"zstd",
]
[[package]]
name = "actix-http-test"
version = "3.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "061d27c2a6fea968fdaca0961ff429d23a4ec878c4f68f5d08626663ade69c80"
dependencies = [
"actix-codec",
"actix-rt",
"actix-server",
"actix-service",
"actix-tls",
"actix-utils",
"awc",
"bytes",
"futures-core",
"http 0.2.12",
"log",
"serde",
"serde_json",
"serde_urlencoded",
"slab",
"socket2",
"tokio",
]
[[package]]
name = "actix-macros"
version = "0.2.4"
@ -112,6 +137,7 @@ version = "2.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24eda4e2a6e042aa4e55ac438a2ae052d3b5da0ecf83d7411e1a368946925208"
dependencies = [
"actix-macros",
"futures-core",
"tokio",
]
@ -144,6 +170,48 @@ dependencies = [
"pin-project-lite",
]
[[package]]
name = "actix-test"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "439022b5a7b5dac10798465029a9566e8e0cca7a6014541ed277b695691fac5f"
dependencies = [
"actix-codec",
"actix-http",
"actix-http-test",
"actix-rt",
"actix-service",
"actix-utils",
"actix-web",
"awc",
"futures-core",
"futures-util",
"log",
"serde",
"serde_json",
"serde_urlencoded",
"tokio",
]
[[package]]
name = "actix-tls"
version = "3.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac453898d866cdbecdbc2334fe1738c747b4eba14a677261f2b768ba05329389"
dependencies = [
"actix-rt",
"actix-service",
"actix-utils",
"futures-core",
"http 0.2.12",
"http 1.2.0",
"impl-more",
"pin-project-lite",
"tokio",
"tokio-util",
"tracing",
]
[[package]]
name = "actix-utils"
version = "3.0.1"
@ -239,7 +307,7 @@ version = "0.7.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "891477e0c6a8957309ee5c45a6368af3ae14bb510732d2684ffa19af310920f9"
dependencies = [
"getrandom",
"getrandom 0.2.15",
"once_cell",
"version_check",
]
@ -251,7 +319,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011"
dependencies = [
"cfg-if",
"getrandom",
"getrandom 0.2.15",
"once_cell",
"version_check",
"zerocopy",
@ -314,6 +382,16 @@ version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50"
[[package]]
name = "assert-json-diff"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47e4f2b81832e72834d7518d8487a0396a28cc408186a2e8854c0f98011faf12"
dependencies = [
"serde",
"serde_json",
]
[[package]]
name = "async-recursion"
version = "1.1.1"
@ -412,6 +490,39 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
[[package]]
name = "awc"
version = "3.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "79049b2461279b886e46f1107efc347ebecc7b88d74d023dda010551a124967b"
dependencies = [
"actix-codec",
"actix-http",
"actix-rt",
"actix-service",
"actix-tls",
"actix-utils",
"base64",
"bytes",
"cfg-if",
"cookie",
"derive_more",
"futures-core",
"futures-util",
"h2",
"http 0.2.12",
"itoa",
"log",
"mime",
"percent-encoding",
"pin-project-lite",
"rand",
"serde",
"serde_json",
"serde_urlencoded",
"tokio",
]
[[package]]
name = "backtrace"
version = "0.3.74"
@ -760,11 +871,23 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "186dce98367766de751c42c4f03970fc60fc012296e706ccbb9d5df9b6c1e271"
[[package]]
name = "colored"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "117725a109d387c937a1533ce01b450cbde6b88abceea8473c4d7a85853cda3c"
dependencies = [
"lazy_static",
"windows-sys 0.59.0",
]
[[package]]
name = "compareware"
version = "0.1.3"
dependencies = [
"actix-files",
"actix-rt",
"actix-test",
"actix-web",
"chrono",
"console_error_panic_hook",
@ -778,18 +901,22 @@ dependencies = [
"leptos_router",
"mio 0.8.11",
"mockall",
"mockito",
"nostr-sdk",
"paste",
"rstest",
"rusqlite",
"secp256k1 0.30.0",
"serde",
"serde_json",
"tempfile",
"thiserror 2.0.9",
"tokio",
"urlencoding",
"uuid",
"wasm-bindgen",
"wasm-bindgen-futures",
"wasm-bindgen-test",
"web-sys",
"zerofrom",
]
@ -991,6 +1118,12 @@ dependencies = [
"syn 2.0.90",
]
[[package]]
name = "difference"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198"
[[package]]
name = "difflib"
version = "0.4.0"
@ -1052,6 +1185,16 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "errno"
version = "0.3.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d"
dependencies = [
"libc",
"windows-sys 0.59.0",
]
[[package]]
name = "fallible-iterator"
version = "0.2.0"
@ -1064,6 +1207,12 @@ version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a"
[[package]]
name = "fastrand"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be"
[[package]]
name = "flate2"
version = "1.0.35"
@ -1181,6 +1330,12 @@ version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988"
[[package]]
name = "futures-timer"
version = "3.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f288b0a4f20f9a56b5d1da57e2227c661b7b16168e2f72365f57b63326e29b24"
[[package]]
name = "futures-util"
version = "0.3.31"
@ -1218,16 +1373,34 @@ dependencies = [
"cfg-if",
"js-sys",
"libc",
"wasi",
"wasi 0.11.0+wasi-snapshot-preview1",
"wasm-bindgen",
]
[[package]]
name = "getrandom"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43a49c392881ce6d5c3b8cb70f98717b7c07aabbdff06687b9030dbfbe2725f8"
dependencies = [
"cfg-if",
"libc",
"wasi 0.13.3+wasi-0.2.2",
"windows-targets 0.52.6",
]
[[package]]
name = "gimli"
version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f"
[[package]]
name = "glob"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8d1add55171497b4705a648c6b583acafb01d58050a51727785f0b2c8e0a2b2"
[[package]]
name = "gloo-net"
version = "0.5.0"
@ -1786,7 +1959,7 @@ dependencies = [
"cfg-if",
"drain_filter_polyfill",
"futures",
"getrandom",
"getrandom 0.2.15",
"html-escape",
"indexmap",
"itertools 0.12.1",
@ -1977,6 +2150,12 @@ dependencies = [
"serde_test",
]
[[package]]
name = "linux-raw-sys"
version = "0.4.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab"
[[package]]
name = "litemap"
version = "0.7.4"
@ -2079,6 +2258,16 @@ dependencies = [
"unicase",
]
[[package]]
name = "minicov"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f27fe9f1cc3c22e1687f9446c2083c4c5fc7f0bcf1c7a86bdbded14985895b4b"
dependencies = [
"cc",
"walkdir",
]
[[package]]
name = "minimal-lexical"
version = "0.2.1"
@ -2102,7 +2291,7 @@ checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c"
dependencies = [
"libc",
"log",
"wasi",
"wasi 0.11.0+wasi-snapshot-preview1",
"windows-sys 0.48.0",
]
@ -2114,7 +2303,7 @@ checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd"
dependencies = [
"libc",
"log",
"wasi",
"wasi 0.11.0+wasi-snapshot-preview1",
"windows-sys 0.52.0",
]
@ -2145,6 +2334,24 @@ dependencies = [
"syn 1.0.109",
]
[[package]]
name = "mockito"
version = "0.30.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d10030163d67f681db11810bc486df3149e6d91c8b4f3f96fa8b62b546c2cef8"
dependencies = [
"assert-json-diff",
"colored",
"difference",
"httparse",
"lazy_static",
"log",
"rand",
"regex",
"serde_json",
"serde_urlencoded",
]
[[package]]
name = "negentropy"
version = "0.3.1"
@ -2187,7 +2394,7 @@ dependencies = [
"cbc",
"chacha20",
"chacha20poly1305",
"getrandom",
"getrandom 0.2.15",
"instant",
"negentropy 0.3.1",
"negentropy 0.4.3",
@ -2612,7 +2819,7 @@ version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
"getrandom 0.2.15",
]
[[package]]
@ -2659,6 +2866,12 @@ version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
[[package]]
name = "relative-path"
version = "1.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba39f3699c378cd8970968dcbff9c43159ea4cfbd88d43c00b22f2ef10a435d2"
[[package]]
name = "ring"
version = "0.17.8"
@ -2667,13 +2880,42 @@ checksum = "c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d"
dependencies = [
"cc",
"cfg-if",
"getrandom",
"getrandom 0.2.15",
"libc",
"spin",
"untrusted",
"windows-sys 0.52.0",
]
[[package]]
name = "rstest"
version = "0.18.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97eeab2f3c0a199bc4be135c36c924b6590b88c377d416494288c14f2db30199"
dependencies = [
"futures",
"futures-timer",
"rstest_macros",
"rustc_version",
]
[[package]]
name = "rstest_macros"
version = "0.18.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d428f8247852f894ee1be110b375111b586d4fa431f6c46e64ba5a0dcccbe605"
dependencies = [
"cfg-if",
"glob",
"proc-macro2",
"quote",
"regex",
"relative-path",
"rustc_version",
"syn 2.0.90",
"unicode-ident",
]
[[package]]
name = "rstml"
version = "0.11.2"
@ -2724,6 +2966,19 @@ dependencies = [
"semver",
]
[[package]]
name = "rustix"
version = "0.38.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fdb5bc1ae2baa591800df16c9ca78619bf65c0488b41b96ccec5d11220d8c154"
dependencies = [
"bitflags 2.6.0",
"errno",
"libc",
"linux-raw-sys",
"windows-sys 0.59.0",
]
[[package]]
name = "rustls"
version = "0.23.20"
@ -2779,6 +3034,12 @@ dependencies = [
"winapi-util",
]
[[package]]
name = "scoped-tls"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294"
[[package]]
name = "scopeguard"
version = "1.2.0"
@ -3141,6 +3402,20 @@ dependencies = [
"syn 2.0.90",
]
[[package]]
name = "tempfile"
version = "3.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22e5a0acb1f3f55f65cc4a866c361b2fb2a0ff6366785ae6fbb5f85df07ba230"
dependencies = [
"cfg-if",
"fastrand",
"getrandom 0.3.1",
"once_cell",
"rustix",
"windows-sys 0.59.0",
]
[[package]]
name = "termtree"
version = "0.5.1"
@ -3543,7 +3818,7 @@ version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8c5f0a0af699448548ad1a2fbf920fb4bee257eae39953ba95cb84891a0446a"
dependencies = [
"getrandom",
"getrandom 0.2.15",
]
[[package]]
@ -3580,6 +3855,15 @@ version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasi"
version = "0.13.3+wasi-0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26816d2e1a4a36a2940b96c5296ce403917633dff8f3440e9b236ed6f6bacad2"
dependencies = [
"wit-bindgen-rt",
]
[[package]]
name = "wasm-bindgen"
version = "0.2.99"
@ -3647,6 +3931,31 @@ version = "0.2.99"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "943aab3fdaaa029a6e0271b35ea10b72b943135afe9bffca82384098ad0e06a6"
[[package]]
name = "wasm-bindgen-test"
version = "0.3.49"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c61d44563646eb934577f2772656c7ad5e9c90fac78aa8013d776fcdaf24625d"
dependencies = [
"js-sys",
"minicov",
"scoped-tls",
"wasm-bindgen",
"wasm-bindgen-futures",
"wasm-bindgen-test-macro",
]
[[package]]
name = "wasm-bindgen-test-macro"
version = "0.3.49"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54171416ce73aa0b9c377b51cc3cb542becee1cd678204812e8392e5b0e4a031"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.90",
]
[[package]]
name = "wasm-streams"
version = "0.4.2"
@ -3854,6 +4163,15 @@ dependencies = [
"memchr",
]
[[package]]
name = "wit-bindgen-rt"
version = "0.33.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3268f3d866458b787f390cf61f4bbb563b922d091359f9608842999eaee3943c"
dependencies = [
"bitflags 2.6.0",
]
[[package]]
name = "write16"
version = "1.0.0"

View file

@ -8,7 +8,8 @@ crate-type = ["cdylib", "rlib"]
[dependencies]
actix-files = { version = "0.6", optional = true }
actix-test = "0.2"
actix-rt = "2.0"
actix-test = "0.1.5"
actix-web = { version = "4", optional = true, features = ["macros"] }
chrono = "0.4"
console_error_panic_hook = "0.1"
@ -22,10 +23,12 @@ leptos_actix = { version = "0.6", optional = true }
leptos_router = { version = "0.6" }
mio = "0.8"
mockall = "0.11"
mockito = "0.30"
nostr-sdk = "0.37"
paste = "1.0"
wasm-bindgen = "=0.2.99"
rusqlite = { version = "0.27.0", optional = true}
rstest = "0.18"
serde = { version = "1.0", features = ["derive"] }
serde_json="1.0.133"
tempfile = "3.3"

View file

@ -9,6 +9,77 @@ mod db_impl {
use crate::models::item::Item;
use leptos::logging::log;
#[cfg(test)]
mod tests {
use super::*;
use tokio::runtime::Runtime;
use uuid::Uuid;
// Helper function to create test database
async fn create_test_db() -> Database {
let db = Database::new(":memory:").unwrap();
db.create_schema().await.unwrap();
db
}
// Test database schema creation
#[tokio::test]
async fn test_schema_creation() {
let db = create_test_db().await;
// Verify tables exist
let conn = db.conn.lock().await;
let mut stmt = conn.prepare("SELECT name FROM sqlite_master WHERE type='table'").unwrap();
let tables: Vec<String> = stmt.query_map([], |row| row.get(0)).unwrap().collect::<Result<_, _>>().unwrap();
assert!(tables.contains(&"urls".to_string()));
assert!(tables.contains(&"items".to_string()));
assert!(tables.contains(&"properties".to_string()));
assert!(tables.contains(&"item_properties".to_string()));
}
// Item Lifecycle Tests
#[tokio::test]
async fn test_full_item_lifecycle() {
let db = create_test_db().await;
let test_url = "https://example.com";
let test_item = Item {
id: Uuid::new_v4().to_string(),
name: "Test Item".into(),
description: "Test Description".into(),
wikidata_id: Some("Q123".into()),
custom_properties: vec![
("price".into(), "100".into()),
("color".into(), "red".into())
].into_iter().collect(),
};
// Test insertion
db.insert_item_by_url(test_url, &test_item).await.unwrap();
// Test retrieval
let items = db.get_items_by_url(test_url).await.unwrap();
assert_eq!(items.len(), 1);
let stored_item = &items[0];
assert_eq!(stored_item.name, test_item.name);
assert_eq!(stored_item.custom_properties.len(), 2);
// Test update
let mut updated_item = test_item.clone();
updated_item.name = "Updated Name".into();
db.insert_item_by_url(test_url, &updated_item).await.unwrap();
// Verify update
let items = db.get_items_by_url(test_url).await.unwrap();
assert_eq!(items[0].name, "Updated Name");
// Test deletion
db.delete_item_by_url(test_url, &test_item.id).await.unwrap();
let items = db.get_items_by_url(test_url).await.unwrap();
assert!(items.is_empty());
}
}
// Define a struct to represent a database connection
#[derive(Debug)]
pub struct Database {