Four events arrive. One has two tags, one has a single tag, and two have no tags. A dashboard should show four events and three tag assignments. A developer reuses the query behind the tag breakdown, and the dashboard suddenly reports three events. What would it take for a Go test to catch that mistake before the query reaches ClickHouse?
How the Go stub evolved
In ClickHouse API Wrapper and Stub in Go, we put a Go interface in front of the database. The production implementation sent statements to ClickHouse. In tests, a callback received the SQL and returned rows chosen by the test. That made it easy to ask what the caller would do with an empty result. It also meant a misspelled column could pass a test if the callback returned the expected rows.
We used another approach in PostgreSQL Implementation and Stub in GO. Its test stub opened an in-memory SQLite database. An insert changed what the next select could see, and joins and filters actually ran. The same idea worked for many of our ClickHouse tests while the queries stayed within the SQL that both engines understood.
ClickHouse arrays exposed the cost of that approach. SQLite can expand a JSON array with json_each, but it cannot run a ClickHouse ARRAY JOIN as written. To keep the SQLite stub, we had to choose a representation for Array(String), translate the query, and decide how empty arrays should behave. Timestamp precision and aggregate functions added more translation rules. A passing test then depended on our interpretation of each ClickHouse feature. With chDB the test feeds the original query to an embedded ClickHouse engine, and we can spend that effort choosing rows that expose a mistake.
Where did the fourth event go?
Suppose we already use ARRAY JOIN to build a breakdown by tag. It is tempting to reuse that query for the dashboard totals. Look at the rows ClickHouse produces:
SELECT id, tag
FROM values('id UInt8, tags Array(String)',
(1, ['api', 'slow']), (2, ['api']), (3, []), (4, []))
ARRAY JOIN tags AS tag
ORDER BY id, tag
id tag
1 api
1 slow
2 api
Event 1 appears twice. Events 3 and 4 disappear because their arrays are empty. Counting the resulting rows gives three. Even countDistinct(id) gives only two, since two event IDs have already dropped out. For the totals we can count the original rows and add up their array lengths before expanding anything.
1. The Go interface (report.go)
The dashboard asks for two totals. TotalsReader defines that operation, and DashboardTotals shows how the caller accepts the interface.
package report
import "context"
type Totals struct {
Events, TagAssignments uint64
}
type TotalsReader interface {
ReadTotals(context.Context) (Totals, error)
}
func DashboardTotals(ctx context.Context, reader TotalsReader) (Totals, error) {
return reader.ReadTotals(ctx)
}
2. The production implementation (clickhouse_impl.go)
ClickHouseImpl receives the live connection opened by the application with the ClickHouse Go driver. Its ReadTotals method runs the query through that connection. The compiler checks that *ClickHouseImpl implements TotalsReader.
package report
import (
"context"
"database/sql"
)
type ClickHouseImpl struct{ DB *sql.DB }
var _ TotalsReader = (*ClickHouseImpl)(nil)
func (c *ClickHouseImpl) ReadTotals(ctx context.Context) (Totals, error) {
return queryTotals(ctx, c.DB)
}
func queryTotals(ctx context.Context, db *sql.DB) (Totals, error) {
const query = "SELECT count(), sum(length(tags)) FROM events"
var totals Totals
err := db.QueryRowContext(ctx, query).Scan(&totals.Events, &totals.TagAssignments)
return totals, err
}
At startup, once db is connected to the production ClickHouse server, the caller uses DashboardTotals(ctx, &ClickHouseImpl{DB: db}).
3. The test stub (report_test.go)
ClickHouseStub owns a connection to an embedded chDB database. Its constructor creates the table and seeds the four events. Its ReadTotals method calls the same queryTotals function as ClickHouseImpl, so an edit to the dashboard SQL is exercised by the test. The stub type, constructor, and chDB import all live in the test file.
package report
import (
"context"
"database/sql"
"testing"
_ "github.com/chdb-io/chdb-go/v2/chdb/driver"
)
type ClickHouseStub struct{ db *sql.DB }
var _ TotalsReader = (*ClickHouseStub)(nil)
func (s *ClickHouseStub) ReadTotals(ctx context.Context) (Totals, error) {
return queryTotals(ctx, s.db)
}
func newReportStub(t *testing.T) *ClickHouseStub {
t.Helper()
db, err := sql.Open("chdb", "session="+t.TempDir())
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { db.Close() })
db.SetMaxOpenConns(1)
for _, stmt := range []string{
"CREATE TABLE events (id UInt8, tags Array(String)) ENGINE = Memory",
"INSERT INTO events VALUES (1, ['api','slow']), (2, ['api']), (3, []), (4, [])",
} {
if _, err := db.ExecContext(t.Context(), stmt); err != nil {
t.Fatal(err)
}
}
return &ClickHouseStub{db: db}
}
func TestReportTotals(t *testing.T) {
got, err := DashboardTotals(t.Context(), newReportStub(t))
if err != nil {
t.Fatal(err)
}
if want := (Totals{Events: 4, TagAssignments: 3}); got != want {
t.Fatalf("got %+v, want %+v", got, want)
}
}
Run the example on a desktop
Save all three files in an empty directory and initialize the Go module. This example uses Go 1.24 or later for t.Context().
go mod init example.com/report
go get github.com/chdb-io/chdb-go/v2/chdb/driver@v2.4.0
There are two dependencies to pin. The Go module provides the driver. The native libchdb library provides the SQL engine and must match the operating system and CPU of the test process. Our desktop installer pins chDB 26.11.1 and selects the Linux or macOS archive for x86-64 or ARM64. The release assets are available here.
The install script caches the extracted library under ~/.local/lib/chdb/<version>/<platform>/. On the first run it downloads the archive into a temporary directory, checks it against the SHA-256 digest pinned for that asset, extracts libchdb.so, and moves the completed file into the cache. Subsequent runs reuse the file. It writes progress to standard error and prints only the absolute library path to standard output, so the caller can capture it:
library=$(./deploy/chdb/install_chdb.sh) || exit 1
CHDB_LIB_PATH="$library" go test -v -run '^TestReportTotals$' ./...
Those commands assume the installer is in your checkout. To run just the three-file example, extract the matching release archive yourself and set CHDB_LIB_PATH to the full path of its libchdb.so. For an IDE test run, set the same variable in the Go test configuration. The test should return Events=4 and TagAssignments=3. Append ARRAY JOIN tags AS tag to the report query and run it again. The event count drops to three.
Run the same engine in Jenkins
On a Linux Jenkins agent with a local Docker daemon, the test runs inside a Go container. A path in the agent's home directory only helps the container if we mount the file. The Jenkins wrapper calls the same installer on the agent, mounts the cached library read-only, and sets CHDB_LIB_PATH to the path inside the container. Its core is:
#!/usr/bin/env bash
set -euo pipefail
library=$("$(dirname "$0")/install_chdb.sh")
target=/usr/local/lib/libchdb.so
exec docker run --mount "type=bind,src=$library,dst=$target,readonly" \
-e "CHDB_LIB_PATH=$target" "$@"
A Jenkins shell step can then run the tests from the mounted checkout:
./deploy/chdb/jenkins_docker_run.sh --rm \
--mount "type=bind,src=$WORKSPACE,dst=/src" -w /src \
golang:1.26.4 go test ./...
The wrapper forwards Docker's exit status to Jenkins, so a failing query fails the build. Each test gets its own temporary database. Builds on the same agent reuse the library download. The bind mount assumes the Docker daemon can see the agent's cache and the container uses a compatible Linux architecture. On a Mac desktop, use the macOS library for native Go tests and a Linux library for tests inside a Linux container.
What still belongs in a server-backed test?
This test catches a wrong answer from the report SQL. Connecting with production credentials, handling network timeouts, and executing DDL across shards or replicas all require a server environment. ClickHouse Cluster Agnostic DDL explains the distinction for schema changes. Pin the embedded engine in CI and check its version and relevant settings against the deployed ClickHouse version when results disagree.
Final note
For the dashboard regression, the most valuable fixture is the second untagged event. Remove it and both totals happen to be three. Keeping it makes the difference between counting events and counting tag assignments visible in a single assertion.
