2018-08-16 21:36:35 +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 tend_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2018-10-22 14:14:02 +00:00
|
|
|
"math/rand"
|
2018-08-16 21:36:35 +00:00
|
|
|
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
|
2018-10-11 20:25:14 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/fakes"
|
2018-08-16 21:36:35 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/transformers/tend"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
|
|
|
|
"github.com/vulcanize/vulcanizedb/test_config"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("TendRepository", func() {
|
2018-10-11 18:21:49 +00:00
|
|
|
var (
|
|
|
|
db *postgres.DB
|
|
|
|
tendRepository tend.TendRepository
|
|
|
|
headerRepository repositories.HeaderRepository
|
2018-10-12 14:17:49 +00:00
|
|
|
headerId int64
|
2018-10-11 18:21:49 +00:00
|
|
|
err error
|
|
|
|
)
|
2018-08-16 21:36:35 +00:00
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
node := test_config.NewTestNode()
|
|
|
|
db = test_config.NewTestDB(node)
|
|
|
|
test_config.CleanTestDB(db)
|
|
|
|
headerRepository = repositories.NewHeaderRepository(db)
|
2018-10-22 13:03:41 +00:00
|
|
|
tendRepository = tend.TendRepository{}
|
|
|
|
tendRepository.SetDB(db)
|
2018-08-16 21:36:35 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
Describe("Create", func() {
|
2018-10-18 11:15:11 +00:00
|
|
|
BeforeEach(func() {
|
|
|
|
headerId, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
err = tendRepository.Create(headerId, []interface{}{test_data.TendModel})
|
2018-08-16 21:36:35 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2018-10-11 18:21:49 +00:00
|
|
|
})
|
2018-08-16 21:36:35 +00:00
|
|
|
|
2018-10-11 18:21:49 +00:00
|
|
|
It("persists a tend record", func() {
|
2018-08-16 21:36:35 +00:00
|
|
|
var count int
|
|
|
|
err = db.QueryRow(`SELECT count(*) from maker.tend`).Scan(&count)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(count).To(Equal(1))
|
|
|
|
|
|
|
|
dbResult := tend.TendModel{}
|
2018-09-04 20:50:29 +00:00
|
|
|
err = db.Get(&dbResult, `SELECT bid_id, lot, bid, guy, tic, tx_idx, raw_log FROM maker.tend WHERE header_id = $1`, headerId)
|
2018-08-16 21:36:35 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2018-09-04 20:50:29 +00:00
|
|
|
Expect(dbResult.BidId).To(Equal(test_data.TendModel.BidId))
|
2018-08-16 21:36:35 +00:00
|
|
|
Expect(dbResult.Lot).To(Equal(test_data.TendModel.Lot))
|
|
|
|
Expect(dbResult.Bid).To(Equal(test_data.TendModel.Bid))
|
|
|
|
Expect(dbResult.Guy).To(Equal(test_data.TendModel.Guy))
|
|
|
|
Expect(dbResult.Tic).To(Equal(test_data.TendModel.Tic))
|
|
|
|
Expect(dbResult.TransactionIndex).To(Equal(test_data.TendModel.TransactionIndex))
|
2018-09-04 20:50:29 +00:00
|
|
|
Expect(dbResult.Raw).To(MatchJSON(test_data.RawLogNoteJson))
|
2018-08-16 21:36:35 +00:00
|
|
|
})
|
|
|
|
|
2018-09-20 21:42:31 +00:00
|
|
|
It("marks header as checked", func() {
|
|
|
|
var headerChecked bool
|
|
|
|
err = db.Get(&headerChecked, `SELECT tend_checked FROM public.checked_headers WHERE header_id = $1`, headerId)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(headerChecked).To(BeTrue())
|
|
|
|
})
|
|
|
|
|
2018-08-16 21:36:35 +00:00
|
|
|
It("returns an error if inserting a tend record fails", func() {
|
2018-10-12 14:17:49 +00:00
|
|
|
err = tendRepository.Create(headerId, []interface{}{test_data.TendModel})
|
2018-08-16 21:36:35 +00:00
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("deletes the tend record if its corresponding header record is deleted", func() {
|
|
|
|
var count int
|
|
|
|
err = db.QueryRow(`SELECT count(*) from maker.tend`).Scan(&count)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(count).To(Equal(1))
|
|
|
|
|
|
|
|
_, err = db.Exec(`DELETE FROM headers where id = $1`, headerId)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
err = db.QueryRow(`SELECT count(*) from maker.tend`).Scan(&count)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(count).To(Equal(0))
|
|
|
|
})
|
2018-10-22 12:19:53 +00:00
|
|
|
|
|
|
|
It("Returns an error if model is of wrong type", func() {
|
|
|
|
err = tendRepository.Create(headerId, []interface{}{test_data.WrongModel{}})
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err.Error()).To(ContainSubstring("model of type"))
|
|
|
|
})
|
2018-08-16 21:36:35 +00:00
|
|
|
})
|
|
|
|
|
2018-09-20 21:42:31 +00:00
|
|
|
Describe("MarkHeaderChecked", func() {
|
2018-10-11 18:21:49 +00:00
|
|
|
BeforeEach(func() {
|
2018-10-11 20:25:14 +00:00
|
|
|
headerId, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
|
2018-10-11 18:21:49 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
2018-09-20 21:42:31 +00:00
|
|
|
It("creates a row for a new headerID", func() {
|
|
|
|
err = tendRepository.MarkHeaderChecked(headerId)
|
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
var headerChecked bool
|
|
|
|
err = db.Get(&headerChecked, `SELECT tend_checked FROM public.checked_headers WHERE header_id = $1`, headerId)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(headerChecked).To(BeTrue())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("updates row when headerID already exists", func() {
|
|
|
|
_, err = db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerId)
|
|
|
|
|
|
|
|
err = tendRepository.MarkHeaderChecked(headerId)
|
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
var headerChecked bool
|
|
|
|
err = db.Get(&headerChecked, `SELECT tend_checked FROM public.checked_headers WHERE header_id = $1`, headerId)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(headerChecked).To(BeTrue())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-08-16 21:36:35 +00:00
|
|
|
Describe("MissingHeaders", func() {
|
2018-10-11 18:21:49 +00:00
|
|
|
var (
|
|
|
|
tendBlock, startingBlock, endingBlock, outOfRangeBlock int64
|
|
|
|
headerIds []int64
|
|
|
|
)
|
2018-08-16 21:36:35 +00:00
|
|
|
|
|
|
|
BeforeEach(func() {
|
2018-10-22 14:14:02 +00:00
|
|
|
tendBlock = rand.Int63()
|
2018-10-11 18:21:49 +00:00
|
|
|
startingBlock = tendBlock - 1
|
|
|
|
endingBlock = tendBlock + 1
|
|
|
|
outOfRangeBlock = tendBlock + 2
|
|
|
|
|
|
|
|
headerIds = []int64{}
|
|
|
|
for _, number := range []int64{startingBlock, tendBlock, endingBlock, outOfRangeBlock} {
|
2018-10-11 20:25:14 +00:00
|
|
|
headerId, err := headerRepository.CreateOrUpdateHeader(fakes.GetFakeHeader(number))
|
2018-08-16 21:36:35 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
headerIds = append(headerIds, headerId)
|
|
|
|
}
|
2018-10-11 18:21:49 +00:00
|
|
|
})
|
2018-08-16 21:36:35 +00:00
|
|
|
|
2018-10-11 18:21:49 +00:00
|
|
|
It("returns headers for which there isn't an associated tend record", func() {
|
2018-09-20 21:42:31 +00:00
|
|
|
err = tendRepository.MarkHeaderChecked(headerIds[1])
|
2018-08-16 21:36:35 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2018-10-11 18:21:49 +00:00
|
|
|
headers, err := tendRepository.MissingHeaders(startingBlock, endingBlock)
|
|
|
|
|
2018-08-16 21:36:35 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(len(headers)).To(Equal(2))
|
2018-10-11 18:21:49 +00:00
|
|
|
Expect(headers[0].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock)))
|
|
|
|
Expect(headers[1].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock)))
|
2018-08-16 21:36:35 +00:00
|
|
|
})
|
|
|
|
|
2018-09-20 21:42:31 +00:00
|
|
|
It("only treats headers as checked if deal have been checked", func() {
|
2018-10-11 18:21:49 +00:00
|
|
|
_, err := db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerIds[1])
|
2018-09-20 21:42:31 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2018-10-11 18:21:49 +00:00
|
|
|
headers, err := tendRepository.MissingHeaders(startingBlock, endingBlock)
|
2018-09-20 21:42:31 +00:00
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(len(headers)).To(Equal(3))
|
2018-10-11 18:21:49 +00:00
|
|
|
Expect(headers[0].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(tendBlock)))
|
|
|
|
Expect(headers[1].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(tendBlock)))
|
|
|
|
Expect(headers[2].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(tendBlock)))
|
2018-09-20 21:42:31 +00:00
|
|
|
})
|
|
|
|
|
2018-08-16 21:36:35 +00:00
|
|
|
It("only returns missing headers for the current node", func() {
|
|
|
|
node2 := core.Node{}
|
|
|
|
db2 := test_config.NewTestDB(node2)
|
|
|
|
headerRepository2 := repositories.NewHeaderRepository(db2)
|
2018-10-22 13:03:41 +00:00
|
|
|
tendRepository2 := tend.TendRepository{}
|
|
|
|
tendRepository2.SetDB(db2)
|
2018-08-16 21:36:35 +00:00
|
|
|
|
2018-10-11 18:21:49 +00:00
|
|
|
for _, number := range []int64{startingBlock, tendBlock, endingBlock} {
|
2018-10-11 20:25:14 +00:00
|
|
|
headerRepository2.CreateOrUpdateHeader(fakes.GetFakeHeader(number))
|
2018-08-16 21:36:35 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
}
|
|
|
|
|
2018-09-20 21:42:31 +00:00
|
|
|
err = tendRepository.MarkHeaderChecked(headerIds[1])
|
2018-08-16 21:36:35 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2018-10-11 18:21:49 +00:00
|
|
|
node1MissingHeaders, err := tendRepository.MissingHeaders(startingBlock, endingBlock)
|
2018-08-16 21:36:35 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(len(node1MissingHeaders)).To(Equal(2))
|
|
|
|
|
2018-10-11 18:21:49 +00:00
|
|
|
node2MissingHeaders, err := tendRepository2.MissingHeaders(startingBlock, endingBlock)
|
2018-08-16 21:36:35 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(len(node2MissingHeaders)).To(Equal(3))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|