Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions eventsv1/serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
type SerializableEvent struct {
Attributes map[string]any `json:"attributes"`
Trails []string `json:"trails"`
EventType string `json:"event_type,omitempty"`
}

// SerializeEventFromJson serializes a v1 event from datatrails eventv1 api respone
Expand Down
49 changes: 49 additions & 0 deletions eventsv1/serializer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ func TestSerializeEventFromProtoConsistency(t *testing.T) {
type event struct {
attributes map[string]any
trails []string
event_type string
}
tests := []struct {
name string
event1 event
event2 event
sameSerialization bool
hasEventType bool
}{
{
name: "same attributes and trails in same order",
Expand Down Expand Up @@ -55,6 +57,39 @@ func TestSerializeEventFromProtoConsistency(t *testing.T) {
},
sameSerialization: true,
},
{
name: "same attributes and trails in same order and event type",
event1: event{
attributes: map[string]any{
"flour": "500g",
"sugar": "250g",
"eggs": "2",
"milk": "300ml",
"vanilla extract": "1 tsp",
},
trails: []string{
"cake",
"viccy sponge",
},
event_type: "receipt",
},
event2: event{
attributes: map[string]any{
"flour": "500g",
"sugar": "250g",
"eggs": "2",
"milk": "300ml",
"vanilla extract": "1 tsp",
},
trails: []string{
"cake",
"viccy sponge",
},
event_type: "receipt",
},
sameSerialization: true,
hasEventType: true,
},
{
name: "same attributes and trails, attributes in different order",
event1: event{
Expand Down Expand Up @@ -183,11 +218,13 @@ func TestSerializeEventFromProtoConsistency(t *testing.T) {
serializableEvent1 := SerializableEvent{
Attributes: test.event1.attributes,
Trails: test.event1.trails,
EventType: test.event1.event_type,
}

serializableEvent2 := SerializableEvent{
Attributes: test.event2.attributes,
Trails: test.event2.trails,
EventType: test.event2.event_type,
}

serializedEvent1, err := serializableEvent1.Serialize()
Expand All @@ -196,12 +233,24 @@ func TestSerializeEventFromProtoConsistency(t *testing.T) {
serializedEvent2, err := serializableEvent2.Serialize()
require.Nil(t, err)

// check that two events serilaize to the same value
// or not depending on test data
if test.sameSerialization {
assert.Equal(t, serializedEvent1, serializedEvent2)
} else {
assert.NotEqual(t, serializedEvent1, serializedEvent2)
}

// check that event_type got serialized or ommitted if not present
// in the initila data
if test.hasEventType {
assert.Contains(t, string(serializedEvent1), "event_type")
assert.Contains(t, string(serializedEvent2), "event_type")
} else {
assert.NotContains(t, string(serializedEvent1), "event_type")
assert.NotContains(t, string(serializedEvent2), "event_type")
}

})
}
}
Expand Down
Loading