github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/model/events/parse_test.go (about) 1 package events_test 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 10 "github.com/onflow/flow-go/model/events" 11 "github.com/onflow/flow-go/model/flow" 12 "github.com/onflow/flow-go/utils/unittest" 13 ) 14 15 func TestParseEvent(t *testing.T) { 16 t.Parallel() 17 18 tests := []struct { 19 name string 20 eventType flow.EventType 21 expected events.ParsedEvent 22 }{ 23 { 24 name: "flow event", 25 eventType: "flow.AccountCreated", 26 expected: events.ParsedEvent{ 27 Type: events.ProtocolEventType, 28 EventType: "flow.AccountCreated", 29 Contract: "flow", 30 ContractName: "flow", 31 Name: "AccountCreated", 32 }, 33 }, 34 { 35 name: "account event", 36 eventType: "A.0000000000000001.Contract1.EventA", 37 expected: events.ParsedEvent{ 38 Type: events.AccountEventType, 39 EventType: "A.0000000000000001.Contract1.EventA", 40 Address: "0000000000000001", 41 Contract: "A.0000000000000001.Contract1", 42 ContractName: "Contract1", 43 Name: "EventA", 44 }, 45 }, 46 } 47 48 for _, test := range tests { 49 t.Run(test.name, func(t *testing.T) { 50 event, err := events.ParseEvent(test.eventType) 51 require.NoError(t, err) 52 53 assert.Equal(t, test.expected.Type, event.Type) 54 assert.Equal(t, test.expected.EventType, event.EventType) 55 assert.Equal(t, test.expected.Address, event.Address) 56 assert.Equal(t, test.expected.Contract, event.Contract) 57 assert.Equal(t, test.expected.Name, event.Name) 58 }) 59 } 60 } 61 62 func TestParseEvent_Invalid(t *testing.T) { 63 t.Parallel() 64 65 eventTypes := []flow.EventType{ 66 "", // not enough parts 67 "invalid", // not enough parts 68 "invalid.event", // invalid first part 69 "B.0000000000000001.invalid.event", // invalid first part 70 "flow", // incorrect number of parts for protocol event 71 "flow.invalid.event", // incorrect number of parts for protocol event 72 "A.0000000000000001.invalid", // incorrect number of parts for account event 73 "A.0000000000000001.invalid.a.b", // incorrect number of parts for account event 74 75 } 76 77 for _, eventType := range eventTypes { 78 _, err := events.ParseEvent(eventType) 79 assert.Error(t, err, "expected error for event type: %s", eventType) 80 } 81 } 82 83 func TestValidateEvent(t *testing.T) { 84 t.Parallel() 85 86 tests := []struct { 87 name string 88 eventType flow.EventType 89 expected events.ParsedEvent 90 }{ 91 { 92 name: "flow event", 93 eventType: "flow.AccountCreated", 94 expected: events.ParsedEvent{ 95 Type: events.ProtocolEventType, 96 EventType: "flow.AccountCreated", 97 Contract: "flow", 98 ContractName: "flow", 99 Name: "AccountCreated", 100 }, 101 }, 102 { 103 name: "account event", 104 eventType: "A.0000000000000001.Contract1.EventA", 105 expected: events.ParsedEvent{ 106 Type: events.AccountEventType, 107 EventType: "A.0000000000000001.Contract1.EventA", 108 Address: "0000000000000001", 109 Contract: "A.0000000000000001.Contract1", 110 ContractName: "Contract1", 111 Name: "EventA", 112 }, 113 }, 114 } 115 116 for _, test := range tests { 117 t.Run(test.name, func(t *testing.T) { 118 event, err := events.ValidateEvent(test.eventType, flow.MonotonicEmulator.Chain()) 119 require.NoError(t, err) 120 121 assert.Equal(t, test.expected.Type, event.Type) 122 assert.Equal(t, test.expected.EventType, event.EventType) 123 assert.Equal(t, test.expected.Address, event.Address) 124 assert.Equal(t, test.expected.Contract, event.Contract) 125 assert.Equal(t, test.expected.Name, event.Name) 126 }) 127 } 128 } 129 130 func TestValidateEvent_Invalid(t *testing.T) { 131 t.Parallel() 132 133 eventTypes := []flow.EventType{ 134 "", // not enough parts 135 "invalid", // not enough parts 136 "invalid.event", // invalid first part 137 "B.0000000000000001.invalid.event", // invalid first part 138 "flow", // incorrect number of parts for protocol event 139 "flow.invalid.event", // incorrect number of parts for protocol event 140 "A.0000000000000001.invalid", // incorrect number of parts for account event 141 "A.0000000000000001.invalid.a.b", // incorrect number of parts for account event 142 flow.EventType(fmt.Sprintf("A.%s.Contract1.EventA", unittest.RandomAddressFixture())), // address from wrong chain 143 } 144 145 for _, eventType := range eventTypes { 146 _, err := events.ValidateEvent(eventType, flow.MonotonicEmulator.Chain()) 147 assert.Error(t, err, "expected error for event type: %s", eventType) 148 } 149 }