From 3dd15210fff4cb5f41c7767092532ce2bef73a0a Mon Sep 17 00:00:00 2001 From: imotai Date: Wed, 9 Aug 2023 10:39:49 +0800 Subject: [PATCH] test: add test case --- sdk/package.json | 2 +- sdk/src/index.ts | 3 ++- sdk/src/store/document_v2.ts | 13 +++++++++++++ sdk/tests/client_v2.test.ts | 17 +++++++++++++++++ src/storage/src/doc_store.rs | 6 +++--- 5 files changed, 36 insertions(+), 5 deletions(-) diff --git a/sdk/package.json b/sdk/package.json index dcc2b530..11d127ed 100644 --- a/sdk/package.json +++ b/sdk/package.json @@ -1,6 +1,6 @@ { "name": "db3.js", - "version": "0.4.1", + "version": "0.4.6", "description": "DB3 Network Javascript API", "author": "dbpunk labs", "keywords": [ diff --git a/sdk/src/index.ts b/sdk/src/index.ts index aaba2b9b..904cd043 100644 --- a/sdk/src/index.ts +++ b/sdk/src/index.ts @@ -22,6 +22,7 @@ export { createFromExternal, } from './account/db3_account' export type { Client, ReadClient } from './client/types' +export type { DocumentData, DocumentEntry } from './client/base' export type { Database, Collection, @@ -30,7 +31,7 @@ export type { MutationResult, QueryResult, } from './store/types' -export { addDoc, updateDoc, deleteDoc, queryDoc } from './store/document_v2' +export { addDoc, updateDoc, deleteDoc, queryDoc, getDoc } from './store/document_v2' export { SystemConfig, SystemStatus, Version } from './proto/db3_base' export { diff --git a/sdk/src/store/document_v2.ts b/sdk/src/store/document_v2.ts index 61922907..9c9baef6 100644 --- a/sdk/src/store/document_v2.ts +++ b/sdk/src/store/document_v2.ts @@ -116,6 +116,19 @@ export async function queryDoc( } } +/** + * + * This function gets a document from the database by its ID. + * + * ```ts + * const doc = await getDoc(collection, "10") + * const id = doc.id + * const content = doc.doc + * ``` + * @param col - the instance of collection + * @param id - the id of document + * @returns the {@link DocumentEntry} if the document was found. Otherwise, raises an error. + **/ export async function getDoc(col: Collection, id: string) { const response = await col.db.client.indexer.getDoc( col.db.addr, diff --git a/sdk/tests/client_v2.test.ts b/sdk/tests/client_v2.test.ts index 982e392f..0452fe85 100644 --- a/sdk/tests/client_v2.test.ts +++ b/sdk/tests/client_v2.test.ts @@ -36,6 +36,7 @@ import { deleteDoc, updateDoc, queryDoc, + getDoc } from '../src/store/document_v2' import { createFromPrivateKey, @@ -370,6 +371,21 @@ describe('test db3.js client module', () => { age: 1, }) await new Promise((r) => setTimeout(r, 1000)) + { + const doc = await getDoc(collection, doc2Ret.id) + expect(doc).toBeDefined() + expect(doc.id).toBe(doc2Ret.id) + expect(doc.doc.city).toBe('beijing2') + expect(doc.doc.author).toBe('imotai1') + expect(doc.doc.age).toBe(1) + } + { + try { + const doc = await getDoc(collection, 1000000000000) + except(1).toBe(0) + } catch(e) {} + } + { const queryStr = '/[city = beijing]' const resultSet = await queryDoc( @@ -465,6 +481,7 @@ describe('test db3.js client module', () => { age: 10, }) await new Promise((r) => setTimeout(r, 2000)) + { const queryStr = '/[city = beijing]' const resultSet = await queryDoc( diff --git a/src/storage/src/doc_store.rs b/src/storage/src/doc_store.rs index 5854cce1..d83d0700 100644 --- a/src/storage/src/doc_store.rs +++ b/src/storage/src/doc_store.rs @@ -346,7 +346,7 @@ mod tests { fn doc_get_test() { let (doc_store, id) = prepare_the_dataset(); let doc_str = r#"{"f2":"f2", "f1":"f1"}"#; - if let Ok(value) = doc_store.get_doc(&DB3Address::ZERO, "col1", id) { + if let Ok(Some(value)) = doc_store.get_doc(&DB3Address::ZERO, "col1", id) { let value: serde_json::Value = serde_json::from_str(value.as_str()).unwrap(); let right: serde_json::Value = serde_json::from_str(doc_str).unwrap(); assert_eq!(value, right); @@ -414,7 +414,7 @@ mod tests { fn doc_store_smoke_test() { let (doc_store, id) = prepare_the_dataset(); let db_id = DB3Address::ZERO; - if let Ok(value) = doc_store.get_doc(&db_id, "col1", id) { + if let Ok(Some(value)) = doc_store.get_doc(&db_id, "col1", id) { println!("{}", value.as_str()); let value: serde_json::Value = serde_json::from_str(value.as_str()).unwrap(); assert_eq!(value["f1"].as_str(), Some("f1")); @@ -472,7 +472,7 @@ mod tests { assert!(false); } - if let Ok(value) = doc_store.get_doc(&db_id, "col1", id) { + if let Ok(Some(value)) = doc_store.get_doc(&db_id, "col1", id) { let value: serde_json::Value = serde_json::from_str(value.as_str()).unwrap(); assert_eq!(value["test"].as_str(), Some("v2")); } else {