forked from stipsan/ioredis-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added setnx and fixed hsetnx (stipsan#65)
* hsetnx lied when it said it didn't overwrite * added setnx command * 1.6.0 * updated compat
- Loading branch information
Showing
7 changed files
with
46 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); | ||
}); |