From 6e597a9cdb19efe999e999d2a20923737b1ecce4 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Fri, 23 May 2014 08:57:51 -0400 Subject: [PATCH] fixed tests, and added getSortedSetUnion method to redis db, added test for new redis method --- src/database/redis/sorted.js | 29 ++++++++++++++++++++++++ src/routes/debug.js | 7 +++--- src/topics/tags.js | 2 ++ tests/database.js | 11 +++++++-- tests/groups.js | 2 +- tests/topics.js | 43 ++++++++++++++++++++++-------------- 6 files changed, 72 insertions(+), 22 deletions(-) diff --git a/src/database/redis/sorted.js b/src/database/redis/sorted.js index 13dad80013..4c6ab89c8a 100644 --- a/src/database/redis/sorted.js +++ b/src/database/redis/sorted.js @@ -73,4 +73,33 @@ module.exports = function(redisClient, module) { multi.exec(callback); }; + + module.getSortedSetUnion = function(sets, start, stop, callback) { + // start and stop optional + if (typeof start === 'function') { + callback = start; + start = 0; + stop = -1; + } else if (typeof stop === 'function') { + callback = stop; + stop = -1; + } + + var multi = redisClient.multi(); + + // zunionstore prep + sets.unshift(sets.length); + sets.unshift('temp'); + + multi.zunionstore.apply(multi, sets); + multi.zrange('temp', start, stop); + multi.del('temp'); + multi.exec(function(err, results) { + if (!err && typeof callback === 'function') { + callback(null, results[1]); + } else if (err) { + callback(err); + } + }); + } }; \ No newline at end of file diff --git a/src/routes/debug.js b/src/routes/debug.js index 2534576d20..ed988db2ee 100644 --- a/src/routes/debug.js +++ b/src/routes/debug.js @@ -54,9 +54,10 @@ module.exports = function(app, middleware, controllers) { }); app.get('/test', function(req, res) { - var privileges = require('../privileges'); - privileges.topics.get(1299, 1, function(err, result) { - res.json(result); + var db = require('../database'); + db.getSortedSetUnion(['uid:1:posts', 'uid:3:posts'], function(err, pids) { + console.log(err); + res.json(pids); }); }); }); diff --git a/src/topics/tags.js b/src/topics/tags.js index 492723f750..6cee4f76b5 100644 --- a/src/topics/tags.js +++ b/src/topics/tags.js @@ -23,6 +23,8 @@ module.exports = function(Topics) { next(err); }); }, callback); + } else { + callback(); } }; diff --git a/tests/database.js b/tests/database.js index cdd5041407..6176d26c9a 100644 --- a/tests/database.js +++ b/tests/database.js @@ -20,7 +20,7 @@ describe('Test database', function() { } function set(callback) { - db.set('testingStr', 'opppa gangastayla', function(err, data) { + db.set('testingStr', 'oppa gangnam style', function(err, data) { callback(err, {'set': data}); }); } @@ -324,6 +324,12 @@ describe('Test database', function() { }); } + function getSortedSetUnion(callback) { + db.getSortedSetUnion(['users:joindate', 'users:derp', 'users:postcount'], function(err, data) { + callback(err, {'sortedSetUnion': data}); + }); + } + var sortedSetTasks = [ sortedSetAdd, sortedSetAdd, @@ -337,7 +343,8 @@ describe('Test database', function() { sortedSetCount, sortedSetScore, sortedSetsScore, - getSortedSetRevRangeByScore + getSortedSetRevRangeByScore, + getSortedSetUnion ]; async.series(sortedSetTasks, function(err, results) { diff --git a/tests/groups.js b/tests/groups.js index 96d98eeaeb..d3c4bb5d85 100644 --- a/tests/groups.js +++ b/tests/groups.js @@ -14,7 +14,7 @@ describe('Groups', function() { }, function(next) { // Create a new user - User.create({ + User.create({ username: 'testuser', email: 'b@c.com' }, done); diff --git a/tests/topics.js b/tests/topics.js index 5433ab3746..8d94bbd59a 100644 --- a/tests/topics.js +++ b/tests/topics.js @@ -3,31 +3,42 @@ var assert = require('assert'), db = require('../mocks/databasemock'), topics = require('../src/topics'), - categories = require('../src/categories'); + categories = require('../src/categories'), + User = require('../src/user'); describe('Topic\'s', function() { var topic, categoryObj; before(function(done) { + var userData = { + username: 'John Smith', + password: 'swordfish', + email: 'john@example.com', + callback: undefined + }; - categories.create({ - name: 'Test Category', - description: 'Test category created by testing script', - icon: 'fa-check', - blockclass: 'category-blue', - order: '5' - }, function(err, category) { - categoryObj = category; + User.create({username: userData.username, password: userData.password, email: userData.email}, function(err, uid) { + categories.create({ + name: 'Test Category', + description: 'Test category created by testing script', + icon: 'fa-check', + blockclass: 'category-blue', + order: '5' + }, function(err, category) { + categoryObj = category; - topic = { - userId: 1, - categoryId: categoryObj.cid, - title: 'Test Topic Title', - content: 'The content of test topic' - }; - done(); + topic = { + userId: uid, + categoryId: categoryObj.cid, + title: 'Test Topic Title', + content: 'The content of test topic' + }; + done(); + }); }); + + }); describe('.post', function() {