Skip to content

Commit

Permalink
Add more test cases for update method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sediug committed Jun 5, 2018
1 parent 8b63b3e commit e245052
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
12 changes: 6 additions & 6 deletions lib/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function get(point) {

for (let i = 0; i < tree.length; i++) {
if(i !== tree.length-1) {
if(typeof parent[tree[i]] === 'undefined') {
if(parent[tree[i]] === undefined) {
// If there is no child here, won't be deeper. Return undefined
return undefined;
}
Expand Down Expand Up @@ -121,7 +121,7 @@ function set(data, point, silent) {

for (let i = 0; i < tree.length; i++) {
if (i !== tree.length - 1) {
if (typeof parent[tree[i]] === 'undefined') {
if (parent[tree[i]] === undefined) {
parent[tree[i]] = {};
}

Expand Down Expand Up @@ -176,11 +176,11 @@ function update(data, point) {
let parent = goblin.db;

for (let i = 0; i < tree.length; i++) {
if (i < tree.length - 1) {
if (typeof parent[tree[i]] === 'undefined') {
return logger('DB_UPDATE_POIN_NOT_EXIST', 'Invalid point: ' + point);
}
if (parent[tree[i]] === undefined) {
return logger('DB_UPDATE_POIN_NOT_EXIST', 'Invalid point: ' + point);
}

if (i < tree.length - 1) {
parent = parent[tree[i]];
} else {
const oldValue = parent[tree[i]];
Expand Down
17 changes: 16 additions & 1 deletion test/goblin.js
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ describe('Database', function() {
});

describe('Methods:', function() {
var demoContent = {
const demoContent = {
'data-test': 'testing content',
'more-data-test': [123, true, 'hello'],
'to': {
Expand Down Expand Up @@ -788,6 +788,21 @@ describe('Database', function() {
goblinDB.push({'more':'data'});
expect(Object.keys(goblinDB.get()).length).to.be.equal(4);
});
it('Method update(): Update object', function() {
goblinDB.update({'more':'data'}, 'data-test');
goblinDB.update([1,2,3,4,5], 'more-data-test');
goblinDB.update('nothing', 'to');
expect(goblinDB.get()).to.deep.equal({
'data-test': {'more':'data'},
'more-data-test': [1,2,3,4,5],
'to': 'nothing'
});
});
it('Method update(): Throw error when invalid pointer (invalid tree node route).', function() {
expect(() => {
goblinDB.update('nothing', 'this-should-not-exist');
}).to.throw(errors.DB_UPDATE_POIN_NOT_EXIST)
});
it('Deep method set(): Create a deep object', function() {
goblinDB.set({are: 'deep'}, 'internal.references.in.goblin');
expect(goblinDB.get('internal')).to.deep.equal({'references': {'in': {'goblin': {'are': 'deep'}}}}); // internal.references.in.goblin.are.deep
Expand Down

0 comments on commit e245052

Please sign in to comment.