Skip to content

Commit 5280c16

Browse files
committed
add eslint, implement set, get, del,reset, extend unit test
1 parent 210e77a commit 5280c16

File tree

5 files changed

+154
-37
lines changed

5 files changed

+154
-37
lines changed

.eslintrc

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"globals": {
3+
},
4+
"env": {
5+
"node": true,
6+
"es6": true
7+
},
8+
9+
"extends": "eslint:recommended",
10+
11+
"rules": {
12+
"no-cond-assign": 0,
13+
"no-constant-condition": 0,
14+
"no-empty": 0,
15+
"no-fallthrough": 0,
16+
"no-unused-vars": 1,
17+
"no-console": 1,
18+
19+
"semi": 2,
20+
"curly": 2,
21+
"consistent-this": [2, "self"],
22+
"indent": [ 2, 4, { "SwitchCase": 1 } ],
23+
"linebreak-style": [2, "unix"],
24+
"no-nested-ternary": 2,
25+
26+
"new-parens": 2,
27+
"no-dupe-class-members": 2,
28+
"require-yield": 2,
29+
"arrow-spacing": 1,
30+
"no-var": 2,
31+
32+
"no-multi-spaces": 1,
33+
"space-return-throw-case": 1,
34+
"space-infix-ops": [1, {"int32Hint": false}],
35+
"brace-style": 1,
36+
"space-before-blocks": 1,
37+
"operator-linebreak": [1, "before"],
38+
"no-unneeded-ternary": 1,
39+
"no-lonely-if": 1,
40+
"key-spacing": 1,
41+
"quotes": [1, "double", "avoid-escape"],
42+
"no-trailing-spaces": [1, { "skipBlankLines": true }]
43+
}
44+
}

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33
default: test
44

