Permalink
Browse files

Get the Riak adapter tests mostly working.

  • Loading branch information...
1 parent 4faee8e commit 8b51a1b9bd5e5e68f8aa9124237f36a0525620eb mde committed Jun 23, 2012
Showing with 93 additions and 14 deletions.
  1. +8 −12 lib/model/adapters/riak/index.js
  2. +85 −2 lib/model/adapters/riak/test.js
@@ -20,14 +20,6 @@ var Riak = function (config) {
// Model string
this.model = config.model;
- /*
- this.dbName = geddy.config.db.riak.db;
- this.port = geddy.config.db.riak.port || 27017;
- this.host = geddy.config.db.riak.host || 'localhost';
- this.username = geddy.config.db.riak.username || null;
- this.password = geddy.config.db.riak.password || null;
- */
-
// Connect to the DB
this.db = riak.getClient(_config);
@@ -46,7 +38,7 @@ var Riak = function (config) {
, opts = args.shift() || {};
if (typeof query == 'object') {
- opts.limit = 1;
+ //opts.limit = 1; Why?
return this.all(query, opts, callback);
}
else {
@@ -65,10 +57,13 @@ var Riak = function (config) {
handler = function (err, res) {
var data;
- if (res) {
+ // When there's an err, it's also returned as the
+ // result, WTF
+ if (!err && res) {
if (Array.isArray(res)) {
data = [];
- res.forEach(function (item) {
+ res.forEach(function (doc) {
+ data.push(geddy.model[self.model].create(doc.data));
});
}
else {
@@ -82,7 +77,8 @@ var Riak = function (config) {
this.db.get(this.model, query.id, opts, handler);
}
else {
- this.db.getAll(this.model, {where: query}, opts, handler);
+ opts.where = query;
+ this.db.getAll(this.model, opts, handler);
}
};
@@ -5,6 +5,8 @@ var path = require('path')
, tests
, Riak
, currentId
+ , nameList
+ , idList
, User
, _params;
@@ -56,6 +58,14 @@ _params = {
, firstName: 'Neil'
};
+nameList = [
+ 'Neil'
+, 'Alex'
+, 'Geddy'
+];
+
+idList = [];
+
tests = {
'test validate and save item': function (next) {
var user = User.create(_params);
@@ -66,7 +76,6 @@ tests = {
next();
});
}
-
, 'test load item': function (next) {
User.load(currentId, function (err, data) {
assert.ok(data.isValid());
@@ -76,11 +85,85 @@ tests = {
, 'test remove item': function (next) {
User.remove(currentId, function (err, data) {
- assert.ok(!err);
+ if (err) {
+ throw err;
+ }
+ // This is fucking ridiculous, deletes cause lookups to fail
+ // for a while: https://issues.basho.com/show_bug.cgi?id=1269
+ setTimeout(next, 5000);
+ });
+ }
+
+, 'test save multiple items': function (next) {
+ var save = function () {
+ var name = nameList.shift()
+ , user;
+ if (name) {
+ _params.firstName = name;
+ user = User.create(_params);
+ assert.ok(user.isValid());
+ user.save(function (err, data) {
+ assert.ok(data.isValid());
+ // Add to the list for use in lookup tests
+ idList.push(data.id);
+ save();
+ });
+ }
+ else {
+ next();
+ }
+ };
+ save();
+ }
+
+, 'test load multiple items': function (next) {
+ User.load(function (err, data) {
+ if (err) {
+ throw err;
+ }
+ assert.ok(data.length > 1); // Should have three
+ assert.ok(data[2].isValid());
next();
});
}
+, 'test filter for specific items': function (next) {
+ User.load({firstName: 'Alex'}, function (err, data) {
+ if (err) {
+ throw err;
+ }
+ assert.ok(data.length == 1);
+ assert.ok(data[0].isValid());
+ assert.ok(data[0].id == idList[1]);
+ next();
+ });
+ }
+
+, 'test remove all items': function (next) {
+ User.load(function (err, data) {
+ var ids = data.map(function (item) {
+ return item.id;
+ });
+ var remove = function () {
+ var id = ids.shift();
+ if (id) {
+ User.remove(id, function (err, data) {
+ if (err) {
+ throw err;
+ }
+ remove();
+ });
+ }
+ else {
+ // This is fucking ridiculous, deletes cause lookups to fail
+ // for a while: https://issues.basho.com/show_bug.cgi?id=1269
+ setTimeout(next, 5000);
+ }
+ };
+ remove();
+ });
+ }
+
};
module.exports = tests;

0 comments on commit 8b51a1b

Please sign in to comment.