Skip to content

Commit 3544fbd

Browse files
authored
Fix Value method for NullRawMessage (#2)
* Fix Value method for NullRawMessage Update all tests to exercise the Value method
1 parent 56b5672 commit 3544fbd

File tree

5 files changed

+37
-1
lines changed

5 files changed

+37
-1
lines changed

json.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ func (n NullRawMessage) Value() (driver.Value, error) {
3636
if !n.Valid {
3737
return nil, nil
3838
}
39-
return n.RawMessage, nil
39+
return []byte(n.RawMessage), nil
4040
}

tests/inet_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ func TestInet(t *testing.T) {
3535
if diff := cmp.Diff(addr, ip.IPNet.String()); diff != "" {
3636
t.Errorf("IPNet mismatch (-want +got):\n%s", diff)
3737
}
38+
if _, err := db.Exec(`SELECT abbrev($1)`, ip); err != nil {
39+
t.Fatal(err)
40+
}
3841
})
3942
}
4043
t.Run("NULL", func(t *testing.T) {
@@ -45,5 +48,8 @@ func TestInet(t *testing.T) {
4548
if diff := cmp.Diff(false, ip.Valid); diff != "" {
4649
t.Errorf("valid mismatch (-want +got):\n%s", diff)
4750
}
51+
if _, err := db.Exec(`SELECT abbrev($1)`, ip); err != nil {
52+
t.Fatal(err)
53+
}
4854
})
4955
}

tests/json_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ func TestJSONRawMessage(t *testing.T) {
3232
if diff := cmp.Diff(string(json.RawMessage(payload)), string(n.RawMessage)); diff != "" {
3333
t.Errorf("json mismatch (-want +got):\n%s", diff)
3434
}
35+
if _, err := db.Exec(`SELECT json_typeof($1)`, n); err != nil {
36+
t.Fatal(err)
37+
}
3538
if err := db.QueryRow(fmt.Sprintf(`SELECT '%s'::jsonb`, payload)).Scan(&n); err != nil {
3639
t.Fatal(err)
3740
}
@@ -41,6 +44,9 @@ func TestJSONRawMessage(t *testing.T) {
4144
if diff := cmp.Diff(string(json.RawMessage(payload)), string(n.RawMessage)); diff != "" {
4245
t.Errorf("jsonb mismatch (-want +got):\n%s", diff)
4346
}
47+
if _, err := db.Exec(`SELECT jsonb_typeof($1)`, n); err != nil {
48+
t.Fatal(err)
49+
}
4450
})
4551
t.Run("/stdlib/"+payload, func(t *testing.T) {
4652
var n json.RawMessage
@@ -50,12 +56,18 @@ func TestJSONRawMessage(t *testing.T) {
5056
if diff := cmp.Diff(string(json.RawMessage(payload)), string(n)); diff != "" {
5157
t.Errorf("json mismatch (-want +got):\n%s", diff)
5258
}
59+
if _, err := db.Exec(`SELECT json_typeof($1)`, n); err != nil {
60+
t.Fatal(err)
61+
}
5362
if err := db.QueryRow(fmt.Sprintf(`SELECT '%s'::jsonb`, payload)).Scan(&n); err != nil {
5463
t.Fatal(err)
5564
}
5665
if diff := cmp.Diff(string(json.RawMessage(payload)), string(n)); diff != "" {
5766
t.Errorf("jsonb mismatch (-want +got):\n%s", diff)
5867
}
68+
if _, err := db.Exec(`SELECT jsonb_typeof($1)`, n); err != nil {
69+
t.Fatal(err)
70+
}
5971
})
6072
}
6173
t.Run("NULL", func(t *testing.T) {
@@ -66,11 +78,17 @@ func TestJSONRawMessage(t *testing.T) {
6678
if diff := cmp.Diff(false, n.Valid); diff != "" {
6779
t.Errorf("valid mismatch (-want +got):\n%s", diff)
6880
}
81+
if _, err := db.Exec(`SELECT json_typeof($1)`, n); err != nil {
82+
t.Fatal(err)
83+
}
6984
if err := db.QueryRow(`SELECT NULL::jsonb`).Scan(&n); err != nil {
7085
t.Fatal(err)
7186
}
7287
if diff := cmp.Diff(false, n.Valid); diff != "" {
7388
t.Errorf("valid mismatch (-want +got):\n%s", diff)
7489
}
90+
if _, err := db.Exec(`SELECT jsonb_typeof($1)`, n); err != nil {
91+
t.Fatal(err)
92+
}
7593
})
7694
}

tests/macaddr8_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ func TestMacaddr8(t *testing.T) {
6161
if diff := cmp.Diff(addr.output, cidr.Addr.String()); diff != "" {
6262
t.Errorf("mismatch (-want +got):\n%s", diff)
6363
}
64+
if _, err := db.Exec(`SELECT trunc($1::macaddr8)`, cidr); err != nil {
65+
t.Fatal(err)
66+
}
6467
})
6568
}
6669
t.Run("NULL", func(t *testing.T) {
@@ -71,5 +74,8 @@ func TestMacaddr8(t *testing.T) {
7174
if diff := cmp.Diff(false, cidr.Valid); diff != "" {
7275
t.Errorf("valid mismatch (-want +got):\n%s", diff)
7376
}
77+
if _, err := db.Exec(`SELECT trunc($1::macaddr8)`, cidr); err != nil {
78+
t.Fatal(err)
79+
}
7480
})
7581
}

tests/macaddr_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ func TestMacaddr(t *testing.T) {
5555
if diff := cmp.Diff(addr.output, cidr.Addr.String()); diff != "" {
5656
t.Errorf("mismatch (-want +got):\n%s", diff)
5757
}
58+
if _, err := db.Exec(`SELECT trunc($1::macaddr)`, cidr); err != nil {
59+
t.Fatal(err)
60+
}
5861
})
5962
}
6063
t.Run("NULL", func(t *testing.T) {
@@ -65,5 +68,8 @@ func TestMacaddr(t *testing.T) {
6568
if diff := cmp.Diff(false, cidr.Valid); diff != "" {
6669
t.Errorf("valid mismatch (-want +got):\n%s", diff)
6770
}
71+
if _, err := db.Exec(`SELECT trunc($1::macaddr)`, cidr); err != nil {
72+
t.Fatal(err)
73+
}
6874
})
6975
}

0 commit comments

Comments
 (0)