Coder Social home page Coder Social logo

Comments (1)

pashagolub avatar pashagolub commented on August 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.