Skip to content

Commit 4cf5122

Browse files
achambersdschmidt
authored andcommitted
First crack at fake redis client
This means we basically can test what is being passed to the redis client
1 parent e496d9b commit 4cf5122

File tree

4 files changed

+68
-50
lines changed

4 files changed

+68
-50
lines changed

index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,10 @@ module.exports = {
3939
return context.commandOptions.revision || context.revisionKey;
4040
},
4141
redisDeployClient: function(context) {
42-
if(context.redisDeployClient) {
43-
return context.redisDeployClient;
44-
}
42+
var options = context.config.redis;
43+
var fakeRedis = context.redisDeployClient;
4544

46-
return new (context.redisDeployClientClass || Redis)(context.config.redis);
45+
return new Redis(options, fakeRedis);
4746
}
4847
},
4948
configure: function(/* context */) {

lib/redis.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ var CoreObject = require('core-object');
22
var Promise = require('ember-cli/lib/ext/promise');
33

44
module.exports = CoreObject.extend({
5-
init: function(options) {
6-
if (options.redisClient) {
7-
this._client = options.redisClient;
8-
} else if (options.url) {
9-
this._client = require('then-redis').createClient(options.url);
5+
init: function(options, client) {
6+
var redisOptions = options;
7+
var redisLib = client;
8+
9+
if (options.url) {
10+
redisOptions = options.url;
1011
} else {
11-
var redisOptions = {
12+
redisOptions = {
1213
host: options.host,
1314
port: options.port
1415
};
@@ -20,9 +21,14 @@ module.exports = CoreObject.extend({
2021
if (options.database) {
2122
redisOptions.database = options.database;
2223
}
24+
}
2325

24-
this._client = require('then-redis').createClient(redisOptions);
26+
if (!client) {
27+
redisLib = require('then-redis');
2528
}
29+
30+
this._client = redisLib.createClient(redisOptions);
31+
2632
this._maxNumberOfRecentUploads = 10;
2733
this._allowOverwrite = options.allowOverwrite;
2834
},

tests/unit/index-nodetest.js

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@ var stubProject = {
1010
}
1111
};
1212

13+
var fakeClient = {
14+
createClient: function(options) {
15+
this.options = options;
16+
return {
17+
get: function(key) {
18+
return Promise.resolve('some-other-value');
19+
},
20+
set: function() { },
21+
lpush: function() { },
22+
ltrim: function() { }
23+
}
24+
}
25+
};
26+
1327
describe('redis plugin', function() {
1428
var subject, mockUi;
1529

@@ -69,7 +83,6 @@ describe('redis plugin', function() {
6983
name: 'redis'
7084
});
7185

72-
var redisDeployClientClassInitialised = false;
7386
var context = {
7487
ui: mockUi,
7588
project: stubProject,
@@ -80,19 +93,15 @@ describe('redis plugin', function() {
8093
database: 4
8194
}
8295
},
83-
redisDeployClientClass: CoreObject.extend({
84-
init: function (options) {
85-
assert.equal(options.host, 'somehost');
86-
assert.equal(options.port, 1234);
87-
assert.equal(options.database, 4);
88-
redisDeployClientClassInitialised = true;
89-
}
90-
})
96+
redisDeployClient: fakeClient
9197
};
9298
plugin.beforeHook(context);
9399
plugin.configure(context);
94-
plugin.readConfig("redisDeployClient");
95-
assert.ok(redisDeployClientClassInitialised);
100+
plugin.readConfig('redisDeployClient');
101+
102+
assert.equal(fakeClient.options.host, 'somehost');
103+
assert.equal(fakeClient.options.port, 1234);
104+
assert.equal(fakeClient.options.database, 4);
96105
});
97106

98107
describe('resolving revisionKey from the pipeline', function() {
@@ -295,11 +304,6 @@ describe('redis plugin', function() {
295304
});
296305

297306
context = {
298-
redisClient: {
299-
upload: function(keyPrefix, revisionKey) {
300-
return Promise.resolve(keyPrefix + ':' + revisionKey);
301-
}
302-
},
303307
ui: mockUi,
304308
project: stubProject,
305309
config: {
@@ -309,7 +313,11 @@ describe('redis plugin', function() {
309313
distDir: 'tests',
310314
revisionKey: '123abc',
311315
redisDeployClient: function(context) {
312-
return context.redisClient || new Redis(context.config.redis);
316+
return {
317+
upload: function(keyPrefix, revisionKey) {
318+
return Promise.resolve(keyPrefix + ':' + revisionKey);
319+
}
320+
};
313321
}
314322
}
315323
}
@@ -333,11 +341,6 @@ describe('redis plugin', function() {
333341
});
334342

335343
var context = {
336-
redisClient: {
337-
activate: function() {
338-
activateCalled = true;
339-
}
340-
},
341344
ui: mockUi,
342345
project: stubProject,
343346
config: {
@@ -346,7 +349,13 @@ describe('redis plugin', function() {
346349
filePattern: 'index.html',
347350
distDir: 'tests',
348351
revisionKey: '123abc',
349-
redisDeployClient: function(context){ return context.redisClient; }
352+
redisDeployClient: function(context){
353+
return {
354+
activate: function() {
355+
activateCalled = true;
356+
}
357+
};
358+
}
350359
}
351360
}
352361
};
@@ -365,11 +374,6 @@ describe('redis plugin', function() {
365374
});
366375

367376
var context = {
368-
redisClient: {
369-
activate: function() {
370-
return Promise.reject('some-error');
371-
}
372-
},
373377
ui: mockUi,
374378
project: stubProject,
375379
config: {
@@ -379,7 +383,11 @@ describe('redis plugin', function() {
379383
distDir: 'tests',
380384
revisionKey: '123abc',
381385
redisDeployClient: function(context) {
382-
return context.redisClient || new Redis(context.config.redis);
386+
return {
387+
activate: function() {
388+
return Promise.reject('some-error');
389+
}
390+
};
383391
}
384392
}
385393
}

tests/unit/lib/redis-nodetest.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,23 @@ describe('redis', function() {
1010
Redis = require('../../../lib/redis');
1111
});
1212

13+
var fakeClient = {
14+
createClient: function(options) {
15+
this.options = options;
16+
return {
17+
get: function(key) {
18+
return Promise.resolve('some-other-value');
19+
},
20+
set: function() { },
21+
lpush: function() { },
22+
ltrim: function() { }
23+
}
24+
}
25+
};
26+
1327
describe('#upload', function() {
1428
it('rejects if the key already exists in redis', function() {
15-
var redis = new Redis({
16-
redisClient: {
17-
get: function(key) {
18-
return Promise.resolve('some-other-value');
19-
},
20-
set: function() { },
21-
lpush: function() { },
22-
ltrim: function() { }
23-
}
24-
});
29+
var redis = new Redis({}, fakeClient);
2530

2631
var promise = redis.upload('key', 'value');
2732
return assert.isRejected(promise, /^Value already exists for key: key:default$/);

0 commit comments

Comments
 (0)