1/* 2Copyright 2014 SAP SE 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15*/ 16 17package driver 18 19import ( 20 "database/sql" 21 "fmt" 22 "testing" 23) 24 25func TestTransactionCommit(t *testing.T) { 26 27 db1, err := sql.Open(DriverName, TestDSN) 28 if err != nil { 29 t.Fatal(err) 30 } 31 defer db1.Close() 32 33 db2, err := sql.Open(DriverName, TestDSN) 34 if err != nil { 35 t.Fatal(err) 36 } 37 defer db2.Close() 38 39 table := RandomIdentifier("testTxCommit_") 40 if _, err := db1.Exec(fmt.Sprintf("create table %s.%s (i tinyint)", TestSchema, table)); err != nil { 41 t.Fatal(err) 42 } 43 44 tx1, err := db1.Begin() 45 if err != nil { 46 t.Fatal(err) 47 } 48 49 tx2, err := db2.Begin() 50 if err != nil { 51 t.Fatal(err) 52 } 53 defer tx2.Rollback() 54 55 //insert record in transaction 1 56 if _, err := tx1.Exec(fmt.Sprintf("insert into %s.%s values(42)", TestSchema, table)); err != nil { 57 t.Fatal(err) 58 } 59 60 //count records in transaction 1 61 i := 0 62 if err := tx1.QueryRow(fmt.Sprintf("select count(*) from %s.%s", TestSchema, table)).Scan(&i); err != nil { 63 t.Fatal(err) 64 } 65 if i != 1 { 66 t.Fatal(fmt.Errorf("tx1: invalid number of records %d - 1 expected", i)) 67 } 68 69 //count records in transaction 2 - isolation level 'read committed'' (default) expected, so no record should be there 70 if err := tx2.QueryRow(fmt.Sprintf("select count(*) from %s.%s", TestSchema, table)).Scan(&i); err != nil { 71 t.Fatal(err) 72 } 73 if i != 0 { 74 t.Fatal(fmt.Errorf("tx2: invalid number of records %d - 0 expected", i)) 75 } 76 77 //commit insert 78 if err := tx1.Commit(); err != nil { 79 t.Fatal(err) 80 } 81 82 //in isolation level 'read commited' (default) record should be visible now 83 if err := tx2.QueryRow(fmt.Sprintf("select count(*) from %s.%s", TestSchema, table)).Scan(&i); err != nil { 84 t.Fatal(err) 85 } 86 if i != 1 { 87 t.Fatal(fmt.Errorf("tx2: invalid number of records %d - 1 expected", i)) 88 } 89} 90 91func TestTransactionRollback(t *testing.T) { 92 93 db, err := sql.Open(DriverName, TestDSN) 94 if err != nil { 95 t.Fatal(err) 96 } 97 defer db.Close() 98 99 table := RandomIdentifier("testTxRollback_") 100 if _, err := db.Exec(fmt.Sprintf("create table %s.%s (i tinyint)", TestSchema, table)); err != nil { 101 t.Fatal(err) 102 } 103 104 tx, err := db.Begin() 105 if err != nil { 106 t.Fatal(err) 107 } 108 109 //insert record 110 if _, err := tx.Exec(fmt.Sprintf("insert into %s.%s values(42)", TestSchema, table)); err != nil { 111 t.Fatal(err) 112 } 113 114 //count records 115 i := 0 116 if err := tx.QueryRow(fmt.Sprintf("select count(*) from %s.%s", TestSchema, table)).Scan(&i); err != nil { 117 t.Fatal(err) 118 } 119 if i != 1 { 120 t.Fatal(fmt.Errorf("tx: invalid number of records %d - 1 expected", i)) 121 } 122 123 //rollback insert 124 if err := tx.Rollback(); err != nil { 125 t.Fatal(err) 126 } 127 128 //new transaction 129 tx, err = db.Begin() 130 if err != nil { 131 t.Fatal(err) 132 } 133 defer tx.Rollback() 134 135 //rollback - no record expected 136 if err := tx.QueryRow(fmt.Sprintf("select count(*) from %s.%s", TestSchema, table)).Scan(&i); err != nil { 137 t.Fatal(err) 138 } 139 if i != 0 { 140 t.Fatal(fmt.Errorf("tx: invalid number of records %d - 0 expected", i)) 141 } 142} 143