-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
105 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package database | ||
|
||
//go:generate mockgen -destination=./mock/configuration.go -package=mock -mock_names=Configuration=Configuration github.com/cloudtrust/common-service Configuration | ||
//go:generate mockgen -destination=./mock/cloudtrustdb.go -package=mock -mock_names=CloudtrustDB=CloudtrustDB github.com/cloudtrust/common-service/database CloudtrustDB |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package database | ||
|
||
// Transaction interface | ||
type Transaction interface { | ||
Commit() error | ||
Rollback() error | ||
|
||
// Close: if not explicitely Commited or Rolled back, Rollback the transaction | ||
Close() error | ||
} | ||
|
||
type dbTransaction struct { | ||
db CloudtrustDB | ||
closed bool | ||
} | ||
|
||
// NewTransaction creates a transaction | ||
func NewTransaction(db CloudtrustDB) (Transaction, error) { | ||
var _, err = db.Exec("START TRANSACTION") | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &dbTransaction{db: db, closed: false}, nil | ||
} | ||
|
||
func (tx *dbTransaction) close(cmd string) error { | ||
if tx.closed { | ||
return nil | ||
} | ||
var _, err = tx.db.Exec(cmd) | ||
tx.closed = true | ||
return err | ||
} | ||
|
||
func (tx *dbTransaction) Commit() error { | ||
return tx.close("COMMIT") | ||
} | ||
|
||
func (tx *dbTransaction) Rollback() error { | ||
return tx.close("ROLLBACK") | ||
} | ||
|
||
func (tx *dbTransaction) Close() error { | ||
return tx.Rollback() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package database | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/cloudtrust/common-service/database/mock" | ||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTransaction(t *testing.T) { | ||
var mockCtrl = gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
var tx Transaction | ||
var mockDB = mock.NewCloudtrustDB(mockCtrl) | ||
|
||
mockDB.EXPECT().Exec("START TRANSACTION").Return(nil, errors.New("db error")).Times(1) | ||
_, err := NewTransaction(mockDB) | ||
assert.NotNil(t, err) | ||
|
||
mockDB.EXPECT().Exec("START TRANSACTION").Return(nil, nil).Times(1) | ||
tx, err = NewTransaction(mockDB) | ||
assert.Nil(t, err) | ||
|
||
mockDB.EXPECT().Exec("COMMIT").Return(nil, nil).Times(1) | ||
assert.Nil(t, tx.Commit()) | ||
|
||
// Already closed | ||
assert.Nil(t, tx.Close()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters