forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontains_test.go
88 lines (69 loc) · 2.02 KB
/
contains_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
package tests
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
type UserWithFavs struct {
ID string
Name string
Favourites []string
}
func NewUserWithFavs() *UserWithFavs {
return &UserWithFavs{}
}
func containsTestcontainsTest(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
{
session := openSessionMust(t, store)
userCreator := func(name string, favs []string) {
user := NewUserWithFavs()
user.Name = name
user.Favourites = favs
err = session.Store(user)
assert.NoError(t, err)
}
userCreator("John", []string{"java", "c#"})
userCreator("Tarzan", []string{"java", "go"})
userCreator("Jane", []string{"pascal"})
err = session.SaveChanges()
assert.NoError(t, err)
session.Close()
}
{
session := openSessionMust(t, store)
q := session.QueryCollectionForType(reflect.TypeOf(&UserWithFavs{}))
q = q.ContainsAny("Favourites", []interface{}{"pascal", "go"})
projType := reflect.TypeOf("")
q = q.SelectFields(projType, "Name")
var pascalOrGoDeveloperNames []string
err = q.GetResults(&pascalOrGoDeveloperNames)
assert.NoError(t, err)
assert.Equal(t, 2, len(pascalOrGoDeveloperNames))
assert.Equal(t, pascalOrGoDeveloperNames[0], "Tarzan")
assert.Equal(t, pascalOrGoDeveloperNames[1], "Jane")
session.Close()
}
{
session := openSessionMust(t, store)
q := session.QueryCollectionForType(reflect.TypeOf(&UserWithFavs{}))
q = q.ContainsAll("Favourites", []interface{}{"java"})
projType := reflect.TypeOf("")
q = q.SelectFields(projType, "Name")
var javaDevelopers []string
err = q.GetResults(&javaDevelopers)
assert.NoError(t, err)
assert.Equal(t, 2, len(javaDevelopers))
assert.Equal(t, javaDevelopers[0], "John")
assert.Equal(t, javaDevelopers[1], "Tarzan")
session.Close()
}
}
func TestContains(t *testing.T) {
driver := createTestDriver(t)
destroy := func() { destroyDriver(t, driver) }
defer recoverTest(t, destroy)
containsTestcontainsTest(t, driver)
}