Skip to content

Commit

Permalink
Update dependencies, update ioredis to v5, allow testing redis to be …
Browse files Browse the repository at this point in the history
…configurable, and move ioredis to peer dependency
  • Loading branch information
vsumner committed Dec 1, 2022
1 parent 40bc726 commit a344777
Show file tree
Hide file tree
Showing 4 changed files with 2,645 additions and 2,168 deletions.
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
"dependencies": {
"@babel/runtime": "^7.8.3",
"co-wrap-all": "^1.0.0",
"debug": "^4.1.1",
"ioredis": "^4.14.1"
"debug": "^4.1.1"
},
"devDependencies": {
"@babel/cli": "^7.8.3",
Expand All @@ -46,10 +45,14 @@
"remark-preset-github": "^0.0.16",
"rimraf": "^3.0.0",
"should": "^13.2.3",
"xo": "^0.25.3"
"xo": "^0.25.3",
"ioredis": "^5.2.4"
},
"peerDependencies": {
"ioredis": "5.x"
},
"engines": {
"node": ">= 4"
"node": ">= 14"
},
"files": [
"lib"
Expand Down
14 changes: 9 additions & 5 deletions test/async.test.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
const should = require('should');

// allow the default url to be overriden for local testing
const redisUrl = process.env.REDIS_URL ? process.env.REDIS_URL : 'redis://localhost:6379/';


if (parseFloat(process.version.slice(1)) >= 7.6) {
describe('test/async.test.js', function() {
it('should set with ttl ok', async () => {
const store = require('..')();
const store = require('..')({url: redisUrl});
await store.set('key:ttl', { a: 1 }, 86400000);
(await store.get('key:ttl')).should.eql({ a: 1 });
(await store.client.ttl('key:ttl')).should.equal(86400);
await store.quit();
});

it('should not throw error with bad JSON', async () => {
const store = require('..')();
const store = require('..')({url: redisUrl});
await store.client.set('key:badKey', '{I will cause an error!}');
should.not.exist(await store.get('key:badKey'));
await store.quit();
});

it('should set without ttl ok', async () => {
const store = require('..')();
const store = require('..')({url: redisUrl});
await store.set('key:nottl', { a: 1 });
(await store.get('key:nottl')).should.eql({ a: 1 });
(await store.client.ttl('key:nottl')).should.equal(-1);
await store.quit();
});

it('should destroy ok', async () => {
const store = require('..')();
const store = require('..')({url: redisUrl});
await store.destroy('key:nottl');
await store.destroy('key:ttl');
await store.destroy('key:badKey');
Expand All @@ -44,7 +48,7 @@ if (parseFloat(process.version.slice(1)) >= 7.6) {
});
}

const store = require('..')();
const store = require('..')({url: redisUrl});
await store.set('key:ttl2', { a: 1, b: 2 }, 1000);
await sleep(1200); // Some odd delay introduced by co-mocha
should.not.exist(await store.get('key:ttl2'));
Expand Down
29 changes: 17 additions & 12 deletions test/koa-redis.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
const should = require('should');
const Redis = require('ioredis');

// allow the default url to be overriden for local testing
const redisUrl = process.env.REDIS_URL ? process.env.REDIS_URL : 'redis://localhost:6379/';

function event(object, name) {
// Convert events to promises
return new Promise(resolve => {
Expand All @@ -23,7 +26,7 @@ function event(object, name) {

describe('test/koa-redis.test.js', () => {
it('should connect and ready with external client and quit ok', function*() {
const store = require('..')({ client: new Redis() });
const store = require('..')({ client: new Redis(redisUrl) });
yield event(store, 'connect');
store.connected.should.eql(true);
yield event(store, 'ready');
Expand All @@ -34,7 +37,7 @@ describe('test/koa-redis.test.js', () => {

it('should connect and ready with duplicated external client and disconnect ok', function*() {
const store = require('..')({
client: new Redis(),
client: new Redis(redisUrl),
duplicate: true
});
yield event(store, 'connect');
Expand All @@ -47,7 +50,7 @@ describe('test/koa-redis.test.js', () => {

it('should connect and ready with url and quit ok', function*() {
const store = require('..')({
url: 'redis://localhost:6379/'
url: redisUrl
});
yield event(store, 'connect');
store.connected.should.eql(true);
Expand All @@ -58,8 +61,8 @@ describe('test/koa-redis.test.js', () => {
});

it('should set and delete with db ok', function*() {
const store = require('..')({ db: 2 });
const client = new Redis();
const store = require('..')({ db: 2, url: redisUrl });
const client = new Redis(redisUrl);
client.select(2);
yield store.set('key:db1', { a: 2 });
(yield store.get('key:db1')).should.eql({ a: 2 });
Expand All @@ -71,15 +74,15 @@ describe('test/koa-redis.test.js', () => {
});

it('should set with ttl ok', function*() {
const store = require('..')();
const store = require('..')({url: redisUrl});
yield store.set('key:ttl', { a: 1 }, 86400000);
(yield store.get('key:ttl')).should.eql({ a: 1 });
(yield store.client.ttl('key:ttl')).should.equal(86400);
yield store.quit();
});

it('should not throw error with bad JSON', function*() {
const store = require('..')();
const store = require('..')({url: redisUrl});
yield store.client.set('key:badKey', '{I will cause an error!}');
should.not.exist(yield store.get('key:badKey'));
yield store.quit();
Expand All @@ -88,7 +91,8 @@ describe('test/koa-redis.test.js', () => {
it('should use default JSON.parse/JSON.stringify without serialize/unserialize function', function*() {
const store = require('..')({
serialize: 'Not a function',
unserialize: 'Not a function'
unserialize: 'Not a function',
url: redisUrl
});
yield store.set('key:notserialized', { a: 1 });
(yield store.get('key:notserialized')).should.eql({ a: 1 });
Expand All @@ -98,23 +102,24 @@ describe('test/koa-redis.test.js', () => {
it('should parse bad JSON with custom unserialize function', function*() {
const store = require('..')({
serialize: value => 'JSON:' + JSON.stringify(value),
unserialize: value => JSON.parse(value.slice(5))
unserialize: value => JSON.parse(value.slice(5)),
url: redisUrl
});
yield store.set('key:notserialized', { a: 1 });
(yield store.get('key:notserialized')).should.eql({ a: 1 });
yield store.quit();
});

it('should set without ttl ok', function*() {
const store = require('..')();
const store = require('..')({url: redisUrl});
yield store.set('key:nottl', { a: 1 });
(yield store.get('key:nottl')).should.eql({ a: 1 });
(yield store.client.ttl('key:nottl')).should.equal(-1);
yield store.quit();
});

it('should destroy ok', function*() {
const store = require('..')();
const store = require('..')({url: redisUrl});
yield store.destroy('key:nottl');
yield store.destroy('key:ttl');
yield store.destroy('key:badKey');
Expand All @@ -132,7 +137,7 @@ describe('test/koa-redis.test.js', () => {
});
}

const store = require('..')();
const store = require('..')({url: redisUrl});
yield store.set('key:ttl2', { a: 1, b: 2 }, 1000);
yield sleep(1200); // Some odd delay introduced by co-mocha
should.not.exist(yield store.get('key:ttl2'));
Expand Down
Loading

0 comments on commit a344777

Please sign in to comment.