Skip to content

Commit 60ff7bb

Browse files
committed
Adds tests for chaining instance methods
(Almost) full example: ```js Model.find(...).each().filter(function (inst1, inst2) { // ... }).sort(function (inst1, inst2) { // ... }).count(function (count) { // count instances }).save(function (err) { // ... }).get(function (instances) { // ... }); ``` These methods behave exactly as the native Array methods.
1 parent 101f799 commit 60ff7bb

5 files changed

Lines changed: 153 additions & 8 deletions

File tree

‎lib/ChainInstance.js‎

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ function ChainInstance(chain) {
77

88
var load = function () {
99
loading = true;
10-
console.log("loading..");
1110
chain.run(function (err, items) {
1211
instances = items;
1312

14-
console.log("loaded");
1513
return next();
1614
});
1715
};
@@ -34,14 +32,53 @@ function ChainInstance(chain) {
3432
item.hwd.apply(calls, item.args);
3533
};
3634
var calls = {
37-
map: promise(function (cb) {
38-
for (var i = 0; i < instances.length; i++) {
39-
cb(instances[i]);
40-
}
35+
filter: promise(function (cb) {
36+
instances = instances.filter(cb);
37+
38+
return next();
39+
}),
40+
forEach: promise(function (cb) {
41+
instances.forEach(cb);
42+
4143
return next();
4244
}),
43-
save: promise(function () {
44-
console.log("save!?");
45+
sort: promise(function (cb) {
46+
instances.sort(cb);
47+
48+
return next();
49+
}),
50+
count: promise(function (cb) {
51+
cb(instances.length);
52+
53+
return next();
54+
}),
55+
get: promise(function (cb) {
56+
cb(instances);
57+
58+
return next();
59+
}),
60+
save: promise(function (cb) {
61+
var saveNext = function (i) {
62+
if (i >= instances.length) {
63+
if (typeof cb == "function") {
64+
cb();
65+
}
66+
return next();
67+
}
68+
69+
return instances[i].save(function (err) {
70+
if (err) {
71+
if (typeof cb == "function") {
72+
cb(err);
73+
}
74+
return next();
75+
}
76+
77+
return saveNext(i + 1);
78+
});
79+
};
80+
81+
return saveNext(0);
4582
})
4683
};
4784
return calls;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var common = require('../common');
2+
var assert = require('assert');
3+
4+
common.createConnection(function (err, db) {
5+
common.createModelTable('test_find_chain_instance_count', db.driver.db, function () {
6+
common.insertModelData('test_find_chain_instance_count', db.driver.db, [
7+
{ id : 1, name : 'test1' },
8+
{ id : 2, name : 'test2' },
9+
{ id : 3, name : 'test3' },
10+
{ id : 4, name : 'test4' }
11+
], function (err) {
12+
if (err) throw err;
13+
14+
var TestModel = db.define('test_find_chain_instance_count', common.getModelProperties());
15+
16+
TestModel.find().each().filter(function (instance) {
17+
return (instance.id > 2);
18+
}).count(function (count) {
19+
assert.equal(count, 2);
20+
db.close();
21+
});
22+
});
23+
});
24+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var common = require('../common');
2+
var assert = require('assert');
3+
4+
common.createConnection(function (err, db) {
5+
common.createModelTable('test_find_chain_instance_filter', db.driver.db, function () {
6+
common.insertModelData('test_find_chain_instance_filter', db.driver.db, [
7+
{ id : 1, name : 'test1' },
8+
{ id : 2, name : 'test3' },
9+
{ id : 3, name : 'test4' },
10+
{ id : 4, name : 'test2' }
11+
], function (err) {
12+
if (err) throw err;
13+
14+
var TestModel = db.define('test_find_chain_instance_filter', common.getModelProperties());
15+
16+
TestModel.find().each().filter(function (instance) {
17+
return instance.name.match(/test[24]/);
18+
}).sort(function (inst1, inst2) {
19+
return inst1.id < inst2.id;
20+
}).get(function (instances) {
21+
assert.equal(instances.length, 2);
22+
assert.equal(instances[0].id, 4);
23+
assert.equal(instances[1].id, 3);
24+
db.close();
25+
});
26+
});
27+
});
28+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var common = require('../common');
2+
var assert = require('assert');
3+
4+
common.createConnection(function (err, db) {
5+
common.createModelTable('test_find_chain_instance_get', db.driver.db, function () {
6+
common.insertModelData('test_find_chain_instance_get', db.driver.db, [
7+
{ id : 1, name : 'test1' },
8+
{ id : 2, name : 'test2' },
9+
{ id : 3, name : 'test3' },
10+
{ id : 4, name : 'test4' }
11+
], function (err) {
12+
if (err) throw err;
13+
14+
var TestModel = db.define('test_find_chain_instance_get', common.getModelProperties());
15+
16+
TestModel.find().each().forEach(function (instance) {
17+
instance.name = instance.id;
18+
}).get(function (instances) {
19+
assert.equal(instances.length, 4);
20+
assert.equal(instances[0].name, 1);
21+
assert.equal(instances[1].name, 2);
22+
assert.equal(instances[2].name, 3);
23+
assert.equal(instances[3].name, 4);
24+
db.close();
25+
});
26+
});
27+
});
28+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var common = require('../common');
2+
var assert = require('assert');
3+
4+
common.createConnection(function (err, db) {
5+
common.createModelTable('test_find_chain_instance_sort', db.driver.db, function () {
6+
common.insertModelData('test_find_chain_instance_sort', db.driver.db, [
7+
{ id : 1, name : 'test1' },
8+
{ id : 2, name : 'test3' },
9+
{ id : 3, name : 'test4' },
10+
{ id : 4, name : 'test2' }
11+
], function (err) {
12+
if (err) throw err;
13+
14+
var TestModel = db.define('test_find_chain_instance_sort', common.getModelProperties());
15+
16+
TestModel.find().each().sort(function (inst1, inst2) {
17+
return inst1.id < inst2.id;
18+
}).get(function (instances) {
19+
assert.equal(instances.length, 4);
20+
assert.equal(instances[0].id, 4);
21+
assert.equal(instances[1].id, 3);
22+
assert.equal(instances[2].id, 2);
23+
assert.equal(instances[3].id, 1);
24+
db.close();
25+
});
26+
});
27+
});
28+
});

0 commit comments

Comments
 (0)