Skip to content

Commit

Permalink
Adding support for createSync
Browse files Browse the repository at this point in the history
  • Loading branch information
troyharvey committed Nov 15, 2016
1 parent 143d37d commit ff52daf
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
12 changes: 10 additions & 2 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let TearDownCommand = require('./src/TearDownCommand');
let fs = require('fs');

module.exports = {
create: (objectName) => {
create: (objectName, sync) => {

let objectLocator = new ObjectLocator(fs, objectName);
let objects = objectLocator.run();
Expand All @@ -15,7 +15,15 @@ module.exports = {
let driverLocator = new DriverLocator(fs, process.cwd() + "/drivers/");
let createCommand = new CreateCommand(dependencyGraph, driverLocator);

return createCommand.run();
if (sync) {
return createCommand.runSync();
} else {
return createCommand.run();
}
},

createSync: (objectName) => {
return this.create(objectName, true);
},

tearDown: (objects) => {
Expand Down
17 changes: 17 additions & 0 deletions src/CreateCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,23 @@ class CreateCommand {
return this.runRecursive();
}

runSync() {
let graph = this.dependencyGraph.run();
let drivers = this.driverLocator.drivers();
let results = [];

graph.forEach((graphElement) => {
let lowerElement = graphElement.toLowerCase();
let driver = drivers[lowerElement];
this.driversToExecute.push(driver);
});

this.driversToExecute.forEach((driver) => {
results.push(driver.create());
});

return results;
}
}

module.exports = CreateCommand;
50 changes: 50 additions & 0 deletions test/CreateCommandTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,54 @@ describe('Create command tests', () => {
expect(JSON.stringify(r)).to.equal(JSON.stringify(expectedResult));
});
});

it('should synchronously seed objects', () => {

let AccountDriver = {
create: function () {

return {
'type': 'account',
'properties': {
'id': 1,
'firstName': 'Bob',
'lastName': 'Jones',
'email': 'bob@jones.com'
}
};
}
};

let ContactDriver = {
create: function () {

return {
'type': 'contact',
'properties': {
'id': 1,
'firstName': 'Bob',
'lastName': 'Jones',
'email': 'bob@jones.com',
'accountId': 1
}
};
}
}

let drivers = {
'account': AccountDriver,
'contact': ContactDriver
};

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

let createCommand = new CreateCommand(DependencyGraph, DriverLocator);

let results = createCommand.runSync();
expect(JSON.stringify(results)).to.equal(JSON.stringify(expectedResult));
});
});

0 comments on commit ff52daf

Please sign in to comment.