Skip to content

Commit

Permalink
test(x/data): add event tests (#1414)
Browse files Browse the repository at this point in the history
* test(x/data): add event tests

* fix: use insert

* refactor: loop over events from end -> front to get most recent events

Co-authored-by: tyler <{ID}+{username}@users.noreply.github.com>
Co-authored-by: Ryan Christoffersen <[email protected]>
  • Loading branch information
3 people authored Aug 26, 2022
1 parent b58cb0f commit afa26b8
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 4 deletions.
3 changes: 2 additions & 1 deletion types/testutil/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func MatchEvent(expected proto.Message, emitted sdk.Event) error {
// the proto.MessageName of the passed msg.
func GetEvent(msg proto.Message, events []sdk.Event) (e sdk.Event, found bool) {
eventName := proto.MessageName(msg)
for _, e := range events {
for i := len(events) - 1; i >= 0; i-- {
e = events[i]
if eventName == e.Type {
return e, true
}
Expand Down
12 changes: 12 additions & 0 deletions x/data/server/features/msg_anchor.feature
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,15 @@ Feature: Msg/Anchor
Given alice has anchored the data at block time "2020-01-01"
When bob attempts to anchor the data at block time "2020-01-02"
Then the anchor entry exists with timestamp "2020-01-01"

Rule: Event is emitted

Scenario: EventAnchor is emitted
When alice attempts to anchor the data at block time "2020-01-01"
Then the anchor entry exists with timestamp "2020-01-01"
And expect event with properties
"""
{
"iri": "regen:112wkBET2rRgE8pahuaczxKbmv7ciehqsne57F9gtzf1PVhwuFTX.bin"
}
"""
16 changes: 16 additions & 0 deletions x/data/server/features/msg_attest.feature
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,19 @@ Feature: Msg/Attest
Given alice has attested to the data at block time "2020-01-01"
When bob attempts to attest to the data at block time "2020-01-02"
Then the attestor entry for bob exists with timestamp "2020-01-02"

Rule: event is emitted

Background:
Given alice's address "regen1k82wewrfkhdmegw6uxrgwwzrsd7593t8tej2d5"

Scenario: EventAttest is emitted
When alice attempts to attest to the data at block time "2020-01-01"
Then the anchor entry exists with timestamp "2020-01-01"
And event is emitted with properties
"""
{
"iri": "regen:13toVfvC2YxrrfSXWB5h2BGHiXZURsKxWUz72uDRDSPMCrYPguGUXSC.rdf",
"attestor": "regen1k82wewrfkhdmegw6uxrgwwzrsd7593t8tej2d5"
}
"""
13 changes: 13 additions & 0 deletions x/data/server/features/msg_define_resolver.feature
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,16 @@ Feature: Msg/DefineResolver
Given alice has defined a resolver with url "https://foo.bar"
When alice attempts to define a resolver with url "https://foo.bar"
Then expect the error "a resolver with the same URL and manager already exists: unique key violation"

Rule: Event is emitted

Scenario: EventDefineResolver is emitted
Given alice has defined a resolver with url "https://foo.bar"
When bob attempts to define a resolver with url "https://foo.bar"
Then expect the resolver with id "2" and url "https://foo.bar" and manager bob
And expect event with properties
"""
{
"id": 2
}
"""
14 changes: 14 additions & 0 deletions x/data/server/features/msg_register_resolver.feature
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,17 @@ Feature: Msg/RegisterResolver
Scenario: the registrant is not the manager
When bob attempts to register the data to the resolver
Then expect the error "unauthorized resolver manager"

Rule: event is emitted

Scenario: EventRegisterResolver is emitted
Given alice has defined the resolver with url "https://foo.bar"
When alice attempts to register the data to the resolver
Then the data resolver entry exists
And expect event with properties
"""
{
"id": 1,
"iri": "regen:112wkBET2rRgE8pahuaczxKbmv7ciehqsne57F9gtzf1PVhwuFTX.bin"
}
"""
14 changes: 14 additions & 0 deletions x/data/server/msg_anchor_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"encoding/json"
"testing"

"github.com/gogo/protobuf/jsonpb"
Expand All @@ -10,6 +11,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/regen-network/regen-ledger/types"
"github.com/regen-network/regen-ledger/types/testutil"
"github.com/regen-network/regen-ledger/x/data"
)

Expand Down Expand Up @@ -90,3 +92,15 @@ func (s *anchorSuite) TheAnchorEntryExistsWithTimestamp(a string) {
require.NotNil(s.t, dataAnchor)
require.Equal(s.t, anchorTime, dataAnchor.Timestamp.AsTime())
}

func (s *anchorSuite) ExpectEventWithProperties(a gocuke.DocString) {
var event data.EventAnchor
err := json.Unmarshal([]byte(a.Content), &event)
require.NoError(s.t, err)

sdkEvent, found := testutil.GetEvent(&event, s.sdkCtx.EventManager().Events())
require.True(s.t, found)

err = testutil.MatchEvent(&event, sdkEvent)
require.NoError(s.t, err)
}
20 changes: 20 additions & 0 deletions x/data/server/msg_attest_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"encoding/json"
"testing"

"github.com/gogo/protobuf/jsonpb"
Expand All @@ -10,6 +11,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/regen-network/regen-ledger/types"
"github.com/regen-network/regen-ledger/types/testutil"
"github.com/regen-network/regen-ledger/x/data"
)

Expand Down Expand Up @@ -37,6 +39,12 @@ func (s *attestSuite) TheContentHash(a gocuke.DocString) {
require.NoError(s.t, err)
}

func (s *attestSuite) AlicesAddress(a string) {
addr, err := sdk.AccAddressFromBech32(a)
require.NoError(s.t, err)
s.alice = addr
}

func (s *attestSuite) AliceHasAnchoredTheDataAtBlockTime(a string) {
blockTime, err := types.ParseDate("block time", a)
require.NoError(s.t, err)
Expand Down Expand Up @@ -118,6 +126,18 @@ func (s *attestSuite) TheAttestorEntryForBobExistsWithTimestamp(a string) {
require.Equal(s.t, attestTime, dataAttestor.Timestamp.AsTime())
}

func (s *attestSuite) EventIsEmittedWithProperties(a gocuke.DocString) {
var event data.EventAttest
err := json.Unmarshal([]byte(a.Content), &event)
require.NoError(s.t, err)

sdkEvent, found := testutil.GetEvent(&event, s.sdkCtx.EventManager().Events())
require.True(s.t, found)

err = testutil.MatchEvent(&event, sdkEvent)
require.NoError(s.t, err)
}

func (s *attestSuite) getDataID() []byte {
iri, err := s.ch.ToIRI()
require.NoError(s.t, err)
Expand Down
21 changes: 18 additions & 3 deletions x/data/server/msg_define_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package server

import (
"encoding/json"
"strconv"
"testing"

Expand All @@ -10,6 +11,8 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"

api "github.com/regen-network/regen-ledger/api/regen/data/v1"
"github.com/regen-network/regen-ledger/types/testutil"
"github.com/regen-network/regen-ledger/x/data"
)

Expand All @@ -31,9 +34,9 @@ func (s *defineResolverSuite) Before(t gocuke.TestingT) {
}

func (s *defineResolverSuite) AliceHasDefinedAResolverWithUrl(a string) {
_, err := s.server.DefineResolver(s.ctx, &data.MsgDefineResolver{
Manager: s.alice.String(),
ResolverUrl: a,
err := s.server.stateStore.ResolverTable().Insert(s.ctx, &api.Resolver{
Url: a,
Manager: s.alice,
})
require.NoError(s.t, err)
}
Expand Down Expand Up @@ -65,3 +68,15 @@ func (s *defineResolverSuite) ExpectTheResolverWithIdAndUrlAndManagerBob(a strin
func (s *defineResolverSuite) ExpectTheError(a string) {
require.EqualError(s.t, s.err, a)
}

func (s *defineResolverSuite) ExpectEventWithProperties(a gocuke.DocString) {
var event data.EventDefineResolver
err := json.Unmarshal([]byte(a.Content), &event)
require.NoError(s.t, err)

sdkEvent, found := testutil.GetEvent(&event, s.sdkCtx.EventManager().Events())
require.True(s.t, found)

err = testutil.MatchEvent(&event, sdkEvent)
require.NoError(s.t, err)
}
14 changes: 14 additions & 0 deletions x/data/server/msg_register_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package server

import (
"encoding/json"
"strconv"
"testing"

Expand All @@ -12,6 +13,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/regen-network/regen-ledger/types"
"github.com/regen-network/regen-ledger/types/testutil"
"github.com/regen-network/regen-ledger/x/data"
)

Expand Down Expand Up @@ -134,6 +136,18 @@ func (s *registerResolverSuite) ExpectTheError(a string) {
require.EqualError(s.t, s.err, a)
}

func (s *registerResolverSuite) ExpectEventWithProperties(a gocuke.DocString) {
var event data.EventRegisterResolver
err := json.Unmarshal([]byte(a.Content), &event)
require.NoError(s.t, err)

sdkEvent, found := testutil.GetEvent(&event, s.sdkCtx.EventManager().Events())
require.True(s.t, found)

err = testutil.MatchEvent(&event, sdkEvent)
require.NoError(s.t, err)
}

func (s *registerResolverSuite) getDataID() []byte {
iri, err := s.ch.ToIRI()
require.NoError(s.t, err)
Expand Down

0 comments on commit afa26b8

Please sign in to comment.