Skip to content

Commit

Permalink
Improved documentation and test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed May 12, 2011
1 parent e572332 commit 2c25fb0
Show file tree
Hide file tree
Showing 16 changed files with 74 additions and 26 deletions.
12 changes: 6 additions & 6 deletions lib/Cache.js
Expand Up @@ -38,10 +38,10 @@ Cache.prototype.getKey = function (key) {
* Cache one or more values.
*
* To cache a single value by key:
* cache.set('foo', 'bar', callback);
* cache.set('foo', 'bar', callback);
*
* To set multiple cached values by key:
* cache.set({foo:'bar', key2:'value2'}, callback);
* cache.set({foo:'bar', key2:'value2'}, callback);
*
* @param {string} key
* @param {string} value
Expand Down Expand Up @@ -100,13 +100,13 @@ Cache.prototype.add = function (key, value, callback) {
* Get one or more values from the cache.
*
* To get a single cached value by key:
* cache.get('foo', callback);
* cache.get('foo', callback);
*
* To get multiple cached values by key:
* cache.get(['foo','bar'], callback);
* cache.get(['foo','bar'], callback);
*
* To get all cached values:
* cache.get(callback);
* cache.get(callback);
*
* @param {string} key
* @param {string} value
Expand Down Expand Up @@ -342,7 +342,7 @@ Cache.prototype.ttl = function (key, callback) {
}

/**
* Remove the cache keys associated expiry TTL.
* Remove the cache key's associated expiry.
*
* @param {string} key
* @param {Function} callback
Expand Down
2 changes: 1 addition & 1 deletion lib/Channel.js
Expand Up @@ -35,7 +35,7 @@ var Channel = exports.Channel = function (client, channel_name) {
Channel.prototype = new EventEmitter();

/**
* Bind a new Redis client if not exclusively using pub/sub mode.
* Bind a new Redis client (e.g. if not exclusively using pub/sub mode).
*
* @param {Object} client
* @return this
Expand Down
13 changes: 9 additions & 4 deletions lib/Redback.js
Expand Up @@ -14,11 +14,16 @@ var redis = require('redis'),
Cache = require('./Cache').Cache;

/**
* Define the available structures.
* Define the Redis structures.
*/

var base = ['Hash','List','Set','SortedSet','Bitfield'],
advanced = ['KeyPair','DensitySet','CappedList','SocialGraph'];
var base = ['Hash','List','Set','SortedSet','Bitfield'];

/**
* Define the available advanced structures.
*/

var advanced = ['KeyPair','DensitySet','CappedList','SocialGraph'];

/**
* The Redback object wraps the Redis client and acts as a factory
Expand Down Expand Up @@ -108,7 +113,7 @@ Redback.prototype.save = function (background, callback) {
}

/**
* Creating a new Redback client.
* Create a new Redback client.
*
* @param {string} host (optional)
* @param {int} port (optional)
Expand Down
2 changes: 1 addition & 1 deletion lib/Structure.js
Expand Up @@ -67,7 +67,7 @@ Structure.prototype.ttl = function (callback) {
}

/**
* Remove the associated expiry TTL.
* Remove the structure's associated expiry.
*
* @param {Function} callback
* @return this
Expand Down
3 changes: 1 addition & 2 deletions lib/advanced_structures/CappedList.js
Expand Up @@ -28,8 +28,7 @@ var List = require('../base_structures/List').List;
var CappedList = exports.CappedList = List.prototype.extend();

/**
* Setup the Capped List. init() is called automatically by the constructor
* so use `redback.createCappedList(list_name, list_length)` to create the list.
* Setup the Capped List.
*
* @param {int} length - the maximum length of the list
* @param {Function} callback
Expand Down
2 changes: 1 addition & 1 deletion lib/advanced_structures/KeyPair.js
Expand Up @@ -142,7 +142,7 @@ KeyPair.prototype.get = function (value, callback) {
callback = value;
this.client.hgetall(this.key, callback);
} else if (Array.isArray(value)) {
for (var i = value, l = value.length; i < l; i++) {
for (var i = 0, l = value.length; i < l; i++) {
value[i] = this.hashValue(value[i]);
}
this.client.hmget(this.idkey, value, callback)
Expand Down
8 changes: 4 additions & 4 deletions lib/advanced_structures/SocialGraph.js
Expand Up @@ -14,14 +14,14 @@ var Structure = require('../Structure');
* Build a social graph similar to Twitter's.
*
* Usage:
* `redback.createSocialGraph(user_id [, prefix]);`
* `redback.createSocialGraph(user_id [, prefix]);`
*
* Reference:
* http://redis.io/topics/data-types#sets
* http://redis.io/topics/data-types#sets
*
* Redis Structure:
* `(namespace:)(prefix:)user_id:following = set(user_ids)`
* `(namespace:)(prefix:)user_id:followers = set(user_ids)`
* `(namespace:)(prefix:)user_id:following = set(user_ids)`
* `(namespace:)(prefix:)user_id:followers = set(user_ids)`
*/

var SocialGraph = exports.SocialGraph = Structure.new();
Expand Down
2 changes: 1 addition & 1 deletion lib/base_structures/Bitfield.js
Expand Up @@ -40,7 +40,7 @@ Bitfield.prototype.get = function (bit, callback) {
}

/**
* Set a single bit. The callback receives the previous value of the bit
* Set a single bit. The callback receives the previous value.
*
* @param {int} bit
* @param {bool} value
Expand Down
2 changes: 1 addition & 1 deletion lib/base_structures/List.js
Expand Up @@ -26,7 +26,7 @@ var Structure = require('../Structure');
var List = exports.List = Structure.new();

/**
* Get an array of all values in the list.
* Get an array of values in the list.
*
* @param {Function} callback
* @return this
Expand Down
4 changes: 2 additions & 2 deletions lib/base_structures/Set.js
Expand Up @@ -17,10 +17,10 @@ var Structure = require('../Structure');
* `redback.createSet(key);`
*
* Reference:
* http://redis.io/topics/data-types#sets
* http://redis.io/topics/data-types#sets
*
* Redis Structure:
* `(namespace:)key = set(elements)`
* `(namespace:)key = set(elements)`
*/

var Set = exports.Set = Structure.new();
Expand Down
6 changes: 3 additions & 3 deletions lib/base_structures/SortedSet.js
Expand Up @@ -32,10 +32,10 @@ var SortedSet = exports.SortedSet = Structure.new();
* Add one or more elements to the set.
*
* To add a single element and score:
* set.add(12, 'foo', callback);
* set.add(12, 'foo', callback);
*
* To add multiple elements/scores:
* set.add({foo:12, bar:3}, callback);
* set.add({foo:12, bar:3}, callback);
*
* @param {string|Object} element(s)
* @param {Function} callback
Expand Down Expand Up @@ -253,7 +253,7 @@ SortedSet.prototype.get = function (without_scores, callback) {

/**
* Return a callback that parses a WITHSCORES result:
* ['foo','1','bar','2'] => {foo:1, bar:2}
* ['foo','1','bar','2'] => {foo:1, bar:2}
*
* @param {Function} callback
* @api private
Expand Down
24 changes: 24 additions & 0 deletions test/cappedlist.test.js
Expand Up @@ -94,6 +94,30 @@ module.exports = {
});
});
});
},

'test capped list unshift': function () {
var list = redback.createCappedList('test_cappedlist_unshift', 3);

list.unshift(['a','b'], function (err) {
list.unshift('c', function (err) {
list.values(function (err, values) {
assert.equal(3, values.length);
assert.equal('c', values.shift());
assert.equal('a', values.shift());
assert.equal('b', values.shift());

list.unshift('z', function (err) {
list.values(function (err, values) {
assert.equal(3, values.length);
assert.equal('c', values.shift());
assert.equal('a', values.shift());
assert.equal('b', values.shift());
});
});
});
});
});
}

}
3 changes: 3 additions & 0 deletions test/channel.test.js
Expand Up @@ -30,6 +30,9 @@ module.exports = {
});

setTimeout(function () {
channel.unsubscribe(function(){
assert.ok(true);
});
assert.ok(received);
}, 200);
}
Expand Down
4 changes: 4 additions & 0 deletions test/keypair.test.js
Expand Up @@ -18,6 +18,10 @@ module.exports = {
keypair.add('bar', function (err, id) {
assert.equal(2, id);

keypair.add('bar', function (err, id) {
assert.equal(2, id);
});

keypair.length(function (err, length) {
assert.equal(2, length);
});
Expand Down
5 changes: 5 additions & 0 deletions test/socialgraph.test.js
Expand Up @@ -151,6 +151,11 @@ module.exports = {
while (l--) assert.equal(expected.shift(), common.shift());
});

user1.getCommonFollowing([user2, 3], function (err, common) {
var expected = [5,7], l = expected.length;
while (l--) assert.equal(expected.shift(), common.shift());
});

user1.getCommonFollowers(9, function (err, common) {
var expected = [2], l = expected.length;
while (l--) assert.equal(expected.shift(), common.shift());
Expand Down
8 changes: 8 additions & 0 deletions test/sortedset.test.js
Expand Up @@ -234,6 +234,14 @@ module.exports = {
assert.equal(2, set.bar);
});

zset.countScores(1, 3, function (err, count) {
assert.equal(3, count);
});

zset.countScores(5, null, function (err, count) {
assert.equal(0, count);
});

zset.getScores(2, 3, function (err, set) {
var expected = ['bar','foobar'], i;
for (i in set) {
Expand Down

0 comments on commit 2c25fb0

Please sign in to comment.