Comparing version 1.3.0 to 1.4.0
Changelog | ||
---- | ||
### Thu Jan 5 00:51:59 2017 -0600 | ||
- Commit: b8ef6a2a58931916df75951c1d2fcc69b0e95d89 | ||
- Author: Tom Bremer <tom@tbremer.com> | ||
- Version: 1.4.0 | ||
#### Changes | ||
- allow for callback in DB `new Giraffe(callback)`. | ||
- create buildEdges for quick edge calculation | ||
- this takes edges and binds all of it's nodes blindly. | ||
- add tests | ||
- move `db.update` higher in index | ||
- add conditional callback calling in all db methods | ||
- Add babel transform rest-spread for making unit tests easier to write. | ||
- _does not affect bundle_ | ||
### Wed Jan 4 13:29:17 2017 -0600 | ||
@@ -5,0 +20,0 @@ - Commit: 93748aa95fe81ef6435b3ab839a5202ee28f25c5 |
/** | ||
* Module: giraffe | ||
* Version: 1.3.0 | ||
* Version: 1.4.0 | ||
* By: Tom Bremer <tom@tbremer.com> | ||
@@ -134,3 +134,26 @@ * URL: https://github.com/tbremer/Giraffe#readme | ||
function Giraffe(dataset) { | ||
/** | ||
* Build Edges when all Nodes are known | ||
* `this` is bound in it's callee | ||
*/ | ||
function buildEdges(edges) { | ||
var built = []; | ||
for (var idx in edges) { | ||
var edge = edges[idx]; | ||
var from = _extends({}, this.nodes[edge.from]); // eslint-disable-line | ||
var through = _extends({}, this.nodes[edge.through]); // eslint-disable-line | ||
built.push(_extends({}, edge, { from: from, through: through })); | ||
} | ||
return built; | ||
} | ||
function Giraffe(dataset, callback) { | ||
if (dataset && dataset.constructor === Function && !callback) { | ||
callback = dataset; | ||
dataset = {}; | ||
} | ||
this.nodes = []; | ||
@@ -142,2 +165,3 @@ this.edges = []; | ||
}; | ||
this.callback = callback; | ||
@@ -198,5 +222,33 @@ /** | ||
if (this.callback) this.callback('create', node); | ||
return node; | ||
}; | ||
Giraffe.prototype.update = function nodes(nodes, labels, data) { | ||
if (nodes.constructor !== Array) nodes = [nodes]; | ||
if (labels.constructor === Object) { | ||
data = labels; | ||
labels = null; | ||
} | ||
if (labels !== null && labels.constructor !== Array) labels = [labels]; | ||
if (!data) data = {}; | ||
for (var idx in nodes) { | ||
var _node$labels; | ||
var node = nodes[idx]; | ||
var isEdge = 'label' in node; | ||
if (isEdge && labels) throw new TypeError('Edge Labels cannot be changed.'); | ||
if (labels) node.labels = (_node$labels = node.labels).concat.apply(_node$labels, toConsumableArray(labels)); | ||
node.properties = _extends({}, node.properties, data); | ||
} | ||
if (this.callback) this.callback('update', nodes); | ||
return nodes; | ||
}; | ||
Giraffe.prototype.remove = function remove(nodes) { | ||
@@ -274,2 +326,4 @@ if (nodes.constructor !== Array) nodes = [nodes]; | ||
} | ||
if (this.callback) this.callback('remove'); | ||
}; | ||
@@ -301,2 +355,4 @@ | ||
if (this.callback) this.callback('edge', buildEdges.call(this, edges)); | ||
return edges; | ||
@@ -379,31 +435,9 @@ }; | ||
if (this.callback) this.callback('query', results); | ||
return results; | ||
}; | ||
Giraffe.prototype.update = function nodes(nodes, labels, data) { | ||
if (nodes.constructor !== Array) nodes = [nodes]; | ||
if (labels.constructor === Object) { | ||
data = labels; | ||
labels = null; | ||
} | ||
if (labels !== null && labels.constructor !== Array) labels = [labels]; | ||
if (!data) data = {}; | ||
for (var idx in nodes) { | ||
var _node$labels; | ||
var node = nodes[idx]; | ||
var isEdge = 'label' in node; | ||
if (isEdge && labels) throw new TypeError('Edge Labels cannot be changed.'); | ||
if (labels) node.labels = (_node$labels = node.labels).concat.apply(_node$labels, toConsumableArray(labels)); | ||
node.properties = _extends({}, node.properties, data); | ||
} | ||
return nodes; | ||
}; | ||
return Giraffe; | ||
}))); |
@@ -1,1 +0,1 @@ | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):e.Giraffe=r()}(this,function(){"use strict";function e(e){var r=e.id,t=e.label,s=e.data;this.identity=r,this.properties=a({},s),this.labels=t?[t]:[],this.edges=[]}function r(e){return h.indexOf(e.constructor)>-1}function t(e){var t=e.from,s=e.through,i=e.id,n=e.data,o=e.label;if(!o||!r(o))throw new Error("All Edges need a single Label");this.identity=i,this.from=t.identity,this.through=s.identity,this.label=o,this.properties=a({},n)}function s(e,r){var t=e.properties;for(var s in r)if("edges"!==s){if(!(s in t))return!1;if(t[s]!==r[s])return!1}return!0}function i(e,r){return Boolean(r&&e in r)}function n(e,r){if(!e||e.constructor!==Array)throw new Error("Objects needs to be an array");for(var t in e){var s=e[t];for(var i in r)if(!(i in s)||s[i].constructor!==r[i])throw new Error("Incorrect shape for "+JSON.stringify(s))}return!0}function o(e){this.nodes=[],this.edges=[],this.labels={edges:{},nodes:{}},i("nodes",e)&&n(e.nodes,l)&&(this.nodes=e.nodes),i("edges",e)&&n(e.edges,f)&&(this.edges=e.edges);for(var r in this.nodes){var t=this.nodes[r];if(t.labels.length)for(var s in t.labels){var o=t.labels[s];o in this.labels.nodes||(this.labels.nodes[o]=[]),this.labels.nodes[o].push(t.identity)}}for(var a in this.edges){var d=this.edges[a];if(!d.label)throw new Error("All Edges need a label");var h=d.label;h in this.labels.edges||(this.labels.edges[h]=[]),this.labels.edges[h].push(d.identity)}}var a=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var s in t)Object.prototype.hasOwnProperty.call(t,s)&&(e[s]=t[s])}return e},d=function(e){if(Array.isArray(e)){for(var r=0,t=Array(e.length);r<e.length;r++)t[r]=e[r];return t}return Array.from(e)},l={identity:Number,properties:Object,labels:Array,edges:Array},h=[String,Number],f={identity:Number,properties:Object,label:String,from:Number,through:Number};return o.prototype.create=function(r,t){r.constructor===Object&&(t=r,r=null);var s=this.nodes.length,i=new e({id:s,label:r,data:t});if(this.nodes=[].concat(d(this.nodes),[i]),r){var n=this.labels.nodes;r in n||(n[r]=[]),n[r]=[].concat(d(n[r]),[i.identity])}return i},o.prototype.remove=function(e){e.constructor!==Array&&(e=[e]);for(var r in e){var t=e[r],s=t.identity;for(var i in t.edges){var n=t.edges[i],o=this.edges[n],a=o.label,d=o.identity;if(a in this.labels.edges){var l=this.labels.edges[a].indexOf(d);l!==-1&&this.labels.edges[a].splice(l,1)}this.edges[n]=void 0}for(var h in t.labels){var a=t.labels[h];if(a in this.labels.nodes){var f=this.labels.nodes[a].indexOf(s);f!==-1&&this.labels.nodes[a].splice(f,1)}}for(var u in this.edges){var c=this.edges[u];if(c&&c.through===s){var g=this.nodes[c.from];if(g){var b=g.edges.indexOf(c.identity);b!==-1&&(g.edges.splice(b,1),this.edges[u]=void 0)}}}this.nodes[s]=void 0}},o.prototype.edge=function(e,r,s,i){e.constructor!==Array&&(e=[e]),r.constructor!==Array&&(r=[r]);var n=[];for(var o in e){var a=e[o];for(var l in r){var h=r[l],f=this.edges.length,u=new t({id:f,label:s,data:i,from:a,through:h}),c=this.labels.edges;s in c||(c[s]=[]),c[s]=[].concat(d(c[s]),[u.identity]),a.edges.push(f),this.edges=[].concat(d(this.edges),[u]),n.push(u)}}return n},o.prototype.query=function(e,r){e||r||(e=r=null),e&&e.constructor===Object&&(r=e,e=null);var t=[];for(var i in this.nodes){var n=this.nodes[i];if(n&&(!e||n.labels.indexOf(e)!==-1)){var o=s(n,r),d=r&&"edges"in r;if(d){var l=r.edges.length,h=0;for(var f in r.edges){var u=r.edges[f],c=[];if(u in this.labels.edges){for(var g in this.labels.edges[u]){var b=this.labels.edges[u][g],v=this.edges[b];v&&c.push(v.from)}c.indexOf(n.identity)>-1&&h++}}if(h<l)continue}if(!r||o){var p=a({},n);for(var y in p.edges){var m=p.edges[y],O=a({},this.edges[m]),A=this.nodes[O.through];O.through=a({},A),O.from=p,p.edges[y]=O}t.push(p)}}}return t},o.prototype.update=function e(e,r,t){e.constructor!==Array&&(e=[e]),r.constructor===Object&&(t=r,r=null),null!==r&&r.constructor!==Array&&(r=[r]),t||(t={});for(var s in e){var i,n=e[s],o="label"in n;if(o&&r)throw new TypeError("Edge Labels cannot be changed.");r&&(n.labels=(i=n.labels).concat.apply(i,d(r))),n.properties=a({},n.properties,t)}return e},o}); | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):e.Giraffe=r()}(this,function(){"use strict";function e(e){var r=e.id,t=e.label,s=e.data;this.identity=r,this.properties=l({},s),this.labels=t?[t]:[],this.edges=[]}function r(e){return c.indexOf(e.constructor)>-1}function t(e){var t=e.from,s=e.through,i=e.id,n=e.data,a=e.label;if(!a||!r(a))throw new Error("All Edges need a single Label");this.identity=i,this.from=t.identity,this.through=s.identity,this.label=a,this.properties=l({},n)}function s(e,r){var t=e.properties;for(var s in r)if("edges"!==s){if(!(s in t))return!1;if(t[s]!==r[s])return!1}return!0}function i(e,r){return Boolean(r&&e in r)}function n(e,r){if(!e||e.constructor!==Array)throw new Error("Objects needs to be an array");for(var t in e){var s=e[t];for(var i in r)if(!(i in s)||s[i].constructor!==r[i])throw new Error("Incorrect shape for "+JSON.stringify(s))}return!0}function a(e){var r=[];for(var t in e){var s=e[t],i=l({},this.nodes[s.from]),n=l({},this.nodes[s.through]);r.push(l({},s,{from:i,through:n}))}return r}function o(e,r){e&&e.constructor===Function&&!r&&(r=e,e={}),this.nodes=[],this.edges=[],this.labels={edges:{},nodes:{}},this.callback=r,i("nodes",e)&&n(e.nodes,h)&&(this.nodes=e.nodes),i("edges",e)&&n(e.edges,f)&&(this.edges=e.edges);for(var t in this.nodes){var s=this.nodes[t];if(s.labels.length)for(var a in s.labels){var o=s.labels[a];o in this.labels.nodes||(this.labels.nodes[o]=[]),this.labels.nodes[o].push(s.identity)}}for(var l in this.edges){var d=this.edges[l];if(!d.label)throw new Error("All Edges need a label");var c=d.label;c in this.labels.edges||(this.labels.edges[c]=[]),this.labels.edges[c].push(d.identity)}}var l=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var s in t)Object.prototype.hasOwnProperty.call(t,s)&&(e[s]=t[s])}return e},d=function(e){if(Array.isArray(e)){for(var r=0,t=Array(e.length);r<e.length;r++)t[r]=e[r];return t}return Array.from(e)},h={identity:Number,properties:Object,labels:Array,edges:Array},c=[String,Number],f={identity:Number,properties:Object,label:String,from:Number,through:Number};return o.prototype.create=function(r,t){r.constructor===Object&&(t=r,r=null);var s=this.nodes.length,i=new e({id:s,label:r,data:t});if(this.nodes=[].concat(d(this.nodes),[i]),r){var n=this.labels.nodes;r in n||(n[r]=[]),n[r]=[].concat(d(n[r]),[i.identity])}return this.callback&&this.callback("create",i),i},o.prototype.update=function e(e,r,t){e.constructor!==Array&&(e=[e]),r.constructor===Object&&(t=r,r=null),null!==r&&r.constructor!==Array&&(r=[r]),t||(t={});for(var s in e){var i,n=e[s],a="label"in n;if(a&&r)throw new TypeError("Edge Labels cannot be changed.");r&&(n.labels=(i=n.labels).concat.apply(i,d(r))),n.properties=l({},n.properties,t)}return this.callback&&this.callback("update",e),e},o.prototype.remove=function(e){e.constructor!==Array&&(e=[e]);for(var r in e){var t=e[r],s=t.identity;for(var i in t.edges){var n=t.edges[i],a=this.edges[n],o=a.label,l=a.identity;if(o in this.labels.edges){var d=this.labels.edges[o].indexOf(l);d!==-1&&this.labels.edges[o].splice(d,1)}this.edges[n]=void 0}for(var h in t.labels){var o=t.labels[h];if(o in this.labels.nodes){var c=this.labels.nodes[o].indexOf(s);c!==-1&&this.labels.nodes[o].splice(c,1)}}for(var f in this.edges){var u=this.edges[f];if(u&&u.through===s){var g=this.nodes[u.from];if(g){var b=g.edges.indexOf(u.identity);b!==-1&&(g.edges.splice(b,1),this.edges[f]=void 0)}}}this.nodes[s]=void 0}this.callback&&this.callback("remove")},o.prototype.edge=function(e,r,s,i){e.constructor!==Array&&(e=[e]),r.constructor!==Array&&(r=[r]);var n=[];for(var o in e){var l=e[o];for(var h in r){var c=r[h],f=this.edges.length,u=new t({id:f,label:s,data:i,from:l,through:c}),g=this.labels.edges;s in g||(g[s]=[]),g[s]=[].concat(d(g[s]),[u.identity]),l.edges.push(f),this.edges=[].concat(d(this.edges),[u]),n.push(u)}}return this.callback&&this.callback("edge",a.call(this,n)),n},o.prototype.query=function(e,r){e||r||(e=r=null),e&&e.constructor===Object&&(r=e,e=null);var t=[];for(var i in this.nodes){var n=this.nodes[i];if(n&&(!e||n.labels.indexOf(e)!==-1)){var a=s(n,r),o=r&&"edges"in r;if(o){var d=r.edges.length,h=0;for(var c in r.edges){var f=r.edges[c],u=[];if(f in this.labels.edges){for(var g in this.labels.edges[f]){var b=this.labels.edges[f][g],v=this.edges[b];v&&u.push(v.from)}u.indexOf(n.identity)>-1&&h++}}if(h<d)continue}if(!r||a){var p=l({},n);for(var y in p.edges){var m=p.edges[y],O=l({},this.edges[m]),A=this.nodes[O.through];O.through=l({},A),O.from=p,p.edges[y]=O}t.push(p)}}}return this.callback&&this.callback("query",t),t},o}); |
{ | ||
"name": "giraffe", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Lightweight Graph DB implementation", | ||
@@ -32,2 +32,3 @@ "main": "dist/bundle.js", | ||
"babel-plugin-transform-object-assign": "^6.8.0", | ||
"babel-plugin-transform-object-rest-spread": "^6.20.2", | ||
"babel-preset-es2015": "^6.18.0", | ||
@@ -34,0 +35,0 @@ "babel-preset-es2015-rollup": "^3.0.0", |
@@ -25,3 +25,3 @@ Giraffe | ||
## Methods | ||
- `new Giraffe(data)` | ||
- `new Giraffe(data, callback)` | ||
- **`data`**: `Object` _Optional_ | ||
@@ -31,4 +31,8 @@ - _Should be in the shape of `{ nodes: [], edges: [] }`_ | ||
- _`Nodes` and `Edges` are checked for their correct shape._ | ||
- Create the DB instace | ||
- **`callback`**: `Function` _Optional_ | ||
- _Can be passed first if no data is supplied_ | ||
- _Callback is called just before returning on each Database Method_ | ||
- Create the DB instance | ||
- `.create(label, data)` | ||
@@ -38,2 +42,3 @@ - **`label`**: `String` _Optional_ | ||
- `.remove(nodes)` | ||
@@ -43,2 +48,3 @@ - **`nodes`**: `Array` _Array of Nodes to be removed from graph_ | ||
- `.edge([ from ], [ to ], label, properties)` | ||
@@ -50,2 +56,3 @@ - **`from`** `Array` _Array of Nodes where edge originates_ | ||
- `.query(label, properties)` | ||
@@ -58,2 +65,3 @@ - **`label`**: `String` _Optional_ | ||
- `.update([ nodes ], [ labels ], data)` | ||
@@ -126,2 +134,5 @@ - **`nodes`**: `Array` (or single) node to be updated | ||
## Coming Features | ||
1. Provide callback for when any action occurs (`new Giraffe(() => {})`) | ||
1. Nested Queries | ||
1. Better `Node` & `Id` creation | ||
1. Typescript? | ||
1. ??? |
import Node, { shape as nodeShape } from './Node'; | ||
import Edge, { shape as edgeShape } from './Edge'; | ||
import { checkProperties, lookForKey, ensureObjectsShape } from './lib'; | ||
import { checkProperties, lookForKey, ensureObjectsShape, buildEdges } from './lib'; | ||
export default function Giraffe(dataset) { | ||
export default function Giraffe(dataset, callback) { | ||
if (dataset && dataset.constructor === Function && !callback) { | ||
callback = dataset; | ||
dataset = {}; | ||
} | ||
this.nodes = []; | ||
@@ -12,2 +17,3 @@ this.edges = []; | ||
}; | ||
this.callback = callback; | ||
@@ -68,5 +74,31 @@ /** | ||
if (this.callback) this.callback('create', node); | ||
return node; | ||
}; | ||
Giraffe.prototype.update = function nodes (nodes, labels, data) { | ||
if (nodes.constructor !== Array) nodes = [ nodes ]; | ||
if (labels.constructor === Object) { | ||
data = labels; | ||
labels = null; | ||
} | ||
if (labels !== null && labels.constructor !== Array) labels = [ labels ]; | ||
if (!data) data = {}; | ||
for (const idx in nodes) { | ||
const node = nodes[idx]; | ||
const isEdge = ('label' in node); | ||
if (isEdge && labels) throw new TypeError('Edge Labels cannot be changed.'); | ||
if (labels) node.labels = node.labels.concat(...labels); | ||
node.properties = Object.assign({}, node.properties, data); | ||
} | ||
if (this.callback) this.callback('update', nodes); | ||
return nodes; | ||
}; | ||
Giraffe.prototype.remove = function remove (nodes) { | ||
@@ -142,2 +174,4 @@ if (nodes.constructor !== Array) nodes = [ nodes ]; | ||
} | ||
if (this.callback) this.callback('remove'); | ||
}; | ||
@@ -169,2 +203,4 @@ | ||
if (this.callback) this.callback('edge', buildEdges.call(this, edges)); | ||
return edges; | ||
@@ -247,25 +283,5 @@ }; | ||
if (this.callback) this.callback('query', results); | ||
return results; | ||
}; | ||
Giraffe.prototype.update = function nodes (nodes, labels, data) { | ||
if (nodes.constructor !== Array) nodes = [ nodes ]; | ||
if (labels.constructor === Object) { | ||
data = labels; | ||
labels = null; | ||
} | ||
if (labels !== null && labels.constructor !== Array) labels = [ labels ]; | ||
if (!data) data = {}; | ||
for (const idx in nodes) { | ||
const node = nodes[idx]; | ||
const isEdge = ('label' in node); | ||
if (isEdge && labels) throw new TypeError('Edge Labels cannot be changed.'); | ||
if (labels) node.labels = node.labels.concat(...labels); | ||
node.properties = Object.assign({}, node.properties, data); | ||
} | ||
return nodes; | ||
}; |
@@ -47,1 +47,19 @@ export function checkProperties(node, properties) { | ||
} | ||
/** | ||
* Build Edges when all Nodes are known | ||
* `this` is bound in it's callee | ||
*/ | ||
export function buildEdges(edges) { | ||
const built = []; | ||
for (const idx in edges) { | ||
const edge = edges[idx]; | ||
const from = Object.assign({}, this.nodes[edge.from]); // eslint-disable-line | ||
const through = Object.assign({}, this.nodes[edge.through]); // eslint-disable-line | ||
built.push(Object.assign({}, edge, { from, through })); | ||
} | ||
return built; | ||
} |
@@ -5,3 +5,4 @@ import expect from 'expect'; | ||
lookForKey, | ||
ensureObjectsShape | ||
ensureObjectsShape, | ||
buildEdges | ||
} from './'; | ||
@@ -98,2 +99,16 @@ | ||
}); | ||
describe('buildEdges', () => { | ||
it('builds edges', () => { | ||
const nodes = [ | ||
{ identity: 0, properties: { name: 'Cat' }, edges: [ 0 ], labels: [] }, | ||
{ identity: 1, properties: { name: 'Dog' }, edges: [], labels: [] } | ||
]; | ||
const edges = [ { identity: 0, label: 'CHASES', properties: {}, from: 0, through: 1 } ]; | ||
const assert = buildEdges.call({ nodes, edges }, edges); | ||
const expected = [ { identity: 0, label: 'CHASES', properties: {}, from: nodes[0], through: nodes[1] } ]; | ||
expect(assert).toMatch(expected); | ||
}); | ||
}); | ||
}); |
204
src/test.js
@@ -1,2 +0,2 @@ | ||
import expect from 'expect'; | ||
import expect, { createSpy } from 'expect'; | ||
import Giraffe from './'; | ||
@@ -16,60 +16,176 @@ import Node from './Node'; | ||
describe('database', () => { | ||
it('allows for preloaded data', () => { | ||
const nodes = [ | ||
new Node({ id: 0, data: { name: 'Cat' } }), | ||
new Node({ id: 1, data: { name: 'Dog' } }) | ||
]; | ||
const edges = [ | ||
new Edge({ from: nodes[0], through: nodes[1], label: 'CHASES', id: 0 }) | ||
]; | ||
describe('initial data', () => { | ||
it('allows for preloaded data', () => { | ||
const nodes = [ | ||
new Node({ id: 0, data: { name: 'Cat' } }), | ||
new Node({ id: 1, data: { name: 'Dog' } }) | ||
]; | ||
const edges = [ | ||
new Edge({ from: nodes[0], through: nodes[1], label: 'CHASES', id: 0 }) | ||
]; | ||
db = new Giraffe({ nodes, edges }); | ||
db = new Giraffe({ nodes, edges }); | ||
expect(db.nodes.length).toEqual(2); | ||
expect(db.edges.length).toEqual(1); | ||
expect(db.labels.nodes).toEqual({}); | ||
expect(db.labels.edges).toIncludeKey('CHASES'); | ||
}); | ||
expect(db.nodes.length).toEqual(2); | ||
expect(db.edges.length).toEqual(1); | ||
expect(db.labels.nodes).toEqual({}); | ||
expect(db.labels.edges).toIncludeKey('CHASES'); | ||
}); | ||
it('throws errors if edge does not have a label', () => { | ||
const nodes = [ | ||
new Node({ id: 0, data: { name: 'Cat' } }), | ||
new Node({ id: 1, data: { name: 'Dog' } }) | ||
]; | ||
const edges = [ | ||
new Edge({ from: nodes[0], through: nodes[1], label: 'CHASES', id: 0 }) | ||
]; | ||
it('throws errors if edge does not have a label', () => { | ||
const nodes = [ | ||
new Node({ id: 0, data: { name: 'Cat' } }), | ||
new Node({ id: 1, data: { name: 'Dog' } }) | ||
]; | ||
const edges = [ | ||
new Edge({ from: nodes[0], through: nodes[1], label: 'CHASES', id: 0 }) | ||
]; | ||
delete edges[0].label; | ||
delete edges[0].label; | ||
expect(() => new Giraffe({ nodes, edges })) | ||
.toThrow(/Incorrect shape for/); | ||
}); | ||
expect(() => new Giraffe({ nodes, edges })) | ||
.toThrow(/Incorrect shape for/); | ||
}); | ||
it('throws errors if Node does not have the correct shape', () => { | ||
const nodes = [ { identity: 'foo' } ]; | ||
it('throws errors if Node does not have the correct shape', () => { | ||
const nodes = [ { identity: 'foo' } ]; | ||
expect(() => new Giraffe({ nodes })) | ||
.toThrow(/Incorrect shape for/); | ||
}); | ||
expect(() => new Giraffe({ nodes })) | ||
.toThrow(/Incorrect shape for/); | ||
}); | ||
it('throws errors if Edge does not have the correct shape', () => { | ||
const edges = [ { identity: 'foo' } ]; | ||
it('throws errors if Edge does not have the correct shape', () => { | ||
const edges = [ { identity: 'foo' } ]; | ||
expect(() => new Giraffe({ edges })) | ||
.toThrow(/Incorrect shape for/); | ||
expect(() => new Giraffe({ edges })) | ||
.toThrow(/Incorrect shape for/); | ||
}); | ||
it('throws errors if creation is sent a non-array for nodes', () => { | ||
const nodes = { identity: 'foo' }; | ||
expect(() => new Giraffe({ nodes })) | ||
.toThrow(/Objects needs to be an array/); | ||
}); | ||
it('throws errors if creation is sent a non-array for edges', () => { | ||
const edges = { from: 0, through: 1, label: '', identity: 'foo' }; | ||
expect(() => new Giraffe({ edges })) | ||
.toThrow(/Objects needs to be an array/); | ||
}); | ||
}); | ||
it('throws errors if creation is sent a non-array for nodes', () => { | ||
const nodes = { identity: 'foo' }; | ||
describe('callback', () => { | ||
it('handles callback as first argument', () => { | ||
const callback = createSpy(); | ||
expect(() => new Giraffe({ nodes })) | ||
.toThrow(/Objects needs to be an array/); | ||
db = new Giraffe(callback); | ||
expect(db.callback).toEqual(callback); | ||
expect(db.nodes).toEqual([]); | ||
expect(db.edges).toEqual([]); | ||
expect(db.labels).toEqual({ | ||
nodes: {}, | ||
edges: {} | ||
}); | ||
}); | ||
it('calls callback on `create`', () => { | ||
const callback = createSpy(); | ||
db = new Giraffe(callback); | ||
const node = db.create({ name: 'Cat' }); | ||
expect(callback).toHaveBeenCalled(); | ||
expect(callback).toHaveBeenCalledWith('create', node); | ||
}); | ||
it('calls callback on `update`', () => { | ||
const callback = createSpy(); | ||
db = new Giraffe(callback); | ||
const updatedNode = db.update(db.create({ name: 'Cat' }), { type: 'Tabby' }); | ||
expect(callback).toHaveBeenCalled(); | ||
expect(callback).toHaveBeenCalledWith('update', updatedNode); | ||
}); | ||
it('calls callback on `remove`', () => { | ||
const callback = createSpy(); | ||
db = new Giraffe(callback); | ||
const node = db.create({ name: 'Cat' }); | ||
db.remove(node); | ||
expect(callback).toHaveBeenCalled(); | ||
expect(callback).toHaveBeenCalledWith('remove'); | ||
}); | ||
it('calls callback on `edge`', () => { | ||
const callback = createSpy(); | ||
db = new Giraffe(callback); | ||
const cat = db.create({ name: 'Cat' }); | ||
const dog = db.create({ name: 'Dog' }); | ||
const [ edge ] = db.edge(cat, dog, relationship); | ||
const calledWith = [ | ||
{ | ||
...edge, | ||
from: cat, | ||
through: dog | ||
} | ||
]; | ||
expect(callback).toHaveBeenCalled(); | ||
const [ callbackType, callbackNodes ] = callback.calls[2].arguments; | ||
expect(callbackType).toEqual('edge'); | ||
expect(callbackNodes).toMatch(calledWith); | ||
}); | ||
it('calls callback on `query`', () => { | ||
const callback = createSpy(); | ||
db = new Giraffe(callback); | ||
db.create(label, { name: 'Cat' }); | ||
db.create(label, { name: 'Dog' }); | ||
db.create(label, { name: 'CatDog' }); | ||
const results = db.query(label); | ||
expect(callback).toHaveBeenCalled(); | ||
const [ callbackType, callbackData ] = callback.calls[3].arguments; | ||
expect(callbackType).toEqual('query'); | ||
expect(callbackData).toEqual(results); | ||
}); | ||
}); | ||
it('throws errors if creation is sent a non-array for edges', () => { | ||
const edges = { from: 0, through: 1, label: '', identity: 'foo' }; | ||
describe('initial data and callback', () => { | ||
it('handles both', () => { | ||
const callback = createSpy(); | ||
const nodes = [ | ||
new Node({ id: 0, data: { name: 'Cat' } }), | ||
new Node({ id: 1, data: { name: 'Dog' } }) | ||
]; | ||
const edges = [ | ||
new Edge({ from: nodes[0], through: nodes[1], label: 'CHASES', id: 0 }) | ||
]; | ||
expect(() => new Giraffe({ edges })) | ||
.toThrow(/Objects needs to be an array/); | ||
db = new Giraffe({ nodes, edges }, callback); | ||
expect(db.nodes.length).toEqual(2); | ||
expect(db.edges.length).toEqual(1); | ||
expect(db.labels.nodes).toEqual({}); | ||
expect(db.labels.edges).toIncludeKey('CHASES'); | ||
expect(db.callback).toEqual(callback); | ||
}); | ||
}); | ||
@@ -76,0 +192,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
53606
1220
132
15