From 50cc0c245984a18560d2eae3ce7edb136c648b89 Mon Sep 17 00:00:00 2001 From: Edvard Date: Mon, 7 Jan 2019 15:20:05 +0100 Subject: [PATCH] Update tests for watcher and repository --- libraries/shared/watcher_test.go | 70 +++++-------------- .../shared/repository_utility_test.go | 22 +++--- 2 files changed, 28 insertions(+), 64 deletions(-) diff --git a/libraries/shared/watcher_test.go b/libraries/shared/watcher_test.go index c5ea6836..c93c6336 100644 --- a/libraries/shared/watcher_test.go +++ b/libraries/shared/watcher_test.go @@ -2,6 +2,7 @@ package shared_test import ( "errors" + "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" . "github.com/onsi/ginkgo" @@ -19,19 +20,17 @@ import ( var _ = Describe("Watcher", func() { It("initialises correctly", func() { db := test_config.NewTestDB(core.Node{ID: "testNode"}) - fetcher := &mocks.MockLogFetcher{} - repository := &mocks.MockWatcherRepository{} + bc := fakes.NewMockBlockChain() - watcher := shared.NewWatcher(db, fetcher, repository) + watcher := shared.NewWatcher(db, bc) Expect(watcher.DB).To(Equal(db)) - Expect(watcher.Fetcher).To(Equal(fetcher)) + Expect(watcher.Fetcher).NotTo(BeNil()) Expect(watcher.Chunker).NotTo(BeNil()) - Expect(watcher.Repository).To(Equal(repository)) }) It("adds transformers", func() { - watcher := shared.NewWatcher(nil, nil, nil) + watcher := shared.NewWatcher(nil, nil) fakeTransformer := &mocks.MockTransformer{} fakeTransformer.SetTransformerConfig(mocks.FakeTransformerConfig) watcher.AddTransformers([]shared2.TransformerInitializer{fakeTransformer.FakeTransformerInitializer}) @@ -43,7 +42,7 @@ var _ = Describe("Watcher", func() { }) It("adds transformers from multiple sources", func() { - watcher := shared.NewWatcher(nil, nil, nil) + watcher := shared.NewWatcher(nil, nil) fakeTransformer1 := &mocks.MockTransformer{} fakeTransformer1.SetTransformerConfig(mocks.FakeTransformerConfig) @@ -64,22 +63,22 @@ var _ = Describe("Watcher", func() { var ( db *postgres.DB watcher shared.Watcher + mockBlockChain fakes.MockBlockChain fakeTransformer *mocks.MockTransformer headerRepository repositories.HeaderRepository - mockFetcher mocks.MockLogFetcher repository mocks.MockWatcherRepository ) BeforeEach(func() { db = test_config.NewTestDB(test_config.NewTestNode()) test_config.CleanTestDB(db) - mockFetcher = mocks.MockLogFetcher{} + mockBlockChain = fakes.MockBlockChain{} headerRepository = repositories.NewHeaderRepository(db) _, err := headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) Expect(err).NotTo(HaveOccurred()) repository = mocks.MockWatcherRepository{} - watcher = shared.NewWatcher(db, &mockFetcher, &repository) + watcher = shared.NewWatcher(db, &mockBlockChain) }) It("executes each transformer", func() { @@ -122,10 +121,10 @@ var _ = Describe("Watcher", func() { Topics: []common.Hash{common.HexToHash("0xA")}} logB := types.Log{Address: common.HexToAddress("0xB"), Topics: []common.Hash{common.HexToHash("0xB")}} - mockFetcher.SetFetchedLogs([]types.Log{logA, logB}) + mockBlockChain.SetGetEthLogsWithCustomQueryReturnLogs([]types.Log{logA, logB}) repository.SetMissingHeaders([]core.Header{fakes.FakeHeader}) - watcher = shared.NewWatcher(db, &mockFetcher, &repository) + watcher = shared.NewWatcher(db, &mockBlockChain) watcher.AddTransformers([]shared2.TransformerInitializer{ transformerA.FakeTransformerInitializer, transformerB.FakeTransformerInitializer}) @@ -135,43 +134,6 @@ var _ = Describe("Watcher", func() { Expect(transformerB.PassedLogs).To(Equal([]types.Log{logB})) }) - Describe("uses the repository correctly:", func() { - - It("calls MissingHeaders", func() { - err := watcher.Execute() - Expect(err).To(Not(HaveOccurred())) - Expect(repository.MissingHeadersCalled).To(BeTrue()) - }) - - It("propagates MissingHeaders errors", func() { - missingHeadersError := errors.New("MissingHeadersError") - repository.MissingHeadersError = missingHeadersError - - err := watcher.Execute() - Expect(err).To(MatchError(missingHeadersError)) - }) - - It("calls CreateNotCheckedSQL", func() { - err := watcher.Execute() - Expect(err).NotTo(HaveOccurred()) - Expect(repository.CreateNotCheckedSQLCalled).To(BeTrue()) - }) - - It("calls GetCheckedColumnNames", func() { - err := watcher.Execute() - Expect(err).NotTo(HaveOccurred()) - Expect(repository.GetCheckedColumnNamesCalled).To(BeTrue()) - }) - - It("propagates GetCheckedColumnNames errors", func() { - getCheckedColumnNamesError := errors.New("GetCheckedColumnNamesError") - repository.GetCheckedColumnNamesError = getCheckedColumnNamesError - - err := watcher.Execute() - Expect(err).To(MatchError(getCheckedColumnNamesError)) - }) - }) - Describe("uses the LogFetcher correctly:", func() { BeforeEach(func() { repository.SetMissingHeaders([]core.Header{fakes.FakeHeader}) @@ -180,12 +142,18 @@ var _ = Describe("Watcher", func() { It("fetches logs", func() { err := watcher.Execute() Expect(err).NotTo(HaveOccurred()) - Expect(mockFetcher.FetchLogsCalled).To(BeTrue()) + + fakeHash := common.HexToHash(fakes.FakeHeader.Hash) + mockBlockChain.AssertGetEthLogsWithCustomQueryCalledWith(ethereum.FilterQuery{ + BlockHash: &fakeHash, + Addresses: nil, + Topics: [][]common.Hash{nil}, + }) }) It("propagates log fetcher errors", func() { fetcherError := errors.New("FetcherError") - mockFetcher.SetFetcherError(fetcherError) + mockBlockChain.SetGetEthLogsWithCustomQueryErr(fetcherError) err := watcher.Execute() Expect(err).To(MatchError(fetcherError)) diff --git a/pkg/transformers/shared/repository_utility_test.go b/pkg/transformers/shared/repository_utility_test.go index 1561a1ac..2699108e 100644 --- a/pkg/transformers/shared/repository_utility_test.go +++ b/pkg/transformers/shared/repository_utility_test.go @@ -31,8 +31,6 @@ var _ = Describe("Repository utilities", func() { Describe("MissingHeaders", func() { var ( db *postgres.DB - repository shared.Repository - repositoryTwo shared.Repository headerRepository datastore.HeaderRepository startingBlockNumber int64 endingBlockNumber int64 @@ -46,13 +44,11 @@ var _ = Describe("Repository utilities", func() { BeforeEach(func() { db = test_config.NewTestDB(test_config.NewTestNode()) test_config.CleanTestDB(db) - repository = shared.Repository{} - repositoryTwo = shared.Repository{} headerRepository = repositories.NewHeaderRepository(db) - columnNames, err := repository.GetCheckedColumnNames(db) + columnNames, err := shared.GetCheckedColumnNames(db) Expect(err).NotTo(HaveOccurred()) - notCheckedSQL = repository.CreateNotCheckedSQL(columnNames) + notCheckedSQL = shared.CreateNotCheckedSQL(columnNames) startingBlockNumber = rand.Int63() eventSpecificBlockNumber = startingBlockNumber + 1 @@ -73,7 +69,7 @@ var _ = Describe("Repository utilities", func() { _, err = db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerIDs[1]) Expect(err).NotTo(HaveOccurred()) - headers, err := repository.MissingHeaders(startingBlockNumber, endingBlockNumber, db, notCheckedSQL) + headers, err := shared.MissingHeaders(startingBlockNumber, endingBlockNumber, db, notCheckedSQL) Expect(err).NotTo(HaveOccurred()) Expect(len(headers)).To(Equal(3)) @@ -91,14 +87,14 @@ var _ = Describe("Repository utilities", func() { } Expect(err).NotTo(HaveOccurred()) - nodeOneMissingHeaders, err := repository.MissingHeaders(startingBlockNumber, endingBlockNumber, db, notCheckedSQL) + nodeOneMissingHeaders, err := shared.MissingHeaders(startingBlockNumber, endingBlockNumber, db, notCheckedSQL) Expect(err).NotTo(HaveOccurred()) Expect(len(nodeOneMissingHeaders)).To(Equal(3)) Expect(nodeOneMissingHeaders[0].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(eventSpecificBlockNumber), Equal(endingBlockNumber))) Expect(nodeOneMissingHeaders[1].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(eventSpecificBlockNumber), Equal(endingBlockNumber))) Expect(nodeOneMissingHeaders[2].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(startingBlockNumber), Equal(eventSpecificBlockNumber), Equal(endingBlockNumber))) - nodeTwoMissingHeaders, err := repositoryTwo.MissingHeaders(startingBlockNumber, endingBlockNumber+10, dbTwo, notCheckedSQL) + nodeTwoMissingHeaders, err := shared.MissingHeaders(startingBlockNumber, endingBlockNumber+10, dbTwo, notCheckedSQL) Expect(err).NotTo(HaveOccurred()) Expect(len(nodeTwoMissingHeaders)).To(Equal(3)) Expect(nodeTwoMissingHeaders[0].BlockNumber).To(Or(Equal(startingBlockNumber+10), Equal(eventSpecificBlockNumber+10), Equal(endingBlockNumber+10))) @@ -111,7 +107,7 @@ var _ = Describe("Repository utilities", func() { db := test_config.NewTestDB(test_config.NewTestNode()) test_config.CleanTestDB(db) expectedColumnNames := getExpectedColumnNames() - actualColumnNames, err := shared.Repository{}.GetCheckedColumnNames(db) + actualColumnNames, err := shared.GetCheckedColumnNames(db) Expect(err).NotTo(HaveOccurred()) Expect(actualColumnNames).To(Equal(expectedColumnNames)) }) @@ -121,20 +117,20 @@ var _ = Describe("Repository utilities", func() { It("generates a correct SQL string for one column", func() { columns := []string{"columnA"} expected := "NOT (columnA)" - actual := shared.Repository{}.CreateNotCheckedSQL(columns) + actual := shared.CreateNotCheckedSQL(columns) Expect(actual).To(Equal(expected)) }) It("generates a correct SQL string for several columns", func() { columns := []string{"columnA", "columnB"} expected := "NOT (columnA AND columnB)" - actual := shared.Repository{}.CreateNotCheckedSQL(columns) + actual := shared.CreateNotCheckedSQL(columns) Expect(actual).To(Equal(expected)) }) It("defaults to FALSE when there are no columns", func() { expected := "FALSE" - actual := shared.Repository{}.CreateNotCheckedSQL([]string{}) + actual := shared.CreateNotCheckedSQL([]string{}) Expect(actual).To(Equal(expected)) }) })