An example of a easily tested, SQL-backed JSON web service in Go.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
db/migrations
test
README.md
client.go
contacts.go
database.go
service.go
service_test.go

README.md

This repo demonstrates how to write an easily-tested JSON web service in Go, with a PostgreSQL database. It's a companion to a blog post I wrote.

Tests in this project run against a local PostgreSQL server, and make real HTTP calls to ensure that it works from end-to-end.

Overview

Here's the gist of how tests work:

  • The test sub-package contains shared test setup and helpers.

  • The test.Env type manages setting up and tearing down global test state, like the connection to PostgreSQL.

  • PostgreSQL is run locally (I use Postgres.app), and located via the DATABASE_URL environment variable.

  • The migrate package is used to create a clean database before each test.

  • The standard net/http/httptest package provides a test HTTP server that runs the API code.

  • Test requests are made using standard HTTP.

Dive in and look at the tests, or read the blog post for more detail.

Running the Tests

Tests are run via go test, just like any other Go project. However, the tests require a PostgreSQL database. The database URL is passed in via the DATABASE_URL environment variable, like this:

DATABASE_URL="postgres://username@localhost:5432/service_test?sslmode=disable" go test

Contact Me!

I'd love to know if this helps you! I'm @nerdyc on Twitter.