/
namespace_range_test.go
142 lines (127 loc) · 3.39 KB
/
namespace_range_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
141
package main
import (
"fmt"
"testing"
"math/big"
"golang.org/x/net/context"
"google.golang.org/appengine"
"google.golang.org/appengine/aetest"
"google.golang.org/appengine/datastore"
)
type (
// a test model used to populate namespaces
pet struct {
Value int64 `datastore:"value"`
}
)
func createInNamespace(c context.Context, namespace string) {
ns, _ := appengine.Namespace(c, namespace)
p := new(pet)
p.Value = 1
k := datastore.NewKey(ns, "pet", "", 1, nil)
datastore.Put(ns, k, p)
}
func TestOrdinalization(t *testing.T) {
setupConstants("ab", 2, 0)
tests := []struct {
ordinal *big.Int
namespace string
} {
{big.NewInt(0), ""},
{big.NewInt(1), "a"},
{big.NewInt(2), "aa"},
{big.NewInt(3), "ab"},
{big.NewInt(4), "b"},
{big.NewInt(5), "ba"},
{big.NewInt(6), "bb"},
}
for i, test := range tests {
if ns := ordToNamespace(test.ordinal, 0); ns != test.namespace {
t.Errorf("%d ordToNamespace %s failed - expected %s got %s", i, test.ordinal, test.namespace, ns)
}
if ord := namespaceToOrd(test.namespace); ord.Cmp(test.ordinal) != 0 {
t.Errorf("%d namespaceToOrd %s failed - expected %s got %s", i, test.namespace, test.ordinal, ord)
}
}
}
func TestNamespaceRangeIteration(t *testing.T) {
setupConstants("abc", 3, 3)
}
func TestNamespaceRangeSplit(t *testing.T) {
setupConstants("abc", 3, 3)
}
func TestNamespaceSplit(t *testing.T) {
c, done, err := aetest.NewContext()
if err != nil {
t.Fatal(err)
}
defer done()
for _, x := range "cba" {
for _, y := range "abc" {
for _, z := range "bac" {
v := fmt.Sprintf("%c%c%c", x, y, z)
createInNamespace(c, v)
}
}
}
tests := []struct {
name string
count int
contiguous bool
canQuery bool
ranges []NamespaceRange
} {
{"testSplitWithoutQueries", 4, false, false, []NamespaceRange{
{Start:"", End:"abc"},
{Start:"ac", End:"bb"},
{Start:"bba", End:"caa"},
{Start:"cab", End:"ccc"},
},
},
{"testSplitWithoutQueriesWithContiguous", 4, true, false, []NamespaceRange{
{Start:"", End:"abc"},
{Start:"ac", End:"bb"},
{Start:"bba", End:"caa"},
{Start:"cab", End:"ccc"},
},
},
}
for _, test := range tests {
results, _ := namespaceSplit(c, test.count, test.contiguous, test.canQuery)
if len(results) != len(test.ranges) {
t.Fatalf("%s expected %d ranges got %d", test.name, len(test.ranges), len(results))
}
for i, r := range results {
if r.Start != test.ranges[i].Start || r.End != test.ranges[i].End {
t.Fatalf("%s expected range %d %s-%s got %s-%s", test.name, i, test.ranges[i].Start, test.ranges[i].End, r.Start, r.End)
}
}
}
}
func TestNone(t *testing.T) {
c, done, err := aetest.NewContext()
if err != nil {
t.Fatal(err)
}
defer done()
tests := []struct {
name string
count int
contiguous bool
canQuery bool
ranges []NamespaceRange
} {
{"testSplitWithNoNamespacesInDatastore", 10, false, true, []NamespaceRange{},},
}
for _, test := range tests {
results, _ := namespaceSplit(c, test.count, test.contiguous, test.canQuery)
if len(results) != len(test.ranges) {
t.Fatalf("%s expected %d ranges got %d", test.name, len(test.ranges), len(results))
}
for i, r := range results {
if r.Start != test.ranges[i].Start || r.End != test.ranges[i].End {
t.Fatalf("%s expected range %d %s-%s got %s-%s", test.name, i, test.ranges[i].Start, test.ranges[i].End, r.Start, r.End)
}
}
}
}