12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- 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 expectedRawEvent = 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 expectedRawEventJSON = "[1,2,3,4,\"AAECAwQFBgcICQoLDA0ODw==\",{}]"
- var expectedRawEventCBOR = []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 := expectedRawEvent.Check()
- if err != nil {
- t.Error(err)
- }
- if !expectedEvent.Equal(*actualEvent) {
- t.Errorf("expected %v, but got %v", expectedEvent, actualEvent)
- }
- }
- func TestEncodeRawEventJSON(t *testing.T) {
- output, err := json.Marshal(expectedRawEvent)
- if err != nil {
- t.Error(err)
- }
- actual := string(output)
- if actual != expectedRawEventJSON {
- t.Errorf("expected %v, but got %v", expectedRawEventJSON, actual)
- }
- }
- func TestEncodeRawEventCBOR(t *testing.T) {
- actual, err := cbor.Marshal(expectedRawEvent)
- if err != nil {
- t.Error(err)
- }
- if !bytes.Equal(actual, expectedRawEventCBOR) {
- t.Errorf("expected %v, but got %v", expectedRawEventCBOR, actual)
- }
- }
- func TestDecodeRawEventJSON(t *testing.T) {
- var actual RawEvent
- if err := json.Unmarshal([]byte(expectedRawEventJSON), &actual); err != nil {
- t.Error(err)
- }
- if !expectedRawEvent.Equal(actual) {
- t.Errorf("expected %v, but got %v", expectedRawEvent, actual)
- }
- }
- func TestDecodeRawEventCBOR(t *testing.T) {
- var actual RawEvent
- if err := cbor.Unmarshal([]byte(expectedRawEventCBOR), &actual); err != nil {
- t.Error(err)
- }
- if !expectedRawEvent.Equal(actual) {
- t.Errorf("expected %v, but got %v", expectedRawEvent, actual)
- }
- }
|