-
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.
Adds tests for utilities and classes
Blocker: some .run() tests seem to fail, but worked before - unsure why this occurs
- Loading branch information
Philippe Renzen
committed
Dec 5, 2023
1 parent
56916ec
commit 97bf5f4
Showing
20 changed files
with
1,596 additions
and
12 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.
Empty file.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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,82 @@ | ||
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'; | ||
fs.writeFileSync(filePath, 'Initial content'); | ||
}); | ||
|
||
afterEach(() => { | ||
const filePath = 'file.txt'; | ||
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 = 'file://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://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); | ||
}); | ||
}); | ||
}); |
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,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'); | ||
}); | ||
}); | ||
|
||
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); | ||
}); | ||
}); | ||
}); |
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,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); | ||
}); | ||
}); | ||
|
||
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'); | ||
|
||
}); | ||
}); | ||
}); |
Oops, something went wrong.