Skip to content
This repository was archived by the owner on Dec 1, 2024. It is now read-only.

Commit 89d4a51

Browse files
authored
Create test suite (#677)
* Export test suite * Make test suite work on a db without encoding-down * Temporarily pipe airtap output to file
1 parent 91ff0e7 commit 89d4a51

File tree

11 files changed

+181
-109
lines changed

11 files changed

+181
-109
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ libpeerconnection.log
1010
yarn.lock
1111
package-lock.json
1212
.nyc_output/
13+
airtap.log

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ test
33
.travis.yml
44
.dntrc
55
.nyc_output/
6+
airtap.log

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
"license": "MIT",
66
"main": "lib/levelup.js",
77
"scripts": {
8-
"test": "standard && hallmark && (nyc -s node test | faucet) && nyc report",
8+
"test": "standard && hallmark && (nyc -s node test/self.js | faucet) && nyc report",
99
"coverage": "nyc report --reporter=text-lcov | coveralls",
10-
"test-browsers": "airtap --loopback airtap.local test/index.js",
11-
"test-browser-local": "airtap --local test/index.js",
10+
"test-browsers": "airtap --loopback airtap.local test/self.js > airtap.log",
11+
"test-browser-local": "airtap --local test/self.js",
1212
"hallmark": "hallmark --fix",
1313
"dependency-check": "dependency-check . test/*.js",
1414
"prepublishOnly": "npm run dependency-check"

test/batch-test.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = function (test, testCommon) {
1515
t.ifError(err)
1616

1717
each(['foo', 'bar', 'baz'], function (key, next) {
18-
db.get(key, function (err, value) {
18+
db.get(key, { asBuffer: false }, function (err, value) {
1919
t.ifError(err)
2020
t.is(value, 'a' + key + 'value')
2121
next()
@@ -25,7 +25,7 @@ module.exports = function (test, testCommon) {
2525
})
2626
})
2727

28-
test('array-form batch(): promise interface', function (t) {
28+
testCommon.promises && test('array-form batch(): promise interface', function (t) {
2929
discardable(t, testCommon, function (db, done) {
3030
db.batch([
3131
{ type: 'put', key: 'foo', value: 'afoovalue' },
@@ -34,7 +34,7 @@ module.exports = function (test, testCommon) {
3434
])
3535
.then(function () {
3636
each(['foo', 'bar', 'baz'], function (key, next) {
37-
db.get(key, function (err, value) {
37+
db.get(key, { asBuffer: false }, function (err, value) {
3838
t.ifError(err)
3939
t.is(value, 'a' + key + 'value')
4040
next()
@@ -67,7 +67,7 @@ module.exports = function (test, testCommon) {
6767
function (next) {
6868
// these should exist
6969
each(['2', '3', 'bar', 'baz'], function (key, next) {
70-
db.get(key, function (err, value) {
70+
db.get(key, { asBuffer: false }, function (err, value) {
7171
t.ifError(err)
7272
t.ok(value != null)
7373
next()
@@ -77,7 +77,7 @@ module.exports = function (test, testCommon) {
7777
function (next) {
7878
// these shouldn't exist
7979
each(['1', 'foo'], function (key, next) {
80-
db.get(key, function (err, value) {
80+
db.get(key, { asBuffer: false }, function (err, value) {
8181
t.ok(err)
8282
t.ok(err instanceof errors.NotFoundError)
8383
t.is(value, undefined)
@@ -107,7 +107,7 @@ module.exports = function (test, testCommon) {
107107
t.ifError(err)
108108

109109
each(['one', 'three', '1', '2', '3'], function (key, next) {
110-
db.get(key, function (err) {
110+
db.get(key, { asBuffer: false }, function (err) {
111111
if (['one', 'three', '1', '3'].indexOf(key) > -1) {
112112
t.ok(err)
113113
} else {
@@ -125,9 +125,11 @@ module.exports = function (test, testCommon) {
125125
test('chained batch(): options', function (t) {
126126
discardable(t, testCommon, function (db, done) {
127127
var batch = db.batch()
128+
var underlying = batch
129+
while (underlying.batch) underlying = underlying.batch
128130

129-
var write = batch.batch.write.bind(batch.batch)
130-
batch.batch.write = function (options, cb) {
131+
var write = underlying.write.bind(underlying)
132+
underlying.write = function (options, cb) {
131133
t.same(options, { foo: 'bar' })
132134
write(options, cb)
133135
}
@@ -140,7 +142,7 @@ module.exports = function (test, testCommon) {
140142
})
141143
})
142144

143-
test('chained batch(): promise interface - options', function (t) {
145+
testCommon.promises && test('chained batch(): promise interface - options', function (t) {
144146
discardable(t, testCommon, function (db, done) {
145147
var batch = db.batch()
146148

@@ -157,7 +159,7 @@ module.exports = function (test, testCommon) {
157159
})
158160
})
159161

160-
test('chained batch(): promise interface', function (t) {
162+
testCommon.promises && test('chained batch(): promise interface', function (t) {
161163
discardable(t, testCommon, function (db, done) {
162164
db.put('1', 'one', function (err) {
163165
t.ifError(err)
@@ -174,7 +176,7 @@ module.exports = function (test, testCommon) {
174176
.write()
175177
.then(function () {
176178
each(['one', 'three', '1', '2', '3'], function (key, next) {
177-
db.get(key, function (err) {
179+
db.get(key, { asBuffer: false }, function (err) {
178180
if (['one', 'three', '1', '3'].indexOf(key) > -1) {
179181
t.ok(err)
180182
} else {
@@ -228,7 +230,7 @@ module.exports = function (test, testCommon) {
228230
function (next) {
229231
// these should exist
230232
each(['2', '3', 'bar', 'baz'], function (key, next) {
231-
db.get(key, function (err, value) {
233+
db.get(key, { asBuffer: false }, function (err, value) {
232234
t.ifError(err)
233235
t.ok(value != null)
234236
next()
@@ -238,7 +240,7 @@ module.exports = function (test, testCommon) {
238240
function (next) {
239241
// these shouldn't exist
240242
each(['1', 'foo'], function (key, next) {
241-
db.get(key, function (err, value) {
243+
db.get(key, { asBuffer: false }, function (err, value) {
242244
t.ok(err)
243245
t.ok(err instanceof errors.NotFoundError)
244246
t.is(value, undefined)
@@ -264,7 +266,7 @@ module.exports = function (test, testCommon) {
264266
function (next) {
265267
// these should exist
266268
each(['2', '3'], function (key, next) {
267-
db.get(key, function (err, value) {
269+
db.get(key, { asBuffer: false }, function (err, value) {
268270
t.ifError(err)
269271
t.ok(value != null)
270272
next()
@@ -273,7 +275,7 @@ module.exports = function (test, testCommon) {
273275
},
274276
function (next) {
275277
// this shouldn't exist
276-
db.get('1', function (err, value) {
278+
db.get('1', { asBuffer: false }, function (err, value) {
277279
t.ok(err)
278280
t.ok(err instanceof errors.NotFoundError)
279281
t.is(value, undefined)

test/deferred-open-test.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
var each = require('async-each')
22
var parallel = require('run-parallel')
33
var concat = require('concat-stream')
4-
var sinon = require('sinon')
54
var readStreamContext = require('./util/rs-context')
5+
var rsFactory = require('./util/rs-factory')
66

77
module.exports = function (test, testCommon) {
8+
var createReadStream = rsFactory(testCommon)
9+
810
test('deferred open(): put() and get() on new database', function (t) {
911
// 1) open database without callback, opens in next tick
1012
var db = testCommon.factory()
@@ -19,13 +21,13 @@ module.exports = function (test, testCommon) {
1921
// 3) when the callbacks have returned, the database should be open and those values should be in
2022
// verify that the values are there
2123
each([1, 2, 3], function (k, next) {
22-
db.get('k' + k, function (err, v) {
24+
db.get('k' + k, { asBuffer: false }, function (err, v) {
2325
t.ifError(err)
2426
t.is(v, 'v' + k)
2527
next()
2628
})
2729
}, function () {
28-
db.get('k4', function (err) {
30+
db.get('k4', { asBuffer: false }, function (err) {
2931
t.ok(err)
3032
db.close(t.end.bind(t))
3133
})
@@ -51,13 +53,13 @@ module.exports = function (test, testCommon) {
5153
// 3) when the callbacks have returned, the database should be open and those values should be in
5254
// verify that the values are there
5355
each([1, 2, 3], function (k, next) {
54-
db.get('k' + k, function (err, v) {
56+
db.get('k' + k, { asBuffer: false }, function (err, v) {
5557
t.ifError(err)
5658
t.is(v, 'v' + k)
5759
next()
5860
})
5961
}, function () {
60-
db.get('k4', function (err) {
62+
db.get('k4', { asBuffer: false }, function (err) {
6163
t.ok(err)
6264
db.close(t.end.bind(t))
6365
})
@@ -83,13 +85,13 @@ module.exports = function (test, testCommon) {
8385
// 3) when the callbacks have returned, the database should be open and those values should be in
8486
// verify that the values are there
8587
each([1, 2, 3], function (k, next) {
86-
db.get('k' + k, function (err, v) {
88+
db.get('k' + k, { asBuffer: false }, function (err, v) {
8789
t.ifError(err)
8890
t.is(v, 'v' + k)
8991
next()
9092
})
9193
}, function () {
92-
db.get('k4', function (err) {
94+
db.get('k4', { asBuffer: false }, function (err) {
9395
t.ok(err)
9496
db.close(t.end.bind(t))
9597
})
@@ -101,7 +103,7 @@ module.exports = function (test, testCommon) {
101103
t.is(db.isClosed(), false)
102104
})
103105

104-
test('deferred open(): test deferred ReadStream', function (t) {
106+
testCommon.streams && test('deferred open(): test deferred ReadStream', function (t) {
105107
var ctx = readStreamContext(t)
106108
var db = testCommon.factory()
107109

@@ -116,7 +118,7 @@ module.exports = function (test, testCommon) {
116118
t.ifError(err, 'no open error')
117119
})
118120

119-
db.createReadStream()
121+
createReadStream(db)
120122
.on('data', ctx.dataSpy)
121123
.on('end', ctx.endSpy)
122124
.on('close', function () {
@@ -130,12 +132,12 @@ module.exports = function (test, testCommon) {
130132
})
131133
})
132134

133-
test('deferred open(): maxListeners warning', function (t) {
135+
test('deferred open(): no maxListeners warning', function (t) {
134136
// 1) open database without callback, opens in next tick
135137
var db = testCommon.factory()
136-
var stderrMock = sinon.mock(console)
138+
var fail = t.fail.bind(t)
137139

138-
stderrMock.expects('error').never()
140+
process.on('warning', fail)
139141

140142
// 2) provoke an EventEmitter maxListeners warning
141143
var toPut = 11
@@ -144,13 +146,14 @@ module.exports = function (test, testCommon) {
144146
db.put('some', 'string', function (err) {
145147
t.ifError(err)
146148
if (!--toPut) {
149+
process.removeListener('warning', fail)
147150
db.close(t.end.bind(t))
148151
}
149152
})
150153
}
151154
})
152155

153-
test('deferred open(): value of queued operation is not serialized', function (t) {
156+
testCommon.encodings && test('deferred open(): value of queued operation is not serialized', function (t) {
154157
var db = testCommon.factory({ valueEncoding: 'json' })
155158

156159
// deferred-leveldown < 2.0.2 would serialize the object to a string.
@@ -165,7 +168,7 @@ module.exports = function (test, testCommon) {
165168
})
166169
})
167170

168-
test('deferred open(): key of queued operation is not serialized', function (t) {
171+
testCommon.encodings && test('deferred open(): key of queued operation is not serialized', function (t) {
169172
var db = testCommon.factory({ keyEncoding: 'json' })
170173

171174
// deferred-leveldown < 2.0.2 would serialize the key to a string.

test/get-put-del-test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module.exports = function (test, testCommon) {
3737
discardable(t, testCommon, function (db, done) {
3838
db.put('some key', 'some value stored in the database', function (err) {
3939
t.ifError(err)
40-
db.get('some key', function (err, value) {
40+
db.get('some key', { asBuffer: false }, function (err, value) {
4141
t.ifError(err)
4242
t.is(value, 'some value stored in the database')
4343
done()
@@ -50,7 +50,7 @@ module.exports = function (test, testCommon) {
5050
discardable(t, testCommon, function (db, done) {
5151
db.put('some key', 'some value stored in the database')
5252
.then(function () {
53-
return db.get('some key')
53+
return db.get('some key', { asBuffer: false })
5454
})
5555
.then(function (value) {
5656
t.is(value, 'some value stored in the database')
@@ -90,7 +90,7 @@ module.exports = function (test, testCommon) {
9090
},
9191
function (next) {
9292
each(['foo', 'bar', 'baz'], function (key, next) {
93-
db.get(key, function (err, value) {
93+
db.get(key, { asBuffer: false }, function (err, value) {
9494
// we should get foo & baz but not bar
9595
if (key === 'bar') {
9696
t.ok(err)

test/index.js

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,32 @@
1-
// Promise polyfill for IE and others.
2-
if (process.browser && typeof Promise !== 'function') {
3-
global.Promise = require('pinkie')
4-
}
5-
6-
var test = require('tape')
7-
var memdown = require('memdown')
8-
var encode = require('encoding-down')
9-
var levelup = require('../lib/levelup')
1+
'use strict'
102

11-
var testCommon = require('./common')({
12-
test: test,
13-
factory: function (options) {
14-
return levelup(encode(memdown(), options))
15-
},
16-
clear: true,
17-
deferredOpen: true,
18-
promises: true,
19-
streams: true,
20-
encodings: true
21-
})
3+
var common = require('./common')
224

23-
require('./argument-checking-test')(test, testCommon)
24-
require('./batch-test')(test, testCommon)
25-
if (testCommon.encodings) require('./binary-test')(test, testCommon)
26-
if (testCommon.clear) require('./clear-test')(test)
27-
if (testCommon.snapshots) require('./create-stream-vs-put-racecondition')(test, testCommon)
28-
if (testCommon.deferredOpen) require('./deferred-open-test')(test, testCommon)
29-
require('./get-put-del-test')(test, testCommon)
30-
require('./idempotent-test')(test, testCommon)
31-
require('./init-test')(test, testCommon)
32-
if (testCommon.encodings) require('./custom-encoding-test')(test, testCommon)
33-
if (testCommon.encodings) require('./json-encoding-test')(test, testCommon)
34-
if (testCommon.streams) require('./key-value-streams-test')(test, testCommon)
35-
require('./maybe-error-test')(test, testCommon)
36-
require('./no-encoding-test')(test, testCommon)
37-
require('./null-and-undefined-test')(test, testCommon)
38-
if (testCommon.deferredOpen) require('./open-patchsafe-test')(test, testCommon)
39-
if (testCommon.streams) require('./read-stream-test')(test, testCommon)
40-
if (testCommon.snapshots) require('./snapshot-test')(test, testCommon)
41-
require('./iterator-test')(test, testCommon)
42-
if (testCommon.seek) require('./iterator-seek-test')(test, testCommon)
5+
function suite (options) {
6+
var testCommon = common(options)
7+
var test = testCommon.test
438

44-
if (!process.browser) {
45-
require('./browserify-test')(test)
9+
require('./argument-checking-test')(test, testCommon)
10+
require('./batch-test')(test, testCommon)
11+
if (testCommon.encodings) require('./binary-test')(test, testCommon)
12+
if (testCommon.clear) require('./clear-test')(test)
13+
if (testCommon.snapshots) require('./create-stream-vs-put-racecondition')(test, testCommon)
14+
if (testCommon.deferredOpen) require('./deferred-open-test')(test, testCommon)
15+
require('./get-put-del-test')(test, testCommon)
16+
require('./idempotent-test')(test, testCommon)
17+
require('./init-test')(test, testCommon)
18+
if (testCommon.encodings) require('./custom-encoding-test')(test, testCommon)
19+
if (testCommon.encodings) require('./json-encoding-test')(test, testCommon)
20+
if (testCommon.streams) require('./key-value-streams-test')(test, testCommon)
21+
require('./maybe-error-test')(test, testCommon)
22+
require('./no-encoding-test')(test, testCommon)
23+
require('./null-and-undefined-test')(test, testCommon)
24+
if (testCommon.deferredOpen) require('./open-patchsafe-test')(test, testCommon)
25+
if (testCommon.streams) require('./read-stream-test')(test, testCommon)
26+
if (testCommon.snapshots && testCommon.streams) require('./snapshot-test')(test, testCommon)
27+
require('./iterator-test')(test, testCommon)
28+
if (testCommon.seek) require('./iterator-seek-test')(test, testCommon)
4629
}
30+
31+
suite.common = common
32+
module.exports = suite

0 commit comments

Comments
 (0)