Skip to content
This repository has been archived by the owner on Aug 31, 2021. It is now read-only.

Commit

Permalink
DRY up storage diff repo tests
Browse files Browse the repository at this point in the history
  • Loading branch information
m0ar committed Feb 20, 2019
1 parent 5746de7 commit 56ca455
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 345 deletions.
72 changes: 13 additions & 59 deletions pkg/transformers/storage_diffs/maker/cat/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
. "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/maker/cat"
. "github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/maker/test_helpers"
"github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/shared"
"github.com/vulcanize/vulcanizedb/test_config"
)
Expand Down Expand Up @@ -40,9 +41,7 @@ var _ = Describe("Cat storage repository", func() {

err = db.Get(&result, `SELECT block_number, block_hash, nflip AS value FROM maker.cat_nflip`)
Expect(err).NotTo(HaveOccurred())
Expect(result.BlockNumber).To(Equal(fakeBlockNumber))
Expect(result.BlockHash).To(Equal(fakeBlockHash))
Expect(result.Value).To(Equal(fakeUint256))
AssertVariable(result, fakeBlockNumber, fakeBlockHash, fakeUint256)
})
})

Expand All @@ -55,9 +54,7 @@ var _ = Describe("Cat storage repository", func() {

err = db.Get(&result, `SELECT block_number, block_hash, live AS value FROM maker.cat_live`)
Expect(err).NotTo(HaveOccurred())
Expect(result.BlockNumber).To(Equal(fakeBlockNumber))
Expect(result.BlockHash).To(Equal(fakeBlockHash))
Expect(result.Value).To(Equal(fakeUint256))
AssertVariable(result, fakeBlockNumber, fakeBlockHash, fakeUint256)
})
})

Expand All @@ -70,9 +67,7 @@ var _ = Describe("Cat storage repository", func() {

err = db.Get(&result, `SELECT block_number, block_hash, vat AS value FROM maker.cat_vat`)
Expect(err).NotTo(HaveOccurred())
Expect(result.BlockNumber).To(Equal(fakeBlockNumber))
Expect(result.BlockHash).To(Equal(fakeBlockHash))
Expect(result.Value).To(Equal(fakeAddress))
AssertVariable(result, fakeBlockNumber, fakeBlockHash, fakeAddress)
})
})

Expand All @@ -85,9 +80,7 @@ var _ = Describe("Cat storage repository", func() {

err = db.Get(&result, `SELECT block_number, block_hash, pit AS value FROM maker.cat_pit`)
Expect(err).NotTo(HaveOccurred())
Expect(result.BlockNumber).To(Equal(fakeBlockNumber))
Expect(result.BlockHash).To(Equal(fakeBlockHash))
Expect(result.Value).To(Equal(fakeAddress))
AssertVariable(result, fakeBlockNumber, fakeBlockHash, fakeAddress)
})
})

Expand All @@ -100,9 +93,7 @@ var _ = Describe("Cat storage repository", func() {

err = db.Get(&result, `SELECT block_number, block_hash, vow AS value FROM maker.cat_vow`)
Expect(err).NotTo(HaveOccurred())
Expect(result.BlockNumber).To(Equal(fakeBlockNumber))
Expect(result.BlockHash).To(Equal(fakeBlockHash))
Expect(result.Value).To(Equal(fakeAddress))
AssertVariable(result, fakeBlockNumber, fakeBlockHash, fakeAddress)
})
})
})
Expand All @@ -119,10 +110,7 @@ var _ = Describe("Cat storage repository", func() {

err = db.Get(&result, `SELECT block_number, block_hash, ilk AS key, flip AS value FROM maker.cat_ilk_flip`)
Expect(err).NotTo(HaveOccurred())
Expect(result.BlockNumber).To(Equal(fakeBlockNumber))
Expect(result.BlockHash).To(Equal(fakeBlockHash))
Expect(result.Key).To(Equal(fakeIlk))
Expect(result.Value).To(Equal(fakeAddress))
AssertMapping(result, fakeBlockNumber, fakeBlockHash, fakeIlk, fakeAddress)
})

