forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbulk_inserts_test.go
164 lines (129 loc) · 3.88 KB
/
bulk_inserts_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
package tests
import (
"strings"
"testing"
"time"
ravendb "github.com/ravendb/ravendb-go-client"
"github.com/stretchr/testify/assert"
)
func bulkInsertsTestSimpleBulkInsertShouldWork(t *testing.T, driver *RavenTestDriver) {
fooBar1 := &FooBar{}
fooBar1.Name = "John Doe"
fooBar2 := &FooBar{}
fooBar2.Name = "Jane Doe"
fooBar3 := &FooBar{}
fooBar3.Name = "Mega John"
fooBar4 := &FooBar{}
fooBar4.Name = "Mega Jane"
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
{
bulkInsert := store.BulkInsert("")
_, err = bulkInsert.Store(fooBar1, nil)
assert.NoError(t, err)
_, err = bulkInsert.Store(fooBar2, nil)
assert.NoError(t, err)
_, err = bulkInsert.Store(fooBar3, nil)
assert.NoError(t, err)
_, err = bulkInsert.Store(fooBar4, nil)
assert.NoError(t, err)
err = bulkInsert.Close()
assert.NoError(t, err)
}
{
session := openSessionMust(t, store)
var doc1, doc2, doc3, doc4 *FooBar
err = session.Load(&doc1, "FooBars/1-A")
assert.NoError(t, err)
err = session.Load(&doc2, "FooBars/2-A")
assert.NoError(t, err)
err = session.Load(&doc3, "FooBars/3-A")
assert.NoError(t, err)
err = session.Load(&doc4, "FooBars/4-A")
assert.NoError(t, err)
assert.Equal(t, doc1.Name, "John Doe")
assert.Equal(t, doc2.Name, "Jane Doe")
assert.Equal(t, doc3.Name, "Mega John")
assert.Equal(t, doc4.Name, "Mega Jane")
session.Close()
}
}
func bulkInsertsTestKilledToEarly(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
{
bulkInsert := store.BulkInsert("")
_, err = bulkInsert.Store(&FooBar{}, nil)
assert.NoError(t, err)
err = bulkInsert.Abort()
if err == nil {
_, err = bulkInsert.Store(&FooBar{}, nil)
}
if err == nil {
err = bulkInsert.Close()
}
assert.Error(t, err)
_, ok := err.(*ravendb.BulkInsertAbortedError)
assert.True(t, ok, "expected error to be of type ravendb.BulkInsertAbortedError, got type '%T', value: '%s'", err, err)
}
}
func bulkInsertsTestShouldNotAcceptIdsEndingWithPipeLine(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
{
bulkInsert := store.BulkInsert("")
err = bulkInsert.StoreWithID(&FooBar{}, "foobars|", nil)
assert.Error(t, err)
_, ok := err.(*ravendb.UnsupportedOperationError)
assert.True(t, ok)
ok = strings.Contains(err.Error(), "Document ids cannot end with '|', but was called with foobars|")
assert.True(t, ok)
err = bulkInsert.Close()
assert.NoError(t, err)
}
}
func bulkInsertsTestCanModifyMetadataWithBulkInsert(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
et := time.Now().Add(time.Hour * 24 * 365)
expirationDate := ravendb.Time(et).Format()
{
bulkInsert := store.BulkInsert("")
fooBar := &FooBar{}
fooBar.Name = "Jon Show"
metadata := &ravendb.MetadataAsDictionary{}
metadata.Put(ravendb.MetadataExpires, expirationDate)
_, err = bulkInsert.Store(fooBar, metadata)
assert.NoError(t, err)
err = bulkInsert.Close()
assert.NoError(t, err)
}
{
session := openSessionMust(t, store)
var entity *FooBar
err = session.Load(&entity, "FooBars/1-A")
assert.NoError(t, err)
meta, err := session.Advanced().GetMetadataFor(entity)
assert.NoError(t, err)
metadataExpirationDate, ok := meta.Get(ravendb.MetadataExpires)
assert.True(t, ok)
assert.Equal(t, metadataExpirationDate, expirationDate)
}
}
type FooBar struct {
Name string
}
func TestBulkInserts(t *testing.T) {
driver := createTestDriver(t)
destroy := func() { destroyDriver(t, driver) }
defer recoverTest(t, destroy)
// matches order of Java tests
bulkInsertsTestSimpleBulkInsertShouldWork(t, driver)
bulkInsertsTestShouldNotAcceptIdsEndingWithPipeLine(t, driver)
bulkInsertsTestKilledToEarly(t, driver)
bulkInsertsTestCanModifyMetadataWithBulkInsert(t, driver)
}