|
| 1 | +package history_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/8thlight/vulcanizedb/pkg/core" |
| 5 | + "github.com/8thlight/vulcanizedb/pkg/fakes" |
| 6 | + "github.com/8thlight/vulcanizedb/pkg/history" |
| 7 | + "github.com/8thlight/vulcanizedb/pkg/repositories" |
| 8 | + . "github.com/onsi/ginkgo" |
| 9 | + . "github.com/onsi/gomega" |
| 10 | +) |
| 11 | + |
| 12 | +var _ = Describe("Populating blocks", func() { |
| 13 | + |
| 14 | + It("fills in the only missing block", func() { |
| 15 | + blocks := []core.Block{{Number: 1, Hash: "x012343"}} |
| 16 | + blockchain := fakes.NewBlockchainWithBlocks(blocks) |
| 17 | + repository := repositories.NewInMemory() |
| 18 | + repository.CreateBlock(core.Block{Number: 2}) |
| 19 | + |
| 20 | + history.PopulateBlocks(blockchain, repository, 1) |
| 21 | + |
| 22 | + block := repository.FindBlockByNumber(1) |
| 23 | + Expect(block).NotTo(BeNil()) |
| 24 | + Expect(block.Hash).To(Equal("x012343")) |
| 25 | + }) |
| 26 | + |
| 27 | + It("fills in two missing blocks", func() { |
| 28 | + blockchain := fakes.NewBlockchainWithBlocks([]core.Block{ |
| 29 | + {Number: 4}, |
| 30 | + {Number: 5}, |
| 31 | + {Number: 8}, |
| 32 | + {Number: 10}, |
| 33 | + {Number: 13}, |
| 34 | + }) |
| 35 | + repository := repositories.NewInMemory() |
| 36 | + repository.CreateBlock(core.Block{Number: 1}) |
| 37 | + repository.CreateBlock(core.Block{Number: 2}) |
| 38 | + repository.CreateBlock(core.Block{Number: 3}) |
| 39 | + repository.CreateBlock(core.Block{Number: 6}) |
| 40 | + repository.CreateBlock(core.Block{Number: 7}) |
| 41 | + repository.CreateBlock(core.Block{Number: 9}) |
| 42 | + repository.CreateBlock(core.Block{Number: 11}) |
| 43 | + repository.CreateBlock(core.Block{Number: 12}) |
| 44 | + |
| 45 | + history.PopulateBlocks(blockchain, repository, 5) |
| 46 | + |
| 47 | + Expect(repository.BlockCount()).To(Equal(11)) |
| 48 | + Expect(repository.FindBlockByNumber(4)).To(BeNil()) |
| 49 | + Expect(repository.FindBlockByNumber(5)).NotTo(BeNil()) |
| 50 | + Expect(repository.FindBlockByNumber(8)).NotTo(BeNil()) |
| 51 | + Expect(repository.FindBlockByNumber(10)).NotTo(BeNil()) |
| 52 | + Expect(repository.FindBlockByNumber(13)).To(BeNil()) |
| 53 | + }) |
| 54 | + |
| 55 | + It("returns the number of blocks created", func() { |
| 56 | + blockchain := fakes.NewBlockchainWithBlocks([]core.Block{ |
| 57 | + {Number: 4}, |
| 58 | + {Number: 5}, |
| 59 | + }) |
| 60 | + repository := repositories.NewInMemory() |
| 61 | + repository.CreateBlock(core.Block{Number: 3}) |
| 62 | + repository.CreateBlock(core.Block{Number: 6}) |
| 63 | + |
| 64 | + numberOfBlocksCreated := history.PopulateBlocks(blockchain, repository, 3) |
| 65 | + |
| 66 | + Expect(numberOfBlocksCreated).To(Equal(2)) |
| 67 | + }) |
| 68 | + |
| 69 | +}) |
0 commit comments