forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrack_entity_test.go
111 lines (93 loc) · 2.66 KB
/
track_entity_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 (
"strings"
"testing"
"github.com/ravendb/ravendb-go-client"
"github.com/stretchr/testify/assert"
)
func trackEntityTestDeletingEntityThatIsNotTrackedShouldThrow(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
{
session := openSessionMust(t, store)
err = session.Delete(&User{})
assert.Error(t, err)
_ = err.(*ravendb.IllegalStateError)
msg := err.Error()
assert.True(t, strings.HasSuffix(msg, "is not associated with the session, cannot delete unknown entity instance"))
session.Close()
}
}
func trackEntityTestLoadingDeletedDocumentShouldReturnNull(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
{
session := openSessionMust(t, store)
user1 := &User{}
user1.setName("John")
user1.ID = "users/1"
user2 := &User{}
user2.setName("Jonathan")
user2.ID = "users/2"
err = session.Store(user1)
assert.NoError(t, err)
err = session.Store(user2)
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
session.Close()
}
{
session := openSessionMust(t, store)
err = session.DeleteByID("users/1", "")
assert.NoError(t, err)
err = session.DeleteByID("users/2", "")
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
session.Close()
}
{
session := openSessionMust(t, store)
var user *User
err = session.Load(&user, "users/1")
assert.NoError(t, err)
err = session.Load(&user, "users/2")
assert.NoError(t, err)
session.Close()
}
}
func trackEntityTestStoringDocumentWithTheSameIdInTheSameSessionShouldThrow(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
{
session := openSessionMust(t, store)
user := &User{}
user.ID = "users/1"
user.setName("User1")
err = session.Store(user)
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
newUser := &User{}
newUser.setName("User2")
newUser.ID = "users/1"
err = session.Store(newUser)
_ = err.(*ravendb.NonUniqueObjectError)
msg := err.Error()
assert.True(t, strings.HasPrefix(msg, "Attempted to associate a different object with id 'users/1'"))
session.Close()
}
}
func TestTrackEntity(t *testing.T) {
driver := createTestDriver(t)
destroy := func() { destroyDriver(t, driver) }
defer recoverTest(t, destroy)
// matches order of java tests
trackEntityTestLoadingDeletedDocumentShouldReturnNull(t, driver)
trackEntityTestDeletingEntityThatIsNotTrackedShouldThrow(t, driver)
trackEntityTestStoringDocumentWithTheSameIdInTheSameSessionShouldThrow(t, driver)
}