Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 39 ยท Testing and Deployment

Testing with Databases, Fakes and Middleware

Test Gin handlers that use a database: a Store interface with fakes, error injection, in-memory SQLite with GORM, test helpers, middleware tests and integration tests.

Handlers rarely stand alone: they read from and write to a database, call other services and sit behind middleware. Tests must control those collaborators. If every test needs a running MySQL server and a lucky data state, nobody will run them. This lesson shows the three levels most Gin projects use: fakes behind an interface for fast unit tests, an in-memory database for realistic data-layer tests, and a few integration tests against the real thing.

The Gin, GORM and testing code is read-only here. The runnable examples use net/http and plain Go to demonstrate the same patterns. If you have not read Testing Gin Handlers, start there.

Put an interface between handlers and storage

A handler that calls db.Where(...) directly can only be tested with a database. A handler that calls an interface can be tested with anything.

Go
type TodoStore interface {
	List(ctx context.Context) ([]Todo, error)
	Get(ctx context.Context, id int) (Todo, error)
	Create(ctx context.Context, title string) (Todo, error)
}

var ErrNotFound = errors.New("not found")

type TodoHandler struct{ Store TodoStore }

func (h TodoHandler) Get(c *gin.Context) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
		return
	}
	todo, err := h.Store.Get(c.Request.Context(), id)
	switch {
	case errors.Is(err, ErrNotFound):
		c.JSON(http.StatusNotFound, gin.H{"error": "todo not found"})
	case err != nil:
		c.JSON(http.StatusInternalServerError, gin.H{"error": "internal error"})
	default:
		c.JSON(http.StatusOK, todo)
	}
}

Two implementations satisfy it: GormStore for production and FakeStore for tests. The interface stays small: only what handlers need, defined in the package that uses it. Go interfaces are satisfied implicitly, so the real store does not need to know the interface exists.

A fake, with failure injection

A fake is a simple working implementation (usually a map) that can also be told to fail. Failure injection lets you test paths that are hard to produce with a real database, such as a lost connection.

Go A fake store drives every handler branch

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

(The //go:debug first line only enables path patterns in this module-less runner.) Four requests, four different branches: success, 404, 400 and 500. Note that the 500 body says only "internal error": the real error text (connection refused) belongs in your logs, not in the response.

A real database in memory

Fakes prove handler logic. They cannot prove your SQL, GORM tags or migrations are right. For that, run the real data layer against an in-memory SQLite database, which starts in milliseconds and is thrown away after each test.

Go
import (
	"github.com/glebarez/sqlite" // pure Go; or gorm.io/driver/sqlite with cgo
	"gorm.io/gorm"
)

func newTestDB(t *testing.T) *gorm.DB {
	t.Helper()
	db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
	if err != nil {
		t.Fatal(err)
	}
	if err := db.AutoMigrate(&Todo{}, &User{}); err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() {
		sqlDB, _ := db.DB()
		sqlDB.Close()
	})
	return db
}

func TestGormStoreCreateAndGet(t *testing.T) {
	store := NewGormStore(newTestDB(t))

	created, err := store.Create(context.Background(), "Write docs")
	require.NoError(t, err)

	got, err := store.Get(context.Background(), created.ID)
	require.NoError(t, err)
	assert.Equal(t, "Write docs", got.Title)

	_, err = store.Get(context.Background(), 9999)
	assert.ErrorIs(t, err, ErrNotFound)
}

t.Helper() makes failures point at the calling test instead of the helper, and t.Cleanup runs after the test, even when it fails. Keep in mind that SQLite and MySQL differ (types, locking, case rules, ON UPDATE). When a query uses a database-specific feature, run that test against the real engine (see integration tests below). For more on migrations in tests, see Transactions, Hooks and Migrations.

Fast isolation with transactions

Instead of recreating the schema per test, you can open a transaction, run the test against it and roll it back:

Go
func withTx(t *testing.T, db *gorm.DB) *gorm.DB {
	tx := db.Begin()
	t.Cleanup(func() { tx.Rollback() })
	return tx
}

Every test sees a clean database, and the schema is created once in TestMain.

Testing middleware on its own

Middleware is just a handler wrapper, so test it with a one-route router and a handler that records what it saw.

Go
func TestRequireAdmin(t *testing.T) {
	gin.SetMode(gin.TestMode)
	r := gin.New()
	r.Use(func(c *gin.Context) { c.Set("role", c.GetHeader("X-Test-Role")) }) // fake auth
	r.GET("/admin", RequireRole("admin"), func(c *gin.Context) { c.String(200, "ok") })

	for role, want := range map[string]int{"admin": 200, "editor": 403, "": 403} {
		w := httptest.NewRecorder()
		req := httptest.NewRequest("GET", "/admin", nil)
		req.Header.Set("X-Test-Role", role)
		r.ServeHTTP(w, req)
		assert.Equal(t, want, w.Code, "role %q", role)
	}
}

Here a tiny stand-in middleware plays the part of authentication, so the test exercises only RequireRole.

Go Testing middleware with a recording handler

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

The reached counter proves that the rejected requests never touched the protected handler, which is the property that matters.

Integration tests

A small number of tests should run the whole stack against real services: the router, real middleware, a real MySQL or PostgreSQL started with Docker Compose or testcontainers-go. They are slower and more fragile, so keep them few and focused on critical flows (sign up, log in, create an order). Mark them so they are opt-in:

Go
//go:build integration

package app_test

Run them with go test -tags=integration ./..., usually in CI only. A healthy suite is a pyramid: many fast tests with fakes, some with an in-memory database, a few full-stack tests.

flowchart TB
  A["Few: integration tests<br/>real DB, full stack"] --> B["Some: data-layer tests<br/>in-memory SQLite"]
  B --> C["Many: handler and middleware tests<br/>fakes, httptest"]

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why put an interface between handlers and the database?

    Handlers that depend on an interface can be tested with an in-memory fake, with no database running.
  2. 2. What is failure injection in a fake?

    Setting an error on the fake lets you check that the handler answers 500 without leaking details.
  3. 3. What does t.Cleanup do?

    Cleanup functions run in reverse order after the test and its subtests complete.
  4. 4. When should you test against the real database engine instead of SQLite?

    SQLite differs from MySQL and PostgreSQL in types, locking and functions, so engine-specific queries need the real engine.
  5. 5. Why should a 500 response say only 'internal error'?

    Log the detail for yourself; give clients a safe, generic message.

Summary

  • Depend on small interfaces; use fakes with failure injection for fast handler tests.
  • Test the data layer with in-memory SQLite (t.Helper, t.Cleanup, transactions for isolation), and use the real engine where behavior differs.
  • Test middleware alone with a tiny router and a recording handler.
  • Keep a few integration tests behind a build tag and run them in CI.

Next: Docker and Deployment