Skip to content

Commit d56986f

Browse files
author
Braydon Fuller
committed
Added unit tests for db.getMetadata
1 parent 63e71d7 commit d56986f

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

test/services/db.unit.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var chainHashes = require('../data/hashes.json');
1616
var chainData = require('../data/testnet-blocks.json');
1717
var errors = index.errors;
1818
var memdown = require('memdown');
19+
var levelup = require('levelup');
1920
var bitcore = require('bitcore');
2021
var Transaction = bitcore.Transaction;
2122

@@ -566,6 +567,56 @@ describe('DB Service', function() {
566567
});
567568
});
568569

570+
describe('#getMetadata', function() {
571+
it('will get metadata', function() {
572+
var db = new DB(baseConfig);
573+
var json = JSON.stringify({
574+
tip: '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f',
575+
tipHeight: 101,
576+
cache: {
577+
hashes: {},
578+
chainHashes: {}
579+
}
580+
});
581+
db.store = {};
582+
db.store.get = sinon.stub().callsArgWith(2, null, json);
583+
db.getMetadata(function(err, data) {
584+
data.tip.should.equal('000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f');
585+
data.tipHeight.should.equal(101);
586+
data.cache.should.deep.equal({
587+
hashes: {},
588+
chainHashes: {}
589+
});
590+
});
591+
});
592+
it('will handle a notfound error from leveldb', function() {
593+
var db = new DB(baseConfig);
594+
db.store = {};
595+
var error = new levelup.errors.NotFoundError();
596+
db.store.get = sinon.stub().callsArgWith(2, error);
597+
db.getMetadata(function(err, data) {
598+
should.not.exist(err);
599+
data.should.deep.equal({});
600+
});
601+
});
602+
it('will handle error from leveldb', function() {
603+
var db = new DB(baseConfig);
604+
db.store = {};
605+
db.store.get = sinon.stub().callsArgWith(2, new Error('test'));
606+
db.getMetadata(function(err) {
607+
err.message.should.equal('test');
608+
});
609+
});
610+
it('give an error when parsing invalid json', function() {
611+
var db = new DB(baseConfig);
612+
db.store = {};
613+
db.store.get = sinon.stub().callsArgWith(2, null, '{notvalid@json}');
614+
db.getMetadata(function(err) {
615+
err.message.should.equal('Could not parse metadata');
616+
});
617+
});
618+
});
619+
569620
describe('#connectBlock', function() {
570621
it('should remove block from mempool and call blockHandler with true', function(done) {
571622
var db = new DB(baseConfig);

0 commit comments

Comments
 (0)