Skip to content

Commit 72876c1

Browse files
authored
Merge pull request #2 from 9ssi7/feature/txnsql-adpater
feature/txnsql-adpater: upgrade the logic
2 parents 7f06b1a + fb24d2d commit 72876c1

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

txnsql/sql.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
type SqlAdapter interface {
1313
txn.Adapter
1414

15-
// IsTx returns true if the current transaction is active.
16-
IsTx() bool
15+
// Returns current transaction if it exists.
16+
Tx() *sql.Tx
1717
}
1818

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

62-
func (a *sqlAdapter) IsTx() bool {
63-
return a.tx != nil
62+
func (a *sqlAdapter) Tx() *sql.Tx {
63+
return a.tx
6464
}

txnsql/sql_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,23 +141,23 @@ func TestSqlAdapter_End(t *testing.T) {
141141
})
142142
}
143143

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

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

155155
adapter.Begin(context.Background())
156-
assert.True(t, adapter.IsTx())
156+
assert.True(t, adapter.Tx() != nil)
157157
})
158158

159-
t.Run("IsTx false", func(t *testing.T) {
159+
t.Run("Tx is nil", func(t *testing.T) {
160160
adapter := &sqlAdapter{db: db}
161-
assert.False(t, adapter.IsTx())
161+
assert.True(t, adapter.Tx() == nil)
162162
})
163163
}

0 commit comments

Comments
 (0)