Skip to content

Commit e3abb83

Browse files
authored
Merge pull request neo4j#57 from ali-ince/1.7-verify-error-on-commit
Add tests to verify on-commit-error behaviour
2 parents 80cf7ca + 1ee1b0e commit e3abb83

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
!: BOLT 1
2+
!: AUTO INIT
3+
!: AUTO RESET
4+
5+
C: RUN "BEGIN" {}
6+
DISCARD_ALL
7+
S: SUCCESS {}
8+
SUCCESS {}
9+
C: RUN "CREATE (n {name: 'Bob'})" {}
10+
PULL_ALL
11+
S: SUCCESS {"fields": []}
12+
SUCCESS {}
13+
C: RUN "COMMIT" {}
14+
S: <EXIT>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
!: BOLT 3
2+
!: AUTO HELLO
3+
!: AUTO RESET
4+
5+
C: BEGIN {}
6+
S: SUCCESS {}
7+
C: RUN "CREATE (n {name: 'Bob'})" {} {}
8+
PULL_ALL
9+
S: SUCCESS {"fields": []}
10+
SUCCESS {}
11+
C: COMMIT
12+
S: <EXIT>

neo4j/test-stub/tx_test.go

+64
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,63 @@ func Test_Transaction(t *testing.T) {
7070
assert.Equal(t, "bookmark:1", session.LastBookmark())
7171
}
7272

73+
var verifyFailureOnExplicitCommit = func(t *testing.T, script string) {
74+
stub := control.NewStubServer(t, 9001, script)
75+
defer stub.Finished(t)
76+
77+
driver := newDriver(t, "bolt://localhost:9001")
78+
defer driver.Close()
79+
80+
session := createWriteSession(t, driver)
81+
defer session.Close()
82+
83+
tx := createTx(t, session)
84+
defer tx.Close()
85+
86+
result, err := tx.Run("CREATE (n {name: 'Bob'})", nil)
87+
require.NoError(t, err)
88+
require.NotNil(t, result)
89+
90+
require.False(t, result.Next())
91+
require.NoError(t, result.Err())
92+
93+
err = tx.Commit()
94+
assert.Error(t, err)
95+
assert.Contains(t, err.Error(), "unexpected connection state")
96+
}
97+
98+
var verifyFailureOnTxFuncCommit = func(t *testing.T, script string) {
99+
stub := control.NewStubServer(t, 9001, script)
100+
defer stub.Finished(t)
101+
102+
driver := newDriver(t, "bolt://localhost:9001")
103+
defer driver.Close()
104+
105+
session := createWriteSession(t, driver)
106+
defer session.Close()
107+
108+
result, err := session.WriteTransaction(func(tx neo4j.Transaction) (interface{}, error) {
109+
innerResult, innerErr := tx.Run("CREATE (n {name: 'Bob'})", nil)
110+
require.NoError(t, innerErr)
111+
require.NotNil(t, innerResult)
112+
return innerResult, innerErr
113+
})
114+
115+
assert.Nil(t, result)
116+
assert.Error(t, err)
117+
assert.Contains(t, err.Error(), "unexpected connection state")
118+
}
119+
120+
t.Run("V1", func(t *testing.T) {
121+
t.Run("shouldFailOnConnectionFailureOnExplicitCommit", func(t *testing.T) {
122+
verifyFailureOnExplicitCommit(t, path.Join("v1", "connection_error_on_commit.script"))
123+
})
124+
125+
t.Run("shouldFailOnConnectionFailureOnTxFuncCommit", func(t *testing.T) {
126+
verifyFailureOnTxFuncCommit(t, path.Join("v1", "connection_error_on_commit.script"))
127+
})
128+
})
129+
73130
t.Run("V3", func(t *testing.T) {
74131
t.Run("shouldExecuteSimpleQuery", func(t *testing.T) {
75132
verifyReturn1(t, path.Join("v3", "return_1_in_tx.script"), nil)
@@ -83,5 +140,12 @@ func Test_Transaction(t *testing.T) {
83140
verifyReturn1(t, path.Join("v3", "begin_with_timeout.script"), nil, neo4j.WithTxTimeout(12340*time.Millisecond))
84141
})
85142

143+
t.Run("shouldFailOnConnectionFailureOnExplicitCommit", func(t *testing.T) {
144+
verifyFailureOnExplicitCommit(t, path.Join("v3", "connection_error_on_commit.script"))
145+
})
146+
147+
t.Run("shouldFailOnConnectionFailureOnTxFuncCommit", func(t *testing.T) {
148+
verifyFailureOnTxFuncCommit(t, path.Join("v3", "connection_error_on_commit.script"))
149+
})
86150
})
87151
}

0 commit comments

Comments
 (0)