Skip to content

Commit

Permalink
Upgrade dependencies and add builder tests (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
devashishdxt authored Jul 4, 2024
1 parent c8c7605 commit 8226b22
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 17 deletions.
22 changes: 11 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = ["futures"]
default = ["builder"]
doc = []
builder = ["futures"]
futures = ["tokio"]

[dependencies]
js-sys = "0.3.67"
num-traits = "0.2.17"
thiserror = "1.0.56"
tokio = { version = "1.35.1", features = ["sync"], optional = true }
wasm-bindgen = "0.2.90"
web-sys = { version = "0.3.67", features = [
js-sys = "0.3.69"
num-traits = "0.2.19"
thiserror = "1.0.61"
tokio = { version = "1.38.0", features = ["sync"], optional = true }
wasm-bindgen = "0.2.92"
web-sys = { version = "0.3.69", features = [
"DomException",
"DomStringList",
"Event",
Expand Down Expand Up @@ -53,10 +53,10 @@ gloo = { version = "0.11.0", default-features = false, features = [
"timers",
"futures",
] }
serde = "1.0.195"
serde-wasm-bindgen = "0.6.3"
serde_json = "1.0.111"
wasm-bindgen-test = "0.3.40"
serde = "1.0.203"
serde-wasm-bindgen = "0.6.5"
serde_json = "1.0.120"
wasm-bindgen-test = "0.3.42"

[package.metadata.docs.rs]
all-features = true
4 changes: 1 addition & 3 deletions src/builder/database_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ impl DatabaseBuilder {
let mut stores_to_remove = Vec::new();

for db_store_name in db_store_names {
if !store_names.contains(&db_store_name) {
store_names.remove(&db_store_name);
} else {
if !store_names.remove(&db_store_name) {
stores_to_remove.push(db_store_name);
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/builder/object_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ impl ObjectStoreBuilder {
let mut indexes_to_remove = Vec::new();

for db_index_name in db_index_names {
if index_names.contains(db_index_name.as_str()) {
index_names.remove(db_index_name.as_str());
} else {
if !index_names.remove(db_index_name.as_str()) {
indexes_to_remove.push(db_index_name);
}
}
Expand Down
106 changes: 106 additions & 0 deletions tests/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use idb::{
builder::{DatabaseBuilder, IndexBuilder, ObjectStoreBuilder},
Factory, KeyPath, TransactionMode,
};
use wasm_bindgen_test::wasm_bindgen_test;

#[wasm_bindgen_test]
async fn test_database_builder_name_and_version() {
let factory = Factory::new().unwrap();
factory.delete("test").unwrap().await.unwrap();

let database = DatabaseBuilder::new("test")
.version(1)
.build()
.await
.unwrap();

assert_eq!(database.name(), "test");
assert_eq!(database.version(), Ok(1));

database.close();

factory.delete("test").unwrap().await.unwrap();
}

#[wasm_bindgen_test]
async fn test_database_builder_store_names() {
let factory = Factory::new().unwrap();
factory.delete("test").unwrap().await.unwrap();

let database = DatabaseBuilder::new("test")
.version(1)
.add_object_store(ObjectStoreBuilder::new("store1"))
.add_object_store(ObjectStoreBuilder::new("store2"))
.add_object_store(ObjectStoreBuilder::new("store3"))
.build()
.await
.unwrap();

let store_names = database.store_names();
assert_eq!(store_names.len(), 3);
assert!(store_names.contains(&"store1".to_string()));
assert!(store_names.contains(&"store2".to_string()));
assert!(store_names.contains(&"store3".to_string()));

database.close();
factory.delete("test").unwrap().await.unwrap();
}

#[wasm_bindgen_test]
async fn test_database_builder_store_names_with_index() {
let factory = Factory::new().unwrap();
factory.delete("test").unwrap().await.unwrap();

let database = DatabaseBuilder::new("test")
.version(1)
.add_object_store(
ObjectStoreBuilder::new("store1").add_index(IndexBuilder::new(
"index1".to_string(),
KeyPath::new_single("id"),
)),
)
.add_object_store(
ObjectStoreBuilder::new("store2").add_index(IndexBuilder::new(
"index2".to_string(),
KeyPath::new_single("id"),
)),
)
.add_object_store(
ObjectStoreBuilder::new("store3").add_index(IndexBuilder::new(
"index3".to_string(),
KeyPath::new_single("id"),
)),
)
.build()
.await
.unwrap();

let store_names = database.store_names();
assert_eq!(store_names.len(), 3);
assert!(store_names.contains(&"store1".to_string()));
assert!(store_names.contains(&"store2".to_string()));
assert!(store_names.contains(&"store3".to_string()));

let transaction = database
.transaction(&["store1", "store2", "store3"], TransactionMode::ReadOnly)
.unwrap();

let store1 = transaction.object_store("store1").unwrap();
let index1 = store1.index_names();
assert_eq!(1, index1.len());
assert!(index1.contains(&"index1".to_string()));

let store2 = transaction.object_store("store2").unwrap();
let index2 = store2.index_names();
assert_eq!(1, index2.len());
assert!(index2.contains(&"index2".to_string()));

let store3 = transaction.object_store("store3").unwrap();
let index3 = store3.index_names();
assert_eq!(1, index3.len());
assert!(index3.contains(&"index3".to_string()));

database.close();
factory.delete("test").unwrap().await.unwrap();
}
3 changes: 3 additions & 0 deletions tests/web.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#![cfg(target_arch = "wasm32")]

mod builder;
mod cursor;
mod database;
mod factory;
Expand Down

0 comments on commit 8226b22

Please sign in to comment.