MCPcopy Create free account
hub / github.com/francoispqt/gojay / TestEncoceSQLNullString

Function TestEncoceSQLNullString

encode_sqlnull_test.go:12–74  ·  view source on GitHub ↗

Null String

(t *testing.T)

Source from the content-addressed store, hash-verified

10
11// Null String
12func TestEncoceSQLNullString(t *testing.T) {
13 testCases := []struct {
14 name string
15 sqlNullString sql.NullString
16 expectedResult string
17 err bool
18 }{
19 {
20 name: "it should encode a null string",
21 sqlNullString: sql.NullString{
22 String: "foo bar",
23 },
24 expectedResult: `"foo bar"`,
25 },
26 {
27 name: "it should return an err as the string is invalid",
28 sqlNullString: sql.NullString{
29 String: "foo \t bar",
30 },
31 expectedResult: `"foo \t bar"`,
32 },
33 }
34
35 for _, testCase := range testCases {
36 t.Run(testCase.name, func(t *testing.T) {
37 var b strings.Builder
38 enc := NewEncoder(&b)
39 err := enc.EncodeSQLNullString(&testCase.sqlNullString)
40 if testCase.err {
41 assert.NotNil(t, err)
42 } else {
43 assert.Nil(t, err)
44 assert.Equal(t, testCase.expectedResult, b.String())
45 }
46 })
47 }
48
49 t.Run(
50 "should panic as the encoder is pooled",
51 func(t *testing.T) {
52 builder := &strings.Builder{}
53 enc := NewEncoder(builder)
54 enc.isPooled = 1
55 defer func() {
56 err := recover()
57 assert.NotNil(t, err, "err should not be nil")
58 assert.IsType(t, InvalidUsagePooledEncoderError(""), err, "err should be of type InvalidUsagePooledEncoderError")
59 }()
60 _ = enc.EncodeSQLNullString(&sql.NullString{})
61 assert.True(t, false, "should not be called as encoder should have panicked")
62 },
63 )
64
65 t.Run(
66 "should return an error as the writer encounters an error",
67 func(t *testing.T) {
68 builder := TestWriterError("")
69 enc := NewEncoder(builder)

Callers

nothing calls this directly

Calls 5

EncodeSQLNullStringMethod · 0.95
NewEncoderFunction · 0.85
TestWriterErrorTypeAlias · 0.85
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…