forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore_test.go
160 lines (130 loc) · 3.83 KB
/
store_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package tests
import (
"testing"
ravendb "github.com/ravendb/ravendb-go-client"
"github.com/stretchr/testify/assert"
)
func storeTestRefreshTest(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
{
session := openSessionMust(t, store)
user := &User{}
user.setName("RavenDB")
err = session.StoreWithID(user, "users/1")
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
{
innerSession := openSessionMust(t, store)
var innerUser *User
err = innerSession.Load(&innerUser, "users/1")
assert.NoError(t, err)
innerUser.setName("RavenDB 4.0")
err = innerSession.SaveChanges()
assert.NoError(t, err)
}
err = session.Advanced().Refresh(user)
assert.NoError(t, err)
name := *user.Name
assert.Equal(t, name, "RavenDB 4.0")
session.Close()
}
}
func storeTestStoreDocument(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
{
session := openSessionMust(t, store)
user := &User{}
user.setName("RavenDB")
err = session.StoreWithID(user, "users/1")
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
user = nil
err = session.Load(&user, "users/1")
assert.NoError(t, err)
assert.NotNil(t, user)
name := *user.Name
assert.Equal(t, name, "RavenDB")
session.Close()
}
}
func storeTestStoreDocuments(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
{
session := openSessionMust(t, store)
user1 := &User{}
user1.setName("RavenDB")
err = session.StoreWithID(user1, "users/1")
assert.NoError(t, err)
user2 := &User{}
user2.setName("Hibernating Rhinos")
err = session.StoreWithID(user2, "users/2")
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
users := map[string]*User{}
err = session.LoadMulti(users, []string{"users/1", "users/2"})
assert.NoError(t, err)
assert.Equal(t, len(users), 2)
session.Close()
}
}
func storeTestNotifyAfterStore(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
storeLevelCallBack := []*ravendb.MetadataAsDictionary{nil}
sessionLevelCallback := []*ravendb.MetadataAsDictionary{nil}
fn := func(event *ravendb.AfterSaveChangesEventArgs) {
storeLevelCallBack[0] = event.GetDocumentMetadata()
}
store.AddAfterSaveChangesListener(fn)
{
session := openSessionMust(t, store)
fn := func(event *ravendb.AfterSaveChangesEventArgs) {
sessionLevelCallback[0] = event.GetDocumentMetadata()
}
session.Advanced().AddAfterSaveChangesListener(fn)
user1 := &User{}
user1.setName("RavenDB")
err = session.StoreWithID(user1, "users/1")
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
isLoaded := session.Advanced().IsLoaded("users/1")
assert.True(t, isLoaded)
changeVector, err := session.Advanced().GetChangeVectorFor(user1)
assert.NoError(t, err)
assert.NotNil(t, changeVector)
lastModified, err := session.Advanced().GetLastModifiedFor(user1)
assert.NoError(t, err)
assert.NotNil(t, lastModified)
session.Close()
}
assert.NotNil(t, storeLevelCallBack[0])
assert.Equal(t, storeLevelCallBack[0], sessionLevelCallback[0])
assert.NotNil(t, sessionLevelCallback[0])
iMetadataDictionary := sessionLevelCallback[0]
entrySet := iMetadataDictionary.EntrySet()
for key, value := range entrySet {
assert.NotEqual(t, key, "")
assert.NotNil(t, value)
}
}
func TestStore(t *testing.T) {
driver := createTestDriver(t)
destroy := func() { destroyDriver(t, driver) }
defer recoverTest(t, destroy)
// matches order of java tests
storeTestRefreshTest(t, driver)
storeTestStoreDocument(t, driver)
storeTestStoreDocuments(t, driver)
storeTestNotifyAfterStore(t, driver)
}