Skip to content

Commit

Permalink
Merge pull request #257 from dapperlabs/judez/fixing-event-tests
Browse files Browse the repository at this point in the history
NBA-2865: Fixed event unit tests
  • Loading branch information
judezhu authored Aug 12, 2024
2 parents f3e14bd + 043f9c8 commit a28c918
Show file tree
Hide file tree
Showing 18 changed files with 99 additions and 198 deletions.
19 changes: 6 additions & 13 deletions lib/go/events/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder"
)

var (
const (
// This variable specifies that there is a Deposit Event on a TopShot Contract located at address 0x04
EventDeposit = "TopShot.Deposit"
)
Expand Down Expand Up @@ -37,22 +37,15 @@ func (evt depositEvent) Owner() string {
return evt.To()
}

func (evt depositEvent) validate() error {
if evt["eventType"].(string) != EventDeposit {
return fmt.Errorf("error validating event: event is not a valid moment destroyed event, expected type %s, got %s",
EventDeposit, evt["eventType"].(string))
}
return nil
}

func DecodeDepositEvent(b []byte) (DepositEvent, error) {
eventMap, err := decoder.DecodeToEventMap(b)
cadenceValue, err := decoder.GetCadenceEvent(b)
if err != nil {
return nil, err
}
event := depositEvent(eventMap)
if err := event.validate(); err != nil {
return nil, fmt.Errorf("error decoding event: %w", err)
if cadenceValue.EventType.QualifiedIdentifier != EventDeposit {
return nil, fmt.Errorf("unexpected event type: %s", cadenceValue.EventType.QualifiedIdentifier)
}
eventMap, err := decoder.ConvertEvent(cadenceValue)
event := depositEvent(eventMap)
return event, nil
}
19 changes: 6 additions & 13 deletions lib/go/events/moment_destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder"
)

var (
const (
EventMomentDestroyed = "TopShot.MomentDestroyed"
)

Expand All @@ -19,22 +19,15 @@ func (evt momentDestroyedEvent) Id() uint64 {
return evt["id"].(uint64)
}

func (evt momentDestroyedEvent) validate() error {
if evt["eventType"].(string) != EventMomentDestroyed {
return fmt.Errorf("error validating event: event is not a valid moment destroyed event, expected type %s, got %s",
EventMomentDestroyed, evt["eventType"].(string))
}
return nil
}

func DecodeMomentDestroyedEvent(b []byte) (MomentDestroyedEvent, error) {
eventMap, err := decoder.DecodeToEventMap(b)
cadenceValue, err := decoder.GetCadenceEvent(b)
if err != nil {
return nil, err
}
event := momentDestroyedEvent(eventMap)
if err := event.validate(); err != nil {
return nil, fmt.Errorf("error decoding event: %w", err)
if cadenceValue.EventType.QualifiedIdentifier != EventMomentDestroyed {
return nil, fmt.Errorf("unexpected event type: %s", cadenceValue.EventType.QualifiedIdentifier)
}
eventMap, err := decoder.ConvertEvent(cadenceValue)
event := momentDestroyedEvent(eventMap)
return event, nil
}
22 changes: 11 additions & 11 deletions lib/go/events/moment_destroy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,27 @@ import (

func TestCadenceEvents_MomentDestroyed(t *testing.T) {
id := uint64(1234)

momentDestroyedEventType := cadence.EventType{
Location: utils.TestLocation,
QualifiedIdentifier: "TopShot.MomentDestroyed",
Fields: []cadence.Field{
momentDestroyedEventType := cadence.NewEventType(
utils.TestLocation,
"TopShot.MomentDestroyed",
[]cadence.Field{
{
Identifier: "setID",
Type: cadence.UInt64Type{},
Identifier: "id",
Type: cadence.UInt64Type,
},
},
}
nil,
)

momentDestroyedEvent := cadence.NewEvent([]cadence.Value{
cadence.NewUInt64(id),
}).WithType(&momentDestroyedEventType)
}).WithType(momentDestroyedEventType)

payload, err := jsoncdc.Encode(momentDestroyedEvent)
require.NoError(t, err, "failed to encode moment destroyed cadence event")

decodeSetLockPurchasedEvent, err := DecodeMomentDestroyedEvent(payload)
decodeSetLockDestroyedEvent, err := DecodeMomentDestroyedEvent(payload)
require.NoError(t, err, "failed to decode moment destroyed cadence event")

assert.Equal(t, id, decodeSetLockPurchasedEvent.Id())
assert.Equal(t, id, decodeSetLockDestroyedEvent.Id())
}
17 changes: 5 additions & 12 deletions lib/go/events/moment_locked.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,19 @@ func (evt momentLockedEvent) ExpiryTimestamp() float64 {
return evt["expiryTimestamp"].(float64)
}

func (evt momentLockedEvent) validate() error {
if evt["eventType"].(string) != MomentLocked {
return fmt.Errorf("error validating event: event is not a valid moment locked event, expected type %s, got %s",
MomentLocked, evt["eventType"].(string))
}
return nil
}

var _ MomentLockedEvent = (*momentLockedEvent)(nil)

func DecodeMomentLockedEvent(b []byte) (MomentLockedEvent, error) {
eventMap, err := decoder.DecodeToEventMap(b)
cadenceValue, err := decoder.GetCadenceEvent(b)
if err != nil {
return nil, err
}
if cadenceValue.EventType.QualifiedIdentifier != MomentLocked {
return nil, fmt.Errorf("unexpected event type: %s", cadenceValue.EventType.QualifiedIdentifier)
}
eventMap, err := decoder.ConvertEvent(cadenceValue)

event := momentLockedEvent(eventMap)
if err := event.validate(); err != nil {
return nil, fmt.Errorf("error decoding event: %w", err)
}

return event, nil
}
19 changes: 6 additions & 13 deletions lib/go/events/moment_minted.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder"
)

var (
const (
// This variable specifies that there is a MomentMinted Event on a TopShot Contract located at address 0x04
EventMomentMinted = "TopShot.MomentMinted"
)
Expand Down Expand Up @@ -43,24 +43,17 @@ func (evt momentMintedEvent) SubeditionId() uint32 {
return 0
}

func (evt momentMintedEvent) validate() error {
if evt["eventType"].(string) != EventMomentMinted {
return fmt.Errorf("error validating event: event is not a valid moment minted event, expected type %s, got %s",
EventMomentMinted, evt["eventType"].(string))
}
return nil
}

var _ MomentMintedEvent = (*momentMintedEvent)(nil)

func DecodeMomentMintedEvent(b []byte) (MomentMintedEvent, error) {
eventMap, err := decoder.DecodeToEventMap(b)
cadenceValue, err := decoder.GetCadenceEvent(b)
if err != nil {
return nil, err
}
event := momentMintedEvent(eventMap)
if err := event.validate(); err != nil {
return nil, fmt.Errorf("error decoding event: %w", err)
if cadenceValue.EventType.QualifiedIdentifier != EventMomentMinted {
return nil, fmt.Errorf("unexpected event type: %s", cadenceValue.EventType.QualifiedIdentifier)
}
eventMap, err := decoder.ConvertEvent(cadenceValue)
event := momentMintedEvent(eventMap)
return event, nil
}
20 changes: 6 additions & 14 deletions lib/go/events/moment_unlocked.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder"
)

var (
const (
MomentUnlocked = "TopShotLocking.MomentUnlocked"
)

Expand All @@ -19,26 +19,18 @@ func (evt momentUnlockedEvent) FlowID() uint64 {
return evt["flowID"].(uint64)
}

func (evt momentUnlockedEvent) validate() error {
if evt["eventType"].(string) != MomentUnlocked {
return fmt.Errorf("error validating event: event is not a valid moment unlocked event, expected type %s, got %s",
MomentUnlocked, evt["eventType"].(string))
}
return nil
}

var _ MomentUnlockedEvent = (*momentUnlockedEvent)(nil)

func DecodeMomentUnlockedEvent(b []byte) (MomentUnlockedEvent, error) {
eventMap, err := decoder.DecodeToEventMap(b)
cadenceValue, err := decoder.GetCadenceEvent(b)
if err != nil {
return nil, err
}

event := momentUnlockedEvent(eventMap)
if err := event.validate(); err != nil {
return nil, fmt.Errorf("error decoding event: %w", err)
if cadenceValue.EventType.QualifiedIdentifier != MomentUnlocked {
return nil, fmt.Errorf("unexpected event type: %s", cadenceValue.EventType.QualifiedIdentifier)
}
eventMap, err := decoder.ConvertEvent(cadenceValue)

event := momentUnlockedEvent(eventMap)
return event, nil
}
21 changes: 7 additions & 14 deletions lib/go/events/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder"
)

var (
const (
EventPlayCreated = "TopShot.PlayCreated"
)

Expand All @@ -21,25 +21,18 @@ func (evt playCreatedEvent) Id() uint32 {
}

func (evt playCreatedEvent) MetaData() map[interface{}]interface{} {
return evt["metaData"].(map[interface{}]interface{})
}

func (evt playCreatedEvent) validate() error {
if evt["eventType"].(string) != EventPlayCreated {
return fmt.Errorf("error validating event: event is not a valid play created event, expected type %s, got %s",
EventPlayCreated, evt["eventType"].(string))
}
return nil
return evt["metadata"].(map[interface{}]interface{})
}

func DecodePlayCreatedEvent(b []byte) (PlayCreatedEvent, error) {
eventMap, err := decoder.DecodeToEventMap(b)
cadenceValue, err := decoder.GetCadenceEvent(b)
if err != nil {
return nil, err
}
event := playCreatedEvent(eventMap)
if err := event.validate(); err != nil {
return nil, fmt.Errorf("error decoding event: %w", err)
if cadenceValue.EventType.QualifiedIdentifier != EventPlayCreated {
return nil, fmt.Errorf("unexpected event type: %s", cadenceValue.EventType.QualifiedIdentifier)
}
eventMap, err := decoder.ConvertEvent(cadenceValue)
event := playCreatedEvent(eventMap)
return event, nil
}
17 changes: 5 additions & 12 deletions lib/go/events/revealed.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,19 @@ func (evt revealedEvent) NFTs() string {
return evt["nfts"].(string)
}

func (evt revealedEvent) validate() error {
if evt["eventType"].(string) != EventRevealed {
return fmt.Errorf("error validating event: event is not a valid revealed event, expected type %s, got %s",
EventRevealed, evt["eventType"].(string))
}
return nil
}

func parseNFTs(nft string) []string {
return strings.Split(nft, ",")
}

func DecodeRevealedEvent(b []byte) (RevealedEvent, error) {
eventMap, err := decoder.DecodeToEventMap(b)
cadenceValue, err := decoder.GetCadenceEvent(b)
if err != nil {
return nil, err
}
event := revealedEvent(eventMap)
if err := event.validate(); err != nil {
return nil, fmt.Errorf("error decoding event: %w", err)
if cadenceValue.EventType.QualifiedIdentifier != EventRevealed {
return nil, fmt.Errorf("unexpected event type: %s", cadenceValue.EventType.QualifiedIdentifier)
}
eventMap, err := decoder.ConvertEvent(cadenceValue)
event := revealedEvent(eventMap)
return event, nil
}
19 changes: 6 additions & 13 deletions lib/go/events/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder"
)

var (
const (
EventSetCreated = "TopShot.SetCreated"
)

Expand All @@ -24,24 +24,17 @@ func (evt setCreatedEvent) Series() uint32 {
return evt["series"].(uint32)
}

func (evt setCreatedEvent) validate() error {
if evt["eventType"].(string) != EventSetCreated {
return fmt.Errorf("error validating event: event is not a valid set created event, expected type %s, got %s",
EventSetCreated, evt["eventType"].(string))
}
return nil
}

var _ SetCreatedEvent = (*setCreatedEvent)(nil)

func DecodeSetCreatedEvent(b []byte) (SetCreatedEvent, error) {
eventMap, err := decoder.DecodeToEventMap(b)
cadenceValue, err := decoder.GetCadenceEvent(b)
if err != nil {
return nil, err
}
event := setCreatedEvent(eventMap)
if err := event.validate(); err != nil {
return nil, fmt.Errorf("error decoding event: %w", err)
if cadenceValue.EventType.QualifiedIdentifier != EventSetCreated {
return nil, fmt.Errorf("unexpected event type: %s", cadenceValue.EventType.QualifiedIdentifier)
}
eventMap, err := decoder.ConvertEvent(cadenceValue)
event := setCreatedEvent(eventMap)
return event, nil
}
19 changes: 6 additions & 13 deletions lib/go/events/set_locked.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder"
)

var (
const (
EventSetLocked = "TopShot.SetLocked"
)

Expand All @@ -21,22 +21,15 @@ func (evt setLockedEvent) SetID() uint32 {
return evt["setID"].(uint32)
}

func (evt setLockedEvent) validate() error {
if evt["eventType"].(string) != EventSetLocked {
return fmt.Errorf("error validating event: event is not a valid set locked event, expected type %s, got %s",
EventSetLocked, evt["eventType"].(string))
}
return nil
}

func DecodeSetLockedEvent(b []byte) (SetLockedEvent, error) {
eventMap, err := decoder.DecodeToEventMap(b)
cadenceValue, err := decoder.GetCadenceEvent(b)
if err != nil {
return nil, err
}
event := setLockedEvent(eventMap)
if err := event.validate(); err != nil {
return nil, fmt.Errorf("error decoding event: %w", err)
if cadenceValue.EventType.QualifiedIdentifier != EventSetLocked {
return nil, fmt.Errorf("unexpected event type: %s", cadenceValue.EventType.QualifiedIdentifier)
}
eventMap, err := decoder.ConvertEvent(cadenceValue)
event := setLockedEvent(eventMap)
return event, nil
}
Loading

0 comments on commit a28c918

Please sign in to comment.