Skip to content

Commit

Permalink
merged master branch
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas Bell authored and thomas Bell committed Oct 19, 2016
2 parents 29b9bfc + a99a56d commit 8f3a559
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/CreateCommand.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
class CreateCommand {
constructor() {

constructor(dependencyGraph, driverLocator, fs) {
this.dependencyGraph = dependencyGraph;
this.driverLocator = driverLocator;
this.fs = fs;
}

run() {
let graph = this.dependencyGraph.run();
let drivers = this.driverLocator.drivers();

let result = [];

graph.forEach((graphElement) => {
let driver = drivers[graphElement];
result.push(driver.create());
});

this.fs.writeFileSync('./seededData.json', JSON.stringify(result));
}
}

Expand Down
29 changes: 29 additions & 0 deletions src/DependencyGraph.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
let DepGraph = require('dependency-graph').DepGraph;

class DependencyGraph {
constructor(objectsToSeed) {
this.objectsToSeed = objectsToSeed;
this.run = this.run.bind(this);
}

run() {
let graph = new DepGraph();
//put all the nodes in the graph
this.objectsToSeed.forEach((obj) => {
graph.addNode(obj.name, {metadata: obj.metadata});
});
//get the objects names that are in the graph
let objectKeys = Object.keys(graph.nodes);

//for each object, get the dependencies from the meta data and add them
objectKeys.forEach((node) => {
graph.nodes[node].metadata.dependencies.forEach((ele) => {
graph.addDependency(node, ele);
});
});
return graph.overallOrder();
}

}

module.exports = DependencyGraph;
79 changes: 79 additions & 0 deletions test/CreateCommandTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
'use strict'

let expect = require('chai').expect;
let DepGraph = require('dependency-graph').DepGraph;

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

describe('Create command tests', () => {
it('should write results from driver to file', () => {

let DependencyGraph = {
run: function () {
return ['Account', 'Contact']
}
};

let AccountDriver = {
create: function () {
return ({
'id': 1,
'firstName': 'Bob',
'lastName': 'Jones',
'email': 'bob@jones.com',
});
}
};

let ContactDriver = {
create: function () {
return ({
'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 dataExpectedToBeWrittenToFile;

let fs = {
writeFileSync: function (fileName, data) {
dataExpectedToBeWrittenToFile = data;
}
}

let createCommand = new CreateCommand(DependencyGraph, DriverLocator, fs);

createCommand.run();

expect(dataExpectedToBeWrittenToFile).to.equal(JSON.stringify([
{
'id': 1,
'firstName': 'Bob',
'lastName': 'Jones',
'email': 'bob@jones.com',
},
{
'id': 1,
'firstName': 'Bob',
'lastName': 'Jones',
'email': 'bob@jones.com',
'accountId': 1
}
]));
});
});
38 changes: 38 additions & 0 deletions test/DependencyGraphTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

var expect = require('chai').expect;
var DependencyGraph = require('../src/DependencyGraph');
describe('Dependency Graph Tests', () => {
it('Should be constructed and return the correct order', () => {
let objectsToSeed = [
{
"name": "Account",
"metadata": {
'dependencies': []
}
},
{
"name": "Contact",
"metadata": {
'dependencies': ['Account']
}
},
{
"name": "SalesOrder",
"metadata": {
'dependencies': ['Contact','Account','Event']
}
},
{
"name": "Event",
"metadata": {
'dependencies': ['Contact']
}
}
];
let dependencyGraph = new DependencyGraph(objectsToSeed);
expect(dependencyGraph.objectsToSeed).to.equal(objectsToSeed);
let result = dependencyGraph.run();
expect(result).to.deep.equal(['Account','Contact','Event','SalesOrder']);
});
});

0 comments on commit 8f3a559

Please sign in to comment.