forked from asdine/storm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan_test.go
115 lines (85 loc) · 2.57 KB
/
scan_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
package storm
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPrefixScan(t *testing.T) {
db, cleanup := createDB(t)
defer cleanup()
node := db.From("node")
doTestPrefixScan(t, node)
doTestPrefixScan(t, db)
nodeWithTransaction, _ := db.Begin(true)
defer nodeWithTransaction.Commit()
doTestPrefixScan(t, nodeWithTransaction)
}
func doTestPrefixScan(t *testing.T, node Node) {
for i := 1; i < 3; i++ {
n := node.From(fmt.Sprintf("%d%02d", 2015, i))
err := n.Save(&SimpleUser{ID: i, Name: "John"})
assert.NoError(t, err)
}
for i := 1; i < 4; i++ {
n := node.From(fmt.Sprintf("%d%02d", 2016, i))
err := n.Save(&SimpleUser{ID: i, Name: "John"})
assert.NoError(t, err)
}
assert.Len(t, node.PrefixScan("2015"), 2)
assert.Len(t, node.PrefixScan("20"), 5)
buckets2016 := node.PrefixScan("2016")
assert.Len(t, buckets2016, 3)
count, err := buckets2016[1].Count(&SimpleUser{})
assert.NoError(t, err)
assert.Equal(t, 1, count)
assert.NoError(t, buckets2016[1].One("ID", 2, &SimpleUser{}))
}
func TestPrefixScanWithEmptyPrefix(t *testing.T) {
db, cleanup := createDB(t)
defer cleanup()
res := db.PrefixScan("")
require.Len(t, res, 1)
}
func TestPrefixScanSkipValues(t *testing.T) {
db, cleanup := createDB(t)
defer cleanup()
db.Set("a", "2015", 1)
err := db.From("a", "2016").Save(&SimpleUser{ID: 1, Name: "John"})
require.NoError(t, err)
res := db.From("a").PrefixScan("20")
require.Len(t, res, 1)
}
func TestRangeScan(t *testing.T) {
db, cleanup := createDB(t)
defer cleanup()
node := db.From("node")
doTestRangeScan(t, node)
doTestRangeScan(t, db)
nodeWithTransaction, _ := db.Begin(true)
defer nodeWithTransaction.Commit()
doTestRangeScan(t, nodeWithTransaction)
}
func doTestRangeScan(t *testing.T, node Node) {
for y := 2012; y <= 2016; y++ {
for m := 1; m <= 12; m++ {
n := node.From(fmt.Sprintf("%d%02d", y, m))
assert.NoError(t, n.Save(&SimpleUser{ID: m, Name: "John"}))
}
}
assert.Len(t, node.RangeScan("2015", "2016"), 12)
assert.Len(t, node.RangeScan("201201", "201203"), 3)
assert.Len(t, node.RangeScan("2012", "201612"), 60)
assert.Len(t, node.RangeScan("2012", "2017"), 60)
secondIn2015 := node.RangeScan("2015", "2016")[1]
assert.NoError(t, secondIn2015.One("ID", 2, &SimpleUser{}))
}
func TestRangeScanSkipValues(t *testing.T) {
db, cleanup := createDB(t)
defer cleanup()
db.Set("a", "2015", 1)
err := db.From("a", "2016").Save(&SimpleUser{ID: 1, Name: "John"})
require.NoError(t, err)
res := db.From("a").RangeScan("2015", "2018")
require.Len(t, res, 1)
}