[GODRIVER-834] Please convert Client, Database, Collection and others structs to interfaces to allow develop Mocks Created: 17/Feb/19 Updated: 01/Dec/21 Resolved: 04/Mar/19 |
|
| Status: | Closed |
| Project: | Go Driver |
| Component/s: | None |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Improvement | Priority: | Major - P3 |
| Reporter: | Aleksandr Sokolovskii | Assignee: | Unassigned |
| Resolution: | Won't Fix | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||
| Description |
|
Dear All, Please look at this cool library: https://github.com/DATA-DOG/go-sqlmock It is very very useful library to "mock" any SQL database for tests. I am ready to develop the same library for MongoDB. But the problem is Client, Database, Collection and others are structs. Could you please refactor them to Go interfaces (like in"sql" Go build-in package). It allows me to "mock" them. Thanks! |
| Comments |
| Comment by Sergey Vilgelm [ 09/Oct/21 ] |
|
In case if anyone still needs the interfaces, I implemented a simple wrapper to provide the interfaces for all main classes: https://github.com/sv-tools/mongoifc it has the interfaces for such object as:
the main problem that it requires to use the wrapper instead of direct object. |
| Comment by Aleksandr Sokolovskii [ 26/Apr/19 ] |
|
Hi, @Sam Ward, thank you for support. @Ian Whalen, there is Go official tool for mocking: https://github.com/golang/mock https://godoc.org/github.com/golang/mock/gomock It would be great to make driver API compatible with it. Thanks. |
| Comment by Sam Ward [ 21/Apr/19 ] |
|
Hey there, I believe the reason Aleksandr wants interfaces is that the best practice for testing in Go is through interfaces. For an example, look at how Amazon Web Service Go SDK provides interface ("iface") packages for every driver:
There is a very good reason for this: in Go tests we want to run our code and "mock" things like backends and other loosely coupled code, then make assertions with the mock. Here's a typical Go mocking library that one might use in their project – notice that it relies on interface types to work: https://github.com/matryer/moq – it generates mock implementations with hooks for tests. In summary, providing interfaces for the full API is expected by the community and absolutely required for any serious project that uses Mongo's Go driver. Please consider reopening this ticket and adding the interfaces to your library – the whole community would love it!
|
| Comment by Ian Whalen (Inactive) [ 04/Mar/19 ] |
|
Hi amsokol, closing this as Won't Fix since the equivalent types in database/sql are structs not interfaces. In the future we're planning to add functionality that will allow mocking at a lower level. |