Skip to content

Commit

Permalink
tests: add surrealdb data store tests
Browse files Browse the repository at this point in the history
  • Loading branch information
enmand committed Sep 15, 2024
1 parent eff13e6 commit bb875e2
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/dwn-rs-stores/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,6 @@ async-std = { version = "1.12.0", default-features = false, features = [
"pin-project-lite",
] }
memoize = { version = "0.4.2", default-features = false }

[dev-dependencies]
rand = "0.8.5"
81 changes: 81 additions & 0 deletions crates/dwn-rs-stores/src/surrealdb/data_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,84 @@ fn chunks_graph_query() -> Query {

Statement::Select(query).into()
}

#[cfg(test)]
mod test {
use async_std::stream::from_iter;
use futures_util::StreamExt;
use std::iter::repeat_with;

use super::*;
use dwn_rs_core::stores::DataStore;

#[tokio::test]
async fn test_open_close() {
let mut db = SurrealDB::new();
db.connect("mem://").await.unwrap();
db.open().await.unwrap();
db.close().await;
}

#[tokio::test]
async fn test_put_get() {
let mut db = SurrealDB::new();
db.connect("mem://").await.unwrap();
db.open().await.unwrap();

let tenant = "test";
let record_id = "test_put_get";
let cid = "test_put_get_cid";

let data = repeat_with(rand::random::<u8>)
.take(1024 * 1024)
.collect::<Vec<u8>>();

let put = db
.put(tenant, record_id, cid, from_iter(data.clone()))
.await
.unwrap();
assert_eq!(put.size, data.len());

let get = db.get(tenant, record_id, cid).await.unwrap();
assert_eq!(get.size, data.len());

let get_data = get.data.collect::<Vec<u8>>().await;
assert_eq!(data, get_data);

db.close().await;
}

#[tokio::test]
async fn test_delete() {
let mut db = SurrealDB::new();
db.connect("mem://").await.unwrap();
db.open().await.unwrap();

let tenant = "test";
let record_id = "test_delete";
let cid = "test_delete_cid";

let data = repeat_with(rand::random::<u8>)
.take(1024 * 1024)
.collect::<Vec<u8>>();

let put = db
.put(tenant, record_id, cid, from_iter(data.clone()))
.await
.unwrap();
assert_eq!(put.size, data.len());

let get = db.get(tenant, record_id, cid).await.unwrap();
assert_eq!(get.size, data.len());

let get_data = get.data.collect::<Vec<u8>>().await;
assert_eq!(data, get_data);

db.delete(tenant, record_id, cid).await.unwrap();

let get = db.get(tenant, record_id, cid).await;
assert!(get.is_err());

db.close().await;
}
}

0 comments on commit bb875e2

Please sign in to comment.