Using SQLC in project how do I mock database Calls with it for unit testing?

This page summarizes the projects mentioned and recommended in the original post on /r/golang

InfluxDB – Built for High-Performance Time Series Workloads
InfluxDB 3 OSS is now GA. Transform, enrich, and act on time series data directly in the database. Automate critical tasks and eliminate the need to move data externally. Download now.
www.influxdata.com
featured
Stream - Scalable APIs for Chat, Feeds, Moderation, & Video.
Stream helps developers build engaging apps that scale to millions with performant and flexible Chat, Feeds, Moderation, and Video APIs and SDKs powered by a global edge network and enterprise-grade infrastructure.
getstream.io
featured
  1. go-sqlmock

    Sql mock driver for golang to test database interactions

    It's not the right call IMO to skip mocking the database connection to achieve 100% test coverage. How your app will behave in failure scenarios that are impossible to imitate during integration tests is part of the software contract. If your choice is to panic, or return an error, document that by testing that behavior. If another dev, or future you inadvertently breaks the contract, the test suite will fail. That's what you want. For unit tests against your database you should be using either go-sqlmock if testing against database/sql or pgxmock if testing against pgx. That being said, the points raised elsewhere in this thread regarding unit tests potentially hiding edge cases in terms of how an actual database will interact with your application that are not reflective of your understanding when writing mocks are 100% valid. You should do both. Unit test your app and write integration tests as well. On my team, we run integration tests using docker-compose.

  2. InfluxDB

    InfluxDB – Built for High-Performance Time Series Workloads. InfluxDB 3 OSS is now GA. Transform, enrich, and act on time series data directly in the database. Automate critical tasks and eliminate the need to move data externally. Download now.

    InfluxDB logo
  3. pgxmock

    pgx mock driver for golang to test database interactions

    It's not the right call IMO to skip mocking the database connection to achieve 100% test coverage. How your app will behave in failure scenarios that are impossible to imitate during integration tests is part of the software contract. If your choice is to panic, or return an error, document that by testing that behavior. If another dev, or future you inadvertently breaks the contract, the test suite will fail. That's what you want. For unit tests against your database you should be using either go-sqlmock if testing against database/sql or pgxmock if testing against pgx. That being said, the points raised elsewhere in this thread regarding unit tests potentially hiding edge cases in terms of how an actual database will interact with your application that are not reflective of your understanding when writing mocks are 100% valid. You should do both. Unit test your app and write integration tests as well. On my team, we run integration tests using docker-compose.

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts

  • [Go, Book Review] Learn Go with Tests by Chris James

    1 project | dev.to | 23 Mar 2024
  • quii/learn-go-with-tests: Learn Go with test-driven development

    1 project | /r/devopsish | 20 Aug 2023
  • Poke 2 Color and Tab Mini C

    1 project | /r/Onyx_Boox | 9 Jul 2023
  • Why elixir over Golang

    10 projects | /r/elixir | 29 May 2023
  • Please share programming/CS books and articles that use Go as examples(but not strictly about Go)

    1 project | /r/golang | 23 Oct 2022

Did you know that Go is
the 4th most popular programming language
based on number of references?