Added unit tests for db.saveMetadata
This commit is contained in:
parent
efc770f7eb
commit
63e71d7f0c
@ -290,19 +290,21 @@ DB.prototype.getPrevHash = function(blockHash, callback) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves metadata to the database
|
* Saves metadata to the database
|
||||||
* @param {Object} metadata - The metadata
|
|
||||||
* @param {Function} callback - A function that accepts: Error
|
* @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) {
|
DB.prototype.saveMetadata = function(callback) {
|
||||||
var self = this;
|
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();
|
return callback();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -314,7 +316,8 @@ DB.prototype.saveMetadata = function(callback) {
|
|||||||
|
|
||||||
self.lastSavedMetadata = new Date();
|
self.lastSavedMetadata = new Date();
|
||||||
|
|
||||||
self.putMetadata(metadata, callback);
|
this.store.put('metadata', JSON.stringify(metadata), {}, callback);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -464,7 +464,7 @@ describe('DB Service', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("#estimateFee", function() {
|
describe('#estimateFee', function() {
|
||||||
it('should pass along the fee from bitcoind', function(done) {
|
it('should pass along the fee from bitcoind', function(done) {
|
||||||
var db = new DB(baseConfig);
|
var db = new DB(baseConfig);
|
||||||
db.node = {};
|
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() {
|
describe('#connectBlock', function() {
|
||||||
it('should remove block from mempool and call blockHandler with true', function(done) {
|
it('should remove block from mempool and call blockHandler with true', function(done) {
|
||||||
var db = new DB(baseConfig);
|
var db = new DB(baseConfig);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user