Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests for utilities and classes #5 #14

Merged
merged 11 commits into from
Dec 7, 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
5 changes: 4 additions & 1 deletion .github/workflows/github-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ jobs:

strategy:
matrix:
node-version: [18.x]
# Using Node LTS https://nodejs.org/en
node-version: [20.10.0]

steps:
- run: echo "Job was automatically triggered by a ${{ github.event_name }} event on ${{ github.ref }} - ${{ github.repository }}."
Expand All @@ -39,6 +40,8 @@ jobs:
run: npm install
- name: Build
run: npm run build
- name: LDWorkbench init & test run
run: rm -rf ./pipelines && npx ld-workbench --init && npx ld-workbench
- name: Test
run: npm run test
- run: echo "Job status ${{ job.status }}."
25 changes: 24 additions & 1 deletion package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
},
"devDependencies": {
"@types/chai": "^4.3.11",
"@types/chai-as-promised": "^7.1.8",
"@types/inquirer": "^9.0.7",
"@types/js-yaml": "^4.0.9",
"@types/lodash.kebabcase": "^4.1.9",
Expand All @@ -65,6 +66,7 @@
"@types/sparqljs": "^3.1.8",
"@typescript-eslint/eslint-plugin": "^6.12.0",
"chai": "^4.3.10",
"chai-as-promised": "^7.1.1",
"commander": "^11.1.0",
"eslint": "^8.54.0",
"eslint-config-prettier": "^9.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/lib/File.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { isFile, isFilePathString } from '../utils/guards.js'
import { dirname } from 'path'

export default class File {
public static $id = 'File'
public readonly $id = 'File'
private $isValid?: boolean
public constructor(private $path: string, private readonly skipExistsCheck: boolean = false) {}

Expand Down
1 change: 1 addition & 0 deletions src/lib/Stage.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Stage extends EventEmitter {
this.generator.on('data', quad => {
writer.addQuad(quad)
quadCount ++
this.emit('generatorResult', quadCount)
})
this.generator.on('end', _ => {
generatorCount++
Expand Down
85 changes: 85 additions & 0 deletions src/lib/tests/File.class.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import * as fs from 'fs';
import * as path from 'path';
import File from "../File.class.js"
import * as chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
chai.use(chaiAsPromised)
const expect = chai.expect

describe('File Class', () => {
describe('constructor', () => {
it('should set properties correctly', () => {
const file = new File(`file://${path.join('./static/example/config.yml')}`)
expect(file).to.be.an.instanceOf(File);
expect(file).to.have.property('$path');
expect(file).to.have.property('skipExistsCheck');
expect(file).to.have.property('$id');
});
});
describe('validate', () => {
it('should validate a valid file path', () => {
const path = './static/example/config.yml';
const validFilePath = `file://${path}`;
const file = new File(validFilePath)
expect(file.validate());
expect(file.path).to.equal(path);
});

it('should throw an error for an invalid file path', () => {
const filePath = 'invalid/file/path.txt';
const file = new File(filePath);
expect((file.validate.bind(file))).to.throw('The filename `invalid/file/path.txt` should start with `file://`');
});

it('should throw an error if file does not exist', () => {
const filePath = 'file://nonexistent/file.txt';
const file = new File(filePath);
expect(file.validate.bind(file)).to.throw('File not found: `nonexistent/file.txt`');
});

it('should skip exists check when skipExistsCheck is true', () => {
const filePath = 'file://nonexistent/file.txt';
const file = new File(filePath, true);
expect(() => file.validate()).to.not.throw();
expect(file.path).to.equal('nonexistent/file.txt');
});
});

describe('getStream', () => {
beforeEach(() => {
const filePath = 'file.txt';
if (!fs.existsSync(filePath)) {
fs.writeFileSync(filePath, 'Initial content');
}
});
afterEach(() => {
const filePath = 'file.txt';
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
if (fs.existsSync('./new')) {
fs.rmSync('./new', { recursive: true, force: true });
}
});
it('should create a write stream for a new file', () => {
const filePath = 'new/file.txt';
const file = new File(filePath);
const writeStream = file.getStream();
expect(writeStream).to.be.an.instanceOf(fs.WriteStream);
});

it('should append to an existing file when append is true', () => {
const filePath = 'file.txt';
const file = new File(filePath);
const writeStream = file.getStream(true);
expect(writeStream).to.be.an.instanceOf(fs.WriteStream);
});
it('should create parent directories if they do not exist', () => {
const filePath = 'file://new/directory/nested/file.txt';
const file = new File(filePath, true).validate();
const writeStream = file.getStream();
expect(writeStream).to.be.an.instanceOf(fs.WriteStream);
expect(fs.existsSync(path.dirname(filePath).replace('file://', ''))).to.equal(true);
});
});
});
66 changes: 66 additions & 0 deletions src/lib/tests/Generator.class.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import Generator from "../Generator.class.js";
import { EventEmitter } from 'events';
import Stage from "../Stage.class.js";
import parseYamlFile from "../../utils/parseYamlFile.js";
import Pipeline from "../Pipeline.class.js";
import * as chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
import { NamedNode } from "n3";
chai.use(chaiAsPromised)
const expect = chai.expect

describe('Generator Class', () => {
describe('constructor', () => {
it('should set query, engine, endpoint, and source properties correctly', () => {
const configuration = parseYamlFile('./static/example/config.yml')
const pipeline = new Pipeline(configuration)
const stageConfig = configuration.stages[0]
const stage = new Stage(pipeline, stageConfig)
const generator = new Generator(stage)
expect(generator).to.be.an.instanceOf(Generator);
expect(generator).to.be.an.instanceOf(EventEmitter);
expect(generator).to.have.property('query');
expect(generator).to.have.property('engine');
expect(generator).to.have.property('endpoint');
expect(generator).to.have.property('source');
});
});
// BUG when both the generator and iterator tests are running, it seems the iterator will never terminate
describe.skip('run', () => {
it('should emit "data" and "end" events with the correct number of statements', async () => {
const configuration = parseYamlFile('./static/example/config.yml')
const pipeline = new Pipeline(configuration)
const stageConfig = configuration.stages[0]
const stage = new Stage(pipeline, stageConfig)
const generator = new Generator(stage);
const emittedEvents: any[] = [];

const testNamedNode = new NamedNode('https://triplydb.com/triply/iris/id/floweringPlant/00106');

async function runGeneratorWithPromise(): Promise<boolean> {
return new Promise((resolve, reject) => {
generator.addListener('data', (quad) => {
emittedEvents.push({ event: 'data', quad });
});
generator.addListener('end', (numResults) => {
emittedEvents.push({ event: 'end', numResults });
resolve(true);
});
generator.addListener('error', (error) => {
reject(error);
});
generator.run(testNamedNode);
});
}

await runGeneratorWithPromise()
expect(emittedEvents).to.have.lengthOf(4);
expect(emittedEvents[0].event).to.equal('data');
expect(emittedEvents[0].quad._subject.id).to.equal('https://triplydb.com/triply/iris/id/floweringPlant/00106')
expect(emittedEvents[0].quad._predicate.id).to.equal('http://www.w3.org/1999/02/22-rdf-syntax-ns#type')
expect(emittedEvents[0].quad._object.id).to.equal('https://schema.org/Thing')
expect(emittedEvents[emittedEvents.length - 1].event).to.equal('end');
expect(emittedEvents[emittedEvents.length - 1].numResults).to.equal(3);
});
});
});
63 changes: 63 additions & 0 deletions src/lib/tests/Iterator.class.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import Iterator from "../Iterator.class.js";
import { EventEmitter } from 'events';
import Stage from "../Stage.class.js";
import Pipeline from "../Pipeline.class.js";
import parseYamlFile from "../../utils/parseYamlFile.js";
import * as chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
chai.use(chaiAsPromised)
const expect = chai.expect

describe('Iterator Class', () => {
describe('constructor', () => {
it('should set query, endpoint, engine, $offset, and totalResults properties correctly', () => {
const configuration = parseYamlFile('./static/example/config.yml')
const pipeline = new Pipeline(configuration)
const stageConfig = configuration.stages[0]
const stage = new Stage(pipeline, stageConfig)
const iterator = new Iterator(stage);
expect(iterator).to.be.an.instanceOf(Iterator);
expect(iterator).to.be.an.instanceOf(EventEmitter);
expect(iterator).to.have.property('query');
expect(iterator).to.have.property('endpoint');
expect(iterator).to.have.property('engine');
expect(iterator).to.have.property('source');
expect(iterator).to.have.property('$offset', 0);
expect(iterator).to.have.property('totalResults', 0);
});
});
// BUG when both the generator and iterator tests are running, it seems the iterator will never terminate
describe.skip('run', () => {
it('should emit "data" and "end" events with the correct $this and numResults', async () => {
const configuration = parseYamlFile('./static/example/config.yml')
const pipeline = new Pipeline(configuration)
const stageConfig = configuration.stages[0]
const stage = new Stage(pipeline, stageConfig)
const iterator = new Iterator(stage);
const emittedEvents: any = []
async function runIteratorWithPromise(): Promise<boolean> {
return new Promise((resolve, reject) => {
iterator.addListener('data', (bindings) => {
emittedEvents.push({ event: 'data', bindings });
});
iterator.addListener('end', () => {
emittedEvents.push({ event: 'end' });
resolve(true);
});
iterator.addListener('error', (error) => {
reject(error);
});
iterator.run();
});
}

await runIteratorWithPromise()
expect(emittedEvents).to.have.lengthOf(154);
expect(emittedEvents[0].event).to.equal('data');
expect(emittedEvents[0].bindings.termType).to.equal('NamedNode')
expect(emittedEvents[0].bindings.value).to.equal('http://dbpedia.org/resource/Iris_virginica')
expect(emittedEvents[emittedEvents.length - 1].event).to.equal('end');

});
});
});
Loading