It("returns an error if metadata missing ilk", func() {
Expand All @@ -142,10 +130,7 @@ var _ = Describe("Cat storage repository", func() {

err = db.Get(&result, `SELECT block_number, block_hash, ilk AS key, chop AS value FROM maker.cat_ilk_chop`)
Expect(err).NotTo(HaveOccurred())
Expect(result.BlockNumber).To(Equal(fakeBlockNumber))
Expect(result.BlockHash).To(Equal(fakeBlockHash))
Expect(result.Key).To(Equal(fakeIlk))
Expect(result.Value).To(Equal(fakeUint256))
AssertMapping(result, fakeBlockNumber, fakeBlockHash, fakeIlk, fakeUint256)
})

It("returns an error if metadata missing ilk", func() {
Expand All @@ -165,10 +150,7 @@ var _ = Describe("Cat storage repository", func() {

err = db.Get(&result, `SELECT block_number, block_hash, ilk AS key, lump AS value FROM maker.cat_ilk_lump`)
Expect(err).NotTo(HaveOccurred())
Expect(result.BlockNumber).To(Equal(fakeBlockNumber))
Expect(result.BlockHash).To(Equal(fakeBlockHash))
Expect(result.Key).To(Equal(fakeIlk))
Expect(result.Value).To(Equal(fakeUint256))
AssertMapping(result, fakeBlockNumber, fakeBlockHash, fakeIlk, fakeUint256)
})

It("returns an error if metadata missing ilk", func() {
Expand All @@ -192,10 +174,7 @@ var _ = Describe("Cat storage repository", func() {

err = db.Get(&result, `SELECT block_number, block_hash, nflip AS key, ilk AS value FROM maker.cat_flip_ilk`)
Expect(err).NotTo(HaveOccurred())
Expect(result.BlockNumber).To(Equal(fakeBlockNumber))
Expect(result.BlockHash).To(Equal(fakeBlockHash))
Expect(result.Key).To(Equal(fakeUint256))
Expect(result.Value).To(Equal(fakeBytes32))
AssertMapping(result, fakeBlockNumber, fakeBlockHash, fakeUint256, fakeBytes32)
})

It("returns an error if metadata missing flip", func() {
Expand All @@ -215,10 +194,7 @@ var _ = Describe("Cat storage repository", func() {

err = db.Get(&result, `SELECT block_number, block_hash, nflip AS key, urn AS value FROM maker.cat_flip_urn`)
Expect(err).NotTo(HaveOccurred())
Expect(result.BlockNumber).To(Equal(fakeBlockNumber))
Expect(result.BlockHash).To(Equal(fakeBlockHash))
Expect(result.Key).To(Equal(fakeUint256))
Expect(result.Value).To(Equal(fakeBytes32))
AssertMapping(result, fakeBlockNumber, fakeBlockHash, fakeUint256, fakeBytes32)
})

It("returns an error if metadata missing flip", func() {
Expand All @@ -238,10 +214,7 @@ var _ = Describe("Cat storage repository", func() {

err = db.Get(&result, `SELECT block_number, block_hash, nflip AS key, ink AS value FROM maker.cat_flip_ink`)
Expect(err).NotTo(HaveOccurred())
Expect(result.BlockNumber).To(Equal(fakeBlockNumber))
Expect(result.BlockHash).To(Equal(fakeBlockHash))
Expect(result.Key).To(Equal(fakeUint256))
Expect(result.Value).To(Equal(fakeUint256))
AssertMapping(result, fakeBlockNumber, fakeBlockHash, fakeUint256, fakeUint256)
})

It("returns an error if metadata missing flip", func() {
Expand All @@ -261,10 +234,7 @@ var _ = Describe("Cat storage repository", func() {

err = db.Get(&result, `SELECT block_number, block_hash, nflip AS key, tab AS value FROM maker.cat_flip_tab`)
Expect(err).NotTo(HaveOccurred())
Expect(result.BlockNumber).To(Equal(fakeBlockNumber))
Expect(result.BlockHash).To(Equal(fakeBlockHash))
Expect(result.Key).To(Equal(fakeUint256))
Expect(result.Value).To(Equal(fakeUint256))
AssertMapping(result, fakeBlockNumber, fakeBlockHash, fakeUint256, fakeUint256)
})

It("returns an error if metadata missing flip", func() {
Expand All @@ -276,19 +246,3 @@ var _ = Describe("Cat storage repository", func() {
})
})
})

type BlockMetadata struct {
BlockNumber int `db:"block_number"`
BlockHash string `db:"block_hash"`
}

type MappingRes struct {
BlockMetadata
Key string
Value string
}

type VariableRes struct {
BlockMetadata
Value string
}
88 changes: 25 additions & 63 deletions pkg/transformers/storage_diffs/maker/pit/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
. "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/maker/pit"
. "github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/maker/test_helpers"
"github.com/vulcanize/vulcanizedb/pkg/transformers/storage_diffs/shared"
"github.com/vulcanize/vulcanizedb/test_config"
)
Expand Down Expand Up @@ -54,18 +55,11 @@ var _ = Describe("Pit storage repository", func() {
err = repo.Create(blockNumber, blockHash, ilkLineMetadata, expectedLine)

Expect(err).NotTo(HaveOccurred())
type IlkLine struct {
BlockMetadata
Ilk string
Line string
}
var result IlkLine
err = db.Get(&result, `SELECT block_number, block_hash, ilk, line FROM maker.pit_ilk_line`)

var result MappingRes
err = db.Get(&result, `SELECT block_number, block_hash, ilk AS key, line AS value FROM maker.pit_ilk_line`)
Expect(err).NotTo(HaveOccurred())
Expect(result.BlockNumber).To(Equal(blockNumber))
Expect(result.BlockHash).To(Equal(blockHash))
Expect(result.Ilk).To(Equal(expectedIlk))
Expect(result.Line).To(Equal(expectedLine))
AssertMapping(result, blockNumber, blockHash, expectedIlk, expectedLine)
})

It("persists an ilk spot", func() {
Expand All @@ -79,18 +73,11 @@ var _ = Describe("Pit storage repository", func() {
err = repo.Create(blockNumber, blockHash, ilkSpotMetadata, expectedSpot)

Expect(err).NotTo(HaveOccurred())
type IlkSpot struct {
BlockMetadata
Ilk string
Spot string
}
var result IlkSpot
err = db.Get(&result, `SELECT block_number, block_hash, ilk, spot FROM maker.pit_ilk_spot`)

var result MappingRes
err = db.Get(&result, `SELECT block_number, block_hash, ilk AS key, spot AS value FROM maker.pit_ilk_spot`)
Expect(err).NotTo(HaveOccurred())
Expect(result.BlockNumber).To(Equal(blockNumber))
Expect(result.BlockHash).To(Equal(blockHash))
Expect(result.Ilk).To(Equal(expectedIlk))
Expect(result.Spot).To(Equal(expectedSpot))
AssertMapping(result, blockNumber, blockHash, expectedIlk, expectedSpot)
})

It("persists a pit drip", func() {
Expand All @@ -99,16 +86,11 @@ var _ = Describe("Pit storage repository", func() {
err = repo.Create(blockNumber, blockHash, pit.DripMetadata, expectedDrip)

Expect(err).NotTo(HaveOccurred())
type PitDrip struct {
BlockMetadata
Drip string
}
var result PitDrip
err = db.Get(&result, `SELECT block_number, block_hash, drip FROM maker.pit_drip`)

var result VariableRes
err = db.Get(&result, `SELECT block_number, block_hash, drip AS value FROM maker.pit_drip`)
Expect(err).NotTo(HaveOccurred())
Expect(result.BlockNumber).To(Equal(blockNumber))
Expect(result.BlockHash).To(Equal(blockHash))
Expect(result.Drip).To(Equal(expectedDrip))
AssertVariable(result, blockNumber, blockHash, expectedDrip)
})

It("persists a pit line", func() {
Expand All @@ -117,16 +99,11 @@ var _ = Describe("Pit storage repository", func() {
err = repo.Create(blockNumber, blockHash, pit.LineMetadata, expectedLine)

Expect(err).NotTo(HaveOccurred())
type PitLine struct {
BlockMetadata
Line string
}
var result PitLine
err = db.Get(&result, `SELECT block_number, block_hash, line FROM maker.pit_line`)

var result VariableRes
err = db.Get(&result, `SELECT block_number, block_hash, line AS value FROM maker.pit_line`)
Expect(err).NotTo(HaveOccurred())
Expect(result.BlockNumber).To(Equal(blockNumber))
Expect(result.BlockHash).To(Equal(blockHash))
Expect(result.Line).To(Equal(expectedLine))
AssertVariable(result, blockNumber, blockHash, expectedLine)
})

It("persists a pit live", func() {
Expand All @@ -135,16 +112,11 @@ var _ = Describe("Pit storage repository", func() {
err = repo.Create(blockNumber, blockHash, pit.LiveMetadata, expectedLive)

Expect(err).NotTo(HaveOccurred())
type PitLive struct {
BlockMetadata
Live string
}
var result PitLive
err = db.Get(&result, `SELECT block_number, block_hash, live FROM maker.pit_live`)

var result VariableRes
err = db.Get(&result, `SELECT block_number, block_hash, live AS value FROM maker.pit_live`)
Expect(err).NotTo(HaveOccurred())
Expect(result.BlockNumber).To(Equal(blockNumber))
Expect(result.BlockHash).To(Equal(blockHash))
Expect(result.Live).To(Equal(expectedLive))
AssertVariable(result, blockNumber, blockHash, expectedLive)
})

It("persists a pit vat", func() {
Expand All @@ -153,20 +125,10 @@ var _ = Describe("Pit storage repository", func() {
err = repo.Create(blockNumber, blockHash, pit.VatMetadata, expectedVat)

Expect(err).NotTo(HaveOccurred())
type PitVat struct {
BlockMetadata
Vat string
}
var result PitVat
err = db.Get(&result, `SELECT block_number, block_hash, vat FROM maker.pit_vat`)

var result VariableRes
err = db.Get(&result, `SELECT block_number, block_hash, vat AS value FROM maker.pit_vat`)
Expect(err).NotTo(HaveOccurred())
Expect(result.BlockNumber).To(Equal(blockNumber))
Expect(result.BlockHash).To(Equal(blockHash))
Expect(result.Vat).To(Equal(expectedVat))
AssertVariable(result, blockNumber, blockHash, expectedVat)
})
})

type BlockMetadata struct {
BlockNumber int `db:"block_number"`
BlockHash string `db:"block_hash"`
}
49 changes: 49 additions & 0 deletions pkg/transformers/storage_diffs/maker/test_helpers/datatypes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package test_helpers

import (
. "github.com/onsi/gomega"
)

type BlockMetadata struct {
BlockNumber int `db:"block_number"`
BlockHash string `db:"block_hash"`
}

type VariableRes struct {
BlockMetadata
Value string
}

type MappingRes struct {
BlockMetadata
Key string
Value string
}

type DoubleMappingRes struct {
BlockMetadata
KeyOne string `db:"key_one"`
KeyTwo string `db:"key_two"`
Value string
}

func AssertVariable(res VariableRes, blockNumber int, blockHash, value string) {
Expect(res.BlockNumber).To(Equal(blockNumber))
Expect(res.BlockHash).To(Equal(blockHash))
Expect(res.Value).To(Equal(value))
}

func AssertMapping(res MappingRes, blockNumber int, blockHash, key, value string) {
Expect(res.BlockNumber).To(Equal(blockNumber))
Expect(res.BlockHash).To(Equal(blockHash))
Expect(res.Key).To(Equal(key))
Expect(res.Value).To(Equal(value))
}

func AssertDoubleMapping(res DoubleMappingRes, blockNumber int, blockHash, keyOne, keyTwo, value string) {
Expect(res.BlockNumber).To(Equal(blockNumber))
Expect(res.BlockHash).To(Equal(blockHash))
Expect(res.KeyOne).To(Equal(keyOne))
Expect(res.KeyTwo).To(Equal(keyTwo))
Expect(res.Value).To(Equal(value))
}
Loading

0 comments on commit 56ca455

Please sign in to comment.