-
Notifications
You must be signed in to change notification settings - Fork 0
/
large_objects_test.go
121 lines (102 loc) · 1.97 KB
/
large_objects_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package pgx_test
import (
"io"
"testing"
"github.com/jackc/pgx"
)
func TestLargeObjects(t *testing.T) {
t.Parallel()
conn, err := pgx.Connect(*defaultConnConfig)
if err != nil {
t.Fatal(err)
}
tx, err := conn.Begin()
if err != nil {
t.Fatal(err)
}
lo, err := tx.LargeObjects()
if err != nil {
t.Fatal(err)
}
id, err := lo.Create(0)
if err != nil {
t.Fatal(err)
}
obj, err := lo.Open(id, pgx.LargeObjectModeRead|pgx.LargeObjectModeWrite)
if err != nil {
t.Fatal(err)
}
n, err := obj.Write([]byte("testing"))
if err != nil {
t.Fatal(err)
}
if n != 7 {
t.Errorf("Expected n to be 7, got %d", n)
}
pos, err := obj.Seek(1, 0)
if err != nil {
t.Fatal(err)
}
if pos != 1 {
t.Errorf("Expected pos to be 1, got %d", pos)
}
res := make([]byte, 6)
n, err = obj.Read(res)
if err != nil {
t.Fatal(err)
}
if string(res) != "esting" {
t.Errorf(`Expected res to be "esting", got %q`, res)
}
if n != 6 {
t.Errorf("Expected n to be 6, got %d", n)
}
n, err = obj.Read(res)
if err != io.EOF {
t.Error("Expected io.EOF, go nil")
}
if n != 0 {
t.Errorf("Expected n to be 0, got %d", n)
}
pos, err = obj.Tell()
if err != nil {
t.Fatal(err)
}
if pos != 7 {
t.Errorf("Expected pos to be 7, got %d", pos)
}
err = obj.Truncate(1)
if err != nil {
t.Fatal(err)
}
pos, err = obj.Seek(-1, 2)
if err != nil {
t.Fatal(err)
}
if pos != 0 {
t.Errorf("Expected pos to be 0, got %d", pos)
}
res = make([]byte, 2)
n, err = obj.Read(res)
if err != io.EOF {
t.Errorf("Expected err to be io.EOF, got %v", err)
}
if n != 1 {
t.Errorf("Expected n to be 1, got %d", n)
}
if res[0] != 't' {
t.Errorf("Expected res[0] to be 't', got %v", res[0])
}
err = obj.Close()
if err != nil {
t.Fatal(err)
}
err = lo.Unlink(id)
if err != nil {
t.Fatal(err)
}
_, err = lo.Open(id, pgx.LargeObjectModeRead)
if e, ok := err.(pgx.PgError); !ok || e.Code != "42704" {
t.Errorf("Expected undefined_object error (42704), got %#v", err)
}
}