-
Notifications
You must be signed in to change notification settings - Fork 10
/
string_array_test.go
61 lines (51 loc) · 2.07 KB
/
string_array_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
package pq_types
import (
"database/sql"
"fmt"
. "gopkg.in/check.v1"
)
func (s *TypesSuite) TestStringArray(c *C) {
type testData struct {
a StringArray
b []byte
}
for _, d := range []testData{
{StringArray(nil), []byte(nil)},
{StringArray{}, []byte(`{}`)},
{StringArray{`1234567`}, []byte(`{1234567}`)},
{StringArray{`abc123, def456 xyz789`, `абв`, `世界,`}, []byte(`{"abc123, def456 xyz789",абв,"世界,"}`)},
{StringArray{"", "`", "``", "```", "````"}, []byte("{\"\",`,``,```,````}")},
{StringArray{``, `'`, `''`, `'''`, `''''`}, []byte(`{"",','',''',''''}`)},
{StringArray{``, `"`, `""`, `"""`, `""""`}, []byte(`{"","\"","\"\"","\"\"\"","\"\"\"\""}`)},
{StringArray{``, `,`, `,,`, `,,,`, `,,,,`}, []byte(`{"",",",",,",",,,",",,,,"}`)},
{StringArray{``, `\`, `\\`, `\\\`, `\\\\`}, []byte(`{"","\\","\\\\","\\\\\\","\\\\\\\\"}`)},
{StringArray{``, `{`, `{{`, `}}`, `}`, `{{}}`}, []byte(`{"","{","{{","}}","}","{{}}"}`)},
{StringArray{`\{`, `\\{{`, `\}\}`, `\}}`}, []byte(`{"\\{","\\\\{{","\\}\\}","\\}}"}`)},
{StringArray{`\"'`, `\\"`, `\\\"`, `"\"\\""`}, []byte(`{"\\\"'","\\\\\"","\\\\\\\"","\"\\\"\\\\\"\""}`)},
} {
s.SetUpTest(c)
_, err := s.db.Exec("INSERT INTO pq_types (string_array) VALUES($1)", d.a)
c.Assert(err, IsNil)
b1 := []byte("lalala")
a1 := StringArray{"lalala"}
err = s.db.QueryRow("SELECT string_array, string_array FROM pq_types").Scan(&b1, &a1)
c.Check(err, IsNil)
c.Check(b1, DeepEquals, d.b, Commentf("\nb1 = %#q\nd.b = %#q", b1, d.b))
c.Check(a1, DeepEquals, d.a)
// check db array length
var length sql.NullInt64
err = s.db.QueryRow("SELECT array_length(string_array, 1) FROM pq_types").Scan(&length)
c.Check(err, IsNil)
c.Check(length.Valid, Equals, len(d.a) > 0)
c.Check(length.Int64, Equals, int64(len(d.a)))
// check db array elements
for i := 0; i < len(d.a); i++ {
q := fmt.Sprintf("SELECT string_array[%d] FROM pq_types", i+1)
var el sql.NullString
err = s.db.QueryRow(q).Scan(&el)
c.Check(err, IsNil)
c.Check(el.Valid, Equals, true)
c.Check(el.String, Equals, d.a[i])
}
}
}