-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable_test.go
137 lines (126 loc) · 3.19 KB
/
table_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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"bytes"
"fmt"
"github.com/stretchr/testify/assert"
"os"
"sort"
"testing"
)
func TestInsertAndSelect(t *testing.T) {
cleanup()
table, err := OpenDB(Options{DBPath: "db.sqlite"})
defer cleanup()
assert.Nil(t, err)
assert.Nil(t, err)
for i := int32(0); i < RowsPerPage; i++ {
name := fmt.Sprintf("name-{%d}", i)
email := fmt.Sprintf("%[email protected]", name)
var a [32]byte
copy(a[:], name)
var b [256]byte
copy(b[:], email)
err := table.InsertRow(Row{
ID: i,
Name: a,
Email: b,
})
assert.Equal(t, nil, err)
}
rows, err := table.SelectAll()
assert.Equal(t, nil, err)
assert.EqualValues(t, RowsPerPage, len(rows))
for i := int32(0); i < RowsPerPage; i++ {
name := fmt.Sprintf("name-{%d}", i)
email := fmt.Sprintf("%[email protected]", name)
var a [32]byte
copy(a[:], name)
var b [256]byte
copy(b[:], email)
assert.EqualValues(t, i, rows[i].ID)
assert.EqualValues(t, name, string(bytes.Trim(rows[i].Name[:], "\x00")))
assert.Equal(t, email, string(bytes.Trim(rows[i].Email[:], "\x00")))
}
}
func TestInsertAndSelectInOrder(t *testing.T) {
cleanup()
table, err := OpenDB(Options{DBPath: "db.sqlite"})
defer cleanup()
assert.Nil(t, err)
idxSlice := []int32{2, 10, 11, 3, 5, 7, 1, 4, 8, 6, 9, 0, 15, 14, 1000, 12, 10000, 9000, 8000, 7000, 6000, 5000, 4000}
for _, idx := range idxSlice{
name := fmt.Sprintf("name-{%d}", idx)
email := fmt.Sprintf("%[email protected]", name)
var a [32]byte
copy(a[:], name)
var b [256]byte
copy(b[:], email)
err := table.InsertRow(Row{
ID: idx,
Name: a,
Email: b,
})
assert.Nil(t, err)
}
rows, err := table.SelectAll()
assert.Nil(t, err)
sort.Slice(idxSlice, func(i, j int) bool {
return idxSlice[i] < idxSlice[j]
})
for idx, row := range rows {
id := idxSlice[idx]
assert.EqualValues(t, id, row.ID)
name := fmt.Sprintf("name-{%d}", id)
email := fmt.Sprintf("%[email protected]", name)
var a [32]byte
copy(a[:], name)
var b [256]byte
copy(b[:], email)
assert.EqualValues(t, a, row.Name)
assert.EqualValues(t, b, row.Email)
}
}
func TestPersistence(t *testing.T) {
cleanup()
table, err := OpenDB(Options{DBPath: "db.sqlite"})
assert.Nil(t, err)
defer cleanup()
idxSlice := []int32{2, 10, 11, 3, 5, 7, 1, 4, 8, 6, 9, 0, 15, 14, 1000, 12, 10000, 9000, 8000, 7000, 6000, 4000, 3000, 2000}
for _, idx := range idxSlice{
name := fmt.Sprintf("name-{%d}", idx)
email := fmt.Sprintf("%[email protected]", name)
var a [32]byte
copy(a[:], name)
var b [256]byte
copy(b[:], email)
err := table.InsertRow(Row{
ID: idx,
Name: a,
Email: b,
})
assert.Nil(t, err)
}
assert.Nil(t, table.Close())
table, err = OpenDB(Options{DBPath: "db.sqlite"})
assert.Nil(t, err)
rows, err := table.SelectAll()
assert.Nil(t, err)
sort.Slice(idxSlice, func(i, j int) bool {
return idxSlice[i] < idxSlice[j]
})
for idx, row := range rows {
id := idxSlice[idx]
assert.EqualValues(t, id, row.ID)
name := fmt.Sprintf("name-{%d}", id)
email := fmt.Sprintf("%[email protected]", name)
var a [32]byte
copy(a[:], name)
var b [256]byte
copy(b[:], email)
assert.EqualValues(t, a, row.Name)
assert.EqualValues(t, b, row.Email)
}
}
func cleanup() {
os.Remove("db.sqlite")
}