From 63e71d7f0cb52c5cea0a9a7b8e3ab5ce45c7e993 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Tue, 1 Sep 2015 14:29:04 -0400 Subject: [PATCH] Added unit tests for db.saveMetadata --- lib/services/db.js | 19 +++++---- test/services/db.unit.js | 86 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 96 insertions(+), 9 deletions(-) diff --git a/lib/services/db.js b/lib/services/db.js index 316bafe4..3527416d 100644 --- a/lib/services/db.js +++ b/lib/services/db.js @@ -290,19 +290,21 @@ DB.prototype.getPrevHash = function(blockHash, callback) { /** * Saves metadata to the database - * @param {Object} metadata - The metadata * @param {Function} callback - A function that accepts: Error */ -DB.prototype.putMetadata = function(metadata, callback) { - this.store.put('metadata', JSON.stringify(metadata), {}, callback); -}; - DB.prototype.saveMetadata = function(callback) { var self = this; - callback = callback || function() {}; + function defaultCallback(err) { + if (err) { + self.emit('error', err); + } + } - if(self.lastSavedMetadata && Date.now() < self.lastSavedMetadata.getTime() + self.lastSavedMetadataThreshold) { + callback = callback || defaultCallback; + + var threshold = self.lastSavedMetadataThreshold; + if (self.lastSavedMetadata && Date.now() < self.lastSavedMetadata.getTime() + threshold) { return callback(); } @@ -314,7 +316,8 @@ DB.prototype.saveMetadata = function(callback) { self.lastSavedMetadata = new Date(); - self.putMetadata(metadata, callback); + this.store.put('metadata', JSON.stringify(metadata), {}, callback); + }; /** diff --git a/test/services/db.unit.js b/test/services/db.unit.js index 8988a85f..a51f5f90 100644 --- a/test/services/db.unit.js +++ b/test/services/db.unit.js @@ -464,7 +464,7 @@ describe('DB Service', function() { }); }); - describe("#estimateFee", function() { + describe('#estimateFee', function() { it('should pass along the fee from bitcoind', function(done) { var db = new DB(baseConfig); db.node = {}; @@ -482,6 +482,90 @@ describe('DB Service', function() { }); }); + describe('#saveMetadata', function() { + it('will emit an error with default callback', function(done) { + var db = new DB(baseConfig); + db.cache = { + hashes: {}, + chainHashes: {} + }; + db.tip = { + hash: '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f', + __height: 0 + }; + db.store = { + put: sinon.stub().callsArgWith(3, new Error('test')) + }; + db.on('error', function(err) { + err.message.should.equal('test'); + done(); + }); + db.saveMetadata(); + }); + it('will give an error with callback', function(done) { + var db = new DB(baseConfig); + db.cache = { + hashes: {}, + chainHashes: {} + }; + db.tip = { + hash: '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f', + __height: 0 + }; + db.store = { + put: sinon.stub().callsArgWith(3, new Error('test')) + }; + db.saveMetadata(function(err) { + err.message.should.equal('test'); + done(); + }); + }); + it('will call store with the correct arguments', function(done) { + var db = new DB(baseConfig); + db.cache = { + hashes: {}, + chainHashes: {} + }; + db.tip = { + hash: '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f', + __height: 0 + }; + db.store = { + put: function(key, value, options, callback) { + key.should.equal('metadata'); + JSON.parse(value).should.deep.equal({ + tip: '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f', + tipHeight: 0, + cache: { + hashes: {}, + chainHashes: {} + } + }); + options.should.deep.equal({}); + callback.should.be.a('function'); + done(); + } + }; + db.saveMetadata(); + }); + it('will not call store with threshold', function(done) { + var db = new DB(baseConfig); + db.lastSavedMetadata = new Date(); + db.lastSavedMetadataThreshold = 30000; + var put = sinon.stub(); + db.store = { + put: put + }; + db.saveMetadata(function(err) { + if (err) { + throw err; + } + put.callCount.should.equal(0); + done(); + }); + }); + }); + describe('#connectBlock', function() { it('should remove block from mempool and call blockHandler with true', function(done) { var db = new DB(baseConfig);