Skip to content

Commit

Permalink
Fix: Fixed hexists edge cases (#331)
Browse files Browse the repository at this point in the history
* Adding edge cases to hexists tests

* Adding edge cases to hexists

* Forgot to add imports

* Make linter happy

* Update CHANGELOG.md
  • Loading branch information
Tyler Wray authored and stipsan committed Nov 9, 2017
1 parent 0fe7246 commit b392026
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixes
* Fixed hexists edge cases (#331 @wraytw)

## [3.1.0] - 2017-10-03
### Features
Expand Down
6 changes: 5 additions & 1 deletion src/commands/hexists.js
@@ -1,3 +1,7 @@
export function hexists(key, field) {
return {}.hasOwnProperty.call(this.data.get(key), field) ? 1 : 0;
const hash = this.data.get(key);
if (!hash || hash[field] === undefined) {
return 0;
}
return {}.hasOwnProperty.call(hash, field) ? 1 : 0;
}
32 changes: 22 additions & 10 deletions test/commands/hexists.js
Expand Up @@ -3,15 +3,27 @@ import expect from 'expect';
import MockRedis from '../../src';

describe('hexists', () => {
const redis = new MockRedis({
data: {
foo: { bar: 'baz' },
},
context('hash exists', () => {
const redis = new MockRedis({
data: {
foo: { bar: 'baz' },
},
});

it('should return 1 if key exists in hash map', () =>
redis.hexists('foo', 'bar').then(status => expect(status).toBe(1))
);

it('should return 0 if key not exists in hash map', () =>
redis.hexists('foo', 'baz').then(status => expect(status).toBe(0))
);
});

context('hash doesn\'t exist', () => {
const redis = new MockRedis();

it('should return 0', () =>
redis.hexists('foo', 'baz').then(status => expect(status).toBe(0))
);
});
it('should return 1 if key exists in hash map', () =>
redis.hexists('foo', 'bar').then(status => expect(status).toBe(1))
);
it('should return 0 if key not exists in hash map', () =>
redis.hexists('foo', 'baz').then(status => expect(status).toBe(0))
);
});

0 comments on commit b392026

Please sign in to comment.