-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathint64_array_test.go
More file actions
59 lines (51 loc) · 1.75 KB
/
int64_array_test.go
File metadata and controls
59 lines (51 loc) · 1.75 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
package pq_types
import (
"database/sql"
"fmt"
. "gopkg.in/check.v1"
)
func (s *TypesSuite) TestInt64Array(c *C) {
type testData struct {
a Int64Array
b []byte
}
for _, d := range []testData{
{Int64Array(nil), []byte(nil)},
{Int64Array{}, []byte(`{}`)},
{Int64Array{1}, []byte(`{1}`)},
{Int64Array{1, 0, -3}, []byte(`{1,0,-3}`)},
{Int64Array{-3, 0, 1}, []byte(`{-3,0,1}`)},
} {
s.SetUpTest(c)
_, err := s.db.Exec("INSERT INTO pq_types (int64_array) VALUES($1)", d.a)
c.Assert(err, IsNil)
b1 := []byte("42")
a1 := Int64Array{42}
err = s.db.QueryRow("SELECT int64_array, int64_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(int64_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 int64_array[%d] FROM pq_types", i+1)
var el sql.NullInt64
err = s.db.QueryRow(q).Scan(&el)
c.Check(err, IsNil)
c.Check(el.Valid, Equals, true)
c.Check(el.Int64, Equals, int64(d.a[i]))
}
}
}
func (s *TypesSuite) TestInt64ArrayEqualWithoutOrder(c *C) {
c.Check(Int64Array{1, 0, -3}.EqualWithoutOrder(Int64Array{-3, 0, 1}), Equals, true)
c.Check(Int64Array{1, 0, -3}.EqualWithoutOrder(Int64Array{1}), Equals, false)
c.Check(Int64Array{1, 0, -3}.EqualWithoutOrder(Int64Array{1, 0, 42}), Equals, false)
c.Check(Int64Array{}.EqualWithoutOrder(Int64Array{}), Equals, true)
c.Check(Int64Array{}.EqualWithoutOrder(Int64Array{1}), Equals, false)
}