forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries_with_custom_functions_test.go
111 lines (89 loc) · 2.88 KB
/
queries_with_custom_functions_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
package tests
import (
"testing"
ravendb "github.com/ravendb/ravendb-go-client"
"github.com/stretchr/testify/assert"
)
func queriesWithCustomFunctionsQueryCmpXchgWhere(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
op, err := ravendb.NewPutCompareExchangeValueOperation("Tom", "Jerry", 0)
assert.NoError(t, err)
err = store.Operations().Send(op, nil)
assert.NoError(t, err)
op, err = ravendb.NewPutCompareExchangeValueOperation("Hera", "Zeus", 0)
assert.NoError(t, err)
err = store.Operations().Send(op, nil)
assert.NoError(t, err)
op, err = ravendb.NewPutCompareExchangeValueOperation("Gaya", "Uranus", 0)
assert.NoError(t, err)
err = store.Operations().Send(op, nil)
assert.NoError(t, err)
op, err = ravendb.NewPutCompareExchangeValueOperation("Jerry@gmail.com", "users/2", 0)
assert.NoError(t, err)
err = store.Operations().Send(op, nil)
assert.NoError(t, err)
op, err = ravendb.NewPutCompareExchangeValueOperation("Zeus@gmail.com", "users/1", 0)
assert.NoError(t, err)
err = store.Operations().Send(op, nil)
assert.NoError(t, err)
{
session := openSessionMust(t, store)
jerry := &User{}
jerry.setName("Jerry")
err = session.StoreWithID(jerry, "users/2")
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
zeus := &User{}
zeus.setName("Zeus")
zeus.setLastName("Jerry")
err = session.StoreWithID(zeus, "users/1")
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
session.Close()
}
{
session := openSessionMust(t, store)
q := session.Advanced().QueryCollectionForType(userType)
q = q.WhereEquals("name", ravendb.CmpXchgValue("Hera"))
q = q.WhereEquals("lastName", ravendb.CmpXchgValue("Tom"))
var users []*User
err = q.GetResults(&users)
assert.NoError(t, err)
assert.Equal(t, len(users), 1)
user := users[0]
assert.Equal(t, *user.Name, "Zeus")
iquery, err := q.GetIndexQuery()
assert.NoError(t, err)
query := iquery.GetQuery()
assert.Equal(t, query, "from Users where name = cmpxchg($p0) and lastName = cmpxchg($p1)")
users = nil
q = session.Advanced().QueryCollectionForType(userType)
q = q.WhereNotEquals("name", ravendb.CmpXchgValue("Hera"))
err = q.GetResults(&users)
assert.NoError(t, err)
assert.Equal(t, len(users), 1)
user = users[0]
assert.Equal(t, *user.Name, "Jerry")
users = nil
{
q := session.Advanced().RawQuery("from Users where name = cmpxchg(\"Hera\")")
err = q.GetResults(&users)
assert.NoError(t, err)
}
assert.Equal(t, len(users), 1)
user = users[0]
assert.Equal(t, *user.Name, "Zeus")
session.Close()
}
}
func TestQueriesWithCustomFunctions(t *testing.T) {
driver := createTestDriver(t)
destroy := func() { destroyDriver(t, driver) }
defer recoverTest(t, destroy)
// matches the order of Java tests
queriesWithCustomFunctionsQueryCmpXchgWhere(t, driver)
}