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

Generate in-memory mocked implementation of the Querier interface #3363

Closed
Adibov opened this issue May 5, 2024 · 2 comments
Closed

Generate in-memory mocked implementation of the Querier interface #3363

Adibov opened this issue May 5, 2024 · 2 comments
Labels
enhancement New feature or request triage New issues that hasn't been reviewed

Comments

@Adibov
Copy link

Adibov commented May 5, 2024

What do you want to change?

We've faced scenarios in which we had to mock the Querier interface, a practical example is to use a mocked implementation for unit tests. A fine-grained approach to mocking this interface is to have an in-memory mocked implementation of this interface. This not only can act as an implementation with high fidelity but also is so fast (since it's in-memory) that can be used in unit tests.

As models and queries change, keeping the mocked implementation is challenging. I've come up with an idea to solve this problem and I would like to share it to get informed about the project owners' opinions.

Since sqlc is a code-gen tool, my idea is to add an option to it to be able to also generate an in-memory mocked implementation. The basis of my idea is to have a struct having a list of each database table as its field. A practical example would be as follows:

type MockedImplementation struct {
    Users []*database.User
    Payments []*database.Payment
}

In this way, we can implement each SQL query by aggregating these fields.

Let me know about your opinions on this idea and I really appreciate any recommendation. I'm also eager to make an MVP for Golang language if the idea get accepted.

What database engines need to be changed?

No response

What programming language backends need to be changed?

Go

@Adibov Adibov added enhancement New feature or request triage New issues that hasn't been reviewed labels May 5, 2024
@Adibov
Copy link
Author

Adibov commented May 5, 2024

I think a better approach would be to create an instance of Queries struct by using the following module:
https://github.com/proullon/ramsql

@kyleconroy
Copy link
Collaborator

@Adibov I think this is a great idea, but it's not going to fit into the main tool. It would work great as a plugin though. Happy to help show you how that works. Your starting point can be https://github.com/sqlc-dev/sqlc-gen-go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request triage New issues that hasn't been reviewed
Projects
None yet
Development

No branches or pull requests

2 participants