-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbool_test.go
More file actions
62 lines (55 loc) · 1.76 KB
/
Copy pathbool_test.go
File metadata and controls
62 lines (55 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package pgtype_test
import (
"context"
"testing"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxtest"
)
func TestBoolCodec(t *testing.T) {
pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, nil, "bool", []pgxtest.ValueRoundTripTest{
{Param: true, Result: new(bool), Test: isExpectedEq(true)},
{Param: false, Result: new(bool), Test: isExpectedEq(false)},
{Param: true, Result: new(pgtype.Bool), Test: isExpectedEq(pgtype.Bool{Bool: true, Valid: true})},
{Param: pgtype.Bool{}, Result: new(pgtype.Bool), Test: isExpectedEq(pgtype.Bool{})},
{Param: nil, Result: new(*bool), Test: isExpectedEq((*bool)(nil))},
})
}
func TestBoolMarshalJSON(t *testing.T) {
successfulTests := []struct {
source pgtype.Bool
result string
}{
{source: pgtype.Bool{}, result: "null"},
{source: pgtype.Bool{Bool: true, Valid: true}, result: "true"},
{source: pgtype.Bool{Bool: false, Valid: true}, result: "false"},
}
for i, tt := range successfulTests {
r, err := tt.source.MarshalJSON()
if err != nil {
t.Errorf("%d: %v", i, err)
}
if string(r) != tt.result {
t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, string(r))
}
}
}
func TestBoolUnmarshalJSON(t *testing.T) {
successfulTests := []struct {
source string
result pgtype.Bool
}{
{source: "null", result: pgtype.Bool{}},
{source: "true", result: pgtype.Bool{Bool: true, Valid: true}},
{source: "false", result: pgtype.Bool{Bool: false, Valid: true}},
}
for i, tt := range successfulTests {
var r pgtype.Bool
err := r.UnmarshalJSON([]byte(tt.source))
if err != nil {
t.Errorf("%d: %v", i, err)
}
if r != tt.result {
t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r)
}
}
}