Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Sep 11, 2024
1 parent b93f48f commit bf0627d
Show file tree
Hide file tree
Showing 14 changed files with 11,106 additions and 84 deletions.
82 changes: 50 additions & 32 deletions lib/matchers.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,81 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { Term } from '@rdfjs/types'
import chai from 'chai'
import { util } from 'chai'
import type { AnyPointer } from 'clownface'
import toNT from '@rdfjs/to-ntriples'
import type deepEqual from 'deep-eql'

declare global {
/* eslint-disable @typescript-eslint/no-namespace */
namespace Chai {
interface Assertion {
term: Assertion
interface Config {
deepEqual: <L, R>(expected: L, actual: R) => void
}

interface ChaiUtils {
eql: typeof deepEqual
}
}
}

const plugin: Chai.ChaiPlugin = (_chai, utils) => {
const plugin: Chai.ChaiPlugin = (_chai) => {
_chai.config.deepEqual = (expected, actual) => {
return util.eql(expected, actual, {
comparator: (obj: unknown, other: Term | unknown) => {
if (isTermOrPointer(obj) && isTermOrPointer<Term>(other)) {
return 'terms' in obj ? other.equals(obj.term) : other.equals(obj)
}

return null
},
})
}

const Assertion = _chai.Assertion

Assertion.overwriteMethod('eq', function (_super) {
return function (this: any, other: Term) {
if (utils.flag(this, 'rdfjs-term')) {
const obj: AnyPointer | Term = this._obj
;['eq', 'equal', 'equals'].forEach((eq) => {
Assertion.overwriteMethod(eq, function (_super) {
return function (this: any, other: Term) {
const obj: AnyPointer | Term | unknown = this._obj

if (isTermOrPointer(obj)) {
if ('terms' in obj) {
if (!obj.term) {
return this.assert(
false,
'expected a pointer with single term #{exp} but got #{act} terms',
'expected a pointer with single term not to equal #{exp}',
toNT(other),
obj.terms.length,
)
}

let term: Term
if ('terms' in obj) {
if (!obj.term) {
return this.assert(
false,
'expected a pointer with single term #{exp} but got #{act} terms',
'expected a pointer with single term not to equal #{exp}',
other.equals(obj.term),
'expected a pointer to #{exp} but got #{act}',
'expected a pointer not to equal #{exp}',
toNT(other),
obj.terms.length,
toNT(obj.term),
)
}

return this.assert(
other.equals(obj.term),
'expected a pointer to #{exp} but got #{act}',
'expected a pointer not to equal #{exp}',
other.equals(obj),
'expected #{this} to equal #{exp}',
'expected #{this} not to equal #{exp}',
toNT(other),
toNT(obj.term),
toNT(obj),
)
}

return this.assert(
other.equals(obj),
'expected #{this} to equal #{exp}',
'expected #{this} not to equal #{exp}',
toNT(other),
toNT(obj),
)
_super.call(this, other)
}

_super.call(this, other)
}
})
})
}

utils.addProperty(chai.Assertion.prototype, 'term', function (this: Chai.Assertion) {
utils.flag(this, 'rdfjs-term', true)
})
function isTermOrPointer<T extends Term | AnyPointer>(value: T | unknown): value is T {
return typeof value === 'object' && value !== null && ('termType' in value || 'terms' in value)
}

export default plugin
17 changes: 0 additions & 17 deletions lib/package.json

This file was deleted.

9 changes: 6 additions & 3 deletions lib/snapshots.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import chai, { expect } from 'chai'
import * as chai from 'chai'
import { jestSnapshotPlugin } from 'mocha-chai-jest-snapshot'
import rdf from '@zazuko/env-node'

Expand All @@ -12,7 +12,10 @@ declare global {
}
}

chai.use(jestSnapshotPlugin())
if (typeof (chai.Assertion.prototype as any).toMatchSnapshot !== 'function') {
// calling jestSnapshotPlugin multiple times has unwanted side effects
chai.use(jestSnapshotPlugin())
}

chai.use((_chai, utils) => {
const toMatchSnapshot = (chai.Assertion.prototype as any).toMatchSnapshot
Expand All @@ -22,7 +25,7 @@ chai.use((_chai, utils) => {

if (utils.flag(this, 'dataset-canonical')) {
// Custom behavior for dataset
expect(rdf.dataset.toCanonical(obj)).toMatchSnapshot(...args)
chai.expect(rdf.dataset.toCanonical(obj)).toMatchSnapshot(...args)
} else {
// Original behavior
toMatchSnapshot.apply(this, args)
Expand Down
39 changes: 7 additions & 32 deletions lib/sparql-clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,34 @@ import toStream from 'into-stream'
import type { ParsingClient } from 'sparql-http-client/ParsingClient.js'
import type { StreamClient } from 'sparql-http-client/StreamClient.js'

function termToTerm<T extends Term>(term: T): T {
// repackage terms using Zazuko's env-node to fix chai assertions
switch (term.termType) {
case 'NamedNode':
return rdf.namedNode(term.value) as T
case 'BlankNode':
return rdf.blankNode(term.value) as T
case 'Literal':
return rdf.literal(term.value, term.language || term.datatype) as T
case 'Variable':
return rdf.variable(term.value) as T
case 'Quad':
return rdf.quad(
termToTerm(term.subject),
termToTerm(term.predicate),
termToTerm(term.object),
termToTerm(term.graph),
) as T
case 'DefaultGraph':
return rdf.defaultGraph() as T
default:
throw new Error(`Unsupported term type: ${term}`)
}
}

function select(store: Oxigraph.Store, query: string) {
const results: Array<Map<string, Term>> = store.query(query, {
const results = store.query(query, {
use_default_graph_as_union: true,
})
}) as Array<Map<string, Term>>

return results.map((result) => {
const bindings: Record<string, Term> = {}

for (const [key, value] of result.entries()) {
bindings[key] = termToTerm(value)
bindings[key] = value
}

return bindings
})
}

function construct(store: Oxigraph.Store, query: string) {
const results: Array<Quad> = store.query(query, {
const results = store.query(query, {
use_default_graph_as_union: true,
})
}) as Quad[]

return rdf.dataset(results.map(termToTerm))
return rdf.dataset(results)
}

async function ask(store: Oxigraph.Store, query: string): Promise<boolean> {
return store.query(query, {
use_default_graph_as_union: true,
})
}) as boolean
}

async function update(store: Oxigraph.Store, query: string) {
Expand Down
Loading

0 comments on commit bf0627d

Please sign in to comment.