ipld-eth-server/pkg/history/populate_headers_test.go
Rob Mulholand 63434f6bc9 Add tests for pkg/geth/blockchain
- inject dependencies instead of initializing them in the constructor
2018-07-18 16:34:13 -05:00

53 lines
1.4 KiB
Go

package history_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/inmemory"
"github.com/vulcanize/vulcanizedb/pkg/fakes"
"github.com/vulcanize/vulcanizedb/pkg/history"
)
var _ = Describe("Populating headers", func() {
var inMemory *inmemory.InMemory
var headerRepository *inmemory.HeaderRepository
BeforeEach(func() {
inMemory = inmemory.NewInMemory()
headerRepository = inmemory.NewHeaderRepository(inMemory)
})
Describe("When 1 missing header", func() {
It("returns number of headers added", func() {
headers := []core.Header{
{BlockNumber: 1},
{BlockNumber: 2},
}
blockChain := fakes.NewMockBlockChainWithHeaders(headers)
headerRepository.CreateOrUpdateHeader(core.Header{BlockNumber: 2})
headersAdded := history.PopulateMissingHeaders(blockChain, headerRepository, 1)
Expect(headersAdded).To(Equal(1))
})
})
It("adds missing headers to the db", func() {
headers := []core.Header{
{BlockNumber: 1},
{BlockNumber: 2},
}
blockChain := fakes.NewMockBlockChainWithHeaders(headers)
dbHeader, _ := headerRepository.GetHeader(1)
Expect(dbHeader.BlockNumber).To(BeZero())
history.PopulateMissingHeaders(blockChain, headerRepository, 1)
dbHeader, _ = headerRepository.GetHeader(1)
Expect(dbHeader.BlockNumber).To(Equal(int64(1)))
})
})