Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get and set features #21

Merged
merged 4 commits into from
Jun 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
ioredis-mock
============

[![AppVeyor branch](https://img.shields.io/appveyor/ci/stipsan/ioredis-mock/master.svg?label=windows)](https://ci.appveyor.com/project/stipsan/ioredis-mock)
[![Travis branch](https://img.shields.io/travis/stipsan/ioredis-mock/master.svg?label=linux)](https://travis-ci.org/stipsan/ioredis-mock)
[![AppVeyor branch](https://img.shields.io/appveyor/ci/stipsan/ioredis-mock.svg?label=windows)](https://ci.appveyor.com/project/stipsan/ioredis-mock)
[![Travis branch](https://img.shields.io/travis/stipsan/ioredis-mock.svg?label=linux)](https://travis-ci.org/stipsan/ioredis-mock)
[![CircleCI](https://img.shields.io/circleci/project/stipsan/ioredis-mock.svg?label=node+matrix)](https://circleci.com/gh/stipsan/ioredis-mock)
[![Code Climate](https://codeclimate.com/github/stipsan/ioredis-mock/badges/gpa.svg)](https://codeclimate.com/github/stipsan/ioredis-mock)
[![Issue Count](https://img.shields.io/codeclimate/issues/github/me-and/mdf.svg)](https://codeclimate.com/github/stipsan/ioredis-mock)
[![Coverage Status](https://coveralls.io/repos/github/stipsan/ioredis-mock/badge.svg?branch=master)](https://coveralls.io/github/stipsan/ioredis-mock?branch=master)
[![Issue Count](https://img.shields.io/codeclimate/issues/github/stipsan/ioredis-mock.svg)](https://codeclimate.com/github/stipsan/ioredis-mock)
[![Coverage Status](https://coveralls.io/repos/github/stipsan/ioredis-mock/badge.svg)](https://coveralls.io/github/stipsan/ioredis-mock)
[![npm package](https://img.shields.io/npm/dm/ioredis-mock.svg)](https://www.npmjs.com/package/ioredis-mock)

[![NPM](https://nodei.co/npm/ioredis-mock.png)](https://www.npmjs.com/package/ioredis-mock)
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.0.3",
"version": "1.0.4",
"description": "This library emulates ioredis by performing all operations in-memory.",
"main": "./lib",
"scripts": {
Expand Down
11 changes: 11 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,24 @@ class RedisMock {
constructor({ data } = { data: {} }) {
this.data = data;
}
get(key) {
return new Promise(resolve => resolve(
this.data.hasOwnProperty(key) ? this.data[key] : null
));
}
incr(key) {
return new Promise(resolve => {
const curVal = Number(this.data[key]);
this.data[key] = curVal + 1;
resolve(this.data[key].toString());
});
}
set(key, value) {
return new Promise(resolve => {
this.data[key] = value;
resolve('OK');
});
}
hsetnx(key, hashKey, hashVal) {
return new Promise(resolve => {
if (!this.data.hasOwnProperty(key)) {
Expand Down
21 changes: 21 additions & 0 deletions test/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import expect from 'expect';

import MockRedis from '../src';

describe('get', () => {
it('should return null on keys that do not exist', () => {
const redis = new MockRedis();

return redis.get('foo').then(result => expect(result).toBe(null));
});

it('should return null on keys that do not exist', () => {
const redis = new MockRedis({
data: {
foo: 'bar',
},
});

return redis.get('foo').then(result => expect(result).toBe('bar'));
});
});
11 changes: 11 additions & 0 deletions test/set.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import expect from 'expect';

import MockRedis from '../src';

describe('set', () => {
it('should return OK when setting a hash key', () => {
const redis = new MockRedis();
return redis.set('foo', 'bar').then(status => expect(status).toBe('OK'))
.then(() => expect(redis.data.foo).toBe('bar'));
});
});