forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo_northwind_test.go
192 lines (157 loc) · 4.91 KB
/
go_northwind_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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package tests
import (
"github.com/ravendb/ravendb-go-client"
"github.com/ravendb/ravendb-go-client/examples/northwind"
"github.com/stretchr/testify/assert"
"reflect"
"testing"
)
func createNorthwindDatabase(t *testing.T, driver *RavenTestDriver, store *ravendb.DocumentStore) {
sampleData := ravendb.NewCreateSampleDataOperation()
err := store.Maintenance().Send(sampleData)
must(err)
err = driver.waitForIndexing(store, store.GetDatabase(), 0)
must(err)
}
func goNorthwindEmployeeLoad(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
createNorthwindDatabase(t, driver, store)
session, err := store.OpenSession("")
assert.NoError(t, err)
defer session.Close()
var e *northwind.Employee
err = session.Load(&e, "employees/7-A")
assert.NoError(t, err)
var results []*northwind.Employee
args := &ravendb.StartsWithArgs{
StartsWith: "employees/",
}
err = session.LoadStartingWith(&results, args)
assert.NoError(t, err)
assert.True(t, len(results) > 5) // it's 9 currently
}
func goNorthwindWhereBetween(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
createNorthwindDatabase(t, driver, store)
session, err := store.OpenSession("")
assert.NoError(t, err)
defer session.Close()
tp := reflect.TypeOf(&northwind.Order{})
q := session.QueryCollectionForType(tp)
q = q.WhereBetween("Freight", 11, 13)
var results []*northwind.Order
err = q.GetResults(&results)
assert.NoError(t, err)
assert.True(t, len(results) > 5) // it's 35 currently
}
// First() should allow zero results
// https://github.com/ravendb/ravendb-go-client/issues/148
func goNorthwindIssue148(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
createNorthwindDatabase(t, driver, store)
session, err := store.OpenSession("")
assert.NoError(t, err)
defer session.Close()
queriedType := reflect.TypeOf(&northwind.Employee{})
query := session.QueryCollectionForType(queriedType)
query = query.Where("FirstName", "==", "name-that-doesn't exists")
var result *northwind.Employee
err = query.First(&result)
// no error, result not set
assert.NoError(t, err)
assert.Nil(t, result)
}
// test that Single()/First()/GetResults() validate early type of result
// https://github.com/ravendb/ravendb-go-client/issues/146
func goNorthwindIssue146(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
createNorthwindDatabase(t, driver, store)
session, err := store.OpenSession("")
assert.NoError(t, err)
defer session.Close()
{
tp := reflect.TypeOf(&northwind.Employee{})
q := session.QueryCollectionForType(tp)
q = q.Where("ID", "=", "employees/1-A")
err = q.Single(nil)
_, ok := err.(*ravendb.IllegalArgumentError)
assert.True(t, ok)
}
{
tp := reflect.TypeOf(&northwind.Employee{})
q := session.QueryCollectionForType(tp)
q = q.Where("ID", "=", "employees/1-A")
var results []*northwind.Employee
err = q.Single(&results)
_, ok := err.(*ravendb.IllegalArgumentError)
assert.True(t, ok)
}
{
tp := reflect.TypeOf(&northwind.Employee{})
q := session.QueryCollectionForType(tp)
q = q.Where("ID", "=", "employees/1-A")
err = q.First(nil)
_, ok := err.(*ravendb.IllegalArgumentError)
assert.True(t, ok)
}
{
tp := reflect.TypeOf(&northwind.Employee{})
q := session.QueryCollectionForType(tp)
q = q.Where("ID", "=", "employees/1-A")
var results []*northwind.Employee
err = q.First(&results)
_, ok := err.(*ravendb.IllegalArgumentError)
assert.True(t, ok)
}
{
tp := reflect.TypeOf(&northwind.Employee{})
q := session.QueryCollectionForType(tp)
err = q.GetResults(nil)
_, ok := err.(*ravendb.IllegalArgumentError)
assert.True(t, ok)
}
{
tp := reflect.TypeOf(&northwind.Employee{})
q := session.QueryCollectionForType(tp)
var results *northwind.Employee
err = q.GetResults(results)
err = q.GetResults(&results)
_, ok := err.(*ravendb.IllegalArgumentError)
assert.True(t, ok)
}
{
tp := reflect.TypeOf(&northwind.Employee{})
q := session.QueryCollectionForType(tp)
strType := reflect.TypeOf("")
q = q.SelectFields(strType, "FirstName")
var results []string
err = q.GetResults(&results)
assert.NoError(t, err)
}
{
tp := reflect.TypeOf(&northwind.Employee{})
q := session.QueryCollectionForType(tp)
var results []*northwind.Employee
err = q.GetResults(results)
_, ok := err.(*ravendb.IllegalArgumentError)
assert.True(t, ok)
assert.Equal(t, "results can't be of type []*northwind.Employee, try *[]*northwind.Employee", err.Error())
}
}
func TestGoNorthwind(t *testing.T) {
driver := createTestDriver(t)
destroy := func() { destroyDriver(t, driver) }
defer recoverTest(t, destroy)
goNorthwindEmployeeLoad(t, driver)
goNorthwindWhereBetween(t, driver)
goNorthwindIssue146(t, driver)
goNorthwindIssue148(t, driver)
}