package borealis import ( "bytes" "encoding/json" "testing" "time" "github.com/fxamacker/cbor/v2" "github.com/segmentio/ksuid" ) var expectedEvent = Event{ Type: 1, SequentialID: 2, Timestamp: time.UnixMicro(1231006508004000), // 2009-01-03T18:15:08.004Z UniqueID: ksuid.KSUID{245, 237, 93, 44, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Payload: map[string]interface{}{}, } var expectedEventRaw = RawEvent{ Type: 1, SequentialID: 2, TimestampS: 3, TimestampMS: 4, UniqueID: []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF}, Payload: map[string]interface{}{}, } var expectedEventJSON = "[1,2,3,4,\"AAECAwQFBgcICQoLDA0ODw==\",{}]" var expectedEventCBOR = []byte{0x86, 1, 2, 3, 4, 0x50, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0xa0} func TestNewRawEvent(t *testing.T) { _, err := NewRawEvent(0x1234, make(map[string]interface{})) if err != nil { t.Error(err) } } func TestCheckEvent(t *testing.T) { actualEvent, err := expectedEventRaw.Check() if err != nil { t.Error(err) } if !expectedEvent.Equal(*actualEvent) { t.Errorf("expected %v, but got %v", expectedEvent, actualEvent) } } func TestEncodeEventJSON(t *testing.T) { output, err := json.Marshal(expectedEventRaw) if err != nil { t.Error(err) } actual := string(output) if actual != expectedEventJSON { t.Errorf("expected %v, but got %v", expectedEventJSON, actual) } } func TestEncodeEventCBOR(t *testing.T) { actual, err := cbor.Marshal(expectedEventRaw) if err != nil { t.Error(err) } if !bytes.Equal(actual, expectedEventCBOR) { t.Errorf("expected %v, but got %v", expectedEventCBOR, actual) } } func TestDecodeEventJSON(t *testing.T) { var actual RawEvent if err := json.Unmarshal([]byte(expectedEventJSON), &actual); err != nil { t.Error(err) } if !expectedEventRaw.Equal(actual) { t.Errorf("expected %v, but got %v", expectedEventRaw, actual) } } func TestDecodeEventCBOR(t *testing.T) { var actual RawEvent if err := cbor.Unmarshal([]byte(expectedEventCBOR), &actual); err != nil { t.Error(err) } if !expectedEventRaw.Equal(actual) { t.Errorf("expected %v, but got %v", expectedEventRaw, actual) } }