Skip to content

Commit

Permalink
Add tests for updating future rows
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabe Laughlin committed Oct 31, 2019
1 parent 123f665 commit 4097110
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,7 @@ AS
$$
BEGIN
PERFORM maker.insert_new_flip(NEW);
PERFORM maker.update_later_flips();
PERFORM maker.update_later_flips(NEW);
RETURN NULL;
END
$$
Expand Down Expand Up @@ -1055,7 +1055,7 @@ AS
$$
BEGIN
PERFORM maker.insert_new_duty(NEW);
PERFORM maker.update_later_rates(NEW);
PERFORM maker.update_later_duties(NEW);
RETURN NULL;
END
$$
Expand Down
12 changes: 6 additions & 6 deletions db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2744,7 +2744,7 @@ CREATE FUNCTION maker.insert_ilk_duties() RETURNS trigger
AS $$
BEGIN
PERFORM maker.insert_new_duty(NEW);
PERFORM maker.update_later_rates(NEW);
PERFORM maker.update_later_duties(NEW);
RETURN NULL;
END
$$;
Expand All @@ -2759,7 +2759,7 @@ CREATE FUNCTION maker.insert_ilk_flips() RETURNS trigger
AS $$
BEGIN
PERFORM maker.insert_new_flip(NEW);
PERFORM maker.update_later_flips();
PERFORM maker.update_later_flips(NEW);
RETURN NULL;
END
$$;
Expand Down Expand Up @@ -4103,7 +4103,7 @@ CREATE FUNCTION maker.update_later_arts(new_diff maker.vat_ilk_art) RETURNS make
LANGUAGE plpgsql
AS $$
DECLARE
diff_ilk_identifier TEXT := (
diff_ilk_identifier TEXT := (
SELECT identifier
FROM maker.ilks
WHERE ilks.id = new_diff.ilk_id);
Expand Down Expand Up @@ -4306,7 +4306,7 @@ CREATE FUNCTION maker.update_later_mats(new_diff maker.spot_ilk_mat) RETURNS mak
LANGUAGE plpgsql
AS $$
DECLARE
diff_ilk_identifier TEXT := (
diff_ilk_identifier TEXT := (
SELECT identifier
FROM maker.ilks
WHERE ilks.id = new_diff.ilk_id);
Expand Down Expand Up @@ -4335,7 +4335,7 @@ CREATE FUNCTION maker.update_later_pips(new_diff maker.spot_ilk_pip) RETURNS mak
LANGUAGE plpgsql
AS $$
DECLARE
diff_ilk_identifier TEXT := (
diff_ilk_identifier TEXT := (
SELECT identifier
FROM maker.ilks
WHERE ilks.id = new_diff.ilk_id);
Expand Down Expand Up @@ -4393,7 +4393,7 @@ CREATE FUNCTION maker.update_later_rhos(new_diff maker.jug_ilk_rho) RETURNS make
LANGUAGE plpgsql
AS $$
DECLARE
diff_ilk_identifier TEXT := (
diff_ilk_identifier TEXT := (
SELECT identifier
FROM maker.ilks
WHERE ilks.id = new_diff.ilk_id);
Expand Down
3 changes: 3 additions & 0 deletions transformers/storage/cat/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ var _ = Describe("Cat storage repository", func() {
shared_behaviors.SharedIlkTriggerTests(shared_behaviors.IlkTriggerTestInput{
Repository: &repo,
Metadata: utils.GetStorageValueMetadata(cat.IlkFlip, map[utils.Key]string{constants.Ilk: test_helpers.FakeIlk.Hex}, utils.Address),
PropertyName: "Flip",
PropertyValue: fakeAddress,
})
})
Expand Down Expand Up @@ -201,6 +202,7 @@ var _ = Describe("Cat storage repository", func() {
shared_behaviors.SharedIlkTriggerTests(shared_behaviors.IlkTriggerTestInput{
Repository: &repo,
Metadata: utils.GetStorageValueMetadata(cat.IlkChop, map[utils.Key]string{constants.Ilk: test_helpers.FakeIlk.Hex}, utils.Uint256),
PropertyName: "Chop",
PropertyValue: strconv.Itoa(rand.Int()),
})
})
Expand Down Expand Up @@ -244,6 +246,7 @@ var _ = Describe("Cat storage repository", func() {
shared_behaviors.SharedIlkTriggerTests(shared_behaviors.IlkTriggerTestInput{
Repository: &repo,
Metadata: utils.GetStorageValueMetadata(cat.IlkLump, map[utils.Key]string{constants.Ilk: test_helpers.FakeIlk.Hex}, utils.Uint256),
PropertyName: "Lump",
PropertyValue: strconv.Itoa(rand.Int()),
})
})
Expand Down
2 changes: 2 additions & 0 deletions transformers/storage/jug/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ var _ = Describe("Jug storage repository", func() {
shared_behaviors.SharedIlkTriggerTests(shared_behaviors.IlkTriggerTestInput{
Repository: &repo,
Metadata: utils.GetStorageValueMetadata(jug.IlkRho, map[utils.Key]string{constants.Ilk: test_helpers.FakeIlk.Hex}, utils.Uint256),
PropertyName: "Rho",
PropertyValue: strconv.Itoa(rand.Int()),
})
})
Expand Down Expand Up @@ -133,6 +134,7 @@ var _ = Describe("Jug storage repository", func() {
shared_behaviors.SharedIlkTriggerTests(shared_behaviors.IlkTriggerTestInput{
Repository: &repo,
Metadata: utils.GetStorageValueMetadata(jug.IlkDuty, map[utils.Key]string{constants.Ilk: test_helpers.FakeIlk.Hex}, utils.Uint256),
PropertyName: "Duty",
PropertyValue: strconv.Itoa(rand.Int()),
})
})
Expand Down
2 changes: 2 additions & 0 deletions transformers/storage/spot/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ var _ = Describe("Spot storage repository", func() {
shared_behaviors.SharedIlkTriggerTests(shared_behaviors.IlkTriggerTestInput{
Repository: &repo,
Metadata: utils.GetStorageValueMetadata(spot.IlkPip, map[utils.Key]string{constants.Ilk: test_helpers.FakeIlk.Hex}, utils.Address),
PropertyName: "Pip",
PropertyValue: fakeAddress,
})
})
Expand Down Expand Up @@ -133,6 +134,7 @@ var _ = Describe("Spot storage repository", func() {
shared_behaviors.SharedIlkTriggerTests(shared_behaviors.IlkTriggerTestInput{
Repository: &repo,
Metadata: utils.GetStorageValueMetadata(spot.IlkMat, map[utils.Key]string{constants.Ilk: test_helpers.FakeIlk.Hex}, utils.Uint256),
PropertyName: "Mat",
PropertyValue: strconv.Itoa(rand.Int()),
})
})
Expand Down
5 changes: 5 additions & 0 deletions transformers/storage/vat/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ var _ = Describe("Vat storage repository", func() {
shared_behaviors.SharedIlkTriggerTests(shared_behaviors.IlkTriggerTestInput{
Repository: &repo,
Metadata: utils.GetStorageValueMetadata(vat.IlkArt, map[utils.Key]string{constants.Ilk: test_helpers.FakeIlk.Hex}, utils.Uint256),
PropertyName: "Art",
PropertyValue: strconv.Itoa(rand.Int()),
})
})
Expand Down Expand Up @@ -224,6 +225,7 @@ var _ = Describe("Vat storage repository", func() {
shared_behaviors.SharedIlkTriggerTests(shared_behaviors.IlkTriggerTestInput{
Repository: &repo,
Metadata: utils.GetStorageValueMetadata(vat.IlkDust, map[utils.Key]string{constants.Ilk: test_helpers.FakeIlk.Hex}, utils.Uint256),
PropertyName: "Dust",
PropertyValue: strconv.Itoa(rand.Int()),
})
})
Expand Down Expand Up @@ -270,6 +272,7 @@ var _ = Describe("Vat storage repository", func() {
shared_behaviors.SharedIlkTriggerTests(shared_behaviors.IlkTriggerTestInput{
Repository: &repo,
Metadata: utils.GetStorageValueMetadata(vat.IlkLine, map[utils.Key]string{constants.Ilk: test_helpers.FakeIlk.Hex}, utils.Uint256),
PropertyName: "Line",
PropertyValue: strconv.Itoa(rand.Int()),
})
})
Expand Down Expand Up @@ -316,6 +319,7 @@ var _ = Describe("Vat storage repository", func() {
shared_behaviors.SharedIlkTriggerTests(shared_behaviors.IlkTriggerTestInput{
Repository: &repo,
Metadata: utils.GetStorageValueMetadata(vat.IlkRate, map[utils.Key]string{constants.Ilk: test_helpers.FakeIlk.Hex}, utils.Uint256),
PropertyName: "Rate",
PropertyValue: strconv.Itoa(rand.Int()),
})
})
Expand Down Expand Up @@ -362,6 +366,7 @@ var _ = Describe("Vat storage repository", func() {
shared_behaviors.SharedIlkTriggerTests(shared_behaviors.IlkTriggerTestInput{
Repository: &repo,
Metadata: utils.GetStorageValueMetadata(vat.IlkSpot, map[utils.Key]string{constants.Ilk: test_helpers.FakeIlk.Hex}, utils.Uint256),
PropertyName: "Spot",
PropertyValue: strconv.Itoa(rand.Int()),
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/fakes"
"math/rand"
"reflect"
"strconv"
)

Expand Down Expand Up @@ -80,6 +81,7 @@ func SharedStorageRepositoryVariableBehaviors(inputs *StorageVariableBehaviorInp
type IlkTriggerTestInput struct {
Repository storage.Repository
Metadata utils.StorageValueMetadata
PropertyName string
PropertyValue string
}

Expand All @@ -90,11 +92,13 @@ func SharedIlkTriggerTests(input IlkTriggerTestInput) {
blockTwo int
headerOne,
headerTwo core.Header
repo = input.Repository
database = test_config.NewTestDB(test_config.NewTestNode())
hashOne = common.BytesToHash([]byte{1, 2, 3, 4, 5})
hashTwo = common.BytesToHash([]byte{5, 4, 3, 2, 1})
getStateQuery = `SELECT ilk_identifier, block_number, rate, art, spot, line, dust, chop, lump, flip, rho, duty, pip, mat, updated FROM api.ilk_state_history ORDER BY block_number`
repo = input.Repository
database = test_config.NewTestDB(test_config.NewTestNode())
hashOne = common.BytesToHash([]byte{1, 2, 3, 4, 5})
hashTwo = common.BytesToHash([]byte{5, 4, 3, 2, 1})
getStateQuery = `SELECT ilk_identifier, block_number, rate, art, spot, line, dust, chop, lump, flip, rho, duty, pip, mat, updated FROM api.ilk_state_history ORDER BY block_number`
getFieldQuery = fmt.Sprintf(`SELECT %s FROM api.ilk_state_history ORDER BY block_number`, input.Metadata.Name)
insertFieldQuery = fmt.Sprintf(`INSERT INTO api.ilk_state_history (ilk_identifier, block_number, %s) VALUES ($1, $2, $3)`, input.Metadata.Name)
)

BeforeEach(func() {
Expand Down Expand Up @@ -143,9 +147,78 @@ func SharedIlkTriggerTests(input IlkTriggerTestInput) {
headerOne.Timestamp, headerOne.Timestamp, initialIlkValues)
assertIlk(ilkStates[0], expectedIlk, headerOne.BlockNumber)
})

It("updates field in subsequent blocks", func() {
initialIlkValues := test_helpers.GetIlkValues(0)
_, setupErr := database.Exec(insertFieldQuery,
test_helpers.FakeIlk.Identifier, headerTwo.BlockNumber, initialIlkValues[input.Metadata.Name])
Expect(setupErr).NotTo(HaveOccurred())

err := repo.Create(blockOne, hashOne.String(), input.Metadata, input.PropertyValue)
Expect(err).NotTo(HaveOccurred())

var ilkStates []test_helpers.IlkState
queryErr := database.Select(&ilkStates, getFieldQuery)
Expect(queryErr).NotTo(HaveOccurred())
Expect(len(ilkStates)).To(Equal(2))
Expect(getIlkProperty(ilkStates[1], input.PropertyName)).To(Equal(input.PropertyValue))
})

It("ignores rows from blocks after the next time the field is updated", func() {
initialIlkValues := test_helpers.GetIlkValues(0)
setupErr := repo.Create(blockTwo, hashTwo.String(), input.Metadata, initialIlkValues[input.Metadata.Name])
Expect(setupErr).NotTo(HaveOccurred())

err := repo.Create(blockOne, hashOne.String(), input.Metadata, input.PropertyValue)
Expect(err).NotTo(HaveOccurred())

var ilkStates []test_helpers.IlkState
queryErr := database.Select(&ilkStates, getFieldQuery)
Expect(queryErr).NotTo(HaveOccurred())
Expect(len(ilkStates)).To(Equal(2))
Expect(getIlkProperty(ilkStates[1], input.PropertyName)).To(Equal(initialIlkValues[input.Metadata.Name]))
})

It("ignores rows from different ilk", func() {
initialIlkValues := test_helpers.GetIlkValues(0)
_, setupErr := database.Exec(insertFieldQuery,
test_helpers.AnotherFakeIlk.Identifier, headerTwo.BlockNumber, initialIlkValues[input.Metadata.Name])
Expect(setupErr).NotTo(HaveOccurred())

err := repo.Create(blockOne, hashOne.String(), input.Metadata, input.PropertyValue)
Expect(err).NotTo(HaveOccurred())

var ilkStates []test_helpers.IlkState
queryErr := database.Select(&ilkStates, getFieldQuery)
Expect(queryErr).NotTo(HaveOccurred())
Expect(len(ilkStates)).To(Equal(2))
Expect(getIlkProperty(ilkStates[1], input.PropertyName)).To(Equal(initialIlkValues[input.Metadata.Name]))
})

It("ignores rows from earlier blocks", func() {
initialIlkValues := test_helpers.GetIlkValues(0)
_, setupErr := database.Exec(insertFieldQuery,
test_helpers.FakeIlk.Identifier, headerOne.BlockNumber, initialIlkValues[input.Metadata.Name])
Expect(setupErr).NotTo(HaveOccurred())

err := repo.Create(blockTwo, hashTwo.String(), input.Metadata, input.PropertyValue)
Expect(err).NotTo(HaveOccurred())

var ilkStates []test_helpers.IlkState
queryErr := database.Select(&ilkStates, getFieldQuery)
Expect(queryErr).NotTo(HaveOccurred())
Expect(len(ilkStates)).To(Equal(2))
Expect(getIlkProperty(ilkStates[0], input.PropertyName)).To(Equal(initialIlkValues[input.Metadata.Name]))
})
})
}

func getIlkProperty(ilk test_helpers.IlkState, fieldName string) string {
r := reflect.ValueOf(ilk)
property := reflect.Indirect(r).FieldByName(fieldName)
return property.String()
}

func assertIlk(actualIlk test_helpers.IlkState, expectedIlk test_helpers.IlkState, expectedBlockNumber int64) {
Expect(actualIlk.IlkIdentifier).To(Equal(expectedIlk.IlkIdentifier))
Expect(actualIlk.BlockNumber).To(Equal(strconv.FormatInt(expectedBlockNumber, 10)))
Expand Down

0 comments on commit 4097110

Please sign in to comment.