-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'); | ||
}); | ||
}); | ||
}); |
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'); | ||
}); | ||
}); | ||
}); |