-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
160 additions
and
2 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
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,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; |
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,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 | ||
} | ||
])); | ||
}); | ||
}); |
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,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']); | ||
}); | ||
}); |