raw_event_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package borealis
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "testing"
  6. "time"
  7. "github.com/fxamacker/cbor/v2"
  8. "github.com/segmentio/ksuid"
  9. )
  10. var expectedEvent = Event{
  11. Type: 1,
  12. SequentialID: 2,
  13. Timestamp: time.UnixMicro(1231006508004000), // 2009-01-03T18:15:08.004Z
  14. UniqueID: ksuid.KSUID{245, 237, 93, 44, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
  15. Payload: map[string]interface{}{},
  16. }
  17. var expectedEventRaw = RawEvent{
  18. Type: 1,
  19. SequentialID: 2,
  20. TimestampS: 3,
  21. TimestampMS: 4,
  22. UniqueID: []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF},
  23. Payload: map[string]interface{}{},
  24. }
  25. var expectedEventJSON = "[1,2,3,4,\"AAECAwQFBgcICQoLDA0ODw==\",{}]"
  26. 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}
  27. func TestCheckEvent(t *testing.T) {
  28. actualEvent, err := expectedEventRaw.Check()
  29. if err != nil {
  30. t.Error(err)
  31. }
  32. if !expectedEvent.Equal(*actualEvent) {
  33. t.Errorf("expected %v, but got %v", expectedEvent, actualEvent)
  34. }
  35. }
  36. func TestEncodeEventJSON(t *testing.T) {
  37. output, err := json.Marshal(expectedEventRaw)
  38. if err != nil {
  39. t.Error(err)
  40. }
  41. actual := string(output)
  42. if actual != expectedEventJSON {
  43. t.Errorf("expected %v, but got %v", expectedEventJSON, actual)
  44. }
  45. }
  46. func TestEncodeEventCBOR(t *testing.T) {
  47. actual, err := cbor.Marshal(expectedEventRaw)
  48. if err != nil {
  49. t.Error(err)
  50. }
  51. if !bytes.Equal(actual, expectedEventCBOR) {
  52. t.Errorf("expected %v, but got %v", expectedEventCBOR, actual)
  53. }
  54. }
  55. func TestDecodeEventJSON(t *testing.T) {
  56. var actual RawEvent
  57. if err := json.Unmarshal([]byte(expectedEventJSON), &actual); err != nil {
  58. t.Error(err)
  59. }
  60. if !expectedEventRaw.Equal(actual) {
  61. t.Errorf("expected %v, but got %v", expectedEventRaw, actual)
  62. }
  63. }
  64. func TestDecodeEventCBOR(t *testing.T) {
  65. var actual RawEvent
  66. if err := cbor.Unmarshal([]byte(expectedEventCBOR), &actual); err != nil {
  67. t.Error(err)
  68. }
  69. if !expectedEventRaw.Equal(actual) {
  70. t.Errorf("expected %v, but got %v", expectedEventRaw, actual)
  71. }
  72. }