-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontenttype_test.go
98 lines (79 loc) · 2.29 KB
/
contenttype_test.go
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package mysqlx
import (
"context"
"encoding/json"
"encoding/xml"
"testing"
)
func TestSendJSON(t *testing.T) {
db := NewDB(t)
defer db.Close()
type Thing struct {
Name string `json:"name,omitempty"`
Number int `json:"number,omitempty"`
}
expected := &Thing{
Name: "Alice",
Number: 42,
}
j, err := json.Marshal(expected)
assertNoError(t, err)
var b []byte
var actual Thing
r, err := db.ExecContext(context.Background(), "INSERT INTO json(json) VALUES(?)", JSON(j))
assertNoError(t, err)
n, err := r.RowsAffected()
assertNoError(t, err)
assertComparableEqual(t, int64(1), n)
id, err := r.LastInsertId()
assertNoError(t, err)
assertNoError(t, db.QueryRowContext(context.Background(), "SELECT json FROM json WHERE id = ?", id).Scan(&b))
assertNoError(t, json.Unmarshal(b, &actual))
assertComparableEqual(t, expected.Name, actual.Name)
assertComparableEqual(t, expected.Number, actual.Number)
}
func TestSendJSONNull(t *testing.T) {
var b []byte
db := NewDB(t)
defer db.Close()
r, err := db.ExecContext(context.Background(), "INSERT INTO json(json) VALUES(?)", Null())
assertNoError(t, err)
n, err := r.RowsAffected()
assertNoError(t, err)
assertComparableEqual(t, int64(1), n)
id, err := r.LastInsertId()
assertNoError(t, err)
assertNoError(t, db.QueryRowContext(context.Background(), "SELECT json FROM json WHERE id = ?", id).Scan(&b))
if !isNil(b) {
t.Fatalf("expected nil, got %T(%v)", b, b)
}
}
func TestSendXML(t *testing.T) {
db := NewDB(t)
defer db.Close()
type Thing struct {
XMLName xml.Name
Name string `xml:"name"`
Number int `xml:"number"`
}
expected := &Thing{
XMLName: xml.Name{Local: "thing"},
Name: "Alice",
Number: 42,
}
var b []byte
var actual Thing
x, err := xml.Marshal(expected)
assertNoError(t, err)
r, err := db.ExecContext(context.Background(), "INSERT INTO xml(xml) VALUES(?)", XML(x))
assertNoError(t, err)
n, err := r.RowsAffected()
assertNoError(t, err)
assertComparableEqual(t, int64(1), n)
id, err := r.LastInsertId()
assertNoError(t, err)
assertNoError(t, db.QueryRowContext(context.Background(), "SELECT xml FROM xml WHERE id = ?", id).Scan(&b))
assertNoError(t, xml.Unmarshal(b, &actual))
assertComparableEqual(t, expected.Name, actual.Name)
assertComparableEqual(t, expected.Number, actual.Number)
}