event_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package borealis
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "testing"
  6. "github.com/fxamacker/cbor/v2"
  7. )
  8. var event = Event{
  9. Type: 1,
  10. TimestampS: 2,
  11. TimestampMS: 3,
  12. SequentialID: 4,
  13. UniqueID: []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF},
  14. Payload: map[string]interface{}{},
  15. }
  16. var eventJSON = "[1,2,3,4,\"AAECAwQFBgcICQoLDA0ODw==\",{}]"
  17. var eventCBOR = []byte{0x86, 1, 2, 3, 4, 0x50, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0xa0}
  18. func TestEncodeEventJSON(t *testing.T) {
  19. output, err := json.Marshal(event)
  20. if err != nil {
  21. t.Error(err)
  22. }
  23. actual := string(output)
  24. if actual != eventJSON {
  25. t.Errorf("expected %v, but got %v", eventJSON, actual)
  26. }
  27. }
  28. func TestEncodeEventCBOR(t *testing.T) {
  29. actual, err := cbor.Marshal(event)
  30. if err != nil {
  31. t.Error(err)
  32. }
  33. if !bytes.Equal(actual, eventCBOR) {
  34. t.Errorf("expected %v, but got %v", eventCBOR, actual)
  35. }
  36. }
  37. func TestDecodeEventJSON(t *testing.T) {
  38. var actual Event
  39. if err := json.Unmarshal([]byte(eventJSON), &actual); err != nil {
  40. t.Error(err)
  41. }
  42. if !event.Equal(actual) {
  43. t.Errorf("expected %v, but got %v", event, actual)
  44. }
  45. }
  46. func TestDecodeEventCBOR(t *testing.T) {
  47. var actual Event
  48. if err := cbor.Unmarshal([]byte(eventCBOR), &actual); err != nil {
  49. t.Error(err)
  50. }
  51. if !event.Equal(actual) {
  52. t.Errorf("expected %v, but got %v", event, actual)
  53. }
  54. }