-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
112 lines (107 loc) · 4.22 KB
/
test.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
var sosa_mongo = require('./');
var assert = require('assert');
var state = {};
function ran (method) {
state[method] || (state[method] = 0);
state[method]++;
}
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/test_' + Math.random().toString(16).substring(2);
MongoClient.connect(url, function (err, db) {
assert.ifError(err);
var collection = sosa_mongo({db: db});
var humans = collection('humans', {
load: function (obj, opts, cb) {
ran('load', obj, opts);
cb(null, obj);
},
save: function (obj, opts, cb) {
ran('save', obj, opts);
cb(null, obj);
},
afterSave: function (obj, opts, cb) {
ran('afterSave', obj, opts);
cb(null, obj);
},
destroy: function (obj, opts, cb) {
ran('destroy', obj, opts);
cb(null, obj);
},
methods: {
whodat: function (obj) {
return obj.name;
}
}
});
humans.load('carlos', function (err, human) {
assert.ifError(err);
assert.strictEqual(human, null);
humans.select(function (err, results) {
assert.ifError(err);
assert.deepEqual(results, []);
var carlos = {id: 'carlos', name: 'los'};
humans.save(carlos, function (err, human) {
assert.ifError(err);
assert.deepEqual(carlos, human);
humans.select(function (err, results) {
assert.ifError(err);
assert.deepEqual(results, [carlos]);
assert.deepEqual(state, {save: 1, afterSave: 1, load: 1});
var brian = {id: 'brian', name: 'brian'};
humans.save(brian, function (err, human) {
assert.ifError(err);
assert.deepEqual(brian, human);
humans.select(function (err, results) {
assert.ifError(err);
assert.deepEqual(results, [carlos, brian]);
assert.deepEqual(state, {save: 2, afterSave: 2, load: 3});
var nick = {id: 'nick', name: 'nick'};
humans.save(nick, function (err, human) {
assert.ifError(err);
assert.deepEqual(nick, human);
humans.select(function (err, results) {
assert.ifError(err);
assert.deepEqual(results, [carlos, brian, nick]);
assert.deepEqual(state, {save: 3, afterSave: 3, load: 6});
humans.destroy('brian', function (err, human) {
assert.ifError(err);
assert.deepEqual(brian, human);
assert.deepEqual(state, {save: 3, afterSave: 3, load: 6, destroy: 1});
humans.load('brian', function (err, human) {
assert.ifError(err);
assert.strictEqual(human, null);
humans.select(function (err, results) {
assert.ifError(err);
assert.deepEqual(results, [carlos, nick]);
assert.deepEqual(state, {save: 3, afterSave: 3, load: 8, destroy: 1});
assert.equal(humans.whodat(carlos), 'los');
humans.in('cool_club').select(function (err, results) {
assert.ifError(err);
assert.deepEqual(results, []);
assert.deepEqual(state, {save: 3, afterSave: 3, load: 8, destroy: 1});
humans.in('cool_club').save({id: 'carlos', name: 'los'}, function (err, human) {
assert.ifError(err);
assert.deepEqual(carlos, human);
humans.in('cool_club').select(function (err, results) {
assert.ifError(err);
assert.deepEqual(results, [carlos]);
db.dropDatabase(function (err) {
assert.ifError(err);
db.close();
console.log('passed');
});
});
});
});
});
});
});
});
});
});
});
});
});
});
});
});