Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to filter mocked rows? #236

Open
rafa-acioly opened this issue Sep 11, 2020 · 2 comments
Open

How to filter mocked rows? #236

rafa-acioly opened this issue Sep 11, 2020 · 2 comments

Comments

@rafa-acioly
Copy link

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
Copy link
Collaborator

rmulley commented Sep 15, 2020

@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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants