Skip to content

Commit

Permalink
Merge pull request #2 from 9ssi7/feature/txnsql-adpater
Browse files Browse the repository at this point in the history
feature/txnsql-adpater: upgrade the logic
  • Loading branch information
9ssi7 authored Aug 13, 2024
2 parents 7f06b1a + fb24d2d commit 72876c1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
8 changes: 4 additions & 4 deletions txnsql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
type SqlAdapter interface {
txn.Adapter

// IsTx returns true if the current transaction is active.
IsTx() bool
// Returns current transaction if it exists.
Tx() *sql.Tx
}

// New creates a new SqlAdapter instance using the provided *sql.DB.
Expand Down Expand Up @@ -59,6 +59,6 @@ func (a *sqlAdapter) End(_ context.Context) {
}
}

func (a *sqlAdapter) IsTx() bool {
return a.tx != nil
func (a *sqlAdapter) Tx() *sql.Tx {
return a.tx
}
10 changes: 5 additions & 5 deletions txnsql/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,23 +141,23 @@ func TestSqlAdapter_End(t *testing.T) {
})
}

func TestSqlAdapter_IsTx(t *testing.T) {
func TestSqlAdapter_Tx(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer db.Close()

t.Run("IsTx true", func(t *testing.T) {
t.Run("Tx is not nil", func(t *testing.T) {
adapter := &sqlAdapter{db: db}
mock.ExpectBegin()

adapter.Begin(context.Background())
assert.True(t, adapter.IsTx())
assert.True(t, adapter.Tx() != nil)
})

t.Run("IsTx false", func(t *testing.T) {
t.Run("Tx is nil", func(t *testing.T) {
adapter := &sqlAdapter{db: db}
assert.False(t, adapter.IsTx())
assert.True(t, adapter.Tx() == nil)
})
}

0 comments on commit 72876c1

Please sign in to comment.