Comments (1)
Hello, and thanks for your interest.
Do I must modify my structure in this way?
Well, you don't need to use pgxmock.PgxIface
. It's possible, but I would recommend something other than that for one reason: you don't want to use anything from the testing environment in your production build. To overcome this problem, you may define and use your own interface. As a start, you may copy-paste PgxPoolIface
and PgxIface
into your code. You can even rename it. The main thing is that this interface will define the methods of the pool.
Check my own implementation of this technique in pg_timetable.
Is it necessary and feasible to implement such a modification?
We can mock only interfaces. Because interface is an agreement, not an implementation. For example, it's straightforward with sql.Db
because it's an interface. It's a contract on what methods underlying objects should implement.
On the contrary, pgx provides us with the structs pgxConn
and pgxPool
. That means they are hard coded. If you use objects (structs) in your struct, that means you nail down their functionality. That's precisely why we want to put our own interfaces in the struct. This way, we can use unique mocking test objects during tests and real pgx objects during the actual workload.
I hope I'm clean enough. If not, you may try to read this article. Check the "Interface substitution" part.
I am still learning to write unit tests. If I make some basic mistakes, I would appreciate your guidance.
I can completely understand your question and frustration. I've been there. :) As I wrote, it's easier to mock sql.Db
because it was made to be an ephemeral interface. That's the reason why pgx library exists in the first place. Because it's nearly impossible to write native PostgreSQL implementation of some functions following sql.Db
interface contract.
from pgxmock.
Related Issues (20)
- `ExpectedCopyFrom.String()` outputs table name instead of columns names
- Link does not exist HOT 1
- ExpectCopyFrom not working as expected HOT 6
- pgxmock.AddRows makes []bytes conversion panic HOT 6
- Rows does not correctly works for QueryRow HOT 3
- Support for pgxpool Stat() in PgxPoolIface
- Add missing methods to PgxPoolIface HOT 2
- Deallocate is not part of a connection pool HOT 8
- Sweep: implement `func (c *pgxmock) ExpectReset() *ExpectedReset` HOT 2
- Sweep: Extend support for batch operation mocking / assertions HOT 1
- Staticcheck Linting Error: undefined 'max' in expectations.go HOT 1
- Request WithNamedArgs or something similar HOT 4
- Issues with new NamedArgs mocks HOT 7
- modification to support golang 1.19
- Scanning a single row without using rows.Next() HOT 2
- pgxmock.ExpectBegin is not documented HOT 6
- When you close row, the test will panic HOT 5
- panic when calling rows.Values() HOT 13
- Extend support for batch operation mocking / assertions
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pgxmock.