-
Notifications
You must be signed in to change notification settings - Fork 0
/
mocha.start.js
154 lines (141 loc) · 3.82 KB
/
mocha.start.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*global assert:true */
'use strict';
var fs = require('fs');
var assert = require('chai').assert;
assert.equalObjects = function (a, b, m) {
assert.deepEqual(JSON.parse(JSON.stringify(a)), JSON.parse(JSON.stringify(b)), m || 'Objects should be equal!');
};
function pprint (msg, obj) {
if (typeof msg === 'string') {
console.log(msg, JSON.stringify(obj, null, 2));
} else {
console.log(JSON.stringify(obj, null, 2));
}
}
var mocha = require('mocha');
var sinon = require('sinon');
var Datastore = require('nedb');
var DSNedbAdapter = require('./');
var JSData = require('js-data');
JSData.DSUtils.Promise = require('bluebird');
var adapter, store, DSUtils, DSErrors, Profile, User, Post, Comment;
var globals = module.exports = {
fail: function (msg) {
assert.equal('should not reach this!: ' + msg, 'failure');
},
TYPES_EXCEPT_STRING: [123, 123.123, null, undefined, {}, [], true, false, function () {
}],
TYPES_EXCEPT_STRING_OR_ARRAY: [123, 123.123, null, undefined, {}, true, false, function () {
}],
TYPES_EXCEPT_STRING_OR_NUMBER: [null, undefined, {}, [], true, false, function () {
}],
TYPES_EXCEPT_STRING_OR_OBJECT: [123, 123.123, null, undefined, [], true, false, function () {
}],
TYPES_EXCEPT_STRING_OR_NUMBER_OBJECT: [null, undefined, [], true, false, function () {
}],
TYPES_EXCEPT_STRING_OR_ARRAY_OR_NUMBER: [null, undefined, {}, true, false, function () {
}],
TYPES_EXCEPT_NUMBER: ['string', null, undefined, {}, [], true, false, function () {
}],
TYPES_EXCEPT_OBJECT: ['string', 123, 123.123, null, undefined, true, false, function () {
}],
TYPES_EXCEPT_BOOLEAN: ['string', 123, 123.123, null, undefined, {}, [], function () {
}],
TYPES_EXCEPT_FUNCTION: ['string', 123, 123.123, null, undefined, {}, [], true, false],
assert: assert,
sinon: sinon,
adapter: undefined,
store: undefined,
pprint: pprint
};
var test = new mocha();
var testGlobals = [];
for (var key in globals) {
global[key] = globals[key];
testGlobals.push(globals[key]);
}
test.globals(testGlobals);
beforeEach(function () {
store = new JSData.DS({
log: false
});
try {
fs.unlinkSync('./profile.db');
} catch (e) {}
try {
fs.unlinkSync('./user.db');
} catch (e) {}
try {
fs.unlinkSync('./post.db');
} catch (e) {}
try {
fs.unlinkSync('./comment.db');
} catch (e) {}
adapter = new DSNedbAdapter();
DSUtils = JSData.DSUtils;
DSErrors = JSData.DSErrors;
globals.Profile = global.Profile = Profile = store.defineResource({
name: 'profile'
});
globals.User = global.User = User = store.defineResource({
name: 'user',
relations: {
hasMany: {
post: {
localField: 'posts',
foreignKey: 'post'
}
},
hasOne: {
profile: {
localField: 'profile',
localKey: 'profileId'
}
}
}
});
globals.Post = global.Post = Post = store.defineResource({
name: 'post',
relations: {
belongsTo: {
user: {
localField: 'user',
localKey: 'userId'
}
},
hasMany: {
comment: {
localField: 'comments',
foreignKey: 'postId'
}
}
}
});
globals.Comment = global.Comment = Comment = store.defineResource({
name: 'comment',
relations: {
belongsTo: {
post: {
localField: 'post',
localKey: 'postId'
},
user: {
localField: 'user',
localKey: 'userId'
}
}
}
});
globals.store = store;
global.store = globals.store;
globals.adapter = adapter;
global.adapter = globals.adapter;
globals.DSUtils = DSUtils;
global.DSUtils = globals.DSUtils;
globals.DSErrors = DSErrors;
global.DSErrors = globals.DSErrors;
});
afterEach(function () {
globals.adapter = null;
global.adapter = null;
});