github.com/avahowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/explorer/info_test.go (about)

     1  package explorer
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/NebulousLabs/Sia/types"
     7  )
     8  
     9  // TestImmediateBlockFacts grabs the block facts object from the block explorer
    10  // at the current height and verifies that the data has been filled out.
    11  func TestImmediateBlockFacts(t *testing.T) {
    12  	et, err := createExplorerTester("TestImmediateBlockFacts")
    13  	if err != nil {
    14  		t.Fatal(err)
    15  	}
    16  
    17  	facts, exists := et.explorer.BlockFacts(et.cs.Height())
    18  	if !exists {
    19  		t.Fatal("could not find block facts for current height")
    20  	}
    21  	var explorerHeight types.BlockHeight
    22  	err = et.explorer.db.View(dbGetInternal(internalBlockHeight, &explorerHeight))
    23  	if err != nil {
    24  		t.Fatal(err)
    25  	}
    26  	if facts.Height != explorerHeight || explorerHeight == 0 {
    27  		t.Error("wrong height reported in facts object")
    28  	}
    29  	if facts.TotalCoins.Cmp(types.CalculateNumSiacoins(et.cs.Height())) != 0 {
    30  		t.Error("wrong number of total coins:", facts.TotalCoins, et.cs.Height())
    31  	}
    32  }
    33  
    34  // TestBlock probes the Block function of the explorer.
    35  func TestBlock(t *testing.T) {
    36  	et, err := createExplorerTester("TestBlock")
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  
    41  	gb := types.GenesisBlock
    42  	gbFetch, height, exists := et.explorer.Block(gb.ID())
    43  	if !exists || height != 0 || gbFetch.ID() != gb.ID() {
    44  		t.Error("call to 'Block' inside explorer failed")
    45  	}
    46  }
    47  
    48  // TestBlockFacts checks that the correct block facts are returned for a query.
    49  func TestBlockFacts(t *testing.T) {
    50  	if testing.Short() {
    51  		t.SkipNow()
    52  	}
    53  	et, err := createExplorerTester("TestBlockFacts")
    54  	if err != nil {
    55  		t.Fatal(err)
    56  	}
    57  
    58  	gb := types.GenesisBlock
    59  	bf, exists := et.explorer.BlockFacts(0)
    60  	if !exists || bf.BlockID != gb.ID() || bf.Height != 0 {
    61  		t.Error("call to 'BlockFacts' inside explorer failed")
    62  		t.Error("Expecting true ->", exists)
    63  		t.Error("Expecting", gb.ID(), "->", bf.BlockID)
    64  		t.Error("Expecting 0 ->", bf.Height)
    65  	}
    66  
    67  	bf, exists = et.explorer.BlockFacts(1)
    68  	if !exists || bf.Height != 1 {
    69  		t.Error("call to 'BlockFacts' has failed")
    70  	}
    71  }