-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add postgres storage implementation tests
- Loading branch information
1 parent
7ce5537
commit af947a2
Showing
4 changed files
with
188 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,174 @@ | ||
package storage | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"github.com/DATA-DOG/go-sqlmock" | ||
"log" | ||
"testing" | ||
) | ||
|
||
const ( | ||
insertQueryShort = "INSERT INTO \"table\"" | ||
) | ||
|
||
func TestGetStorage(t *testing.T) { | ||
db, _, err := sqlmock.New() | ||
if err != nil { | ||
log.Panicf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
defer db.Close() | ||
|
||
storage := postgresStorage{db: db} | ||
|
||
if storage.Get() != db { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestTransactionSuccessful(t *testing.T) { | ||
db, sqlMock, err := sqlmock.New() | ||
if err != nil { | ||
log.Panicf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
defer db.Close() | ||
|
||
storage := postgresStorage{db: db} | ||
|
||
sqlMock.ExpectBegin() | ||
|
||
var expectedLastInsertID int64 = 1 | ||
var expectedLastRowsAffected int64 = 1 | ||
expectedResult := sqlmock.NewResult(expectedLastInsertID, expectedLastRowsAffected) | ||
sqlMock.ExpectExec(insertQueryShort).WithArgs("arg1", "arg2", "arg3").WillReturnResult(expectedResult) | ||
|
||
sqlMock.ExpectCommit() | ||
|
||
txErr := storage.Transaction(context.TODO(), func(context context.Context, tx *sql.Tx) error { | ||
actualResult, err := tx.Exec(insertQueryShort, "arg1", "arg2", "arg3") | ||
actualLastInsertID, _ := actualResult.LastInsertId() | ||
actualRowsAffected, _ := actualResult.RowsAffected() | ||
|
||
if actualLastInsertID != expectedLastInsertID || actualRowsAffected != expectedLastRowsAffected { | ||
t.Fail() | ||
} | ||
return err | ||
}) | ||
|
||
if txErr != nil { | ||
t.Fail() | ||
} | ||
|
||
if err := sqlMock.ExpectationsWereMet(); err != nil { | ||
t.Errorf("there were unfulfilled expectations: %s", err) | ||
} | ||
} | ||
|
||
func TestTransactionFailToBeginTransaction(t *testing.T) { | ||
db, sqlMock, err := sqlmock.New() | ||
if err != nil { | ||
log.Panicf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
defer db.Close() | ||
|
||
sqlMock.ExpectBegin() | ||
|
||
storage := postgresStorage{db: db} | ||
txErr := storage.Transaction(context.TODO(), func(context context.Context, tx *sql.Tx) error { | ||
return nil | ||
}) | ||
|
||
if txErr == nil { | ||
t.Fail() | ||
} | ||
|
||
if err := sqlMock.ExpectationsWereMet(); err != nil { | ||
t.Errorf("there were unfulfilled expectations: %s", err) | ||
} | ||
} | ||
|
||
func TestTransactionSuccessfulRollback(t *testing.T) { | ||
db, sqlMock, err := sqlmock.New() | ||
if err != nil { | ||
log.Panicf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
defer db.Close() | ||
|
||
sqlMock.ExpectBegin() | ||
sqlMock.ExpectRollback() | ||
|
||
storage := postgresStorage{db: db} | ||
expectedErr := fmt.Errorf("unexpected error") | ||
txErr := storage.Transaction(context.TODO(), func(context context.Context, tx *sql.Tx) error { | ||
return expectedErr | ||
}) | ||
|
||
if txErr != expectedErr { | ||
t.Fail() | ||
} | ||
|
||
if err := sqlMock.ExpectationsWereMet(); err != nil { | ||
t.Errorf("there were unfulfilled expectations: %s", err) | ||
} | ||
} | ||
|
||
func TestTransactionFailRollback(t *testing.T) { | ||
db, sqlMock, err := sqlmock.New() | ||
if err != nil { | ||
log.Panicf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
defer db.Close() | ||
|
||
sqlMock.ExpectBegin() | ||
|
||
storage := postgresStorage{db: db} | ||
expectedErr := fmt.Errorf("unexpected error") | ||
txErr := storage.Transaction(context.TODO(), func(context context.Context, tx *sql.Tx) error { | ||
return expectedErr | ||
}) | ||
|
||
if txErr == nil || txErr == expectedErr { | ||
t.Fail() | ||
} | ||
|
||
if err := sqlMock.ExpectationsWereMet(); err != nil { | ||
t.Errorf("there were unfulfilled expectations: %s", err) | ||
} | ||
} | ||
|
||
func TestTransactionFailToCommitTransaction(t *testing.T) { | ||
db, sqlMock, err := sqlmock.New() | ||
if err != nil { | ||
log.Panicf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
defer db.Close() | ||
|
||
storage := postgresStorage{db: db} | ||
txErr := storage.Transaction(context.TODO(), func(context context.Context, tx *sql.Tx) error { | ||
return nil | ||
}) | ||
|
||
if txErr == nil { | ||
t.Fail() | ||
} | ||
|
||
if err := sqlMock.ExpectationsWereMet(); err != nil { | ||
t.Errorf("there were unfulfilled expectations: %s", err) | ||
} | ||
} | ||
|
||
func TestCloseStorage(t *testing.T) { | ||
db, _, err := sqlmock.New() | ||
if err != nil { | ||
log.Panicf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
|
||
storage := postgresStorage{db: db} | ||
storage.Close() | ||
|
||
err = db.Ping() | ||
if err == nil { | ||
t.Fail() | ||
} | ||
} |