Skip to content

Commit

Permalink
Added test for batch operations
Browse files Browse the repository at this point in the history
  • Loading branch information
GarthDB committed Jul 16, 2020
1 parent 0ce22f5 commit 7ac53cd
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
language: node_js
node_js:
- "10"
after_success: npm run coverage
after_success: npm run report-coverage
12 changes: 5 additions & 7 deletions jsondown.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const AbstractLevelDOWN = require("abstract-leveldown").AbstractLevelDOWN;
const os = require("os");
const util = require("util");
const path = require("path");
const mkdirp = require("mkdirp");
const makeDir = require("make-dir");
const MemDOWN = require("memdown");
const fs = require("fs");

Expand All @@ -15,9 +15,6 @@ function serializeStore(store) {
}

function jsonToBatchOps(data) {
if (!data) {
throw new Error();
}
return Object.keys(data).map(function (key) {
var value = data[key];
if (typeof value !== "string") {
Expand Down Expand Up @@ -55,7 +52,6 @@ function reviver(k, v) {
function noop() {}

function JsonDOWN(location) {
if (!(this instanceof JsonDOWN)) return new JsonDOWN(location);
AbstractLevelDOWN.call(this);
MemDOWN.call(this);
this.location = location;
Expand All @@ -77,7 +73,7 @@ JsonDOWN.prototype._open = function (options, callback) {
? this.location.split(path.sep).slice(0, -1).join(path.sep)
: this.location;

mkdirp(subdir)
makeDir(subdir)
.then((made) => {
fs.exists(loc, function (exists) {
if (!exists && options.createIfMissing === false) {
Expand Down Expand Up @@ -120,7 +116,9 @@ JsonDOWN.prototype._open = function (options, callback) {
}
});
})
.catch((errMkdirp) => callback(errMkdirp));
.catch((err) => {
callback(err);
});
};

JsonDOWN.prototype._close = function (cb) {
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
"abstract-leveldown": "^6.3.0",
"coveralls": "^3.1.0",
"levelup": "^4.4.0",
"make-dir": "^3.1.0",
"nyc": "^15.1.0",
"tape": "^5.0.1",
"tempy": "^0.5.0"
},
"scripts": {
"coverage": "nyc report --reporter=text-lcov --reporter=html | coveralls",
"coverage": "nyc report --reporter=text-lcov --reporter=html",
"report-coverage": "npm coverage | coveralls",
"test": "nyc tape test/jsondown.test.js"
},
"homepage": "https://github.com/toolness/jsondown",
Expand Down
52 changes: 36 additions & 16 deletions test/jsondown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const test = require("tape");
const suite = require("abstract-leveldown/test");
const JsonDOWN = require("../jsondown");
const tempy = require("tempy");
const path = require("path");

const testCommon = suite.common({
test,
Expand Down Expand Up @@ -44,20 +45,39 @@ test("malformed JSON should return an error", (t) => {
});
});

test("bad location path should returns an error", (t) => {
const db = testCommon.factory();
db.location = path.join(db.location, "foo\u0000bar");
db.open((err) => {
t.match(
err.toString(),
new RegExp(
`The argument 'path' must be a string or Uint8Array without null bytes. Received`,
"g"
),
"bad path returns an error"
);
t.end();
});
});

test("batchOps with values", (t) => {
const db = testCommon.factory();
db.open(function (err) {
t.error(err);
db.put("foo", "bar", (err) => {
t.error(err);
db.close((err) => {
t.error(err);
db.open((err) => {
t.error(err);
db.close(() => {
t.end();
});
});
});
});
});
});

test("tearDown", testCommon.tearDown);
//
// test('test simple put()', function (t) {
//
// db.put('foo', 'bar', function (err) {
// t.error(err)
// db.get('foo', function (err, value) {
// t.error(err)
// var result = value.toString()
// if (isTypedArray(value)) {
// result = String.fromCharCode.apply(null, new Uint16Array(value))
// }
// t.equal(result, 'bar')
// t.end()
// })
// })
// })

0 comments on commit 7ac53cd

Please sign in to comment.