forked from hubotio/hubot
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatastore_test.js
154 lines (135 loc) · 4.82 KB
/
datastore_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
'use strict'
/* global describe, beforeEach, it */
const chai = require('chai')
const sinon = require('sinon')
chai.use(require('sinon-chai'))
const expect = chai.expect
const Brain = require('../src/brain')
const InMemoryDataStore = require('../src/datastores/memory')
describe('Datastore', function () {
beforeEach(function () {
this.clock = sinon.useFakeTimers()
this.robot = {
emit () {},
on () {},
receive: sinon.spy()
}
// This *should* be callsArgAsync to match the 'on' API, but that makes
// the tests more complicated and seems irrelevant.
sinon.stub(this.robot, 'on').withArgs('running').callsArg(1)
this.robot.brain = new Brain(this.robot)
this.robot.datastore = new InMemoryDataStore(this.robot)
this.robot.brain.userForId('1', { name: 'User One' })
this.robot.brain.userForId('2', { name: 'User Two' })
})
this.afterEach(function () {
this.clock.restore()
})
describe('global scope', function () {
it('returns undefined for values not in the datastore', function () {
return this.robot.datastore.get('blah').then(function (value) {
expect(value).to.be.an('undefined')
})
})
it('can store simple values', function () {
return this.robot.datastore.set('key', 'value').then(() => {
return this.robot.datastore.get('key').then((value) => {
expect(value).to.equal('value')
})
})
})
it('can store arbitrary JavaScript values', function () {
const object = {
name: 'test',
data: [1, 2, 3]
}
return this.robot.datastore.set('key', object).then(() => {
return this.robot.datastore.get('key').then((value) => {
expect(value.name).to.equal('test')
expect(value.data).to.deep.equal([1, 2, 3])
})
})
})
it('can dig inside objects for values', function () {
const object = {
a: 'one',
b: 'two'
}
return this.robot.datastore.set('key', object).then(() => {
return this.robot.datastore.getObject('key', 'a').then((value) => {
expect(value).to.equal('one')
})
})
})
it('can set individual keys inside objects', function () {
const object = {
a: 'one',
b: 'two'
}
return this.robot.datastore.set('object', object).then(() => {
return this.robot.datastore.setObject('object', 'c', 'three').then(() => {
return this.robot.datastore.get('object').then((value) => {
expect(value.a).to.equal('one')
expect(value.b).to.equal('two')
expect(value.c).to.equal('three')
})
})
})
})
it('creates an object from scratch when none exists', function () {
return this.robot.datastore.setObject('object', 'key', 'value').then(() => {
return this.robot.datastore.get('object').then((value) => {
const expected = { key: 'value' }
expect(value).to.deep.equal(expected)
})
})
})
it('can append to an existing array', function () {
return this.robot.datastore.set('array', [1, 2, 3]).then(() => {
return this.robot.datastore.setArray('array', 4).then(() => {
return this.robot.datastore.get('array').then((value) => {
expect(value).to.deep.equal([1, 2, 3, 4])
})
})
})
})
it('creates an array from scratch when none exists', function () {
return this.robot.datastore.setArray('array', 4).then(() => {
return this.robot.datastore.get('array').then((value) => {
expect(value).to.deep.equal([4])
})
})
})
})
describe('User scope', function () {
it('has access to the robot object', function () {
const user = this.robot.brain.userForId('1')
expect(user._getRobot()).to.equal(this.robot)
})
it('can store user data which is separate from global data', function () {
const user = this.robot.brain.userForId('1')
return user.set('blah', 'blah').then(() => {
return user.get('blah').then((userBlah) => {
return this.robot.datastore.get('blah').then((datastoreBlah) => {
expect(userBlah).to.not.equal(datastoreBlah)
expect(userBlah).to.equal('blah')
expect(datastoreBlah).to.be.an('undefined')
})
})
})
})
it('stores user data separate per-user', function () {
const userOne = this.robot.brain.userForId('1')
const userTwo = this.robot.brain.userForId('2')
return userOne.set('blah', 'blah').then(() => {
return userOne.get('blah').then((valueOne) => {
return userTwo.get('blah').then((valueTwo) => {
expect(valueOne).to.not.equal(valueTwo)
expect(valueOne).to.equal('blah')
expect(valueTwo).to.be.an('undefined')
})
})
})
})
})
})