UNPKG

taggable-via-redis

Version:

Add tagging functionality backed by Redis. This is a rewrite of sintaxi's node-redis-tag.

166 lines (153 loc) 5.63 kB
// Generated by CoffeeScript 1.6.3 /* # test for taggabler_via_redis */ (function() { var MODULE_NAME, REDIS_CLIENT, TAGS_COFFEESCRIPT, TAGS_JQUERY, TAGS_NODE, TAGS_RAILS, should, taggable; should = require("should"); taggable = require("../taggabler_via_redis"); TAGS_NODE = "javascript,server,programming".split(",").sort(); TAGS_JQUERY = "javascript,client,programming".split(",").sort(); TAGS_RAILS = "ruby,server,programming".split(",").sort(); TAGS_COFFEESCRIPT = "javascript,client,server,programming".split(",").sort(); REDIS_CLIENT = null; MODULE_NAME = "book"; describe("basic add tests", function() { before(function(done) { var redis; redis = require("redis"); REDIS_CLIENT = redis.createClient(); REDIS_CLIENT.flushall(); taggable.init(REDIS_CLIENT); return setTimeout(done, 1800); }); return describe("taggabler_via_redis", function() { it("popular should work when no any tag", function(done) { taggable.popular(MODULE_NAME, 10, function(err, rsp) { should.not.exist(err); rsp.should.be.empty; done(); }); }); it("should set tags on book 1", function(done) { taggable.set(MODULE_NAME, 1, TAGS_NODE, function(err) { should.not.exist(err); done(); }); }); it("should get tags for book 1", function(done) { taggable.get(MODULE_NAME, 1, function(err, tags) { console.log("[taggabler_via_redis_test] tags:" + tags); should.not.exist(err); tags.sort().should.containDeep(TAGS_NODE); done(); }); }); it("should set tags on book 2", function(done) { taggable.set(MODULE_NAME, 2, TAGS_JQUERY, function(err) { should.not.exist(err); done(); }); }); it("should get tags for book 2", function(done) { taggable.get(MODULE_NAME, 2, function(err, tags) { console.log("[taggabler_via_redis_test] tags:" + tags); should.not.exist(err); tags.sort().should.containDeep(TAGS_JQUERY); done(); }); }); it("should set tags on book 3", function(done) { taggable.set(MODULE_NAME, 3, TAGS_RAILS, function(err) { should.not.exist(err); done(); }); }); it("should get tags for book 3", function(done) { taggable.get(MODULE_NAME, 3, function(err, tags) { console.log("[taggabler_via_redis_test] tags:" + tags); should.not.exist(err); tags.sort().should.containDeep(TAGS_RAILS); done(); }); }); it("should set tags on book 4", function(done) { taggable.set(MODULE_NAME, 4, TAGS_COFFEESCRIPT, function(err) { should.not.exist(err); done(); }); }); it("should get tags for book 4", function(done) { taggable.get(MODULE_NAME, 4, function(err, tags) { console.log("[taggabler_via_redis_test] tags:" + tags); should.not.exist(err); tags.sort().should.containDeep(TAGS_COFFEESCRIPT); done(); }); }); it("should get empty array if book has not been tagged", function(done) { taggable.get(MODULE_NAME, 99, function(err, tags) { should.not.exist(err); Array.isArray(tags).should.be.ok; tags.should.be.empty; done(); }); }); it("should find books from tag", function(done) { taggable.find(MODULE_NAME, "client", function(err, rsp) { should.not.exist(err); rsp.sort().should.containDeep(["2", "4"]); done(); }); }); it("should get empty array for non existing tag", function(done) { taggable.find(MODULE_NAME, "maytag", function(err, rsp) { should.not.exist(err); Array.isArray(rsp).should.be.ok; rsp.should.be.empty; done(); }); }); it("should get all items if no tags specified", function(done) { taggable.find(MODULE_NAME, [], function(err, rsp) { console.log("[taggabler_via_redis_test] rsp:" + rsp); should.not.exist(err); Array.isArray(rsp).should.be.ok; rsp.should.be.empty; done(); }); }); it("should get most popular tags", function(done) { taggable.popular(MODULE_NAME, 10, function(err, rsp) { console.log("[taggabler_via_redis_test] rsp:"); console.dir(rsp); should.not.exist(err); rsp[0].should.containDeep(["programming", 4]); done(); }); }); it("should clear tags on book 1", function(done) { taggable.set(MODULE_NAME, 1, null, function(err) { should.not.exist(err); taggable.get(MODULE_NAME, 1, function(err, tags) { console.log("[taggabler_via_redis_test] tags:" + tags); should.not.exist(err); tags.should.be.empty; return done(); }); }); }); return it("should able to get multi on one go", function(done) { taggable.get(MODULE_NAME, [1, 2, 3, 4], function(err, results) { console.log("[taggabler_via_redis_test] results:"); console.dir(results); results[0].should.be.empty; results[1].sort().should.containDeep(TAGS_JQUERY); results[2].sort().should.containDeep(TAGS_RAILS); results[3].sort().should.containDeep(TAGS_COFFEESCRIPT); done(); }); }); }); }); }).call(this);