go-sqlmock icon indicating copy to clipboard operation
go-sqlmock copied to clipboard

How to filter mocked rows?

Open rafa-acioly opened this issue 5 years ago • 2 comments
trafficstars

It is possible to filter a row within the mocked rows? When i try to perform a query the result is always empty.

type RepositoryTestSuit struct {
	suite.Suite
	repository Repository
	database   *sql.DB
	mock       sqlmock.Sqlmock
}

func (suite *RepositoryTestSuit) SetupSuite() {
	db, mock, _ := sqlmock.New()
	suite.mock = mock
	suite.database = db

	suite.repository = NewRepository(db)
}

func (suite *RepositoryTestSuit) TestIndexFilterNext() {
    var mockedRows = sqlmock.NewRows([]string{"id", "start_at"}).
        AddRow("0", time.Now().AddDate(0, 0, 1)). // tomorrow
        AddRow("another-id", time.Now().AddDate(0, 0, -1)) // yesterday

    suite.mock.ExpectQuery("SELECT \\* FROM table").WillReturnRows(mockedRows)

    result, _ = suite.repository.Index()

   // fmt.Println(len(result)) -- 0
}

func TestRepository(t *testing.T) {
	suite.Run(t, new(RepositoryTestSuit))
}

The repository:

func (r *Repository) Index() ([]Mystruct, error) {
    rows, _ := r.database.Query("SELECT * FROM table WHERE start_at >= NOW()")
   
    for rows.Next() {} // never enter the loop because the result is empty
}

rafa-acioly avatar Sep 11 '20 16:09 rafa-acioly

@rafa-acioly From what I can tell the loop is likely never entered because r.database is never overwritten with the mock suite.database.

For example I would expect to see something like:

suite.repository.database = suite.database

rmulley avatar Sep 15 '20 19:09 rmulley

@rmulley i overwrite the database when i create a New Repository, here's the code from NewRepository

type Repository struct {
    database *sql.DB
}

func NewRepository(db *sql.DB) Repository {
    return Repository{database: db}
}

func (r Repository) Index() ([]Mystruct, error) {
    rows, _ := r.database.Query("SELECT * FROM table WHERE start_at >= NOW()")
   
    for rows.Next() {} // never enter the loop because the result is empty
}

So when i Setup the test:

func (suite *RepositoryTestSuit) SetupSuite() {
	db, mock, _ := sqlmock.New()
	suite.mock = mock
	suite.database = db

	suite.repository = NewRepository(db) // <- passes the database to the repository
}

rafa-acioly avatar Sep 16 '20 00:09 rafa-acioly