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

"Issue Testing Row Scan Error with go-sqlmock: Uncovered Error Handling in Repository Function #324

Open
Bangik opened this issue Oct 13, 2023 · 0 comments

Comments

@Bangik
Copy link

Bangik commented Oct 13, 2023

Issue Description

Hi... I'm facing an issue in my Go application when using go-sqlmock for unit testing my database interactions. The problem arises when trying to simulate a row scan error using sql.ErrNoRows. Although I've set up the test case for such a scenario, it seems that the specific line of code within my repository function is not covered by the test, and I'm not sure why. I would appreciate any help or insights on how to properly handle this case.

Code Sample

I have a function in my repository like this:

func (r *vendorRepository) List() ([]model.Vendor, error) {
    rows, err := r.db.Query("SELECT id, name, address, phone FROM vendors")
    if err != nil {
        return nil, err
    }

    var vendors []model.Vendor
    for rows.Next() {
        var vendor model.Vendor
        if err := rows.Scan(&vendor.Id, &vendor.Name, &vendor.Address, &vendor.Phone); err != nil {
            return nil, err
        }
        vendors = append(vendors, vendor)
    }

    return vendors, nil
}

And here are the relevant test cases:

func (s *VendorRepositorySuite) TestListSuccess() {
    // ... (omitting setup for successful query)
}

func (s *VendorRepositorySuite) TestListFail() {
    // ... (omitting setup for simulating sql.ErrNoRows)
}

func (s *VendorRepositorySuite) TestListRowScanError() {
    // ... (omitting setup for simulating a row scan error)
}

Problem

The issue I'm encountering is that the specific code block where I check for errors during row scanning:

if err := rows.Scan(&vendor.Id, &vendor.Name, &vendor.Address, &vendor.Phone); err != nil {
    return nil, err
}

is not being covered by my tests, even though I have a test case (TestListRowScanError) specifically designed to simulate the error using sql.ErrNoRows. The other test cases (TestListSuccess and TestListFail) are working as expected, but I'm having trouble understanding why the row scan error is not being triggered.

Requested Help

I would greatly appreciate any guidance or suggestions on how to properly test and cover the specific scenario of a row scan error using go-sqlmock. If there's anything I'm missing in my test setup or if there's a better way to test this kind of situation, please let me know.

Thank you in advance for your help and insights!

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

1 participant