Permalink
Browse files

Collection insert for Riak, more common tests

  • Loading branch information...
mde
mde committed Aug 27, 2012
1 parent b968f22 commit 3ee131acbdd74207580d66695d6695a3f1cbf4fe
Showing with 207 additions and 39 deletions.
  1. +37 −21 lib/adapters/riak/index.js
  2. +170 −18 test/adapters/riak/index.js
View
@@ -354,30 +354,46 @@ utils.mixin(Adapter.prototype, new (function () {
this.insert = function (data, opts, callback) {
var self = this
- , item = data
- , bucket = _bucketizeModelName(item.type)
- , id = utils.string.uuid()
- , url = '/riak/' + bucket + '/' + id
- , requestOpts;
-
- item = item.toData({whitelist: ['id', 'createdAt']});
- item = JSON.stringify(item);
-
- requestOpts = {
- url: url
- , method: 'POST'
- , data: item
- };
- this.request(requestOpts, function (err, res) {
- if (err) {
- callback(err, null);
+ , items = Array.isArray(data) ? data : [data]
+ , bucket = _bucketizeModelName(items[0].type)
+ , ret = []
+ , insert;
+
+ items = items.slice();
+
+ insert = function () {
+ var item;
+ if ((item = items.shift())) {
+ var id = utils.string.uuid()
+ , url = '/riak/' + bucket + '/' + id
+ , requestOpts;
+
+ item.id = id;
+ item = item.toData({whitelist: ['id', 'createdAt']});
+ item = JSON.stringify(item);
+
+ requestOpts = {
+ url: url
+ , method: 'POST'
+ , data: item
+ };
+ self.request(requestOpts, function (err, res) {
+ if (err) {
+ callback(err, null);
+ }
+ else {
+ item.id = id;
+ item._saved = true;
+ ret.push(data);
+ insert();
+ }
+ });
}
else {
- data.id = id;
- data._saved = true;
- callback(null, data);
+ callback(null, ret);
}
- });
+ };
+ insert();
};
// May need to set bucket props here?
View
@@ -57,7 +57,7 @@ tests = {
}
var inst = data;
data.updateProperties({
- foo: 'BAR'
+ foo: 'ZZZ'
});
inst.save(function (err, data) {
if (err) {
@@ -67,7 +67,7 @@ tests = {
if (err) {
throw err;
}
- assert.equal('BAR', data.foo);
+ assert.equal('ZZZ', data.foo);
next();
});
});
@@ -85,7 +85,7 @@ tests = {
}
, 'test all, by map-reduce, equality': function (next) {
- Zooby.all({foo: 'BAR'}, {}, function (err, data) {
+ Zooby.all({foo: 'ZZZ'}, {}, function (err, data) {
if (err) {
throw err;
}
@@ -95,7 +95,7 @@ tests = {
}
, 'test all, by map-reduce, like': function (next) {
- Zooby.all({foo: {like: 'B'}}, {}, function (err, data) {
+ Zooby.all({foo: {like: 'Z'}}, {}, function (err, data) {
if (err) {
throw err;
}
@@ -105,7 +105,7 @@ tests = {
}
, 'test all, by map-reduce, like lowercased': function (next) {
- Zooby.all({foo: {like: 'b'}}, {lowercase: ['foo']}, function (err, data) {
+ Zooby.all({foo: {like: 'z'}}, {lowercase: ['foo']}, function (err, data) {
if (err) {
throw err;
}
@@ -115,7 +115,7 @@ tests = {
}
, 'test all, by map-reduce, equality and like': function (next) {
- Zooby.all({createdAt: {ne: null}, foo: {like: 'B'}}, {}, function (err, data) {
+ Zooby.all({createdAt: {ne: null}, foo: {like: 'Z'}}, {}, function (err, data) {
if (err) {
throw err;
}
@@ -124,33 +124,185 @@ tests = {
});
}
-, 'test remove': function (next) {
- Zooby.remove(currentId, {}, function (err, data) {
+, 'test save collection': function (next) {
+ var dt = new Date();
+ testItems.push(Zooby.create({
+ foo: 'FOO'
+ , zong: utils.date.add(dt, 'day', -1)
+ }));
+ testItems.push(Zooby.create({
+ foo: 'BAR'
+ , zong: utils.date.add(dt, 'day', -2)
+ }));
+ testItems.push(Zooby.create({
+ foo: 'BAZ'
+ , zong: utils.date.add(dt, 'day', -3)
+ }));
+ Zooby.save(testItems, function (err, data) {
if (err) {
throw err;
}
- Zooby.first(currentId, {}, function (err, data) {
- if (err) {
- throw err;
- }
- assert.ok(!data);
- next();
+ next();
+ });
+ }
+
+, 'test all collection, by map-reduce, equality': function (next) {
+ Zooby.all({foo: 'FOO'}, {}, function (err, data) {
+ if (err) {
+ throw err;
+ }
+ assert.equal(data.length, 1);
+ assert.equal(testItems[0].foo, data[0].foo);
+ next();
+ });
+ }
+
+, 'test all, by string LIKE case-sensitive': function (next) {
+ Zooby.all({foo: {'like': 'B'}}, {}, function (err, data) {
+ if (err) {
+ throw err;
+ }
+ assert.equal(data.length, 2);
+ next();
+ });
+ }
+
+, 'test all, by string LIKE case-insensitive bool': function (next) {
+ Zooby.all({foo: {'like': 'b'}}, {lowercase: true}, function (err, data) {
+ if (err) {
+ throw err;
+ }
+ assert.equal(data.length, 2);
+ next();
+ });
+ }
+
+, 'test all, by LIKE case-insensitive array': function (next) {
+ Zooby.all({foo: {'like': 'b'}}, {lowercase: ['foo']}, function (err, data) {
+ if (err) {
+ throw err;
+ }
+ assert.equal(data.length, 2);
+ next();
+ });
+ }
+
+, 'test all, sort string column name': function (next) {
+ Zooby.all({}, {sort: 'zong'}, function (err, data) {
+ if (err) {
+ throw err;
+ }
+ next();
+ });
+ }
+
+, 'test all, sort incorrect string column name': function () {
+ assert.throws(function () {
+ Zooby.all({}, {sort: 'zongX'}, function (err, data) {
});
+ }, Error);
+ }
+
+, 'test all, sort array column names': function (next) {
+ Zooby.all({}, {sort: ['foo', 'zong']}, function (err, data) {
+ // Should be sorted BAR, BAZ, FOO
+ assert.equal(data[0].id, testItems[1].id);
+ if (err) {
+ throw err;
+ }
+ next();
+ });
+ }
+
+, 'test all, sort object literal desc': function (next) {
+ Zooby.all({}, {sort: {zong: 'desc'}}, function (err, data) {
+ // Sort by datetime
+ assert.equal(data[0].id, testItems[0].id);
+ if (err) {
+ throw err;
+ }
+ next();
});
}
/*
-, 'test all, by map-reduce, equality': function (next) {
- Zooby.all({foo: 'FOO'}, {sort: {createdAt: 'desc'}}, function (err, data) {
+, 'test all, sort object literal asc': function (next) {
+ Zooby.all({}, {sort: {zong: 'asc'}}, function (err, data) {
+ // Sort by datetime reversed
+ assert.equal(data[0].id, testItems[2].id);
if (err) {
throw err;
}
- assert.ok(data.length > 0);
- assert.equal(data[0].id, currentId);
next();
});
}
*/
+
+, 'test all, sort incorrect sort direction': function () {
+ assert.throws(function () {
+ Zooby.all({}, {sort: {foo: 'asc', bar: 'descX'}}, function (err, data) {
+ });
+ }, Error);
+ }
+
+, 'test all, using or, simple equality': function (next) {
+ Zooby.all({or: [{foo: 'BAR'}, {foo: 'BAZ'}]}, {}, function (err, data) {
+ assert.equal(data.length, 2);
+ if (err) {
+ throw err;
+ }
+ next();
+ });
+ }
+
+, 'test all, using or, like comparison': function (next) {
+ Zooby.all({or: [{foo: {'like': 'b'}}, {foo: 'foo'}]}, {lowercase: ['foo']},
+ function (err, data) {
+ assert.equal(data.length, 3);
+ if (err) {
+ throw err;
+ }
+ next();
+ });
+ }
+
+, 'test all, using or, like comparison with not': function (next) {
+ Zooby.all({or: [{foo: {'like': 'b'}}, {foo: 'foo'}], not: {foo: 'baz'}},
+ {lowercase: ['foo']}, function (err, data) {
+ assert.equal(data.length, 2);
+ if (err) {
+ throw err;
+ }
+ next();
+ });
+ }
+
+, 'test all, using less-than createdAt': function (next) {
+ Zooby.all({createdAt: {lt: new Date()}},
+ {}, function (err, data) {
+ assert.equal(data.length, 4);
+ if (err) {
+ throw err;
+ }
+ next();
+ });
+ }
+
+, 'test remove': function (next) {
+ Zooby.remove(currentId, {}, function (err, data) {
+ if (err) {
+ throw err;
+ }
+ Zooby.first(currentId, {}, function (err, data) {
+ if (err) {
+ throw err;
+ }
+ assert.ok(!data);
+ next();
+ });
+ });
+ }
+
};
module.exports = tests;

0 comments on commit 3ee131a

Please sign in to comment.