raw_event_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package borealis
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "testing"
  6. "time"
  7. "github.com/segmentio/ksuid"
  8. )
  9. var expectedEvent = Event[string]{
  10. Type: 1,
  11. SequentialID: 2,
  12. Timestamp: time.UnixMicro(1231006508004000),
  13. UniqueID: ksuid.KSUID{245, 237, 93, 44, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
  14. Payload: "Testing",
  15. }
  16. var expectedRawEvent = RawEvent[string]{
  17. Version: V1MessageVersion,
  18. Envelop: Envelop{
  19. Type: 1,
  20. SequentialID: 2,
  21. TimestampS: 3,
  22. TimestampMS: 4,
  23. UniqueID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
  24. },
  25. Payload: "Testing",
  26. }
  27. var expectedRawEventJSON = "[1,1,2,3,4,\"AAECAwQFBgcICQoLDA0ODw==\",\"Testing\"]"
  28. var expectedRawEventCBOR = []byte{1, 165, 106, 101, 118, 101, 110, 116, 95, 116, 121, 112, 101, 1, 109, 115, 101, 113,
  29. 117, 101, 110, 116, 105, 97, 108, 95, 105, 100, 2, 107, 116, 105, 109, 101, 115, 116, 97, 109, 112, 95, 115, 3,
  30. 108, 116, 105, 109, 101, 115, 116, 97, 109, 112, 95, 109, 115, 4, 105, 117, 110, 105, 113, 117, 101, 95, 105, 100,
  31. 80, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 103, 84, 101, 115, 116, 105, 110, 103}
  32. func TestNewRawEvent(t *testing.T) {
  33. _, err := NewRawEvent(0x1234, make(map[string]interface{}))
  34. if err != nil {
  35. t.Error(err)
  36. }
  37. }
  38. func TestCheckEvent(t *testing.T) {
  39. actualEvent, err := expectedRawEvent.Check()
  40. if err != nil {
  41. t.Error(err)
  42. }
  43. if !expectedEvent.Equal(*actualEvent) {
  44. t.Errorf("expected %v, but got %v", expectedEvent, actualEvent)
  45. }
  46. }
  47. func TestEncodeRawEventJSON(t *testing.T) {
  48. output, err := json.Marshal(expectedRawEvent)
  49. if err != nil {
  50. t.Error(err)
  51. }
  52. actual := string(output)
  53. if actual != expectedRawEventJSON {
  54. t.Errorf("expected %v, but got %v", expectedRawEventJSON, actual)
  55. }
  56. }
  57. func TestEncodeRawEventCBOR(t *testing.T) {
  58. actual, err := expectedRawEvent.EncodeCBOR()
  59. if err != nil {
  60. t.Error(err)
  61. }
  62. if !bytes.Equal(actual, expectedRawEventCBOR) {
  63. t.Errorf("expected %v, but got %v", expectedRawEventCBOR, actual)
  64. }
  65. }
  66. func TestDecodeRawEventJSON(t *testing.T) {
  67. var actual RawEvent[string]
  68. if err := json.Unmarshal([]byte(expectedRawEventJSON), &actual); err != nil {
  69. t.Error(err)
  70. }
  71. if !expectedRawEvent.Equal(actual) {
  72. t.Errorf("expected %v, but got %v", expectedRawEvent, actual)
  73. }
  74. }
  75. func TestDecodeRawEventCBOR(t *testing.T) {
  76. var actual RawEvent[string]
  77. if err := actual.DecodeCBOR(expectedRawEventCBOR); err != nil {
  78. t.Error(err)
  79. }
  80. if !expectedRawEvent.Equal(actual) {
  81. t.Errorf("expected %v, but got %v", expectedRawEvent, actual)
  82. }
  83. }