Error on missing keys in pit repository

- If ilk missing for ilk-related value, fail fast
This commit is contained in:
Rob Mulholand 2019-02-21 15:03:22 -06:00
parent 3d9341ea1d
commit a5121913f4
2 changed files with 77 additions and 64 deletions

View File

@ -34,9 +34,9 @@ func (repository *PitStorageRepository) SetDB(db *postgres.DB) {
func (repository PitStorageRepository) Create(blockNumber int, blockHash string, metadata shared.StorageValueMetadata, value interface{}) error { func (repository PitStorageRepository) Create(blockNumber int, blockHash string, metadata shared.StorageValueMetadata, value interface{}) error {
switch metadata.Name { switch metadata.Name {
case IlkLine: case IlkLine:
return repository.insertIlkLine(blockNumber, blockHash, metadata.Keys[shared.Ilk], value.(string)) return repository.insertIlkLine(blockNumber, blockHash, metadata, value.(string))
case IlkSpot: case IlkSpot:
return repository.insertIlkSpot(blockNumber, blockHash, metadata.Keys[shared.Ilk], value.(string)) return repository.insertIlkSpot(blockNumber, blockHash, metadata, value.(string))
case PitDrip: case PitDrip:
return repository.insertPitDrip(blockNumber, blockHash, value.(string)) return repository.insertPitDrip(blockNumber, blockHash, value.(string))
case PitLine: case PitLine:
@ -50,7 +50,11 @@ func (repository PitStorageRepository) Create(blockNumber int, blockHash string,
} }
} }
func (repository PitStorageRepository) insertIlkLine(blockNumber int, blockHash string, ilk string, line string) error { func (repository PitStorageRepository) insertIlkLine(blockNumber int, blockHash string, metadata shared.StorageValueMetadata, line string) error {
ilk, err := getIlk(metadata.Keys)
if err != nil {
return err
}
tx, err := repository.db.Begin() tx, err := repository.db.Begin()
if err != nil { if err != nil {
return err return err
@ -74,7 +78,11 @@ func (repository PitStorageRepository) insertIlkLine(blockNumber int, blockHash
return tx.Commit() return tx.Commit()
} }
func (repository PitStorageRepository) insertIlkSpot(blockNumber int, blockHash string, ilk string, spot string) error { func (repository PitStorageRepository) insertIlkSpot(blockNumber int, blockHash string, metadata shared.StorageValueMetadata, spot string) error {
ilk, err := getIlk(metadata.Keys)
if err != nil {
return err
}
tx, err := repository.db.Begin() tx, err := repository.db.Begin()
if err != nil { if err != nil {
return err return err
@ -117,3 +125,11 @@ func (repository PitStorageRepository) insertPitVat(blockNumber int, blockHash s
_, err := repository.db.Exec(`INSERT INTO maker.pit_vat (block_number, block_hash, vat) VALUES ($1, $2, $3)`, blockNumber, blockHash, vat) _, err := repository.db.Exec(`INSERT INTO maker.pit_vat (block_number, block_hash, vat) VALUES ($1, $2, $3)`, blockNumber, blockHash, vat)
return err return err
} }
func getIlk(keys map[shared.Key]string) (string, error) {
ilk, ok := keys[shared.Ilk]
if !ok {
return "", shared.ErrMetadataMalformed{MissingData: shared.Ilk}
}
return ilk, nil
}

View File

@ -30,111 +30,108 @@ import (
var _ = Describe("Pit storage repository", func() { var _ = Describe("Pit storage repository", func() {
var ( var (
blockNumber int db *postgres.DB
blockHash string err error
db *postgres.DB repo pit.PitStorageRepository
err error fakeAddress = "0x12345"
repo pit.PitStorageRepository fakeBlockHash = "expected_block_hash"
fakeBlockNumber = 123
fakeIlk = "fake_ilk"
fakeUint256 = "12345"
) )
BeforeEach(func() { BeforeEach(func() {
blockNumber = 123
blockHash = "expected_block_hash"
db = test_config.NewTestDB(test_config.NewTestNode()) db = test_config.NewTestDB(test_config.NewTestNode())
test_config.CleanTestDB(db) test_config.CleanTestDB(db)
repo = pit.PitStorageRepository{} repo = pit.PitStorageRepository{}
repo.SetDB(db) repo.SetDB(db)
}) })
It("persists an ilk line", func() { Describe("Ilk", func() {
expectedIlk := "fake_ilk" Describe("Line", func() {
expectedLine := "12345" It("writes a row", func() {
ilkLineMetadata := shared.StorageValueMetadata{ ilkLineMetadata := shared.GetStorageValueMetadata(pit.IlkLine, map[shared.Key]string{shared.Ilk: fakeIlk}, shared.Uint256)
Name: pit.IlkLine,
Keys: map[shared.Key]string{shared.Ilk: expectedIlk},
Type: shared.Uint256,
}
err = repo.Create(blockNumber, blockHash, ilkLineMetadata, expectedLine)
Expect(err).NotTo(HaveOccurred()) err = repo.Create(fakeBlockNumber, fakeBlockHash, ilkLineMetadata, fakeUint256)
var result MappingRes Expect(err).NotTo(HaveOccurred())
err = db.Get(&result, `SELECT block_number, block_hash, ilk AS key, line AS value FROM maker.pit_ilk_line`) var result MappingRes
Expect(err).NotTo(HaveOccurred()) err = db.Get(&result, `SELECT block_number, block_hash, ilk AS key, line AS value FROM maker.pit_ilk_line`)
ilkID, err := shared2.GetOrCreateIlk(expectedIlk, db) Expect(err).NotTo(HaveOccurred())
Expect(err).NotTo(HaveOccurred()) ilkID, err := shared2.GetOrCreateIlk(fakeIlk, db)
AssertMapping(result, blockNumber, blockHash, strconv.Itoa(ilkID), expectedLine) Expect(err).NotTo(HaveOccurred())
}) AssertMapping(result, fakeBlockNumber, fakeBlockHash, strconv.Itoa(ilkID), fakeUint256)
})
It("persists an ilk spot", func() { It("returns an error if metadata missing ilk", func() {
expectedIlk := "fake_ilk" malformedIlkLineMetadata := shared.GetStorageValueMetadata(pit.IlkLine, nil, shared.Uint256)
expectedSpot := "12345"
ilkSpotMetadata := shared.StorageValueMetadata{
Name: pit.IlkSpot,
Keys: map[shared.Key]string{shared.Ilk: expectedIlk},
Type: shared.Uint256,
}
err = repo.Create(blockNumber, blockHash, ilkSpotMetadata, expectedSpot)
Expect(err).NotTo(HaveOccurred()) err := repo.Create(fakeBlockNumber, fakeBlockHash, malformedIlkLineMetadata, fakeUint256)
Expect(err).To(MatchError(shared.ErrMetadataMalformed{MissingData: shared.Ilk}))
})
})
var result MappingRes Describe("Spot", func() {
err = db.Get(&result, `SELECT block_number, block_hash, ilk AS key, spot AS value FROM maker.pit_ilk_spot`) It("writes a row", func() {
Expect(err).NotTo(HaveOccurred()) ilkSpotMetadata := shared.GetStorageValueMetadata(pit.IlkSpot, map[shared.Key]string{shared.Ilk: fakeIlk}, shared.Uint256)
ilkID, err := shared2.GetOrCreateIlk(expectedIlk, db)
Expect(err).NotTo(HaveOccurred()) err = repo.Create(fakeBlockNumber, fakeBlockHash, ilkSpotMetadata, fakeUint256)
AssertMapping(result, blockNumber, blockHash, strconv.Itoa(ilkID), expectedSpot)
Expect(err).NotTo(HaveOccurred())
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())
ilkID, err := shared2.GetOrCreateIlk(fakeIlk, db)
Expect(err).NotTo(HaveOccurred())
AssertMapping(result, fakeBlockNumber, fakeBlockHash, strconv.Itoa(ilkID), fakeUint256)
})
It("returns an error if metadata missing ilk", func() {
malformedIlkSpotMetadata := shared.GetStorageValueMetadata(pit.IlkSpot, nil, shared.Uint256)
err := repo.Create(fakeBlockNumber, fakeBlockHash, malformedIlkSpotMetadata, fakeUint256)
Expect(err).To(MatchError(shared.ErrMetadataMalformed{MissingData: shared.Ilk}))
})
})
}) })
It("persists a pit drip", func() { It("persists a pit drip", func() {
expectedDrip := "0x0123456789abcdef0123" err = repo.Create(fakeBlockNumber, fakeBlockHash, pit.DripMetadata, fakeAddress)
err = repo.Create(blockNumber, blockHash, pit.DripMetadata, expectedDrip)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
var result VariableRes var result VariableRes
err = db.Get(&result, `SELECT block_number, block_hash, drip AS value FROM maker.pit_drip`) err = db.Get(&result, `SELECT block_number, block_hash, drip AS value FROM maker.pit_drip`)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
AssertVariable(result, blockNumber, blockHash, expectedDrip) AssertVariable(result, fakeBlockNumber, fakeBlockHash, fakeAddress)
}) })
It("persists a pit line", func() { It("persists a pit line", func() {
expectedLine := "12345" err = repo.Create(fakeBlockNumber, fakeBlockHash, pit.LineMetadata, fakeUint256)
err = repo.Create(blockNumber, blockHash, pit.LineMetadata, expectedLine)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
var result VariableRes var result VariableRes
err = db.Get(&result, `SELECT block_number, block_hash, line AS value FROM maker.pit_line`) err = db.Get(&result, `SELECT block_number, block_hash, line AS value FROM maker.pit_line`)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
AssertVariable(result, blockNumber, blockHash, expectedLine) AssertVariable(result, fakeBlockNumber, fakeBlockHash, fakeUint256)
}) })
It("persists a pit live", func() { It("persists a pit live", func() {
expectedLive := "12345" err = repo.Create(fakeBlockNumber, fakeBlockHash, pit.LiveMetadata, fakeUint256)
err = repo.Create(blockNumber, blockHash, pit.LiveMetadata, expectedLive)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
var result VariableRes var result VariableRes
err = db.Get(&result, `SELECT block_number, block_hash, live AS value FROM maker.pit_live`) err = db.Get(&result, `SELECT block_number, block_hash, live AS value FROM maker.pit_live`)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
AssertVariable(result, blockNumber, blockHash, expectedLive) AssertVariable(result, fakeBlockNumber, fakeBlockHash, fakeUint256)
}) })
It("persists a pit vat", func() { It("persists a pit vat", func() {
expectedVat := "0x0123456789abcdef0123" err = repo.Create(fakeBlockNumber, fakeBlockHash, pit.VatMetadata, fakeAddress)
err = repo.Create(blockNumber, blockHash, pit.VatMetadata, expectedVat)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
var result VariableRes var result VariableRes
err = db.Get(&result, `SELECT block_number, block_hash, vat AS value FROM maker.pit_vat`) err = db.Get(&result, `SELECT block_number, block_hash, vat AS value FROM maker.pit_vat`)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
AssertVariable(result, blockNumber, blockHash, expectedVat) AssertVariable(result, fakeBlockNumber, fakeBlockHash, fakeAddress)
}) })
}) })