Skip to content

Commit

Permalink
Merge afc19b2 into 3fd4248
Browse files Browse the repository at this point in the history
  • Loading branch information
troyharvey committed Oct 20, 2016
2 parents 3fd4248 + afc19b2 commit 40552f2
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 41 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"coveralls": "^2.11.14",
"istanbul": "^0.4.5",
"mocha": "^3.1.2",
"mock-fs": "^3.11.0",
"mockery-partial": "^1.4.0"
}
}
27 changes: 19 additions & 8 deletions src/TearDownCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,44 @@ class TearDownCommand {
this.pathToJsonFile = pathToJsonFile;
this.driverLocator = driverLocator;
this.fs = fs;
this.driversToExecute = [];
this.recordsToTearDown = [];
}

runRecursive() {
if (this.driversToExecute.length == 0){
if (this.recordsToTearDown.length == 0){
return new Promise((resolve) => {
resolve();
});
}

return this.driversToExecute[0].tearDown().then((results) => {
this.driversToExecute.shift();
return this.recordsToTearDown[0].driver.tearDown(this.recordsToTearDown[0].record).then((results) => {
this.recordsToTearDown.shift();
this.runRecursive();
});
}

readFile(path) {
let fileContents;

fileContents = this.fs.readFileSync(path);
return JSON.parse(fileContents, 'utf8');
}

run() {
let seededData = this.fs.readFileSync(this.pathToJsonFile);
let seededData = this.readFile(this.pathToJsonFile);
seededData.reverse();

let drivers = this.driverLocator.drivers();

seededData.forEach((row) => {
let type = row["type"];
seededData.forEach((record) => {
let type = record["type"];

if(type in drivers){
let driver = drivers[type];
this.driversToExecute.push(driver);
this.recordsToTearDown.push({
'driver': driver,
'record': record
});
}else{
throw Error("Seeded data file does not contain a type that you have a driver for");
}
Expand Down
88 changes: 55 additions & 33 deletions test/TearDownCommandTest.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
'use strict'

let expect = require('chai').expect;
let fs = require('fs');
let fsMock = require('mock-fs');

let TearDownCommand = require('../src/TearDownCommand');

describe('Tear down command tests', () => {

after(fsMock.restore);

it("should throw an error when a seeded data json file doesn't exist", () =>{
let drivers = {};

Expand All @@ -14,27 +19,46 @@ describe('Tear down command tests', () => {
}
};

let fsMock = {
readFileSync: function(){
return [
{
"type": "herpderp",
"properties": {
"name": "Bob Dole"
}
let tearDownCommand = new TearDownCommand(
driverLocator,
fs,
'./path/to/seed.json'
);

expect(tearDownCommand.run.bind(tearDownCommand)).to.throw(
'ENOENT: no such file or directory, open \'./path/to/seed.json\''
);

});

it('should throw an error when there is no driver for a seeded object', () => {

let drivers = {};

fsMock({
'seededData.json': JSON.stringify([
{
"type": "herpderp",
"properties": {
"name": "Bob Dole"
}
];
}
])
});

let driverLocator = {
drivers: function () {
return drivers;
}
};

let tearDownCommand = new TearDownCommand(
driverLocator,
fsMock,
'./path/to/seed.json'
fs
);

expect(tearDownCommand.run.bind(tearDownCommand)).to.throw(
"Seeded data file does not contain a type that you have a driver for"
'Seeded data file does not contain a type that you have a driver for'
);

});
Expand Down Expand Up @@ -62,35 +86,33 @@ describe('Tear down command tests', () => {
}
};

let fsMock = {
readFileSync: function(){
return [
{
"type": "contact",
"properties": {
"name": "Bob Dole"
}
},
{
"type": "account",
"properties": {
"items": [
"Pizza"
]
}
fsMock({
'path/to/seed.json': JSON.stringify([
{
"type": "contact",
"properties": {
"name": "Bob Dole"
}
];
}
};
},
{
"type": "account",
"properties": {
"items": [
"Pizza"
]
}
}
])
});

let tearDownCommand = new TearDownCommand(
driverLocator,
fsMock,
fs,
'./path/to/seed.json'
);

tearDownCommand.run().then(() => {
expect(tearDownCommand.driversToExecute.length).to.be.equal(0);
expect(tearDownCommand.recordsToTearDown.length).to.be.equal(0);
});
});
});

0 comments on commit 40552f2

Please sign in to comment.