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

Create test suite #677

Merged
merged 3 commits into from
Sep 30, 2019
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ libpeerconnection.log
yarn.lock
package-lock.json
.nyc_output/
airtap.log
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ test
.travis.yml
.dntrc
.nyc_output/
airtap.log
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"license": "MIT",
"main": "lib/levelup.js",
"scripts": {
"test": "standard && hallmark && (nyc -s node test | faucet) && nyc report",
"test": "standard && hallmark && (nyc -s node test/self.js | faucet) && nyc report",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"test-browsers": "airtap --loopback airtap.local test/index.js",
"test-browser-local": "airtap --local test/index.js",
"test-browsers": "airtap --loopback airtap.local test/self.js > airtap.log",
"test-browser-local": "airtap --local test/self.js",
"hallmark": "hallmark --fix",
"dependency-check": "dependency-check . test/*.js",
"prepublishOnly": "npm run dependency-check"
Expand Down
32 changes: 17 additions & 15 deletions test/batch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = function (test, testCommon) {
t.ifError(err)

each(['foo', 'bar', 'baz'], function (key, next) {
db.get(key, function (err, value) {
db.get(key, { asBuffer: false }, function (err, value) {
t.ifError(err)
t.is(value, 'a' + key + 'value')
next()
Expand All @@ -25,7 +25,7 @@ module.exports = function (test, testCommon) {
})
})

test('array-form batch(): promise interface', function (t) {
testCommon.promises && test('array-form batch(): promise interface', function (t) {
discardable(t, testCommon, function (db, done) {
db.batch([
{ type: 'put', key: 'foo', value: 'afoovalue' },
Expand All @@ -34,7 +34,7 @@ module.exports = function (test, testCommon) {
])
.then(function () {
each(['foo', 'bar', 'baz'], function (key, next) {
db.get(key, function (err, value) {
db.get(key, { asBuffer: false }, function (err, value) {
t.ifError(err)
t.is(value, 'a' + key + 'value')
next()
Expand Down Expand Up @@ -67,7 +67,7 @@ module.exports = function (test, testCommon) {
function (next) {
// these should exist
each(['2', '3', 'bar', 'baz'], function (key, next) {
db.get(key, function (err, value) {
db.get(key, { asBuffer: false }, function (err, value) {
t.ifError(err)
t.ok(value != null)
next()
Expand All @@ -77,7 +77,7 @@ module.exports = function (test, testCommon) {
function (next) {
// these shouldn't exist
each(['1', 'foo'], function (key, next) {
db.get(key, function (err, value) {
db.get(key, { asBuffer: false }, function (err, value) {
t.ok(err)
t.ok(err instanceof errors.NotFoundError)
t.is(value, undefined)
Expand Down Expand Up @@ -107,7 +107,7 @@ module.exports = function (test, testCommon) {
t.ifError(err)

each(['one', 'three', '1', '2', '3'], function (key, next) {
db.get(key, function (err) {
db.get(key, { asBuffer: false }, function (err) {
if (['one', 'three', '1', '3'].indexOf(key) > -1) {
t.ok(err)
} else {
Expand All @@ -125,9 +125,11 @@ module.exports = function (test, testCommon) {
test('chained batch(): options', function (t) {
discardable(t, testCommon, function (db, done) {
var batch = db.batch()
var underlying = batch
while (underlying.batch) underlying = underlying.batch

var write = batch.batch.write.bind(batch.batch)
batch.batch.write = function (options, cb) {
var write = underlying.write.bind(underlying)
underlying.write = function (options, cb) {
t.same(options, { foo: 'bar' })
write(options, cb)
}
Expand All @@ -140,7 +142,7 @@ module.exports = function (test, testCommon) {
})
})

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

Expand All @@ -157,7 +159,7 @@ module.exports = function (test, testCommon) {
})
})

test('chained batch(): promise interface', function (t) {
testCommon.promises && test('chained batch(): promise interface', function (t) {
discardable(t, testCommon, function (db, done) {
db.put('1', 'one', function (err) {
t.ifError(err)
Expand All @@ -174,7 +176,7 @@ module.exports = function (test, testCommon) {
.write()
.then(function () {
each(['one', 'three', '1', '2', '3'], function (key, next) {
db.get(key, function (err) {
db.get(key, { asBuffer: false }, function (err) {
if (['one', 'three', '1', '3'].indexOf(key) > -1) {
t.ok(err)
} else {
Expand Down Expand Up @@ -228,7 +230,7 @@ module.exports = function (test, testCommon) {
function (next) {
// these should exist
each(['2', '3', 'bar', 'baz'], function (key, next) {
db.get(key, function (err, value) {
db.get(key, { asBuffer: false }, function (err, value) {
t.ifError(err)
t.ok(value != null)
next()
Expand All @@ -238,7 +240,7 @@ module.exports = function (test, testCommon) {
function (next) {
// these shouldn't exist
each(['1', 'foo'], function (key, next) {
db.get(key, function (err, value) {
db.get(key, { asBuffer: false }, function (err, value) {
t.ok(err)
t.ok(err instanceof errors.NotFoundError)
t.is(value, undefined)
Expand All @@ -264,7 +266,7 @@ module.exports = function (test, testCommon) {
function (next) {
// these should exist
each(['2', '3'], function (key, next) {
db.get(key, function (err, value) {
db.get(key, { asBuffer: false }, function (err, value) {
t.ifError(err)
t.ok(value != null)
next()
Expand All @@ -273,7 +275,7 @@ module.exports = function (test, testCommon) {
},
function (next) {
// this shouldn't exist
db.get('1', function (err, value) {
db.get('1', { asBuffer: false }, function (err, value) {
t.ok(err)
t.ok(err instanceof errors.NotFoundError)
t.is(value, undefined)
Expand Down
31 changes: 17 additions & 14 deletions test/deferred-open-test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
var each = require('async-each')
var parallel = require('run-parallel')
var concat = require('concat-stream')
var sinon = require('sinon')
var readStreamContext = require('./util/rs-context')
var rsFactory = require('./util/rs-factory')

module.exports = function (test, testCommon) {
var createReadStream = rsFactory(testCommon)

test('deferred open(): put() and get() on new database', function (t) {
// 1) open database without callback, opens in next tick
var db = testCommon.factory()
Expand All @@ -19,13 +21,13 @@ module.exports = function (test, testCommon) {
// 3) when the callbacks have returned, the database should be open and those values should be in
// verify that the values are there
each([1, 2, 3], function (k, next) {
db.get('k' + k, function (err, v) {
db.get('k' + k, { asBuffer: false }, function (err, v) {
t.ifError(err)
t.is(v, 'v' + k)
next()
})
}, function () {
db.get('k4', function (err) {
db.get('k4', { asBuffer: false }, function (err) {
t.ok(err)
db.close(t.end.bind(t))
})
Expand All @@ -51,13 +53,13 @@ module.exports = function (test, testCommon) {
// 3) when the callbacks have returned, the database should be open and those values should be in
// verify that the values are there
each([1, 2, 3], function (k, next) {
db.get('k' + k, function (err, v) {
db.get('k' + k, { asBuffer: false }, function (err, v) {
t.ifError(err)
t.is(v, 'v' + k)
next()
})
}, function () {
db.get('k4', function (err) {
db.get('k4', { asBuffer: false }, function (err) {
t.ok(err)
db.close(t.end.bind(t))
})
Expand All @@ -83,13 +85,13 @@ module.exports = function (test, testCommon) {
// 3) when the callbacks have returned, the database should be open and those values should be in
// verify that the values are there
each([1, 2, 3], function (k, next) {
db.get('k' + k, function (err, v) {
db.get('k' + k, { asBuffer: false }, function (err, v) {
t.ifError(err)
t.is(v, 'v' + k)
next()
})
}, function () {
db.get('k4', function (err) {
db.get('k4', { asBuffer: false }, function (err) {
t.ok(err)
db.close(t.end.bind(t))
})
Expand All @@ -101,7 +103,7 @@ module.exports = function (test, testCommon) {
t.is(db.isClosed(), false)
})

test('deferred open(): test deferred ReadStream', function (t) {
testCommon.streams && test('deferred open(): test deferred ReadStream', function (t) {
var ctx = readStreamContext(t)
var db = testCommon.factory()

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

db.createReadStream()
createReadStream(db)
.on('data', ctx.dataSpy)
.on('end', ctx.endSpy)
.on('close', function () {
Expand All @@ -130,12 +132,12 @@ module.exports = function (test, testCommon) {
})
})

test('deferred open(): maxListeners warning', function (t) {
test('deferred open(): no maxListeners warning', function (t) {
// 1) open database without callback, opens in next tick
var db = testCommon.factory()
var stderrMock = sinon.mock(console)
var fail = t.fail.bind(t)

stderrMock.expects('error').never()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides this not actually working anymore (because node emits a warning event rather than error), it caused problems when calling the test multiple times: sinon complained console was already wrapped.

process.on('warning', fail)

// 2) provoke an EventEmitter maxListeners warning
var toPut = 11
Expand All @@ -144,13 +146,14 @@ module.exports = function (test, testCommon) {
db.put('some', 'string', function (err) {
t.ifError(err)
if (!--toPut) {
process.removeListener('warning', fail)
db.close(t.end.bind(t))
}
})
}
})

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

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

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

// deferred-leveldown < 2.0.2 would serialize the key to a string.
Expand Down
6 changes: 3 additions & 3 deletions test/get-put-del-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports = function (test, testCommon) {
discardable(t, testCommon, function (db, done) {
db.put('some key', 'some value stored in the database', function (err) {
t.ifError(err)
db.get('some key', function (err, value) {
db.get('some key', { asBuffer: false }, function (err, value) {
t.ifError(err)
t.is(value, 'some value stored in the database')
done()
Expand All @@ -50,7 +50,7 @@ module.exports = function (test, testCommon) {
discardable(t, testCommon, function (db, done) {
db.put('some key', 'some value stored in the database')
.then(function () {
return db.get('some key')
return db.get('some key', { asBuffer: false })
})
.then(function (value) {
t.is(value, 'some value stored in the database')
Expand Down Expand Up @@ -90,7 +90,7 @@ module.exports = function (test, testCommon) {
},
function (next) {
each(['foo', 'bar', 'baz'], function (key, next) {
db.get(key, function (err, value) {
db.get(key, { asBuffer: false }, function (err, value) {
// we should get foo & baz but not bar
if (key === 'bar') {
t.ok(err)
Expand Down
70 changes: 28 additions & 42 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,32 @@
// Promise polyfill for IE and others.
if (process.browser && typeof Promise !== 'function') {
global.Promise = require('pinkie')
}

var test = require('tape')
var memdown = require('memdown')
var encode = require('encoding-down')
var levelup = require('../lib/levelup')
'use strict'

var testCommon = require('./common')({
test: test,
factory: function (options) {
return levelup(encode(memdown(), options))
},
clear: true,
deferredOpen: true,
promises: true,
streams: true,
encodings: true
})
var common = require('./common')

require('./argument-checking-test')(test, testCommon)
require('./batch-test')(test, testCommon)
if (testCommon.encodings) require('./binary-test')(test, testCommon)
if (testCommon.clear) require('./clear-test')(test)
if (testCommon.snapshots) require('./create-stream-vs-put-racecondition')(test, testCommon)
if (testCommon.deferredOpen) require('./deferred-open-test')(test, testCommon)
require('./get-put-del-test')(test, testCommon)
require('./idempotent-test')(test, testCommon)
require('./init-test')(test, testCommon)
if (testCommon.encodings) require('./custom-encoding-test')(test, testCommon)
if (testCommon.encodings) require('./json-encoding-test')(test, testCommon)
if (testCommon.streams) require('./key-value-streams-test')(test, testCommon)
require('./maybe-error-test')(test, testCommon)
require('./no-encoding-test')(test, testCommon)
require('./null-and-undefined-test')(test, testCommon)
if (testCommon.deferredOpen) require('./open-patchsafe-test')(test, testCommon)
if (testCommon.streams) require('./read-stream-test')(test, testCommon)
if (testCommon.snapshots) require('./snapshot-test')(test, testCommon)
require('./iterator-test')(test, testCommon)
if (testCommon.seek) require('./iterator-seek-test')(test, testCommon)
function suite (options) {
var testCommon = common(options)
var test = testCommon.test

if (!process.browser) {
require('./browserify-test')(test)
require('./argument-checking-test')(test, testCommon)
require('./batch-test')(test, testCommon)
if (testCommon.encodings) require('./binary-test')(test, testCommon)
if (testCommon.clear) require('./clear-test')(test)
if (testCommon.snapshots) require('./create-stream-vs-put-racecondition')(test, testCommon)
if (testCommon.deferredOpen) require('./deferred-open-test')(test, testCommon)
require('./get-put-del-test')(test, testCommon)
require('./idempotent-test')(test, testCommon)
require('./init-test')(test, testCommon)
if (testCommon.encodings) require('./custom-encoding-test')(test, testCommon)
if (testCommon.encodings) require('./json-encoding-test')(test, testCommon)
if (testCommon.streams) require('./key-value-streams-test')(test, testCommon)
require('./maybe-error-test')(test, testCommon)
require('./no-encoding-test')(test, testCommon)
require('./null-and-undefined-test')(test, testCommon)
if (testCommon.deferredOpen) require('./open-patchsafe-test')(test, testCommon)
if (testCommon.streams) require('./read-stream-test')(test, testCommon)
if (testCommon.snapshots && testCommon.streams) require('./snapshot-test')(test, testCommon)
require('./iterator-test')(test, testCommon)
if (testCommon.seek) require('./iterator-seek-test')(test, testCommon)
}

suite.common = common
module.exports = suite
Loading