@@ -16,6 +16,7 @@ var chainHashes = require('../data/hashes.json');
16
16
var chainData = require ( '../data/testnet-blocks.json' ) ;
17
17
var errors = index . errors ;
18
18
var memdown = require ( 'memdown' ) ;
19
+ var levelup = require ( 'levelup' ) ;
19
20
var bitcore = require ( 'bitcore' ) ;
20
21
var Transaction = bitcore . Transaction ;
21
22
@@ -566,6 +567,56 @@ describe('DB Service', function() {
566
567
} ) ;
567
568
} ) ;
568
569
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
+
569
620
describe ( '#connectBlock' , function ( ) {
570
621
it ( 'should remove block from mempool and call blockHandler with true' , function ( done ) {
571
622
var db = new DB ( baseConfig ) ;
0 commit comments