forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete_by_query_test.go
127 lines (104 loc) · 2.79 KB
/
delete_by_query_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
package tests
import (
"reflect"
"testing"
"time"
"github.com/stretchr/testify/require"
ravendb "github.com/ravendb/ravendb-go-client"
"github.com/stretchr/testify/assert"
)
func deleteByQueryCanDeleteByQuery(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
{
session := openSessionMust(t, store)
user1 := &User{
Age: 5,
}
err = session.Store(user1)
assert.NoError(t, err)
user2 := &User{
Age: 10,
}
err = session.Store(user2)
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
session.Close()
}
{
indexQuery := ravendb.NewIndexQuery("from users where age == 5")
operation, err := ravendb.NewDeleteByQueryOperation(indexQuery, nil)
assert.NoError(t, err)
asyncOp, err := store.Operations().SendAsync(operation, nil)
assert.NoError(t, err)
err = asyncOp.WaitForCompletion()
assert.NoError(t, err)
{
session := openSessionMust(t, store)
q := session.QueryCollectionForType(reflect.TypeOf(&User{}))
count, err := q.Count()
assert.NoError(t, err)
assert.Equal(t, count, 1)
session.Close()
}
}
}
func deleteByQueryCanDeleteByQueryWaitUsingChanges(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
{
session := openSessionMust(t, store)
user1 := &User{
Age: 5,
}
err = session.Store(user1)
assert.NoError(t, err)
user2 := &User{
Age: 10,
}
err = session.Store(user2)
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
session.Close()
}
var cancel ravendb.CancelFunc
{
changesList := make(chan *ravendb.OperationStatusChange, 1)
changes := store.Changes("")
err = changes.EnsureConnectedNow()
require.NoError(t, err)
cb := func(change *ravendb.OperationStatusChange) {
changesList <- change
}
cancel, err = changes.ForAllOperations(cb)
require.NoError(t, err)
indexQuery := ravendb.NewIndexQuery("from users where age == 5")
operation, err := ravendb.NewDeleteByQueryOperation(indexQuery, nil)
assert.NoError(t, err)
_, err = store.Operations().SendAsync(operation, nil)
assert.NoError(t, err)
select {
case change := <-changesList:
// ok, got a change
expID := operation.Command.Result.OperationID
assert.Equal(t, change.OperationID, expID)
case <-time.After(15 * time.Second):
assert.Fail(t, "timed out waiting for operation change notification")
}
cancel()
changes.Close()
}
}
func TestDeleteByQuery(t *testing.T) {
driver := createTestDriver(t)
destroy := func() { destroyDriver(t, driver) }
defer recoverTest(t, destroy)
// matches order of Java tests
//TODO: match order of Java tests
deleteByQueryCanDeleteByQuery(t, driver)
deleteByQueryCanDeleteByQueryWaitUsingChanges(t, driver)
}