2018-08-14 21:59:41 +00:00
|
|
|
// Copyright © 2018 Vulcanize
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package price_feeds_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2018-09-06 21:42:24 +00:00
|
|
|
|
2018-08-14 21:59:41 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/fakes"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/transformers/price_feeds"
|
2018-09-10 21:57:17 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
|
2018-09-06 21:42:24 +00:00
|
|
|
price_feeds_mocks "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks/price_feeds"
|
2018-08-14 21:59:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("Price feed transformer", func() {
|
|
|
|
It("gets missing headers for price feeds", func() {
|
2018-09-10 21:57:17 +00:00
|
|
|
mockConverter := &price_feeds_mocks.MockPriceFeedConverter{}
|
2018-09-06 21:42:24 +00:00
|
|
|
mockRepository := &price_feeds_mocks.MockPriceFeedRepository{}
|
2018-08-14 21:59:41 +00:00
|
|
|
transformer := price_feeds.PriceFeedTransformer{
|
|
|
|
Config: price_feeds.PriceFeedConfig,
|
2018-09-10 21:57:17 +00:00
|
|
|
Converter: mockConverter,
|
2018-09-06 21:42:24 +00:00
|
|
|
Fetcher: &price_feeds_mocks.MockPriceFeedFetcher{},
|
2018-08-14 21:59:41 +00:00
|
|
|
Repository: mockRepository,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := transformer.Execute()
|
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
mockRepository.AssertMissingHeadersCalledwith(price_feeds.PriceFeedConfig.StartingBlockNumber, price_feeds.PriceFeedConfig.EndingBlockNumber)
|
|
|
|
})
|
|
|
|
|
|
|
|
It("returns error is missing headers call returns err", func() {
|
2018-09-10 21:57:17 +00:00
|
|
|
mockConverter := &price_feeds_mocks.MockPriceFeedConverter{}
|
2018-09-06 21:42:24 +00:00
|
|
|
mockRepository := &price_feeds_mocks.MockPriceFeedRepository{}
|
2018-08-14 21:59:41 +00:00
|
|
|
mockRepository.SetMissingHeadersErr(fakes.FakeError)
|
|
|
|
transformer := price_feeds.PriceFeedTransformer{
|
2018-09-10 21:57:17 +00:00
|
|
|
Converter: mockConverter,
|
2018-09-06 21:42:24 +00:00
|
|
|
Fetcher: &price_feeds_mocks.MockPriceFeedFetcher{},
|
2018-08-14 21:59:41 +00:00
|
|
|
Repository: mockRepository,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := transformer.Execute()
|
|
|
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err).To(MatchError(fakes.FakeError))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("fetches logs for missing headers", func() {
|
2018-09-10 21:57:17 +00:00
|
|
|
mockConverter := &price_feeds_mocks.MockPriceFeedConverter{}
|
2018-09-06 21:42:24 +00:00
|
|
|
mockRepository := &price_feeds_mocks.MockPriceFeedRepository{}
|
2018-08-14 21:59:41 +00:00
|
|
|
blockNumberOne := int64(1)
|
|
|
|
blockNumberTwo := int64(2)
|
|
|
|
mockRepository.SetMissingHeaders([]core.Header{{BlockNumber: blockNumberOne}, {BlockNumber: blockNumberTwo}})
|
2018-09-06 21:42:24 +00:00
|
|
|
mockFetcher := &price_feeds_mocks.MockPriceFeedFetcher{}
|
2018-08-14 21:59:41 +00:00
|
|
|
transformer := price_feeds.PriceFeedTransformer{
|
2018-09-10 21:57:17 +00:00
|
|
|
Converter: mockConverter,
|
2018-08-14 21:59:41 +00:00
|
|
|
Fetcher: mockFetcher,
|
|
|
|
Repository: mockRepository,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := transformer.Execute()
|
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
mockFetcher.AssertFetchLogValuesCalledWith([]int64{blockNumberOne, blockNumberTwo})
|
|
|
|
})
|
|
|
|
|
|
|
|
It("returns err if fetcher returns err", func() {
|
2018-09-10 21:57:17 +00:00
|
|
|
mockConverter := &price_feeds_mocks.MockPriceFeedConverter{}
|
2018-09-06 21:42:24 +00:00
|
|
|
mockRepository := &price_feeds_mocks.MockPriceFeedRepository{}
|
2018-08-14 21:59:41 +00:00
|
|
|
mockRepository.SetMissingHeaders([]core.Header{{BlockNumber: 1}})
|
2018-09-06 21:42:24 +00:00
|
|
|
mockFetcher := &price_feeds_mocks.MockPriceFeedFetcher{}
|
2018-08-14 21:59:41 +00:00
|
|
|
mockFetcher.SetReturnErr(fakes.FakeError)
|
|
|
|
transformer := price_feeds.PriceFeedTransformer{
|
2018-09-10 21:57:17 +00:00
|
|
|
Converter: mockConverter,
|
2018-09-13 15:05:16 +00:00
|
|
|
Fetcher: mockFetcher,
|
|
|
|
Repository: mockRepository,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := transformer.Execute()
|
|
|
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err).To(MatchError(fakes.FakeError))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("marks header checked if no logs returned", func() {
|
|
|
|
mockConverter := &price_feeds_mocks.MockPriceFeedConverter{}
|
|
|
|
mockRepository := &price_feeds_mocks.MockPriceFeedRepository{}
|
|
|
|
headerID := int64(123)
|
|
|
|
mockRepository.SetMissingHeaders([]core.Header{{Id: headerID}})
|
|
|
|
mockFetcher := &price_feeds_mocks.MockPriceFeedFetcher{}
|
|
|
|
transformer := price_feeds.PriceFeedTransformer{
|
|
|
|
Converter: mockConverter,
|
|
|
|
Fetcher: mockFetcher,
|
|
|
|
Repository: mockRepository,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := transformer.Execute()
|
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
mockRepository.AssertMarkHeaderCheckedCalledWith(headerID)
|
|
|
|
})
|
|
|
|
|
|
|
|
It("returns error if marking header checked returns err", func() {
|
|
|
|
mockConverter := &price_feeds_mocks.MockPriceFeedConverter{}
|
|
|
|
mockRepository := &price_feeds_mocks.MockPriceFeedRepository{}
|
|
|
|
mockRepository.SetMissingHeaders([]core.Header{{Id: int64(123)}})
|
|
|
|
mockRepository.SetMarkHeaderCheckedErr(fakes.FakeError)
|
|
|
|
mockFetcher := &price_feeds_mocks.MockPriceFeedFetcher{}
|
|
|
|
transformer := price_feeds.PriceFeedTransformer{
|
|
|
|
Converter: mockConverter,
|
2018-08-14 21:59:41 +00:00
|
|
|
Fetcher: mockFetcher,
|
|
|
|
Repository: mockRepository,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := transformer.Execute()
|
|
|
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err).To(MatchError(fakes.FakeError))
|
|
|
|
})
|
|
|
|
|
2018-09-10 21:57:17 +00:00
|
|
|
It("converts log to a model", func() {
|
|
|
|
mockConverter := &price_feeds_mocks.MockPriceFeedConverter{}
|
|
|
|
mockFetcher := &price_feeds_mocks.MockPriceFeedFetcher{}
|
|
|
|
mockFetcher.SetReturnLogs([]types.Log{test_data.EthPriceFeedLog})
|
2018-09-06 21:42:24 +00:00
|
|
|
mockRepository := &price_feeds_mocks.MockPriceFeedRepository{}
|
2018-08-14 21:59:41 +00:00
|
|
|
headerID := int64(11111)
|
|
|
|
mockRepository.SetMissingHeaders([]core.Header{{BlockNumber: 1, Id: headerID}})
|
2018-09-10 21:57:17 +00:00
|
|
|
transformer := price_feeds.PriceFeedTransformer{
|
|
|
|
Fetcher: mockFetcher,
|
|
|
|
Converter: mockConverter,
|
|
|
|
Repository: mockRepository,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := transformer.Execute()
|
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(mockConverter.PassedHeaderID).To(Equal(headerID))
|
2018-09-20 22:04:53 +00:00
|
|
|
Expect(mockConverter.PassedLogs).To(Equal([]types.Log{test_data.EthPriceFeedLog}))
|
2018-09-10 21:57:17 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("returns err if converter returns err", func() {
|
|
|
|
mockConverter := &price_feeds_mocks.MockPriceFeedConverter{}
|
|
|
|
mockConverter.SetConverterErr(fakes.FakeError)
|
2018-09-06 21:42:24 +00:00
|
|
|
mockFetcher := &price_feeds_mocks.MockPriceFeedFetcher{}
|
2018-09-10 21:57:17 +00:00
|
|
|
mockFetcher.SetReturnLogs([]types.Log{test_data.EthPriceFeedLog})
|
|
|
|
mockRepository := &price_feeds_mocks.MockPriceFeedRepository{}
|
|
|
|
headerID := int64(11111)
|
|
|
|
mockRepository.SetMissingHeaders([]core.Header{{BlockNumber: 1, Id: headerID}})
|
|
|
|
transformer := price_feeds.PriceFeedTransformer{
|
|
|
|
Fetcher: mockFetcher,
|
|
|
|
Converter: mockConverter,
|
|
|
|
Repository: mockRepository,
|
2018-08-14 21:59:41 +00:00
|
|
|
}
|
2018-09-10 21:57:17 +00:00
|
|
|
|
|
|
|
err := transformer.Execute()
|
|
|
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err).To(MatchError(fakes.FakeError))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("persists model converted from log", func() {
|
|
|
|
mockConverter := &price_feeds_mocks.MockPriceFeedConverter{}
|
|
|
|
mockRepository := &price_feeds_mocks.MockPriceFeedRepository{}
|
|
|
|
headerID := int64(11111)
|
|
|
|
mockRepository.SetMissingHeaders([]core.Header{{BlockNumber: 1, Id: headerID}})
|
|
|
|
mockFetcher := &price_feeds_mocks.MockPriceFeedFetcher{}
|
|
|
|
mockFetcher.SetReturnLogs([]types.Log{test_data.EthPriceFeedLog})
|
2018-08-14 21:59:41 +00:00
|
|
|
transformer := price_feeds.PriceFeedTransformer{
|
2018-09-10 21:57:17 +00:00
|
|
|
Converter: mockConverter,
|
2018-08-14 21:59:41 +00:00
|
|
|
Fetcher: mockFetcher,
|
|
|
|
Repository: mockRepository,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := transformer.Execute()
|
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2018-09-20 22:04:53 +00:00
|
|
|
mockRepository.AssertCreateCalledWith(headerID, []price_feeds.PriceFeedModel{test_data.PriceFeedModel})
|
2018-08-14 21:59:41 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("returns error if creating price feed update returns error", func() {
|
2018-09-10 21:57:17 +00:00
|
|
|
mockConverter := &price_feeds_mocks.MockPriceFeedConverter{}
|
2018-09-06 21:42:24 +00:00
|
|
|
mockRepository := &price_feeds_mocks.MockPriceFeedRepository{}
|
2018-08-14 21:59:41 +00:00
|
|
|
mockRepository.SetMissingHeaders([]core.Header{{BlockNumber: 1, Id: 2}})
|
|
|
|
mockRepository.SetCreateErr(fakes.FakeError)
|
2018-09-06 21:42:24 +00:00
|
|
|
mockFetcher := &price_feeds_mocks.MockPriceFeedFetcher{}
|
2018-08-14 21:59:41 +00:00
|
|
|
mockFetcher.SetReturnLogs([]types.Log{{}})
|
|
|
|
transformer := price_feeds.PriceFeedTransformer{
|
2018-09-10 21:57:17 +00:00
|
|
|
Converter: mockConverter,
|
2018-08-14 21:59:41 +00:00
|
|
|
Fetcher: mockFetcher,
|
|
|
|
Repository: mockRepository,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := transformer.Execute()
|
|
|
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err).To(MatchError(fakes.FakeError))
|
|
|
|
})
|
|
|
|
})
|