|
1 | 1 | /*jshint node: true */ |
2 | | -/*globals describe, it */ |
| 2 | +/*globals describe, it, before, after, beforeEach */ |
3 | 3 | 'use strict'; |
| 4 | +var expect = require('expect.js'); |
| 5 | +var async = require('async'); |
| 6 | +var Database = require('../'); |
| 7 | +var Graph = require('../lib/graph'); |
| 8 | +var ArangoError = require('../lib/error'); |
| 9 | +var db = new Database(); |
| 10 | +var testVertexCollectionName1 = 'test__vertices_1'; |
| 11 | +var testVertexCollectionName2 = 'test__vertices_2'; |
| 12 | +var testEdgeCollectionName1 = 'test__edges_1'; |
| 13 | +var testEdgeCollectionName2 = 'test__edges_2'; |
| 14 | +var testEdgeCollectionName3 = 'test__edges_3'; |
| 15 | +var testDbName = 'test__database_graphs_1'; |
| 16 | +var testGraphName = 'test__graph_1'; |
| 17 | + |
| 18 | +function mcall(name) { |
| 19 | + return function (obj, cb) { |
| 20 | + obj[name](cb); |
| 21 | + }; |
| 22 | +} |
4 | 23 |
|
5 | 24 | describe('database', function () { |
6 | | - describe('createGraph', function () { |
7 | | - it('is missing tests'); |
| 25 | + var edgeCollection1, edgeCollection2, edgeCollection3; |
| 26 | + var vertexCollection1, vertexCollection2; |
| 27 | + var _db; |
| 28 | + before(function (done) { |
| 29 | + db.database(testDbName, true, function (err1, database) { |
| 30 | + _db = db; |
| 31 | + db = database; |
| 32 | + async.map([ |
| 33 | + {name: testVertexCollectionName1, type: 2}, |
| 34 | + {name: testVertexCollectionName2, type: 2}, |
| 35 | + {name: testEdgeCollectionName1, type: 3}, |
| 36 | + {name: testEdgeCollectionName2, type: 3}, |
| 37 | + {name: testEdgeCollectionName3, type: 3} |
| 38 | + ], db.createCollection.bind(db), function (err, results) { |
| 39 | + if (err) return done(err); |
| 40 | + vertexCollection1 = results[0]; |
| 41 | + vertexCollection2 = results[1]; |
| 42 | + edgeCollection1 = results[2]; |
| 43 | + edgeCollection2 = results[3]; |
| 44 | + edgeCollection3 = results[4]; |
| 45 | + done(); |
| 46 | + }); |
| 47 | + }); |
| 48 | + }); |
| 49 | + after(function (done) { |
| 50 | + _db.dropDatabase(testDbName, function (err) { |
| 51 | + db = _db; |
| 52 | + done(err); |
| 53 | + }); |
| 54 | + }); |
| 55 | + beforeEach(function (done) { |
| 56 | + async.map([ |
| 57 | + vertexCollection1, |
| 58 | + vertexCollection2, |
| 59 | + edgeCollection1, |
| 60 | + edgeCollection2, |
| 61 | + edgeCollection3 |
| 62 | + ], mcall('truncate'), function (err) { |
| 63 | + db.dropGraph(testGraphName, function () { |
| 64 | + done(err); |
| 65 | + }); |
| 66 | + }); |
| 67 | + }); |
| 68 | + describe('createGraph', function (done) { |
| 69 | + it('returns a new graph object with the given name', function (done) { |
| 70 | + db.createGraph({name: testGraphName}, function (err, graph) { |
| 71 | + expect(err).not.to.be.ok(); |
| 72 | + expect(graph).to.be.ok(); |
| 73 | + expect(graph).to.be.a(Graph); |
| 74 | + expect(graph.name).to.equal(testGraphName); |
| 75 | + done(); |
| 76 | + }); |
| 77 | + }); |
| 78 | + it('creates a new graph with the given name', function (done) { |
| 79 | + db.createGraph({name: testGraphName}, function (err) { |
| 80 | + expect(err).not.to.be.ok(); |
| 81 | + db.graph(testGraphName, false, function (err2, graph) { |
| 82 | + expect(err2).not.to.be.ok(); |
| 83 | + expect(graph.name).to.equal(testGraphName); |
| 84 | + done(); |
| 85 | + }); |
| 86 | + }); |
| 87 | + }); |
| 88 | + it('creates a new graph with the given edge defs', function (done) { |
| 89 | + var edgeDefs = [ |
| 90 | + { |
| 91 | + collection: testEdgeCollectionName1, |
| 92 | + from: [testVertexCollectionName1], |
| 93 | + to: [testVertexCollectionName2] |
| 94 | + }, |
| 95 | + { |
| 96 | + collection: testEdgeCollectionName2, |
| 97 | + from: [testVertexCollectionName2], |
| 98 | + to: [testVertexCollectionName1] |
| 99 | + }, |
| 100 | + { |
| 101 | + collection: testEdgeCollectionName3, |
| 102 | + from: [testVertexCollectionName1, testVertexCollectionName2], |
| 103 | + to: [testVertexCollectionName1, testVertexCollectionName2] |
| 104 | + } |
| 105 | + ]; |
| 106 | + db.createGraph({ |
| 107 | + name: testGraphName, |
| 108 | + edgeDefinitions: edgeDefs |
| 109 | + }, function (err) { |
| 110 | + expect(err).not.to.be.ok(); |
| 111 | + db.graph(testGraphName, false, function (err2, graph) { |
| 112 | + expect(err2).not.to.be.ok(); |
| 113 | + expect(graph.edgeDefinitions).to.eql(edgeDefs); |
| 114 | + done(); |
| 115 | + }); |
| 116 | + }); |
| 117 | + }); |
8 | 118 | }); |
9 | 119 | describe('graph', function () { |
10 | | - it('is missing tests'); |
| 120 | + describe('with autoCreate:false', function () { |
| 121 | + it('returns the graph if it exists', function (done) { |
| 122 | + db.createGraph({name: testGraphName}, function (err) { |
| 123 | + expect(err).not.to.be.ok(); |
| 124 | + db.graph(testGraphName, false, function (err2, graph) { |
| 125 | + expect(err2).not.to.be.ok(); |
| 126 | + expect(graph.name).to.equal(testGraphName); |
| 127 | + done(); |
| 128 | + }); |
| 129 | + }); |
| 130 | + }); |
| 131 | + it('returns an ArangoError if the graph does not exist', function (done) { |
| 132 | + db.graph('this_does_not_exist', false, function (err, graph) { |
| 133 | + expect(err).to.be.an(ArangoError); |
| 134 | + expect(graph).not.to.be.ok(); |
| 135 | + done(); |
| 136 | + }); |
| 137 | + }); |
| 138 | + }); |
| 139 | + describe('with autoCreate:true', function () { |
| 140 | + it('returns the graph if it exists', function (done) { |
| 141 | + db.createGraph({name: testGraphName}, function (err) { |
| 142 | + expect(err).not.to.be.ok(); |
| 143 | + db.graph(testGraphName, false, function (err2, graph) { |
| 144 | + expect(err2).not.to.be.ok(); |
| 145 | + expect(graph.name).to.equal(testGraphName); |
| 146 | + done(); |
| 147 | + }); |
| 148 | + }); |
| 149 | + }); |
| 150 | + it('creates the graph if it does not exist', function (done) { |
| 151 | + db.graph(testGraphName, true, function (err, graph) { |
| 152 | + expect(err).not.to.be.ok(); |
| 153 | + expect(graph.name).to.equal(testGraphName); |
| 154 | + done(); |
| 155 | + }); |
| 156 | + }); |
| 157 | + }); |
11 | 158 | }); |
12 | 159 | describe('graphs', function () { |
13 | 160 | it('is missing tests'); |
|
0 commit comments