Skip to content

Commit

Permalink
New has function & safer testing.
Browse files Browse the repository at this point in the history
Tests updated with DB cleanup after every task.
  • Loading branch information
nmaggioni committed Aug 25, 2016
1 parent b289ec6 commit 0e199b8
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 28 deletions.
16 changes: 2 additions & 14 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,12 @@

### Node ###
# Logs
# Runtime data
# Directory for instrumented libs generated by jscoverage/JSCover
# Coverage directory used by tools like istanbul
# nyc test coverage
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
# node-waf configuration
# Compiled binary addons (http://nodejs.org/api/addons.html)
npm-debug.log
# Dependency directories
node_modules
# Optional npm cache directory
# Optional REPL history
### WebStorm ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# Project folder:
.idea

# IntelliJ
# mpeltonen/sbt-idea plugin
# JIRA plugin
.idea
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ The `key` parameter must be a string, `value` can be whatever kind of object can

The `key` parameter must be a string. If the key exhists its value is returned, if it doesn't the function returns `undefined`.

### Check a key
`db.has('key');`

The `key` parameter must be a string. If the key exhists `true` is returned, if it doesn't the function returns `false`.

### Delete a key

`db.delete('key');`
Expand Down
31 changes: 26 additions & 5 deletions dist/jsondb.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var validateJSON = function validateJSON(fileContent) {
};

