Skip to content

Commit

Permalink
Merge pull request #57 from ali-ince/1.7-verify-error-on-commit
Browse files Browse the repository at this point in the history
 Add tests to verify on-commit-error behaviour
  • Loading branch information
ali-ince authored May 2, 2019
2 parents 80cf7ca + 1ee1b0e commit e3abb83
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
14 changes: 14 additions & 0 deletions neo4j/test-stub/scripts/v1/connection_error_on_commit.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
!: BOLT 1
!: AUTO INIT
!: AUTO RESET

C: RUN "BEGIN" {}
DISCARD_ALL
S: SUCCESS {}
SUCCESS {}
C: RUN "CREATE (n {name: 'Bob'})" {}
PULL_ALL
S: SUCCESS {"fields": []}
SUCCESS {}
C: RUN "COMMIT" {}
S: <EXIT>
12 changes: 12 additions & 0 deletions neo4j/test-stub/scripts/v3/connection_error_on_commit.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
!: BOLT 3
!: AUTO HELLO
!: AUTO RESET

C: BEGIN {}
S: SUCCESS {}
C: RUN "CREATE (n {name: 'Bob'})" {} {}
PULL_ALL
S: SUCCESS {"fields": []}
SUCCESS {}
C: COMMIT
S: <EXIT>
64 changes: 64 additions & 0 deletions neo4j/test-stub/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,63 @@ func Test_Transaction(t *testing.T) {
assert.Equal(t, "bookmark:1", session.LastBookmark())
}

var verifyFailureOnExplicitCommit = func(t *testing.T, script string) {
stub := control.NewStubServer(t, 9001, script)
defer stub.Finished(t)

driver := newDriver(t, "bolt://localhost:9001")
defer driver.Close()

session := createWriteSession(t, driver)
defer session.Close()

tx := createTx(t, session)
defer tx.Close()

result, err := tx.Run("CREATE (n {name: 'Bob'})", nil)
require.NoError(t, err)
require.NotNil(t, result)

require.False(t, result.Next())
require.NoError(t, result.Err())

err = tx.Commit()
assert.Error(t, err)
assert.Contains(t, err.Error(), "unexpected connection state")
}

var verifyFailureOnTxFuncCommit = func(t *testing.T, script string) {
stub := control.NewStubServer(t, 9001, script)
defer stub.Finished(t)

driver := newDriver(t, "bolt://localhost:9001")
defer driver.Close()

session := createWriteSession(t, driver)
defer session.Close()

result, err := session.WriteTransaction(func(tx neo4j.Transaction) (interface{}, error) {
innerResult, innerErr := tx.Run("CREATE (n {name: 'Bob'})", nil)
require.NoError(t, innerErr)
require.NotNil(t, innerResult)
return innerResult, innerErr
})

assert.Nil(t, result)
assert.Error(t, err)
assert.Contains(t, err.Error(), "unexpected connection state")
}

t.Run("V1", func(t *testing.T) {
t.Run("shouldFailOnConnectionFailureOnExplicitCommit", func(t *testing.T) {
verifyFailureOnExplicitCommit(t, path.Join("v1", "connection_error_on_commit.script"))
})

t.Run("shouldFailOnConnectionFailureOnTxFuncCommit", func(t *testing.T) {
verifyFailureOnTxFuncCommit(t, path.Join("v1", "connection_error_on_commit.script"))
})
})

t.Run("V3", func(t *testing.T) {
t.Run("shouldExecuteSimpleQuery", func(t *testing.T) {
verifyReturn1(t, path.Join("v3", "return_1_in_tx.script"), nil)
Expand All @@ -83,5 +140,12 @@ func Test_Transaction(t *testing.T) {
verifyReturn1(t, path.Join("v3", "begin_with_timeout.script"), nil, neo4j.WithTxTimeout(12340*time.Millisecond))
})

t.Run("shouldFailOnConnectionFailureOnExplicitCommit", func(t *testing.T) {
verifyFailureOnExplicitCommit(t, path.Join("v3", "connection_error_on_commit.script"))
})

t.Run("shouldFailOnConnectionFailureOnTxFuncCommit", func(t *testing.T) {
verifyFailureOnTxFuncCommit(t, path.Join("v3", "connection_error_on_commit.script"))
})
})
}

0 comments on commit e3abb83

Please sign in to comment.