UNPKG

mongoose-taggable-via-redis

Version:

a mongoose plugin provide fast and flexible tagging ablity to datamodle via redis, supports scope tagging

162 lines (141 loc) 4.77 kB
// Generated by CoffeeScript 1.6.3 /* # test for basic */ (function() { var ALL_TAGS, MODEL_NAME, REDIS_CLIENT, Record, TAGS_COFFEESCRIPT, TAGS_JQUERY, TAGS_NODE, TAGS_NODE2, TAGS_RAILS, async, mongoose, redis, schema, should, taggable; should = require("should"); mongoose = require('mongoose'); taggable = require("../mongoose-taggable-via-redis"); redis = require("redis"); async = require("async"); REDIS_CLIENT = redis.createClient(); MODEL_NAME = "Record"; TAGS_NODE = "javascript,server,programming".split(",").sort(); TAGS_JQUERY = "javascript,client,programming".split(",").sort(); TAGS_RAILS = "ruby,programming".split(",").sort(); TAGS_COFFEESCRIPT = "javascript,client,server,programming".split(",").sort(); TAGS_NODE2 = "javascript,server,programming,async,joyent".split(",").sort(); ALL_TAGS = []; ALL_TAGS.push(TAGS_NODE, TAGS_JQUERY, TAGS_RAILS, TAGS_COFFEESCRIPT, TAGS_NODE2); schema = void 0; Record = null; describe("test basic", function() { after(function(done) { mongoose.connection.close(); return done(); }); before(function(done) { console.log("[basic_test::before 1]"); mongoose.connect("mongodb://localhost/test"); return mongoose.connection.once("connected", function(err) { if (err != null) { return done(err); } schema = new mongoose.Schema({}, { versionKey: false }); schema.add({ _id: String, name: String, createdAt: Date, owner: String }); schema.plugin(taggable, { taggable: "records", redisClient: REDIS_CLIENT, getScope: function() { return "owner/" + this.owner; } }); mongoose.model(MODEL_NAME, schema); return done(); }); }); before(function(done) { console.log("[basic_test::before 2]"); Record = mongoose.model(MODEL_NAME); Record.remove(done); }); before(function(done) { var arr, i, _i; console.log("[basic_test::before 3]"); arr = []; for (i = _i = 0; _i < 100; i = ++_i) { arr.push(i); } async.each(arr, (function(i, cb) { var obj; obj = { _id: "" + ((Date.now()).toString(36)) + i, name: "paginate_" + i, owner: 'tester', createdAt: new Date().setDate(new Date().getDate() - i) }; Record(obj).save(cb); }), done); }); before(function(done) { REDIS_CLIENT.flushall(); setTimeout(done, 1800); }); return describe("mongoose-taggable-via-redis", function() { it("should able to set tags", function(done) { Record.findOne(function(err, item) { should.not.exist(err); item.setTags(TAGS_NODE, function(err) { should.not.exist(err); Record.findWithTags({ _id: item.id }, function(err, results) { var tags; should.not.exist(err); results.length.should.eql(1); tags = results[0].tags; console.log("[basic_test] tags:" + tags); tags.sort().should.containDeep(TAGS_NODE); return Record.where({ _id: item.id }).execWithTag(function(err, results2) { should.not.exist(err); results2.length.should.eql(1); tags = results2[0].tags; tags.sort().should.containDeep(TAGS_NODE); return done(); }); }); }); }); }); it("set more tags", function(done) { return Record.where().limit(10).exec(function(err, results) { should.not.exist(err); return async.each(results, (function(item, cb) { item.setTags(ALL_TAGS[ALL_TAGS.length * Math.random() >>> 0], cb); }), done); }); }); it("popularTags", function(done) { return Record.popularTags(10, function(err, tags) { console.dir(tags); should.not.exist(err); tags.length.should.above(TAGS_NODE.length - 1); return done(); }); }); return it("findByTags", function(done) { Record.findByTags("programming", function(err, results) { var result, _i, _len; console.dir(results); should.not.exist(err); results.length.should.above(1); for (_i = 0, _len = results.length; _i < _len; _i++) { result = results[_i]; console.log("[basic_test] id:" + result.id + ", tags:" + result.tags); } done(); }); }); }); }); }).call(this);