Skip to content

Commit

Permalink
Added setnx and fixed hsetnx (#65)
Browse files Browse the repository at this point in the history
* hsetnx lied when it said it didn't overwrite

* added setnx command

* 1.6.0

* updated compat
  • Loading branch information
stipsan authored Jul 4, 2016
1 parent 718bcd8 commit 5b5aecc
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 7 deletions.
2 changes: 1 addition & 1 deletion compat.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
|[set](http://redis.io/commands/SET)|:white_check_mark:|:white_check_mark:|
|[setbit](http://redis.io/commands/SETBIT)|:white_check_mark:|:x:|
|[setex](http://redis.io/commands/SETEX)|:white_check_mark:|:x:|
|[setnx](http://redis.io/commands/SETNX)|:white_check_mark:|:x:|
|[setnx](http://redis.io/commands/SETNX)|:white_check_mark:|:white_check_mark:|
|[setrange](http://redis.io/commands/SETRANGE)|:white_check_mark:|:x:|
|[shutdown](http://redis.io/commands/SHUTDOWN)|:white_check_mark:|:x:|
|[sinter](http://redis.io/commands/SINTER)|:white_check_mark:|:x:|
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ioredis-mock",
"version": "1.5.0",
"version": "1.6.0",
"description": "This library emulates ioredis by performing all operations in-memory.",
"main": "./lib",
"scripts": {
Expand Down
10 changes: 7 additions & 3 deletions src/commands/hsetnx.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ export function hsetnx(key, hashKey, hashVal) {
if (!this.data.hasOwnProperty(key)) {
this.data[key] = {};
}
const exists = this.data[key].hasOwnProperty(hashKey);
this.data[key][hashKey] = hashVal;

return !exists;
if (!this.data[key].hasOwnProperty(hashKey)) {
this.data[key][hashKey] = hashVal;

return '1';
}

return '0';
}
1 change: 1 addition & 0 deletions src/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export * from './incrby';
export * from './rename';
export * from './sadd';
export * from './set';
export * from './setnx';
export * from './sismember';
export * from './smembers';
export * from './srem';
Expand Down
9 changes: 9 additions & 0 deletions src/commands/setnx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function setnx(key, val) {
if (!this.data.hasOwnProperty(key)) {
this.data[key] = val;

return '1';
}

return '0';
}
8 changes: 6 additions & 2 deletions test/commands/hsetnx.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ describe('hsetnx', () => {
it('should set a key in a hash map if it does not exist already', () => {
const redis = new MockRedis();
return redis.hsetnx('emails', '[email protected]', '1')
.then(userNext => expect(userNext).toBe(true))
.then(status => expect(status).toBe('1'))
.then(() => {
expect(redis.data.emails['[email protected]'])
.toBe('1', 'hash map value failed to persist');
return redis.hsetnx('emails', '[email protected]', '2');
})
.then(userNext => expect(userNext).toBeFalsy('hsetnx no-op failed on existing key'));
.then(status => expect(status).toBe('0', 'hsetnx no-op failed on existing key'))
.then(() => {
expect(redis.data.emails['[email protected]'])
.toBe('1', 'existing hash map value was overwritten');
});
});
});
21 changes: 21 additions & 0 deletions test/commands/setnx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import expect from 'expect';

import MockRedis from '../../src';

describe('setnx', () => {
it('should set a key with value if it does not exist already', () => {
const redis = new MockRedis();
return redis.setnx('foo', 'bar')
.then(status => expect(status).toBe('1'))
.then(() => {
expect(redis.data.foo)
.toBe('bar', 'value failed to persist');
return redis.setnx('foo', 'baz');
})
.then(status => expect(status).toBe('0', 'setnx no-op failed on existing key'))
.then(() => {
expect(redis.data.foo)
.toBe('bar', 'existing value was overwritten');
});
});
});

0 comments on commit 5b5aecc

Please sign in to comment.