From 45a52a59f9a1fa2fbf887ab936e0fe6041d503b0 Mon Sep 17 00:00:00 2001 From: Rob Mulholand Date: Fri, 26 Oct 2018 11:49:12 -0500 Subject: [PATCH] Add repository tests for Deal --- pkg/transformers/deal/repository.go | 4 +-- pkg/transformers/deal/repository_test.go | 35 ++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/pkg/transformers/deal/repository.go b/pkg/transformers/deal/repository.go index 6234bbdd..dca7e41e 100644 --- a/pkg/transformers/deal/repository.go +++ b/pkg/transformers/deal/repository.go @@ -31,7 +31,7 @@ func (repository DealRepository) Create(headerId int64, models []interface{}) er } for _, model := range models { - deal, ok := model.(DealModel) + dealModel, ok := model.(DealModel) if !ok { tx.Rollback() return fmt.Errorf("model of type %T, not %T", model, DealModel{}) @@ -40,7 +40,7 @@ func (repository DealRepository) Create(headerId int64, models []interface{}) er _, err = tx.Exec( `INSERT into maker.deal (header_id, bid_id, contract_address, log_idx, tx_idx, raw_log) VALUES($1, $2, $3, $4, $5, $6)`, - headerId, deal.BidId, deal.ContractAddress, deal.LogIndex, deal.TransactionIndex, deal.Raw, + headerId, dealModel.BidId, dealModel.ContractAddress, dealModel.LogIndex, dealModel.TransactionIndex, dealModel.Raw, ) if err != nil { tx.Rollback() diff --git a/pkg/transformers/deal/repository_test.go b/pkg/transformers/deal/repository_test.go index 5e4f26ac..75b5b049 100644 --- a/pkg/transformers/deal/repository_test.go +++ b/pkg/transformers/deal/repository_test.go @@ -49,11 +49,12 @@ var _ = Describe("Deal Repository", func() { BeforeEach(func() { headerId, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) Expect(err).NotTo(HaveOccurred()) - err := dealRepository.Create(headerId, []interface{}{test_data.DealModel}) - Expect(err).NotTo(HaveOccurred()) }) It("persists a deal record", func() { + err = dealRepository.Create(headerId, []interface{}{test_data.DealModel}) + + Expect(err).NotTo(HaveOccurred()) var count int db.QueryRow(`SELECT count(*) FROM maker.deal`).Scan(&count) Expect(count).To(Equal(1)) @@ -68,6 +69,22 @@ var _ = Describe("Deal Repository", func() { }) It("marks header as checked for logs", func() { + err = dealRepository.Create(headerId, []interface{}{test_data.DealModel}) + + Expect(err).NotTo(HaveOccurred()) + var headerChecked bool + err = db.Get(&headerChecked, `SELECT deal_checked FROM public.checked_headers WHERE header_id = $1`, headerId) + Expect(err).NotTo(HaveOccurred()) + Expect(headerChecked).To(BeTrue()) + }) + + It("updates the header to checked if checked headers row already exists", func() { + _, err = db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerId) + Expect(err).NotTo(HaveOccurred()) + + err = dealRepository.Create(headerId, []interface{}{test_data.DealModel}) + + Expect(err).NotTo(HaveOccurred()) var headerChecked bool err = db.Get(&headerChecked, `SELECT deal_checked FROM public.checked_headers WHERE header_id = $1`, headerId) Expect(err).NotTo(HaveOccurred()) @@ -76,21 +93,35 @@ var _ = Describe("Deal Repository", func() { It("returns an error if inserting a deal record fails", func() { err = dealRepository.Create(headerId, []interface{}{test_data.DealModel}) + Expect(err).NotTo(HaveOccurred()) + + err = dealRepository.Create(headerId, []interface{}{test_data.DealModel}) + Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint")) }) It("deletes the deal record if its corresponding header record is deleted", func() { + err = dealRepository.Create(headerId, []interface{}{test_data.DealModel}) + Expect(err).NotTo(HaveOccurred()) var count int err = db.QueryRow(`SELECT count(*) from maker.deal`).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.deal`).Scan(&count) Expect(err).NotTo(HaveOccurred()) Expect(count).To(Equal(0)) }) + + It("returns an error if model is of wrong type", func() { + err = dealRepository.Create(headerId, []interface{}{test_data.WrongModel{}}) + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("model of type")) + }) }) Describe("MarkHeaderChecked", func() {