Skip to content

Add a complete runnable Postgres example (fixes #24) - #38

Open
peczenyj wants to merge 2 commits into
cockroachdb:masterfrom
peczenyj:examples-pq
Open

Add a complete runnable Postgres example (fixes #24)#38
peczenyj wants to merge 2 commits into
cockroachdb:masterfrom
peczenyj:examples-pq

Conversation

@peczenyj

@peczenyj peczenyj commented Jun 4, 2026

Copy link
Copy Markdown

Summary

Fixes #24.

This adds a complete, self-contained, runnable example under examples/pq, aimed at newcomers:

  • app.go — ordinary application code (no copyist imports): a plain query function and a context.Context-aware variant.
  • app_test.go — a heavily commented walkthrough of the whole workflow: Register, SetSessionInit, TestMain wiring, defer copyist.Open(t).Close(), and opening through the copyist_postgres driver. The comments are the documentation; the file reads top-to-bottom without needing any other file.
  • testdata/app_test.copyist — a committed recording, so go test ./examples/pq passes immediately after cloning with no database, no Docker, no network. Re-recording is one command (go test ./examples/pq -record); the test starts a throwaway postgres:16 container itself via the existing drivertest/dockerdb helper, which also keeps make test (which deletes and re-records all recordings) working.
  • A short README section pointing at the example.

It also answers the second question in #24: context.Context needs no mocking — copyist forwards contexts to the wrapped driver untouched, and TestQueryNameAt demonstrates a QueryRowContext call recorded and played back like any other.

Test Plan

  • go test ./examples/pq passes with no database running (playback from the committed recording)
  • go test ./examples/pq -record records against a real Postgres in Docker and regenerates the recording
  • go test ./... at root passes (the example is part of the root module, so existing CI covers it on Go 1.18)
  • gofmt/go vet clean

🤖 Generated with Claude Code

peczenyj added 2 commits June 4, 2026 20:23
A self-contained, fully commented walkthrough of the record/playback
workflow, including a committed recording so 'go test ./examples/pq'
passes with no database, and a context.Context demonstration.

Addresses cockroachdb#24.
@cockroach-teamcity

Copy link
Copy Markdown
Member

This change is Reviewable

@peczenyj peczenyj mentioned this pull request Jun 4, 2026
@peczenyj

Copy link
Copy Markdown
Author

Hey guys, anything wrong with this pull request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Examples, please

2 participants