Skip to content

Commit

Permalink
feat: empty store
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Oct 17, 2024
1 parent 16b7c44 commit 14b8011
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-tools-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mocha-chai-rdf": patch
---

Changed the type of `this.rdf.dataset`
5 changes: 5 additions & 0 deletions .changeset/wicked-rice-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mocha-chai-rdf": patch
---

Added `createEmpty` function to `store.js`
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,15 @@ You can customize the options:
- include the default graph by adding `includeDefaultGraph: true`
- load all graphs by adding `loadAll: true`
- control the graph name by adding `sliceTestPath: [number, number]`. The default is `[1, -1]` which, as described above, omits the topmost `describe` block name and the test name itself, hence the first and last part are sliced from the array of block titles.

### Initialise empty store

In case you'd only want to initialise an empty store:

```ts
import { createEmpty } from 'mocha-chai-rdf/store.js'

describe('my test suite', () => {
beforeEach(createEmpty)
})
```
29 changes: 27 additions & 2 deletions lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import assert from 'node:assert'
import * as Oxigraph from 'oxigraph'
import type { NamespaceBuilder } from '@rdfjs/namespace'
import rdf from '@zazuko/env-node'
import type { DatasetCore, NamedNode, Quad_Graph } from '@rdfjs/types'
import type { NamedNode, Quad_Graph } from '@rdfjs/types'
import type { AnyPointer } from 'clownface'
import type { Dataset } from '@zazuko/env/lib/Dataset.js'
import type { StreamClient } from 'sparql-http-client/StreamClient.js'
Expand All @@ -14,7 +14,7 @@ import * as clients from './sparql-clients.js'
declare module 'mocha' {
interface Context {
rdf: {
dataset: DatasetCore
dataset: Dataset
graph: AnyPointer
store: Oxigraph.Store
streamClient: StreamClient
Expand All @@ -38,6 +38,31 @@ type Options = (DatasetSourceOptions | GraphSourceOptions) & {
sliceTestPath?: [number, number]
}

export function createEmpty(this: Mocha.Context) {
const store = new Oxigraph.Store()

const rdfFixture = {
dataset: rdf.dataset(),
graph: rdf.clownface({ dataset: rdf.dataset() }),
store,
streamClient: clients.streamClient(store),
parsingClient: clients.parsingClient(store),
}

Object.defineProperty(this, 'rdf', {
get() {
return rdfFixture
},
configurable: true,
})

afterEach(() => {
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-ignore
delete this.rdf
})
}

export function createStore(base: string, { sliceTestPath = [1, -1], ...options }: Options = { }) {
const baseIRI: string | undefined = typeof options.baseIri === 'string'
? options.baseIri
Expand Down
18 changes: 17 additions & 1 deletion test/tests/store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'chai'
import rdf from '@zazuko/env-node'
import { createStore } from '../../lib/store.js'
import { createEmpty, createStore } from '../../lib/store.js'

describe('store.js', () => {
describe('createStore', () => {
Expand Down Expand Up @@ -63,4 +63,20 @@ describe('store.js', () => {
})
})
})

describe('createEmpty', () => {
before(createEmpty)

it('initializes empty dataset', function () {
expect(this.rdf.dataset).to.have.property('size', 0)
})

it('provides access properties', function () {
expect(this.rdf).to.have.property('graph')
expect(this.rdf).to.have.property('dataset')
expect(this.rdf).to.have.property('store')
expect(this.rdf).to.have.property('streamClient')
expect(this.rdf).to.have.property('parsingClient')
})
})
})

0 comments on commit 14b8011

Please sign in to comment.