forked from ZbigniewTomanek/pgvertica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cursor_test.go
91 lines (69 loc) · 2.27 KB
/
cursor_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
package pgvertica
import (
"errors"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/assert"
)
func TestOpenCursor(t *testing.T) {
db, mock, _ := sqlmock.New()
defer db.Close()
query := "SELECT * FROM test"
mock.ExpectPrepare("SELECT")
mock.ExpectQuery("SELECT").WillReturnRows(sqlmock.NewRows([]string{"col1"}).AddRow("val1"))
cursor := newCursor("test", query, TEXT)
err := cursor.open(db)
assert.NoError(t, err)
assert.NotNil(t, cursor.rows)
assert.NotNil(t, cursor.columnTypes)
assert.NoError(t, mock.ExpectationsWereMet())
}
func TestOpenCursorError(t *testing.T) {
db, mock, _ := sqlmock.New()
defer db.Close()
mock.ExpectPrepare("SELECT").WillReturnError(errors.New("prepare error"))
cursor := newCursor("test", "SELECT * FROM test", TEXT)
err := cursor.open(db)
assert.Error(t, err)
assert.Nil(t, cursor.rows)
assert.Nil(t, cursor.columnTypes)
assert.NoError(t, mock.ExpectationsWereMet())
}
func TestCloseCursor(t *testing.T) {
db, mock, _ := sqlmock.New()
defer db.Close()
rows := sqlmock.NewRows([]string{"col1"}).AddRow("val1")
mock.ExpectPrepare("SELECT")
mock.ExpectQuery("SELECT").WillReturnRows(rows).RowsWillBeClosed()
cursor := newCursor("test", "SELECT * FROM test", TEXT)
_ = cursor.open(db)
err := cursor.close()
assert.NoError(t, err)
assert.NoError(t, mock.ExpectationsWereMet())
}
func TestFetchCursor(t *testing.T) {
db, mock, _ := sqlmock.New()
defer db.Close()
rows := sqlmock.NewRows([]string{"col1"}).AddRow("val1")
mock.ExpectPrepare("SELECT")
mock.ExpectQuery("SELECT").WillReturnRows(rows)
cursor := newCursor("test", "SELECT * FROM test", TEXT)
_ = cursor.open(db)
messages, err := cursor.fetch(1)
assert.NoError(t, err)
assert.Len(t, messages, 2) // 1 row description + 1 data row
assert.NoError(t, mock.ExpectationsWereMet())
}
func TestFetchCursorError(t *testing.T) {
db, mock, _ := sqlmock.New()
defer db.Close()
rows := sqlmock.NewRows([]string{"col1"}).AddRow("val1")
mock.ExpectPrepare("SELECT")
mock.ExpectQuery("SELECT").WillReturnRows(rows)
cursor := newCursor("test", "SELECT * FROM test", BINARY) // Binary will cause an error
_ = cursor.open(db)
messages, err := cursor.fetch(1)
assert.Error(t, err)
assert.Nil(t, messages)
assert.NoError(t, mock.ExpectationsWereMet())
}