MCPcopy Create free account
hub / github.com/pace/bricks / TestMarshalPayload

Function TestMarshalPayload

http/jsonapi/response_test.go:23–64  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

21)
22
23func TestMarshalPayload(t *testing.T) {
24 d, e := decimal.NewFromString("9.9999999999999999999")
25 if e != nil {
26 panic(e)
27 }
28 book := &Book{ID: 1, Decimal1: d}
29 books := []*Book{book, {ID: 2}}
30 var jsonData map[string]interface{}
31
32 // One
33 out1 := bytes.NewBuffer(nil)
34 err := MarshalPayload(out1, book)
35 if err != nil {
36 t.Fatal(err)
37 }
38
39 if strings.Contains(out1.String(), `"9.9999999999999999999"`) {
40 t.Fatalf("decimals should be encoded as number, got: %q", out1.String())
41 }
42
43 if err := json.Unmarshal(out1.Bytes(), &jsonData); err != nil {
44 t.Fatal(err)
45 }
46 if _, ok := jsonData["data"].(map[string]interface{}); !ok {
47 t.Fatalf("data key did not contain an Hash/Dict/Map")
48 }
49 fmt.Println(out1.String())
50
51 // Many
52 out2 := bytes.NewBuffer(nil)
53 err = MarshalPayload(out2, books)
54 if err != nil {
55 t.Fatal(err)
56 }
57
58 if err := json.Unmarshal(out2.Bytes(), &jsonData); err != nil {
59 t.Fatal(err)
60 }
61 if _, ok := jsonData["data"].([]interface{}); !ok {
62 t.Fatalf("data key did not contain an Array")
63 }
64}
65
66func TestMarshalPayloadWithNulls(t *testing.T) {
67 books := []*Book{nil, {ID: 101}, nil}

Callers

nothing calls this directly

Calls 4

MarshalPayloadFunction · 0.85
FatalMethod · 0.80
FatalfMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…