-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapper_test.go
116 lines (96 loc) · 2.82 KB
/
mapper_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
package firestorm_test
import (
"reflect"
"sync"
"cloud.google.com/go/datastore"
"github.com/phogolabs/firestorm"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("IndexMapper", func() {
var mapper *firestorm.IndexMapper
BeforeEach(func() {
mapper = &firestorm.IndexMapper{
Mutex: &sync.Mutex{},
Cache: make(map[reflect.Type]*firestorm.IndexTree),
}
})
Describe("Tree", func() {
It("returns the index tree for given type", func() {
maptree := mapper.Tree(reflect.TypeOf(Entity{}))
Expect(maptree).NotTo(BeNil())
list := *maptree
Expect(list).To(HaveLen(1))
Expect(list[0].Name).To(Equal("email"))
Expect(list[0].Properties).To(Equal([][]int{[]int{3}}))
})
Context("when the type is pointer to struct", func() {
It("returns the index tree for given type", func() {
maptree := mapper.Tree(reflect.TypeOf(&Entity{}))
Expect(maptree).NotTo(BeNil())
list := *maptree
Expect(list).To(HaveLen(1))
Expect(list[0].Name).To(Equal("email"))
Expect(list[0].Properties).To(Equal([][]int{[]int{3}}))
})
})
Context("when the tree is cached", func() {
BeforeEach(func() {
mapper.Cache[reflect.TypeOf(Entity{})] = &firestorm.IndexTree{
{Name: "random"},
}
})
It("returns the index tree for given type", func() {
maptree := mapper.Tree(reflect.TypeOf(&Entity{}))
Expect(maptree).NotTo(BeNil())
list := *maptree
Expect(list).To(HaveLen(1))
Expect(list[0].Name).To(Equal("random"))
})
})
Context("when the type is not struct", func() {
It("returns an empty tree", func() {
maptree := mapper.Tree(reflect.TypeOf(0))
Expect(maptree).To(BeNil())
})
})
})
})
var _ = Describe("IndexTree", func() {
var maptree *firestorm.IndexTree
BeforeEach(func() {
mapper := &firestorm.IndexMapper{
Mutex: &sync.Mutex{},
Cache: make(map[reflect.Type]*firestorm.IndexTree),
}
maptree = mapper.Tree(reflect.TypeOf(&Entity{}))
})
Describe("Keys", func() {
It("returns the keys", func() {
entity := &Entity{
ID: datastore.NameKey("entity", "007", nil),
FirstName: "John",
LastName: "Doe",
Email: "[email protected]",
}
keys, err := maptree.Keys(entity.ID, reflect.ValueOf(entity))
Expect(err).To(BeNil())
Expect(keys).To(HaveLen(1))
Expect(keys[0].Key.Name).To(Equal("14491862341308332741"))
Expect(keys[0].Key.Kind).To(Equal("entity_email_index"))
Expect(keys[0].Hash).To(Equal(uint64(14491862341308332741)))
})
Context("when the key is nil", func() {
It("returns an error", func() {
entity := &Entity{
FirstName: "John",
LastName: "Doe",
Email: "[email protected]",
}
keys, err := maptree.Keys(entity.ID, reflect.ValueOf(entity))
Expect(err).To(MatchError("datastore: invalid key"))
Expect(keys).To(BeNil())
})
})
})
})