Skip to content

Commit

Permalink
test: add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
imotai committed Aug 9, 2023
1 parent 3177dab commit 3dd1521
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "db3.js",
"version": "0.4.1",
"version": "0.4.6",
"description": "DB3 Network Javascript API",
"author": "dbpunk labs",
"keywords": [
Expand Down
3 changes: 2 additions & 1 deletion sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions sdk/src/store/document_v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@ export async function queryDoc<T = DocumentData>(
}
}

/**
*
* 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<T = DocumentData>(col: Collection, id: string) {
const response = await col.db.client.indexer.getDoc(
col.db.addr,
Expand Down
17 changes: 17 additions & 0 deletions sdk/tests/client_v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
deleteDoc,
updateDoc,
queryDoc,
getDoc
} from '../src/store/document_v2'
import {
createFromPrivateKey,
Expand Down Expand Up @@ -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<Profile>(
Expand Down Expand Up @@ -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<Profile>(
Expand Down
6 changes: 3 additions & 3 deletions src/storage/src/doc_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 3dd1521

Please sign in to comment.