Unit Test SQL in Golang, Without Mocking, Using Docker (Go, Postgres, Docker)

Unit Test SQL in Golang, Without Mocking, Using Docker (Go, Postgres, Docker)

Recently while working on an API that interfaced with a database, I mistakenly updated our ORM package to the latest version. The code…

UPDATE: I’ve written a companion article using TestContainers, based on a comment that I should look into that package. It accomplishes the same goal, testing database code without mocks.

Recently while working on an API that interfaced with a database, I mistakenly updated our ORM package to the latest version. The code compiled fine and all the tests passed with flying colors. However, in reality, the latest version of the package brought in breaking changes, which were very subtle. My code which worked great before, would now connect the database fine, run its queries without any issue, but would always return an empty dataset! This issue did not come to light until the code was pushed to QA, where it became immediately clear there was a problem. This was a tough one to catch, as no errors were thrown, just no data was returned.

So how did this issue get past the tests? Like the majority of Go engineers, we mocked all of our database code and interactions for unit testing. In this particular scenario, our mocks didn’t need to change for the updated package, so everything worked just fine until we hit a real database. This set me down a path of how could I run my tests against a real database, and completely remove the need for mocks.

Why Get Rid Of Mocks, They’re Great?!

Let me start by saying, I am not advocating for getting rid of mocks entirely. They absolutely have a place in testing and come in handy for a lot of things. For me, mocks are truly critical around resources you have zero control over. For instance, take a service like Twilio. If you need to call out to Twilio and process a response from their APIs, this is a perfect example of where mocking is important. You have no control over their APIs, you simply make a call and process the response. Often if you’re running in a CI/CD pipeline, you may not be able to make these calls, are may not just want to. Mocking is perfect here, as you don’t really care about the call to the API, only to ensure that your code can process the response.

docker testing golang postgres

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Running Golang Unit Tests Inside a Docker Container

Before we get started, this post can absolutely apply to any language where you have a need to run tests inside a Docker container. However, for this post, we’ll be looking at a Go project where I found myself in need of this very thing. Running Golang Unit Tests Inside a Docker Container

Docker Explained: Docker Architecture | Docker Registries

Following the second video about Docker basics, in this video, I explain Docker architecture and explain the different building blocks of the docker engine; docker client, API, Docker Daemon. I also explain what a docker registry is and I finish the video with a demo explaining and illustrating how to use Docker hub.

Software Testing 101: Regression Tests, Unit Tests, Integration Tests

How do scientists demonstrate that a drug or vaccine is effective? Putting it to the test. It's your only choice. And, if you're reasonable, you'd never use medications that haven't been thoroughly tested. So, why don't you test software as thoroughly as you should?

Testing Microservices Applications

The shift towards microservices and modular applications makes testing more important and more challenging at the same time. Learn more here.

How to Do API Testing?

API endpoint when you send the get request to that URL it returns the JSON response. In this article, I am going to use postman assertions for all the examples since it is the most popular tool. But this article is not intended only for the postman tool.