UNPKG

mongoose-taggable-via-redis

Version:

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

133 lines (112 loc) 3.82 kB
// Generated by CoffeeScript 1.6.3 /* # test tags removal when db record removed */ (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 = "RemovalTest"; 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 removal", function() { after(function(done) { mongoose.connection.close(); return done(); }); before(function(done) { console.log("[removal_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: "books", redisClient: REDIS_CLIENT, getScope: function() { return "owner/" + this.owner; } }); mongoose.model(MODEL_NAME, schema); return done(); }); }); before(function(done) { console.log("[removal_test::before 2]"); Record = mongoose.model(MODEL_NAME); Record.remove(done); }); before(function(done) { var obj; console.log("[removal_test::before 3]"); obj = { _id: (Date.now()).toString(36), name: "paginate", owner: 'tester', createdAt: new Date().setDate(new Date().getDate()) }; Record(obj).save(done); }); before(function(done) { REDIS_CLIENT.flushall(); setTimeout(done, 1800); }); return describe("mongoose-taggable-via-redis", function() { this.timeout(10000); it("should clean up tags upon record removal", function(done) { return Record.findOne(function(err, item) { should.not.exist(err); return item.setTags(TAGS_NODE, function(err) { should.not.exist(err); return Record.popularTags(10, function(err, tags) { should.not.exist(err); tags.length.should.above(1); return item.remove(function(err) { should.not.exist(err); return setTimeout((function() { Record.popularTags(10, function(err, tags2) { console.log("[removal_test] tags2:"); console.dir(tags2); should.not.exist(err); tags2.should.be.empty; done(); }); }), 2000); }); }); }); }); }); return it("scoped tags should also be cleaned up", function(done) { return Record.popularTags(10, "owner/tester", function(err, tags) { console.log("[removal_test] tags:"); console.dir(tags); should.not.exist(err); tags.should.be.empty; done(); }); }); }); }); }).call(this);