Skip to content
This repository has been archived by the owner on Jan 1, 2025. It is now read-only.

fix: add higher test coverage for storages. #68

Merged
merged 1 commit into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"rollup-plugin-typescript2": "^0.34.1",
"semantic-release": "^19.0.5",
"semantic-release-monorepo": "^7.0.5",
"sinon": "^16.0.0",
"sinon": "^16.1.3",
"typedoc": "^0.24.8",
"typedoc-plugin-markdown": "^3.15.3",
"typedoc-plugin-merge-modules": "^5.1.0",
Expand All @@ -88,4 +88,4 @@
"dependencies": {
"typedoc-plugin-external-module-map": "^2.0.1"
}
}
}
18 changes: 9 additions & 9 deletions packages/database/coverage/coverage-summary.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{"total": {"lines":{"total":210,"covered":210,"skipped":0,"pct":100},"statements":{"total":221,"covered":221,"skipped":0,"pct":100},"functions":{"total":77,"covered":77,"skipped":0,"pct":100},"branches":{"total":70,"covered":70,"skipped":0,"pct":100},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/home/runner/work/pluto-encrypted/pluto-encrypted/packages/database/src/index.ts": {"lines":{"total":156,"covered":156,"skipped":0,"pct":100},"functions":{"total":67,"covered":67,"skipped":0,"pct":100},"statements":{"total":167,"covered":167,"skipped":0,"pct":100},"branches":{"total":48,"covered":48,"skipped":0,"pct":100}}
,"/home/runner/work/pluto-encrypted/pluto-encrypted/packages/database/src/schemas/Credential.ts": {"lines":{"total":11,"covered":11,"skipped":0,"pct":100},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"statements":{"total":11,"covered":11,"skipped":0,"pct":100},"branches":{"total":4,"covered":4,"skipped":0,"pct":100}}
,"/home/runner/work/pluto-encrypted/pluto-encrypted/packages/database/src/schemas/CredentialRequestMetadata.ts": {"lines":{"total":3,"covered":3,"skipped":0,"pct":100},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"statements":{"total":3,"covered":3,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/home/runner/work/pluto-encrypted/pluto-encrypted/packages/database/src/schemas/DID.ts": {"lines":{"total":1,"covered":1,"skipped":0,"pct":100},"functions":{"total":0,"covered":0,"skipped":0,"pct":100},"statements":{"total":1,"covered":1,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/home/runner/work/pluto-encrypted/pluto-encrypted/packages/database/src/schemas/DIDPair.ts": {"lines":{"total":1,"covered":1,"skipped":0,"pct":100},"functions":{"total":0,"covered":0,"skipped":0,"pct":100},"statements":{"total":1,"covered":1,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/home/runner/work/pluto-encrypted/pluto-encrypted/packages/database/src/schemas/LinkSecret.ts": {"lines":{"total":3,"covered":3,"skipped":0,"pct":100},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"statements":{"total":3,"covered":3,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/home/runner/work/pluto-encrypted/pluto-encrypted/packages/database/src/schemas/Mediator.ts": {"lines":{"total":4,"covered":4,"skipped":0,"pct":100},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"statements":{"total":4,"covered":4,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/home/runner/work/pluto-encrypted/pluto-encrypted/packages/database/src/schemas/Message.ts": {"lines":{"total":3,"covered":3,"skipped":0,"pct":100},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"statements":{"total":3,"covered":3,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/home/runner/work/pluto-encrypted/pluto-encrypted/packages/database/src/schemas/PrivateKey.ts": {"lines":{"total":28,"covered":28,"skipped":0,"pct":100},"functions":{"total":5,"covered":5,"skipped":0,"pct":100},"statements":{"total":28,"covered":28,"skipped":0,"pct":100},"branches":{"total":18,"covered":18,"skipped":0,"pct":100}}
,"/Users/ribo/Projects/personal/pluto-encrypted/packages/database/src/index.ts": {"lines":{"total":156,"covered":156,"skipped":0,"pct":100},"functions":{"total":67,"covered":67,"skipped":0,"pct":100},"statements":{"total":167,"covered":167,"skipped":0,"pct":100},"branches":{"total":48,"covered":48,"skipped":0,"pct":100}}
,"/Users/ribo/Projects/personal/pluto-encrypted/packages/database/src/schemas/Credential.ts": {"lines":{"total":11,"covered":11,"skipped":0,"pct":100},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"statements":{"total":11,"covered":11,"skipped":0,"pct":100},"branches":{"total":4,"covered":4,"skipped":0,"pct":100}}
,"/Users/ribo/Projects/personal/pluto-encrypted/packages/database/src/schemas/CredentialRequestMetadata.ts": {"lines":{"total":3,"covered":3,"skipped":0,"pct":100},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"statements":{"total":3,"covered":3,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/Users/ribo/Projects/personal/pluto-encrypted/packages/database/src/schemas/DID.ts": {"lines":{"total":1,"covered":1,"skipped":0,"pct":100},"functions":{"total":0,"covered":0,"skipped":0,"pct":100},"statements":{"total":1,"covered":1,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/Users/ribo/Projects/personal/pluto-encrypted/packages/database/src/schemas/DIDPair.ts": {"lines":{"total":1,"covered":1,"skipped":0,"pct":100},"functions":{"total":0,"covered":0,"skipped":0,"pct":100},"statements":{"total":1,"covered":1,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/Users/ribo/Projects/personal/pluto-encrypted/packages/database/src/schemas/LinkSecret.ts": {"lines":{"total":3,"covered":3,"skipped":0,"pct":100},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"statements":{"total":3,"covered":3,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/Users/ribo/Projects/personal/pluto-encrypted/packages/database/src/schemas/Mediator.ts": {"lines":{"total":4,"covered":4,"skipped":0,"pct":100},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"statements":{"total":4,"covered":4,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/Users/ribo/Projects/personal/pluto-encrypted/packages/database/src/schemas/Message.ts": {"lines":{"total":3,"covered":3,"skipped":0,"pct":100},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"statements":{"total":3,"covered":3,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/Users/ribo/Projects/personal/pluto-encrypted/packages/database/src/schemas/PrivateKey.ts": {"lines":{"total":28,"covered":28,"skipped":0,"pct":100},"functions":{"total":5,"covered":5,"skipped":0,"pct":100},"statements":{"total":28,"covered":28,"skipped":0,"pct":100},"branches":{"total":18,"covered":18,"skipped":0,"pct":100}}
}
2 changes: 1 addition & 1 deletion packages/indexdb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ const database = db = await Database.createEncrypted(
## QA & Documentation
| Statements | Branches | Functions | Lines |
| --------------------------- | ----------------------- | ------------------------- | ----------------- |
| ![Statements](https://img.shields.io/badge/statements-76.25%25-red.svg?style=flat) | ![Branches](https://img.shields.io/badge/branches-64.12%25-red.svg?style=flat) | ![Functions](https://img.shields.io/badge/functions-76.81%25-red.svg?style=flat) | ![Lines](https://img.shields.io/badge/lines-77%25-red.svg?style=flat) |
| ![Statements](https://img.shields.io/badge/statements-92.68%25-brightgreen.svg?style=flat) | ![Branches](https://img.shields.io/badge/branches-80%25-yellow.svg?style=flat) | ![Functions](https://img.shields.io/badge/functions-94.64%25-brightgreen.svg?style=flat) | ![Lines](https://img.shields.io/badge/lines-93.64%25-brightgreen.svg?style=flat) |
12 changes: 6 additions & 6 deletions packages/indexdb/coverage/coverage-summary.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{"total": {"lines":{"total":287,"covered":221,"skipped":0,"pct":77},"statements":{"total":299,"covered":228,"skipped":0,"pct":76.25},"functions":{"total":69,"covered":53,"skipped":0,"pct":76.81},"branches":{"total":131,"covered":84,"skipped":0,"pct":64.12},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/home/runner/work/pluto-encrypted/pluto-encrypted/packages/indexdb/src/index.ts": {"lines":{"total":1,"covered":1,"skipped":0,"pct":100},"functions":{"total":0,"covered":0,"skipped":0,"pct":100},"statements":{"total":1,"covered":1,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/home/runner/work/pluto-encrypted/pluto-encrypted/packages/indexdb/src/storage-dexie/dexie-helper.ts": {"lines":{"total":95,"covered":86,"skipped":0,"pct":90.52},"functions":{"total":22,"covered":21,"skipped":0,"pct":95.45},"statements":{"total":100,"covered":90,"skipped":0,"pct":90},"branches":{"total":48,"covered":39,"skipped":0,"pct":81.25}}
,"/home/runner/work/pluto-encrypted/pluto-encrypted/packages/indexdb/src/storage-dexie/dexie-query.ts": {"lines":{"total":67,"covered":30,"skipped":0,"pct":44.77},"functions":{"total":14,"covered":5,"skipped":0,"pct":35.71},"statements":{"total":71,"covered":32,"skipped":0,"pct":45.07},"branches":{"total":32,"covered":10,"skipped":0,"pct":31.25}}
,"/home/runner/work/pluto-encrypted/pluto-encrypted/packages/indexdb/src/storage-dexie/rx-storage-dexie.ts": {"lines":{"total":10,"covered":10,"skipped":0,"pct":100},"functions":{"total":3,"covered":3,"skipped":0,"pct":100},"statements":{"total":10,"covered":10,"skipped":0,"pct":100},"branches":{"total":3,"covered":3,"skipped":0,"pct":100}}
,"/home/runner/work/pluto-encrypted/pluto-encrypted/packages/indexdb/src/storage-dexie/rx-storage-instance-dexie.ts": {"lines":{"total":114,"covered":94,"skipped":0,"pct":82.45},"functions":{"total":30,"covered":24,"skipped":0,"pct":80},"statements":{"total":117,"covered":95,"skipped":0,"pct":81.19},"branches":{"total":48,"covered":32,"skipped":0,"pct":66.66}}
{"total": {"lines":{"total":236,"covered":221,"skipped":0,"pct":93.64},"statements":{"total":246,"covered":228,"skipped":0,"pct":92.68},"functions":{"total":56,"covered":53,"skipped":0,"pct":94.64},"branches":{"total":105,"covered":84,"skipped":0,"pct":80},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/Users/ribo/Projects/personal/pluto-encrypted/packages/indexdb/src/index.ts": {"lines":{"total":1,"covered":1,"skipped":0,"pct":100},"functions":{"total":0,"covered":0,"skipped":0,"pct":100},"statements":{"total":1,"covered":1,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/Users/ribo/Projects/personal/pluto-encrypted/packages/indexdb/src/storage-dexie/dexie-helper.ts": {"lines":{"total":95,"covered":86,"skipped":0,"pct":90.52},"functions":{"total":22,"covered":21,"skipped":0,"pct":95.45},"statements":{"total":100,"covered":90,"skipped":0,"pct":90},"branches":{"total":48,"covered":39,"skipped":0,"pct":81.25}}
,"/Users/ribo/Projects/personal/pluto-encrypted/packages/indexdb/src/storage-dexie/dexie-query.ts": {"lines":{"total":32,"covered":30,"skipped":0,"pct":93.75},"functions":{"total":6,"covered":5,"skipped":0,"pct":83.33},"statements":{"total":35,"covered":32,"skipped":0,"pct":91.42},"branches":{"total":14,"covered":10,"skipped":0,"pct":71.42}}
,"/Users/ribo/Projects/personal/pluto-encrypted/packages/indexdb/src/storage-dexie/rx-storage-dexie.ts": {"lines":{"total":10,"covered":10,"skipped":0,"pct":100},"functions":{"total":3,"covered":3,"skipped":0,"pct":100},"statements":{"total":10,"covered":10,"skipped":0,"pct":100},"branches":{"total":3,"covered":3,"skipped":0,"pct":100}}
,"/Users/ribo/Projects/personal/pluto-encrypted/packages/indexdb/src/storage-dexie/rx-storage-instance-dexie.ts": {"lines":{"total":98,"covered":94,"skipped":0,"pct":95.91},"functions":{"total":25,"covered":24,"skipped":0,"pct":96},"statements":{"total":100,"covered":95,"skipped":0,"pct":95},"branches":{"total":40,"covered":32,"skipped":0,"pct":80}}
}
104 changes: 3 additions & 101 deletions packages/indexdb/src/storage-dexie/dexie-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,63 +14,16 @@
// } from './dexie-helper';
// import type { RxStorageInstanceDexie } from './rx-storage-instance-dexie';

import { type DefaultPreparedQuery, INDEX_MIN, type RxDocumentData, type RxQueryPlan, type RxStorageQueryResult, getQueryMatcher, getSortComparator } from 'rxdb'
import { type DefaultPreparedQuery, type RxDocumentData, type RxStorageQueryResult, getQueryMatcher, getSortComparator } from 'rxdb'
import { type QueryMatcher, type RxJsonSchema } from 'rxdb/dist/types/types'
import { DEXIE_DOCS_TABLE_NAME, dexieReplaceIfStartsWithPipe, fromDexieToStorage } from './dexie-helper'
import { dexieReplaceIfStartsWithPipe, fromDexieToStorage } from './dexie-helper'
import { type RxStorageInstanceDexie } from './rx-storage-instance-dexie'
import { fixTxPipe } from '@pluto-encrypted/shared'

export function mapKeyForKeyRange (k: any) {
if (k === INDEX_MIN) {
return -Infinity
} else {
return k
}
}

export function getKeyRangeByQueryPlan (
queryPlan: RxQueryPlan,
IDBKeyRange?: any
) {
if (!IDBKeyRange) {
if (typeof window === 'undefined') {
throw new Error('IDBKeyRange missing')
} else {
IDBKeyRange = window.IDBKeyRange
}
}

const startKeys = queryPlan.startKeys.map(mapKeyForKeyRange)
const endKeys = queryPlan.endKeys.map(mapKeyForKeyRange)

let ret: any
/**
* If index has only one field,
* we have to pass the keys directly, not the key arrays.
*/
if (queryPlan.index.length === 1) {
const equalKeys = startKeys[0] === endKeys[0]
ret = IDBKeyRange.bound(
startKeys[0],
endKeys[0],
equalKeys ? false : !queryPlan.inclusiveStart,
equalKeys ? false : !queryPlan.inclusiveEnd
)
} else {
ret = IDBKeyRange.bound(
startKeys,
endKeys,
!queryPlan.inclusiveStart,
!queryPlan.inclusiveEnd
)
}
return ret
}

/**
* Runs mango queries over the Dexie.js database.
*/
export async function dexieQuery<RxDocType> (
export async function dexieQuery<RxDocType>(
instance: RxStorageInstanceDexie<RxDocType>,
preparedQuery: DefaultPreparedQuery<RxDocType>,
schema: Readonly<RxJsonSchema<RxDocumentData<RxDocType>>>
Expand Down Expand Up @@ -137,54 +90,3 @@ export async function dexieQuery<RxDocType> (
documents: rows
}
}

export async function dexieCount<RxDocType> (
instance: RxStorageInstanceDexie<RxDocType>,
preparedQuery: DefaultPreparedQuery<RxDocType>
): Promise<number> {
const state = await instance.internals
const queryPlan = preparedQuery.queryPlan
const queryPlanFields: string[] = queryPlan.index

const keyRange = getKeyRangeByQueryPlan(
queryPlan,
(state.dexieDb as any)._options.IDBKeyRange
)
let count: number = -1
await state.dexieDb.transaction(
'r',
state.dexieTable,
async (dexieTx) => {
const tx = (dexieTx as any).idbtrans
const store = tx.objectStore(DEXIE_DOCS_TABLE_NAME)
let index: any
if (
queryPlanFields.length === 1 &&
queryPlanFields[0] === instance.primaryPath
) {
index = store
} else {
let indexName: string
if (queryPlanFields.length === 1) {
indexName = dexieReplaceIfStartsWithPipe(queryPlanFields[0]!)
} else {
indexName = '[' +
queryPlanFields
.map(field => dexieReplaceIfStartsWithPipe(field))
.join('+') +
']'
}
index = store.index(indexName)
}

const request = index.count(keyRange)
count = await new Promise<number>((resolve, reject) => {
request.onsuccess = function () {
resolve(request.result)
}
request.onerror = (err: any) => { reject(err) }
})
}
)
return count
}
Loading
Loading