MCPcopy
hub / github.com/go-sql-driver/mysql / TestContextBeginReadOnly

Function TestContextBeginReadOnly

driver_test.go:3006–3042  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

3004}
3005
3006func TestContextBeginReadOnly(t *testing.T) {
3007 runTestsParallel(t, dsn, func(dbt *DBTest, tbl string) {
3008 dbt.mustExec("CREATE TABLE " + tbl + " (v INTEGER)")
3009 ctx, cancel := context.WithCancel(context.Background())
3010 defer cancel()
3011
3012 tx, err := dbt.db.BeginTx(ctx, &sql.TxOptions{
3013 ReadOnly: true,
3014 })
3015 if _, ok := err.(*MySQLError); ok {
3016 dbt.Skip("It seems that your MySQL does not support READ ONLY transactions")
3017 return
3018 } else if err != nil {
3019 dbt.Fatal(err)
3020 }
3021
3022 // INSERT queries fail in a READ ONLY transaction.
3023 _, err = tx.ExecContext(ctx, "INSERT INTO "+tbl+" VALUES (1)")
3024 if _, ok := err.(*MySQLError); !ok {
3025 dbt.Errorf("expected MySQLError, got %v", err)
3026 }
3027
3028 // SELECT queries can be executed.
3029 var v int
3030 row := tx.QueryRowContext(ctx, "SELECT COUNT(*) FROM "+tbl)
3031 if err := row.Scan(&v); err != nil {
3032 dbt.Fatal(err)
3033 }
3034 if v != 0 {
3035 dbt.Errorf("expected val to be 0, got %d", v)
3036 }
3037
3038 if err := tx.Commit(); err != nil {
3039 dbt.Fatal(err)
3040 }
3041 })
3042}
3043
3044func TestRowsColumnTypes(t *testing.T) {
3045 niNULL := sql.NullInt64{Int64: 0, Valid: false}

Callers

nothing calls this directly

Calls 6

runTestsParallelFunction · 0.85
mustExecMethod · 0.80
BeginTxMethod · 0.80
ScanMethod · 0.80
CommitMethod · 0.80
ExecContextMethod · 0.45

Tested by

no test coverage detected