Created
May 18, 2020 06:58
-
-
Save aeharvlee/473076946b1d49a9cb7fd3f588b9d29e to your computer and use it in GitHub Desktop.
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
package repository | |
import ( | |
"fmt" | |
"math/rand" | |
"testing" | |
"github.com/DATA-DOG/go-sqlmock" | |
) | |
func TestShouldGetReportByPeriod(t *testing.T) { | |
db, mock, err := sqlmock.New() | |
if err != nil { | |
t.Errorf("an error '%s' was not expected when opening a stub database connection", err) | |
} | |
defer db.Close() | |
columns := []string{ | |
"col1", "col2", "col3"} | |
tbl := "statistics" | |
limit := 2 | |
qry := fmt.Sprintf( | |
"SELECT "col1, col2, col3"+ | |
"FROM %s ORDER BY col3 DESC LIMIT %d", tbl, limit) | |
rows := sqlmock.NewRows(columns). | |
AddRow(0, 1, 2). | |
AddRow(2, 3, 4) | |
mock.ExpectQuery(`^SELECT (.+) FROM statistics ORDER BY col3 DESC LIMIT \d+([.]\\d+)?$`). | |
WillReturnRows(rows) | |
_, err = db.Query(qry) | |
if err != nil { | |
t.Errorf("error '%s' was not expected, while selecting a row", err) | |
} | |
if err = mock.ExpectationsWereMet(); err != nil { | |
t.Errorf("there were unfulfilled expectations: %s", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment