Skip to content

Commit

Permalink
Added unit tests for db.getMetadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Braydon Fuller committed Sep 1, 2015
1 parent 63e71d7 commit d56986f
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions test/services/db.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var chainHashes = require('../data/hashes.json');
var chainData = require('../data/testnet-blocks.json');
var errors = index.errors;
var memdown = require('memdown');
var levelup = require('levelup');
var bitcore = require('bitcore');
var Transaction = bitcore.Transaction;

Expand Down Expand Up @@ -566,6 +567,56 @@ describe('DB Service', function() {
});
});

describe('#getMetadata', function() {
it('will get metadata', function() {
var db = new DB(baseConfig);
var json = JSON.stringify({
tip: '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f',
tipHeight: 101,
cache: {
hashes: {},
chainHashes: {}
}
});
db.store = {};
db.store.get = sinon.stub().callsArgWith(2, null, json);
db.getMetadata(function(err, data) {
data.tip.should.equal('000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f');
data.tipHeight.should.equal(101);
data.cache.should.deep.equal({
hashes: {},
chainHashes: {}
});
});
});
it('will handle a notfound error from leveldb', function() {
var db = new DB(baseConfig);
db.store = {};
var error = new levelup.errors.NotFoundError();
db.store.get = sinon.stub().callsArgWith(2, error);
db.getMetadata(function(err, data) {
should.not.exist(err);
data.should.deep.equal({});
});
});
it('will handle error from leveldb', function() {
var db = new DB(baseConfig);
db.store = {};
db.store.get = sinon.stub().callsArgWith(2, new Error('test'));
db.getMetadata(function(err) {
err.message.should.equal('test');
});
});
it('give an error when parsing invalid json', function() {
var db = new DB(baseConfig);
db.store = {};
db.store.get = sinon.stub().callsArgWith(2, null, '{notvalid@json}');
db.getMetadata(function(err) {
err.message.should.equal('Could not parse metadata');
});
});
});

describe('#connectBlock', function() {
it('should remove block from mempool and call blockHandler with true', function(done) {
var db = new DB(baseConfig);
Expand Down

0 comments on commit d56986f

Please sign in to comment.