Skip to content

Latest commit

 

History

History
35 lines (20 loc) · 1.54 KB

README.md

File metadata and controls

35 lines (20 loc) · 1.54 KB

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.