55
test:
6-
./node_modules/.bin/mocha
6+
./node_modules/.bin/mocha
7+
lint:
8+
node_modules/eslint/bin/eslint.js .

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"devDependencies": {
1212
"cache-manager": "^1.4.0",
1313
"chai": "^3.5.0",
14+
"eslint": "^1.10.3",
1415
"mocha": "^2.4.5",
1516
"mongoose-mock": "^0.4.0",
1617
"proxyquire": "^1.7.4",

src/index.js

Lines changed: 65 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,21 @@
11
"use strict";
22

3-
const mongoose = require("mongoose");
4-
5-
6-
const schemaTemplate = {
7-
obj: {
8-
_id: String,
9-
val: mongoose.Schema.Types.Mixed,
10-
exp: Number
11-
},
12-
options: {
13-
collection: "cacheman",
14-
versionKey: false
15-
}
16-
};
17-
18-
const EMPTY = {};
19-
20-
class MongooseStoreError extends Error {};
3+
class MongooseStoreError extends Error {}
214

225
class MongooseStore {
236
constructor(args) {
7+
if (!args) {
8+
args = {};
9+
}
10+
this.mongoose = args.mongoose || require("mongoose");
11+
2412
if (args.model) {
2513
switch (typeof args.model) {
2614
case "object":
2715
this.model = args.model;
2816
break;
2917
case "string":
30-
this.model = mongoose.model(args.model);
18+
this.model = this.mongoose.model(args.model);
3119
break;
3220
default:
3321
throw new MongooseStoreError("unexpected type of args.model in constructor");
@@ -39,12 +27,26 @@ class MongooseStore {
3927
}
4028

4129
makeModel(args) {
30+
const schemaTemplate = {
31+
obj: {
32+
_id: String,
33+
val: this.mongoose.Schema.Types.Mixed,
34+
exp: Number
35+
},
36+
options: {
37+
collection: "cacheman",
38+
versionKey: false
39+
}
40+
};
41+
4242
const options = Object.assign({}, schemaTemplate.options, args.modelOptions);
43-
const schema = new mongoose.Schema(
43+
const schema = new this.mongoose.Schema(
4444
schemaTemplate.obj,
4545
options
4646
);
47-
return mongoose.model(args.model, schema);
47+
48+
let model = this.mongoose.model(args.model || "MongooseCache", schema);
49+
return model;
4850
}
4951

5052
result(fn, error, result) {
@@ -54,7 +56,24 @@ class MongooseStore {
5456
}
5557

5658
get(key, options, fn) {
57-
return this.model.findOne({_id: key})
59+
try {
60+
return this.model.findOne(
61+
{_id: key}
62+
)
63+
.then(record => {
64+
if (!record) {
65+
return this.result(fn);
66+
}
67+
if (record.exp < Date.now()) {
68+
return this.del(key, null, fn);
69+
} else {
70+
return this.result(fn, null, record.val);
71+
}
72+
})
73+
.catch(e => this.result(fn, e));
74+
} catch (e) {
75+
this.result(fn, e);
76+
}
5877
}
5978

6079
set(key, val, options, fn) {
@@ -69,26 +88,40 @@ class MongooseStore {
6988
exp: Date.now() + ttl * 1000
7089
},
7190
{upsert: true}
72-
).then(() => {
73-
this.result(fn);
74-
}).catch(e => {
75-
this.result(fn, e)
76-
});
91+
)
92+
.then(() => this.result(fn))
93+
.catch(e => this.result(fn, e));
7794
} catch (e) {
78-
this.result(fn, e)
95+
this.result(fn, e);
7996
}
8097
}
8198

8299
del(key, options, fn) {
83-
100+
try {
101+
return this.model.remove(
102+
{_id: key}
103+
)
104+
.then(() => this.result(fn));
105+
} catch (e) {
106+
this.result(fn, e);
107+
}
84108
}
85109

86110
reset(key, fn) {
111+
try {
112+
if ("function" === typeof key) {
113+
fn = key;
114+
key = null;
115+
}
116+
return this.model.remove({}).then(() => fn());
117+
} catch (e) {
118+
this.result(fn, e);
119+
}
87120
}
88121
}
89122

90123
module.exports = {
91-
create : function (args) {
92-
return new MongooseStore(args);
93-
}
124+
create: function (args) {
125+
return new MongooseStore(args);
126+
}
94127
};

test/indexSpec.js

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-env mocha */
12
"use strict";
23

34
let mongoose = require("mongoose-mock"),
@@ -40,15 +41,51 @@ describe("cache-manager-mongoose", function() {
4041
expect(cache.store.model).not.to.be.undefined;
4142
});
4243

43-
it("set saves key, value, expires record", sinon.test(function(done) {
44+
it("set calls update", sinon.test(function(done) {
4445
let cache = cm.caching({
4546
store: cmm,
4647
model: modelStub
4748
});
48-
cache.set("someKey", "someValue", null, function (err, result) {
49+
let spy = this.stub(modelStub, "update");
50+
cache.set("someKey", "someValue", null, function () {
51+
sinon.assert.calledOnce(spy);
52+
done();
53+
});
54+
}));
55+
56+
it("get calls findOne", sinon.test(function(done) {
57+
let cache = cm.caching({
58+
store: cmm,
59+
model: modelStub
60+
});
61+
let spy = this.stub(modelStub, "findOne");
62+
cache.get("someKey", null, function () {
63+
sinon.assert.calledOnce(spy);
64+
done();
65+
});
66+
}));
67+
68+
it("del calls remove", sinon.test(function(done) {
69+
let cache = cm.caching({
70+
store: cmm,
71+
model: modelStub
72+
});
73+
let spy = this.stub(modelStub, "remove");
74+
cache.del("someKey", null, function () {
75+
sinon.assert.calledOnce(spy);
76+
done();
77+
});
78+
}));
79+
80+
it("reset calls remove", sinon.test(function(done) {
81+
let cache = cm.caching({
82+
store: cmm,
83+
model: modelStub
84+
});
85+
let spy = this.stub(modelStub, "remove");
86+
cache.reset(function () {
87+
sinon.assert.calledOnce(spy);
4988
done();
5089
});
51-
let updateSpy = sinon.stub(modelStub, "update");
52-
sinon.assert.calledOnce(updateSpy);
5390
}));
5491
});

0 commit comments

Comments
 (0)