-
Notifications
You must be signed in to change notification settings - Fork 1
/
export_test.go
140 lines (124 loc) · 2.82 KB
/
export_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
138
139
140
package bstore
import (
"fmt"
"os"
"testing"
"time"
)
func TestExport(t *testing.T) {
type User1 struct {
ID int
Name string
}
type User2 struct {
ID int
String string
Time time.Time
Bool bool
Boolptr *bool
Uint uint64
Bytes []byte
Struct struct {
Value int
}
Slice []string
Slice2 []struct {
Name string
}
Map map[string]int
Map2 map[string]struct{ Name string }
Float32 float32
Float64 float64
Coordinates [2]float32
BM bm
Data [4]byte
}
const path = "testdata/tmp.export.db"
os.Remove(path)
db, err := topen(t, path, nil, User1{}, User2{})
tcheck(t, err, "open")
defer db.Close()
var ids2 []int
err = db.Write(ctxbg, func(tx *Tx) error {
u0 := User1{0, "hi"}
err = tx.Insert(&u0)
tcheck(t, err, "insert")
u1 := User1{0, "hi"}
err = tx.Insert(&u1)
tcheck(t, err, "insert")
u2 := User2{}
err = tx.Insert(&u2)
tcheck(t, err, "insert zero")
xfalse := false
u3 := User2{
0,
"test",
time.Now(),
true,
&xfalse,
123,
[]byte("hi"),
struct{ Value int }{1},
[]string{"a", "b"},
[]struct{ Name string }{{"x"}, {""}},
map[string]int{"a": 123, "b": 0},
map[string]struct{ Name string }{"x": {"y"}, "y": {""}},
1.23,
-100.34,
[2]float32{6.062495, 53.063354},
bm{"test"},
[...]byte{255, 0, 1, 0},
}
err = tx.Insert(&u3)
tcheck(t, err, "insert nonzero")
u4 := User2{
0,
"test",
time.Now(),
true,
nil,
123,
[]byte{},
struct{ Value int }{0},
[]string{},
[]struct{ Name string }{},
map[string]int{},
map[string]struct{ Name string }{},
33.44,
101,
[2]float32{0, 0},
bm{""},
[...]byte{0, 0, 0, 0},
}
err = tx.Insert(&u4)
tcheck(t, err, "insert different nonzero")
ids2 = []int{u2.ID, u3.ID, u4.ID}
return nil
})
tcheck(t, err, "write")
err = db.Read(ctxbg, func(tx *Tx) error {
var xids2 []int
err = tx.Keys("User2", func(id any) error {
xids2 = append(xids2, id.(int))
return nil
})
tcompare(t, err, xids2, ids2, "keys")
var fields []string
expFields := []string{"ID", "String", "Time", "Bool", "Boolptr", "Uint", "Bytes", "Struct", "Slice", "Slice2", "Map", "Map2", "Float32", "Float64", "Coordinates", "BM", "Data"}
xids2 = nil
err = tx.Records("User2", &fields, func(v map[string]any) error {
xids2 = append(xids2, v["ID"].(int))
return nil
})
tcompare(t, err, xids2, ids2, "record ids")
tcompare(t, err, expFields, fields, "records fields")
fields = nil
record, err := tx.Record("User2", fmt.Sprintf("%d", ids2[0]), &fields)
tcompare(t, err, expFields, fields, "record fields")
tcompare(t, err, record["ID"].(int), ids2[0], "record id")
_, err = tx.Record("User2", "999", &fields)
tneed(t, err, ErrAbsent, "Record for unknown key")
return nil
})
tcheck(t, err, "tx")
}