MCPcopy
hub / github.com/jackc/pgx / TestConnBeginTxIsolation

Function TestConnBeginTxIsolation

stdlib/sql_test.go:664–718  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

662}
663
664func TestConnBeginTxIsolation(t *testing.T) {
665 testWithAllQueryExecModes(t, func(t *testing.T, db *sql.DB) {
666 skipCockroachDB(t, db, "Server always uses serializable isolation level")
667
668 var defaultIsoLevel string
669 err := db.QueryRow("show transaction_isolation").Scan(&defaultIsoLevel)
670 require.NoError(t, err)
671
672 supportedTests := []struct {
673 sqlIso sql.IsolationLevel
674 pgIso string
675 }{
676 {sqlIso: sql.LevelDefault, pgIso: defaultIsoLevel},
677 {sqlIso: sql.LevelReadUncommitted, pgIso: "read uncommitted"},
678 {sqlIso: sql.LevelReadCommitted, pgIso: "read committed"},
679 {sqlIso: sql.LevelRepeatableRead, pgIso: "repeatable read"},
680 {sqlIso: sql.LevelSnapshot, pgIso: "repeatable read"},
681 {sqlIso: sql.LevelSerializable, pgIso: "serializable"},
682 }
683 for i, tt := range supportedTests {
684 func() {
685 tx, err := db.BeginTx(context.Background(), &sql.TxOptions{Isolation: tt.sqlIso})
686 if err != nil {
687 t.Errorf("%d. BeginTx failed: %v", i, err)
688 return
689 }
690 defer tx.Rollback()
691
692 var pgIso string
693 err = tx.QueryRow("show transaction_isolation").Scan(&pgIso)
694 if err != nil {
695 t.Errorf("%d. QueryRow failed: %v", i, err)
696 }
697
698 if pgIso != tt.pgIso {
699 t.Errorf("%d. pgIso => %s, want %s", i, pgIso, tt.pgIso)
700 }
701 }()
702 }
703
704 unsupportedTests := []struct {
705 sqlIso sql.IsolationLevel
706 }{
707 {sqlIso: sql.LevelWriteCommitted},
708 {sqlIso: sql.LevelLinearizable},
709 }
710 for i, tt := range unsupportedTests {
711 tx, err := db.BeginTx(context.Background(), &sql.TxOptions{Isolation: tt.sqlIso})
712 if err == nil {
713 t.Errorf("%d. BeginTx should have failed", i)
714 tx.Rollback()
715 }
716 }
717 })
718}
719
720func TestConnBeginTxReadOnly(t *testing.T) {
721 testWithAllQueryExecModes(t, func(t *testing.T, db *sql.DB) {

Callers

nothing calls this directly

Calls 6

skipCockroachDBFunction · 0.70
ScanMethod · 0.65
QueryRowMethod · 0.65
RollbackMethod · 0.65
BeginTxMethod · 0.45

Tested by

no test coverage detected