Skip to content

Commit

Permalink
add basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Chan committed Feb 3, 2012
1 parent ab18466 commit b370508
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 18 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
test:
@./node_modules/.bin/mocha \
--ui exports

.PHONY: test
39 changes: 23 additions & 16 deletions lib/scoreboard.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var redis = require('./redis'),
moment = require('moment');
async = require('async');

var noop = function(){};

exports.version = '0.0.1';
Expand Down Expand Up @@ -87,23 +89,28 @@ Score.prototype.index = function(key, score, value, callback) {
timeKey = exports.genKey(key.toLowerCase(), new Date()),
overallKey = key.toLowerCase();

//Timeseries insert
db.exists(timeKey, function(err, exist) {
if(!exist) {
db.zadd(timeKey, score, value, callback || noop);
} else {
db.zincrby(timeKey, score, value, callback || noop);
async.parallel([
//time series insert
function(done) {
db.exists(timeKey, function(err, exist) {
if(!exist) {
db.zadd(timeKey, score, value, done);
} else {
db.zincrby(timeKey, score, value, done);
}
});
},
// overall insert
function(done){
db.exists(overallKey, function(error, exist) {
if(!exist) {
db.zadd(overallKey, score, value, done);
} else {
db.zincrby(overallKey, score, value, done);
}
});
}
});

//Overall insert
db.exists(overallKey, function(error, exist) {
if(!exist) {
db.zadd(overallKey, score, value, callback || noop);
} else {
db.zincrby(overallKey, score, value, callback || noop);
}
});
], callback || noop);

return this;
};
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
, "keywords": ["redis", "leaderboard", "rank"]
, "author": "Edward Chan <edward@knowsee.com>"
, "dependencies": {
"redis": ">= 0.7.1"
, "moment": ">= 1.3.0"
"redis" : ">= 0.7.1"
, "moment" : ">= 1.3.0"
, "async" : "0.1.15"
}
, "devDependencies": {
"mocha": "*"
, "should": "*"
}
, "main": "index"
, "engines": { "node": ">= 0.4.0 < 0.7.0"}
Expand Down
88 changes: 88 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
var should = require('should');
var scoreboard = require('../');
var Score = scoreboard.Score;

var score = new Score();

describe('scoreboard', function() {
describe('#getDate', function() {
it('test date string', function() {
var d = new Date('2/1/2012');
var dstr = scoreboard.getDate(d);
dstr.should.eql('20120201');
})
})

describe('#genKey', function() {
it('test key generation', function() {
var d = new Date('2/1/2012');
var key = scoreboard.genKey('foo', d);
key.should.eql('20120201_foo');
})
})

describe('#getRangeKeys', function() {
it('test single date range key generation', function(){
var keys = scoreboard.getRangeKeys(['foo'], new Date('2/1/2012'), new Date('2/2/2012'));
keys.should.include('20120201_foo');
keys.should.include('20120202_foo');
})
})

describe('#getRangeKeys', function() {
it('test multiple date range key generation', function(){
var keys = scoreboard.getRangeKeys(['foo', 'bar'], new Date('2/1/2012'), new Date('2/2/2012'));
keys.should.include('20120201_foo');
keys.should.include('20120202_foo');
keys.should.include('20120201_bar');
keys.should.include('20120202_bar');
})
})
})

describe('score', function(){
describe('#index', function() {
it('test index and get value', function(done) {
score.index('foo', 1, 'bar', function(err) {
score.leader({ keys: ['foo'] })
.skip(0).limit(-1)
.run(function(err, response) {
response.should.include('bar');
done();
});
});
})
})

describe('#leader', function() {
it('test multi index and get value', function(done) {
score.index('foo', 1, 'bar', function(err) {
score.index('poo', 1, 'par', function(err) {
score.leader({ keys: ['foo', 'poo'] })
.skip(0).limit(-1)
.run(function(err, response) {
response.should.include('bar');
response.should.include('par');
done();
});
});
});
})
})

describe('#leader', function() {
it('test mult index and get value in range', function(done) {
score.index('foo', 1, 'bar', function(err) {
score.index('poo', 1, 'par', function(err) {
score.leader({ keys: ['foo', 'poo'], date: { $start: new Date('1/31/2012'), $end: new Date('2/02/2012') } })
.skip(0).limit(-1)
.run(function(err, response) {
response.should.include('bar');
response.should.include('par');
done();
});
});
});
})
})
});

0 comments on commit b370508

Please sign in to comment.