/**
* Main constructor, manages exhisting storage file and parses options against default ones.
* Main constructor, manages existing storage file and parses options against default ones.
* @param {string} filePath The path of the file to use as storage.
* @param {object} [options] Configuration options.
* @param {boolean} [options.asyncWrite] Enables the storage to be asynchronously written to disk. Disabled by default (synchronous behaviour).
Expand Down Expand Up @@ -54,20 +54,20 @@ function JSONdb(filePath, options) {
// Storage initialization
this.storage = {};

// File exhistence check
// File existence check
var stats = void 0;
try {
stats = fs.statSync(filePath);
} catch (err) {
if (err.code === 'ENOENT') {
/* File doesn't exhist */
/* File doesn't exist */
return;
} else {
// Other error
throw err; // TODO: Check perm
}
}
/* File exhists */
/* File exists */
try {
fs.accessSync(filePath, fs.constants.R_OK | fs.constants.W_OK);
} catch (err) {
Expand Down Expand Up @@ -97,12 +97,21 @@ JSONdb.prototype.set = function (key, value) {
/**
* Extracts the value of a key from the database.
* @param {string} key The key to search for.
* @returns {object|undefined} The value of the key or `undefined` if it doesn't exhist.
* @returns {object|undefined} The value of the key or `undefined` if it doesn't exist.
*/
JSONdb.prototype.get = function (key) {
return this.storage.hasOwnProperty(key) ? this.storage[key] : undefined;
};

/**
* Checks if a key is contained in the database.
* @param {string} key The key to search for.
* @returns {boolean} `True` if it exists, `false` if not.
*/
JSONdb.prototype.has = function (key) {
return this.storage.hasOwnProperty(key);
};

/**
* Deletes a key from the database.
* @param {string} key The key to delete.
Expand All @@ -114,6 +123,18 @@ JSONdb.prototype.delete = function (key) {
return retVal;
};

/**
* Deletes all keys from the database.
* @returns {object} The JSONdb instance itself.
*/
JSONdb.prototype.deleteAll = function () {
for (var key in this.storage) {
//noinspection JSUnfilteredForInLoop
this.delete(key);
}
return this;
};

/**
* Writes the local storage object to disk.
*/
Expand Down
31 changes: 26 additions & 5 deletions jsondb.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ let validateJSON = function(fileContent) {
};

/**
* Main constructor, manages exhisting storage file and parses options against default ones.
* Main constructor, manages existing storage file and parses options against default ones.
* @param {string} filePath The path of the file to use as storage.
* @param {object} [options] Configuration options.
* @param {boolean} [options.asyncWrite] Enables the storage to be asynchronously written to disk. Disabled by default (synchronous behaviour).
Expand Down Expand Up @@ -53,20 +53,20 @@ function JSONdb(filePath, options) {
// Storage initialization
this.storage = {};

// File exhistence check
// File existence check
let stats;
try {
stats = fs.statSync(filePath);
} catch (err) {
if (err.code === 'ENOENT') {
/* File doesn't exhist */
/* File doesn't exist */
return;
} else {
// Other error
throw err; // TODO: Check perm
}
}
/* File exhists */
/* File exists */
try {
fs.accessSync(filePath, fs.constants.R_OK | fs.constants.W_OK);
} catch (err) {
Expand Down Expand Up @@ -96,12 +96,21 @@ JSONdb.prototype.set = function(key, value) {
/**
* Extracts the value of a key from the database.
* @param {string} key The key to search for.
* @returns {object|undefined} The value of the key or `undefined` if it doesn't exhist.
* @returns {object|undefined} The value of the key or `undefined` if it doesn't exist.
*/
JSONdb.prototype.get = function(key) {
return this.storage.hasOwnProperty(key) ? this.storage[key] : undefined;
};

/**
* Checks if a key is contained in the database.
* @param {string} key The key to search for.
* @returns {boolean} `True` if it exists, `false` if not.
*/
JSONdb.prototype.has = function(key) {
return this.storage.hasOwnProperty(key);
};

/**
* Deletes a key from the database.
* @param {string} key The key to delete.
Expand All @@ -113,6 +122,18 @@ JSONdb.prototype.delete = function(key) {
return retVal;
};

/**
* Deletes all keys from the database.
* @returns {object} The JSONdb instance itself.
*/
JSONdb.prototype.deleteAll = function() {
for (var key in this.storage) {
//noinspection JSUnfilteredForInLoop
this.delete(key);
}
return this;
};

/**
* Writes the local storage object to disk.
*/
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": "simple-json-db",
"version": "1.1.0",
"version": "1.2.0",
"description": "A simple, no-frills, JSON storage engine for Node.JS",
"main": "index.js",
"scripts": {
Expand Down
24 changes: 21 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var JSONdb = require('./jsondb');
var assert = require('chai').assert;
var fs = require('fs');
var filePath = genFilePath();

/**
* Generates a file path and name based on current UNIX timestamp.
Expand All @@ -14,7 +13,7 @@ function genFileName() {
/**
* Makes sure that a unique filename is generated.
*/
function genFilePath() {
(function genFilePath() {
while (true) {
try {
global.filePath = genFileName();
Expand All @@ -23,7 +22,7 @@ function genFilePath() {
break;
}
}
}
})();

/**
* Returns a new instance of JSONdb.
Expand All @@ -34,6 +33,10 @@ function createInstance() {
}

describe("Consistency", function() {
beforeEach('Database cleanup', function() {
createInstance().deleteAll();
});

it("Create a new JSONdb instance and test `instanceOf`", function() {
var db = createInstance();
assert.instanceOf(db, JSONdb);
Expand All @@ -46,6 +49,10 @@ describe("Consistency", function() {
});

describe("Mechanics", function() {
beforeEach('Database cleanup', function() {
createInstance().deleteAll();
});

it("Check that values can change", function() {
var db = createInstance();
var change = { testVal: db.get('foo') };
Expand Down Expand Up @@ -75,6 +82,17 @@ describe("Mechanics", function() {
assert.isUndefined(secondVal, 'Key was not deleted');
});

it("Check that keys existence can be verified (existent key)", function() {
var db = createInstance();
db.set('foo', Date.now());
assert.isTrue(db.has('foo'), 'Key existence is erroneous');
});

it("Check that keys existence can be verified (non-existent key)", function() {
var db = createInstance();
assert.isFalse(db.has('foo'), 'Key existence is erroneous');
});

it("Verify sync to disk", function() {
var db = createInstance();
db.set('foo', Date.now());
Expand Down

0 comments on commit 0e199b8

Please sign in to comment.