MCPcopy Index your code
hub / github.com/CovenantSQL/CovenantSQL / TestTransaction

Function TestTransaction

client/conn_test.go:171–283  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

169}
170
171func TestTransaction(t *testing.T) {
172 Convey("test transaction", t, func() {
173 var stopTestService func()
174 var err error
175 stopTestService, _, err = startTestService()
176 So(err, ShouldBeNil)
177 defer stopTestService()
178
179 var db *sql.DB
180 db, err = sql.Open("covenantsql", "covenantsql://db")
181 So(db, ShouldNotBeNil)
182 So(err, ShouldBeNil)
183
184 var execResult sql.Result
185 var lastInsertID, affectedRows int64
186
187 _, err = db.Exec("create table test (test int)")
188 So(err, ShouldBeNil)
189 execResult, err = db.Exec("insert into test values (1)")
190 So(err, ShouldBeNil)
191 lastInsertID, err = execResult.LastInsertId()
192 So(err, ShouldBeNil)
193 So(lastInsertID, ShouldEqual, 1)
194 affectedRows, err = execResult.RowsAffected()
195 So(affectedRows, ShouldEqual, 1)
196
197 // test start transaction
198 var tx *sql.Tx
199 tx, err = db.Begin()
200 So(tx, ShouldNotBeNil)
201 So(err, ShouldBeNil)
202
203 // test fill invalid read query in transaction
204 _, err = tx.Query("select * from test")
205 So(err, ShouldNotBeNil)
206
207 // test query
208 _, err = tx.Exec("insert into test values(2)")
209 So(err, ShouldBeNil)
210
211 // test rollback
212 err = tx.Rollback()
213 So(err, ShouldBeNil)
214
215 testRowCount := func(expected int) {
216 var row *sql.Row
217 var err error
218 var result int
219 row = db.QueryRow("select count(1) as cnt from test")
220 So(row, ShouldNotBeNil)
221 err = row.Scan(&result)
222 So(err, ShouldBeNil)
223 So(result, ShouldEqual, expected)
224 }
225
226 // test row count on rollback
227 testRowCount(1)
228

Callers

nothing calls this directly

Calls 12

startTestServiceFunction · 0.85
QueryRowMethod · 0.80
ExecMethod · 0.65
QueryMethod · 0.65
RollbackMethod · 0.65
CommitMethod · 0.65
CloseMethod · 0.65
OpenMethod · 0.45
LastInsertIdMethod · 0.45
RowsAffectedMethod · 0.45
BeginMethod · 0.45
ScanMethod · 0.45

Tested by

no test coverage detected