DATA-DOG / go-sqlmock

Sql mock driver for golang to test database interactions
Other
6.05k stars 406 forks source link

How to filter mocked rows? #236

Open rafa-acioly opened 4 years ago

rafa-acioly commented 4 years ago

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
}
rmulley commented 3 years ago

@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
rafa-acioly commented 3 years ago

@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
}