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 properly mock qrm.DB so that it works with jet Query method #38

Closed
MarioSimou opened this issue Apr 25, 2020 · 2 comments
Closed

Comments

@MarioSimou
Copy link

Hello,

I have started using the library for a pet project of mine and I am really excited about it. It removes a lot of the boilerplate that I need to write for anything related to databases. However, I had some issues when I tried to unit test some my code.

To be more precise, I use the qrm.DB interface to pass it to any Query made from jet and mock it for the unit tests, but I couldn't find a way to mok it work properly. When jet calls Query it's seems that internally calls the QueryContext method of the DB interface which has the following signature QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error).
It may either return a sql.Rows pointer or an err, but sql.Rows is a struct that doesn't expose any of its internal fields and I cannot pass anyting else except a nil pointer, which makes QueryContext to panic and propagates to jet Query method.

Can you suggest a solution or an alternative solution to this issue?

@MarioSimou MarioSimou changed the title How to properly mock qrm.DB so that it works with jet Query method How to properly mock qrm.DB so that it works with jet Query method Apr 25, 2020
@go-jet
Copy link
Owner

go-jet commented Apr 26, 2020

My suggestion would be not to mock the database. It adds unnecessary complexity to the project, for very little or no gain at all. Also every test that mocks database can be written without mock.
For instance:

func SomeServiceMethod(db, ...) {
    ...
    customer ->   query db to get account
    ... // business logic
    orders      -> query db to get orders
    ... // business logic
}

can be replaced with:

func SomeServiceMethod(db, ...) {
   data := struct{
        model.Customer
        Orders []model.Orders
   }
   someStatement.Query(db, data)

   bussinesLogic(data)
}

func bussinesLogic(data) {
    ... // business logic
    ... // business logic
}

bussinesLogic - is tested with black box unit tests
SomeServiceMethod - is tested with service tests(tests with real db connection)

If you still want to mock db, I found this library: https://github.com/DATA-DOG/go-sqlmock, but haven't tested it yet.

@MarioSimou
Copy link
Author

Oh, I see the point behind that but that would need to refactor a bit the code. However, I tested sqlmock with go-jet and it works fine. So you can proceed closing this issue.

@go-jet go-jet closed this as completed Apr 26, 2020
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