MCPcopy Create free account
hub / github.com/blues/note / TestBulkEncode

Function TestBulkEncode

lib/bulk_test.go:56–99  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

54}
55
56func TestBulkEncode(t *testing.T) {
57 ctx := context.Background()
58 templateJSON := `{"sensor12":{"temp":12.1,"humidity":12.1},"mylist11":[{"val":11},{"val":11}],"mylist12":[{"val":12},{"val":12}],"mylist13":[{"val":13},{"val":13}],"mylist14":[{"val":14},{"val":14}],"mylist18":[{"val":18},{"val":18}],"mylist21":[{"val":21},{"val":21}],"mylist22":[{"val":22},{"val":22}],"mylist23":[{"val":23},{"val":23}],"mylist24":[{"val":24},{"val":24}],"sep14":"0","sensor14":{"temp":14.1,"humidity":14.1},"sep23":"0","sensor18":{"temp":18.1,"humidity":18.1},"bool1":true,"bool2":true,"bool3":true,"bool4":true,"bool5":true,"bool6":true,"bool7":true,"bool8":true,"bool9":true}`
59
60 bulkBody := BulkBody{}
61 bulkBody.NoteFormat = BulkNoteFormatFlexNano
62 bulkBody.NoteTemplate = templateJSON
63 bulkBodyJSON, err := note.JSONMarshal(bulkBody)
64 require.NoError(t, err)
65
66 context, err := BulkEncodeTemplate(bulkBodyJSON)
67 require.NoError(t, err)
68
69 // Note that some numeric values are in quotes to test that numeric env vars, which are always strings, are encoded correctly
70 dataJSON := `{"beep":true,"sep12":"valueSep12","sensor12":{"hi":"there","temp":"-123.5","humidity":151.5},"sensor14":{"temp":1.9995117,"humidity":"1.6777216E+38"},"sensor18":{"humidity":"9.8765432109876e-307","temp":9.8765432109876e+307},"mylist11":[{"val":"-128"},{"val":127}],"mylist12":[{"val":-32768},{"val":"32767"}],"mylist13":[{"val":-8388608},{"val":"8388607"}],"mylist14":[{"val":"-2147483648"},{"val":2147483647}],"mylist18":[{"val":-9223372036854775800},{"val":"9223372036854775800"}],"mylist21":[{"val":"0"},{"val":255}],"mylist22":[{"val":0},{"val":"65535"}],"mylist23":[{"val":"0"},{"val":16777215}],"mylist24":[{"val":0},{"val":"4294967295"}],"sep23":"valueSep23","bool1":true,"bool2":false,"bool3":"true","bool4":"false","bool5":"1","bool6":1,"bool7":"0","bool8":0,"bool9":true}`
71 var data map[string]interface{}
72 err = note.JSONUnmarshal([]byte(dataJSON), &data)
73 require.NoError(t, err)
74
75 output, err := context.BulkEncodeNextEntry(ctx, data, []byte{}, 0, 0, "", "", false)
76 require.NoError(t, err)
77
78 if verboseBulkTest {
79 fmt.Printf("Encoded output:\n%s\n", hex.Dump(output))
80 }
81
82 require.Equal(t, []byte{0x8, 0x63, 0x0, 0xb8, 0xd7, 0xbc, 0x58, 0x80, 0x7f, 0x0, 0x80, 0xff, 0x7f, 0x0, 0x0, 0x80, 0xff, 0xff, 0x7f, 0x0, 0x0, 0x0, 0x80, 0xff, 0xff, 0xff, 0x7f, 0x8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x80, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x0, 0xff, 0x0, 0x0, 0xff, 0xff, 0x0, 0x0, 0x0, 0xff, 0xff, 0xff, 0x0, 0x0, 0x0, 0x0, 0xff, 0xff, 0xff, 0xff, 0x0, 0x0, 0xf0, 0xff, 0x3f, 0x7c, 0x6f, 0xfc, 0x7e, 0xa, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x53, 0x65, 0x70, 0x32, 0x33, 0x65, 0x8d, 0x92, 0x4e, 0xb1, 0x94, 0xe1, 0x7f, 0xed, 0x15, 0x3b, 0x1a, 0x99, 0x31, 0x66, 0x0, 0x35, 0x1},
83 output)
84
85 context, err = BulkDecodeTemplate(ctx, bulkBodyJSON, output)
86 require.NoError(t, err)
87
88 body, payload, _, _, _, _, success := context.BulkDecodeNextEntry(ctx)
89 require.Equal(t, true, success)
90 require.Equal(t, "", string(payload))
91
92 bodyJSON, err := note.JSONMarshal(body)
93 require.NoError(t, err)
94 if verboseBulkTest {
95 fmt.Printf("Round-trip:\n")
96 fmt.Printf(" *** before ***\n%s\n", string(dataJSON))
97 fmt.Printf(" *** after ***\n%s\n", string(bodyJSON))
98 }
99}
100
101var bulkTests = []struct {
102 template string

Callers

nothing calls this directly

Calls 4

BulkEncodeTemplateFunction · 0.85
BulkDecodeTemplateFunction · 0.85
BulkEncodeNextEntryMethod · 0.80
BulkDecodeNextEntryMethod · 0.80

Tested by

no test coverage detected