UNPKG

2 kBJavaScriptView Raw
1'use strict';
2
3const _ = require('lodash');
4const fs = require('fs');
5const expect = require('chai').expect;
6const connect = require('../index').connect;
7const validateId = require('./util').validateId;
8const Foo = require('./cyclic/foo');
9const Bar = require('./cyclic/bar');
10
11describe('Cyclic', function() {
12
13 // TODO: Should probably use mock database client...
14 const url = 'nedb://memory';
15 //const url = 'mongodb://localhost/camo_test';
16 let database = null;
17
18 before(function(done) {
19 connect(url).then(function(db) {
20 database = db;
21 return database.dropDatabase();
22 }).then(function() {
23 return done();
24 });
25 });
26
27 beforeEach(function(done) {
28 done();
29 });
30
31 afterEach(function(done) {
32 database.dropDatabase().then(function() {}).then(done, done);
33 });
34
35 after(function(done) {
36 database.dropDatabase().then(function() {}).then(done, done);
37 });
38
39 describe('schema', function() {
40 it('should allow cyclic dependencies', function(done) {
41 let f = Foo.create();
42 f.num = 26;
43 let b = Bar.create();
44 b.num = 99;
45
46 f.save().then(function(foo) {
47 b.foo = foo;
48 return b.save();
49 }).then(function(bar) {
50 f.bar = b;
51 return f.save();
52 }).then(function(foo) {
53 return Foo.findOne({ num: 26 });
54 }).then(function(foo) {
55 validateId(foo);
56 validateId(foo.bar);
57 expect(foo.num).to.be.equal(26);
58 expect(foo.bar.num).to.be.equal(99);
59 return Bar.findOne({ num: 99 });
60 }).then(function(bar) {
61 validateId(bar);
62 validateId(bar.foo);
63 expect(bar.num).to.be.equal(99);
64 expect(bar.foo.num).to.be.equal(26);
65 }).then(done, done);
66
67 });
68 });
69});
\No newline at end of file