Cole-Greer commented on code in PR #3434: URL: https://github.com/apache/tinkerpop/pull/3434#discussion_r3365602469
########## gremlin-go/driver/transaction_test.go: ########## @@ -0,0 +1,529 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +*/ + +package gremlingo + +import ( + "crypto/tls" + "testing" + + "github.com/stretchr/testify/assert" +) + +func newTxRemoteConnection(t *testing.T) *DriverRemoteConnection { + url := getEnvOrDefaultString("GREMLIN_SERVER_URL", noAuthUrl) + remote, err := NewDriverRemoteConnection(url, + func(settings *DriverRemoteConnectionSettings) { + settings.TlsConfig = &tls.Config{} + settings.TraversalSource = "gtx" + }) + assert.Nil(t, err) + assert.NotNil(t, remote) + return remote +} + +func newTxClient(t *testing.T) *Client { + url := getEnvOrDefaultString("GREMLIN_SERVER_URL", noAuthUrl) + client, err := NewClient(url, func(settings *ClientSettings) { + settings.TlsConfig = &tls.Config{} + settings.TraversalSource = "gtx" + }) + assert.Nil(t, err) + assert.NotNil(t, client) + return client +} + +// dropTxGraph removes all vertices from the transactional graph to prevent cross-test contamination. +func dropTxGraph(t *testing.T, client *Client) { + rs, err := client.Submit("g.V().drop()") + assert.Nil(t, err) + _, err = rs.All() + assert.Nil(t, err) +} + +func getTxCount(t *testing.T, client *Client, label string) int64 { + rs, err := client.Submit("g.V().hasLabel('" + label + "').count()") + assert.Nil(t, err) + results, err := rs.All() + assert.Nil(t, err) + assert.Equal(t, 1, len(results)) + val, err := results[0].GetInt64() + assert.Nil(t, err) + return val +} + +func TestTransactionCommit(t *testing.T) { + client := newTxClient(t) + defer client.Close() + dropTxGraph(t, client) + + tx := client.Transact() + _, err := tx.Begin() + assert.Nil(t, err) + assert.True(t, tx.IsOpen()) + + _, err = tx.Submit("g.addV('person').property('name','alice')") + assert.Nil(t, err) + + // Uncommitted data not visible outside the transaction + assert.Equal(t, int64(0), getTxCount(t, client, "person")) + + err = tx.Commit() + assert.Nil(t, err) + assert.False(t, tx.IsOpen()) + + // Committed data visible + assert.Equal(t, int64(1), getTxCount(t, client, "person")) +} + +func TestTransactionRollback(t *testing.T) { + client := newTxClient(t) + defer client.Close() + dropTxGraph(t, client) + + tx := client.Transact() + _, err := tx.Begin() + assert.Nil(t, err) + + _, err = tx.Submit("g.addV('person').property('name','bob')") + assert.Nil(t, err) + + err = tx.Rollback() + assert.Nil(t, err) + assert.False(t, tx.IsOpen()) + + // Data discarded + assert.Equal(t, int64(0), getTxCount(t, client, "person")) +} + +func TestTransactionIntraConsistency(t *testing.T) { + client := newTxClient(t) + defer client.Close() + dropTxGraph(t, client) + + tx := client.Transact() + _, err := tx.Begin() + assert.Nil(t, err) + + _, err = tx.Submit("g.addV('test').property('name','A')") + assert.Nil(t, err) + + // Read-your-own-writes + rs, err := tx.Submit("g.V().hasLabel('test').count()") + assert.Nil(t, err) + results, err := rs.All() + assert.Nil(t, err) + val, err := results[0].GetInt64() + assert.Nil(t, err) + assert.Equal(t, int64(1), val) + + _, err = tx.Submit("g.addV('test').property('name','B')") + assert.Nil(t, err) + + err = tx.Commit() + assert.Nil(t, err) + + assert.Equal(t, int64(2), getTxCount(t, client, "test")) +} + +func TestTransactionSubmitAfterCommit(t *testing.T) { + client := newTxClient(t) + defer client.Close() + + tx := client.Transact() + _, err := tx.Begin() + assert.Nil(t, err) + + _, err = tx.Submit("g.addV()") + assert.Nil(t, err) + + err = tx.Commit() + assert.Nil(t, err) + + _, err = tx.Submit("g.V().count()") + assert.NotNil(t, err) + assert.Contains(t, err.Error(), "transaction is not open") +} + +func TestTransactionSubmitAfterRollback(t *testing.T) { + client := newTxClient(t) + defer client.Close() + + tx := client.Transact() + _, err := tx.Begin() + assert.Nil(t, err) + + _, err = tx.Submit("g.addV()") + assert.Nil(t, err) + + err = tx.Rollback() + assert.Nil(t, err) + + _, err = tx.Submit("g.V().count()") + assert.NotNil(t, err) + assert.Contains(t, err.Error(), "transaction is not open") +} + +func TestTransactionDoubleBegin(t *testing.T) { + client := newTxClient(t) + defer client.Close() + + tx := client.Transact() + _, err := tx.Begin() + assert.Nil(t, err) + + _, err = tx.Begin() + assert.NotNil(t, err) Review Comment: Nit: It would be nice to also assert the error code or part of the error message for a lot of these cases. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
