raw_event_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 expectedRawEvent = 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 expectedRawEventJSON = "[1,2,3,4,\"AAECAwQFBgcICQoLDA0ODw==\",{}]"
  26. 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}
  27. func TestNewRawEvent(t *testing.T) {
  28. _, err := NewRawEvent(0x1234, make(map[string]interface{}))
  29. if err != nil {
  30. t.Error(err)
  31. }
  32. }
  33. func TestCheckEvent(t *testing.T) {
  34. actualEvent, err := expectedRawEvent.Check()
  35. if err != nil {
  36. t.Error(err)
  37. }
  38. if !expectedEvent.Equal(*actualEvent) {
  39. t.Errorf("expected %v, but got %v", expectedEvent, actualEvent)
  40. }
  41. }
  42. func TestEncodeRawEventJSON(t *testing.T) {
  43. output, err := json.Marshal(expectedRawEvent)
  44. if err != nil {
  45. t.Error(err)
  46. }
  47. actual := string(output)
  48. if actual != expectedRawEventJSON {
  49. t.Errorf("expected %v, but got %v", expectedRawEventJSON, actual)
  50. }
  51. }
  52. func TestEncodeRawEventCBOR(t *testing.T) {
  53. actual, err := cbor.Marshal(expectedRawEvent)
  54. if err != nil {
  55. t.Error(err)
  56. }
  57. if !bytes.Equal(actual, expectedRawEventCBOR) {
  58. t.Errorf("expected %v, but got %v", expectedRawEventCBOR, actual)
  59. }
  60. }
  61. func TestDecodeRawEventJSON(t *testing.T) {
  62. var actual RawEvent
  63. if err := json.Unmarshal([]byte(expectedRawEventJSON), &actual); err != nil {
  64. t.Error(err)
  65. }
  66. if !expectedRawEvent.Equal(actual) {
  67. t.Errorf("expected %v, but got %v", expectedRawEvent, actual)
  68. }
  69. }
  70. func TestDecodeRawEventCBOR(t *testing.T) {
  71. var actual RawEvent
  72. if err := cbor.Unmarshal([]byte(expectedRawEventCBOR), &actual); err != nil {
  73. t.Error(err)
  74. }
  75. if !expectedRawEvent.Equal(actual) {
  76. t.Errorf("expected %v, but got %v", expectedRawEvent, actual)
  77. }
  78. }