-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(test): moved logic outside individual tests
- Loading branch information
Showing
4 changed files
with
102 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
@prefix ex: <http://example.org#> . | ||
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . | ||
|
||
ex:SomeSquare a ex:Square; | ||
ex:x "0"^^xsd:integer; | ||
ex:y "0"^^xsd:integer; | ||
ex:size "1"^^xsd:integer. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,97 @@ | ||
import { validate } from "../src"; | ||
import { SimpleStream } from "@ajuvercr/js-runner"; | ||
import { describe, test, expect } from "vitest"; | ||
import { describe, test, expect, beforeEach } from "vitest"; | ||
import * as fs from "fs"; | ||
|
||
describe("shacl", () => { | ||
// Valid point. | ||
const valid = fs.readFileSync("./tests/data/valid.ttl").toString(); | ||
const validReport = fs | ||
.readFileSync("./tests/data/valid.report.ttl") | ||
.toString(); | ||
// Channel which streams incoming RDF. | ||
let incoming: SimpleStream<string>; | ||
|
||
// Invalid point. | ||
const invalid = fs.readFileSync("./tests/data/invalid.ttl").toString(); | ||
const invalidReport = fs | ||
.readFileSync("./tests/data/invalid.report.ttl") | ||
.toString(); | ||
// Output channel, if successful. | ||
let outgoing: SimpleStream<string>; | ||
let outgoingData: string; | ||
|
||
// SHACL data. | ||
const shapePath = "./tests/shacl/point.ttl"; | ||
// Reporting channel, if invalid shape is found. | ||
let report: SimpleStream<string>; | ||
let reportData: string; | ||
|
||
test("successful", async () => { | ||
expect.assertions(2); | ||
// Valid point. | ||
const validRdfData = fs.readFileSync("./tests/data/valid.ttl").toString(); | ||
|
||
// Function parameters. | ||
const incoming = new SimpleStream<string>(); | ||
const outgoing = new SimpleStream<string>(); | ||
const report = new SimpleStream<string>(); | ||
// Invalid point. | ||
const invalidRdfData = fs.readFileSync("./tests/data/invalid.ttl").toString(); | ||
const invalidRdfReport = fs | ||
.readFileSync("./tests/data/invalid.report.ttl") | ||
.toString(); | ||
|
||
outgoing.on("data", (data) => { | ||
expect(data).toEqual(valid); | ||
}); | ||
const unknownRdfData = fs.readFileSync("./tests/data/square.ttl").toString(); | ||
|
||
report.on("data", (data) => { | ||
expect(data).toEqual(validReport); | ||
}); | ||
// SHACL data. | ||
const shaclPath = "./tests/shacl/point.ttl"; | ||
|
||
// Initialize and execute the function. | ||
const func = await validate(shapePath, incoming, outgoing, report); | ||
await func(); | ||
// Utility function which waits for all streams to settle. | ||
async function endAll(): Promise<void> { | ||
await incoming.end(); | ||
await outgoing.end(); | ||
await report.end(); | ||
} | ||
|
||
// Send point into the pipeline. | ||
await incoming.push(valid); | ||
beforeEach(async () => { | ||
// Reset and start the streams. | ||
incoming = new SimpleStream<string>(); | ||
outgoing = new SimpleStream<string>(); | ||
report = new SimpleStream<string>(); | ||
|
||
// Finish testing. | ||
await incoming.end(); | ||
await outgoing.end(); | ||
await report.end(); | ||
// Reset the data itself. | ||
outgoingData = ""; | ||
reportData = ""; | ||
|
||
// Reinitialize the data handlers. | ||
outgoing.on("data", (data) => { | ||
outgoingData += data; | ||
}); | ||
|
||
test("invalid", async () => { | ||
expect.assertions(1); | ||
report.on("data", (data) => { | ||
reportData += data; | ||
}); | ||
|
||
// Restart the processor, which is the same for each test. | ||
const func = await validate({ | ||
path: shaclPath, | ||
incoming, | ||
outgoing, | ||
report, | ||
}); | ||
await func(); | ||
}); | ||
|
||
describe("shacl", () => { | ||
test("successful", async () => { | ||
expect.assertions(2); | ||
|
||
await incoming.push(validRdfData); | ||
await endAll(); | ||
|
||
expect(outgoingData).toEqual(validRdfData); | ||
expect(reportData).toEqual(""); | ||
}); | ||
|
||
// Function parameters. | ||
const incoming = new SimpleStream<string>(); | ||
const outgoing = new SimpleStream<string>(); | ||
const error = new SimpleStream<string>(); | ||
test("invalid", async () => { | ||
expect.assertions(2); | ||
|
||
outgoing.on("data", () => { | ||
expect(true).toBeFalsy(); | ||
}); | ||
await incoming.push(invalidRdfData); | ||
await endAll(); | ||
|
||
error.on("data", (data) => { | ||
expect(data).toEqual(invalidReport); | ||
}); | ||
expect(outgoingData).toEqual(""); | ||
expect(reportData).toEqual(invalidRdfReport); | ||
}); | ||
|
||
// Initialize and execute the function. | ||
const func = await validate(shapePath, incoming, outgoing, error); | ||
await func(); | ||
test("unknown", async () => { | ||
expect.assertions(2); | ||
|
||
// Send point into the pipeline. | ||
await incoming.push(invalid); | ||
await incoming.push(unknownRdfData); | ||
await endAll(); | ||
|
||
// Finish testing. | ||
await incoming.end(); | ||
await outgoing.end(); | ||
await error.end(); | ||
expect(outgoingData).toEqual(unknownRdfData); | ||
expect(reportData).toEqual(""); | ||
}); | ||
}); |