-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmemory_adapter_test.go
More file actions
107 lines (89 loc) · 3.24 KB
/
memory_adapter_test.go
File metadata and controls
107 lines (89 loc) · 3.24 KB
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
package store
import (
"os"
"github.com/moleculer-go/cupaloy/v2"
"github.com/moleculer-go/moleculer"
"github.com/moleculer-go/moleculer/payload"
"github.com/moleculer-go/store/mocks"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var snap = cupaloy.New(cupaloy.FailOnUpdate(os.Getenv("UPDATE_SNAPSHOTS") == "true"))
var _ = Describe("MemoryAdapter", func() {
adapter := &MemoryAdapter{
Table: "user",
SearchFields: []string{"name"},
}
var johnSnow, johnTravolta moleculer.Payload
BeforeEach(func() {
johnSnow, _, johnTravolta = mocks.ConnectAndLoadUsers(adapter)
})
AfterEach(func() {
johnTravolta = nil
adapter.Disconnect()
})
It("Find() should return matching records", func() {
r := adapter.Find(payload.New(map[string]interface{}{
"searchFields": []string{"name"},
"search": "John",
}))
Expect(r.Error()).Should(BeNil())
Expect(r.Len()).Should(Equal(2))
Expect(snap.SnapshotMulti("Find()", r.Remove("id", "friends", "master").Sort("lastname"))).Should(Succeed())
})
It("FindById() should return one matching records by ID", func() {
r := adapter.FindById(johnSnow.Get("id"))
Expect(r.Error()).Should(BeNil())
Expect(snap.SnapshotMulti("FindById()", r.Remove("id", "friends"))).Should(Succeed())
})
It("FindByIds() should return one matching records by ID", func() {
r := adapter.FindByIds(payload.EmptyList().AddItem(johnSnow.Get("id")).AddItem(johnTravolta.Get("id")))
Expect(r.Error()).Should(BeNil())
Expect(r.Len()).Should(Equal(2))
Expect(snap.SnapshotMulti("FindByIds()", r.Remove("id", "friends", "master"))).Should(Succeed())
})
It("Count() should return matching records", func() {
r := adapter.Count(payload.New(map[string]interface{}{
"searchFields": []string{"name"},
"search": "John",
}))
Expect(r.Error()).Should(BeNil())
Expect(r.Int()).Should(Equal(2))
Expect(snap.SnapshotMulti("Count()", r)).Should(Succeed())
})
It("Update() should update existing record matching records", func() {
r := adapter.Update(payload.New(map[string]interface{}{
"id": johnTravolta.Get("id").String(),
"age": 67,
}))
Expect(r.Error()).Should(BeNil())
Expect(r.Get("name").String()).Should(Equal("John"))
Expect(r.Get("lastname").String()).Should(Equal("Travolta"))
Expect(r.Get("age").Int()).Should(Equal(67))
})
It("Insert() should insert new records", func() {
r := adapter.Insert(payload.New(map[string]interface{}{
"name": "Julio",
"lastname": "Cesar",
}))
Expect(r.Error()).Should(BeNil())
Expect(r.Get("name").String()).Should(Equal("Julio"))
Expect(r.Get("lastname").String()).Should(Equal("Cesar"))
r = adapter.Find(payload.New(map[string]interface{}{
"searchFields": []string{"name"},
"search": "Julio",
}))
Expect(r.Error()).Should(BeNil())
Expect(r.Len()).Should(Equal(1))
Expect(snap.SnapshotMulti("Insert()", r.Remove("id"))).Should(Succeed())
})
It("RemoveAll() should remove all records and return total of removed items", func() {
total := adapter.Count(payload.Empty())
Expect(total.Int()).Should(Equal(6))
count := adapter.RemoveAll()
Expect(count.Error()).Should(BeNil())
Expect(count.Int()).Should(Equal(6))
total = adapter.Count(payload.Empty())
Expect(total.Int()).Should(Equal(0))
})
})