Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(data-warehouse): steer agents away from low-value source tests (#113423)

Tom Owers683fe5cd73ac
diff --git a/.agents/skills/implementing-warehouse-sources/SKILL.md b/.agents/skills/implementing-warehouse-sources/SKILL.md
index 94f0f133f5ee..63395489a725 100644
--- a/.agents/skills/implementing-warehouse-sources/SKILL.md
+++ b/.agents/skills/implementing-warehouse-sources/SKILL.md
@@ -785,53 +785,22 @@ From `products/warehouse_sources/backend/temporal/data_imports/sources/common/mi
## Testing expectations
-**Never write a test whose assertion restates a declaration.** A test that reads back `source_type`,
-the labels in `get_source_config`, the endpoint list in `settings.py`, or the kwargs a one-line
-`source_for_pipeline` forwards, passes because both halves of the diff were typed together. It cannot
-fail for any reason except someone editing both, so it catches nothing. That pattern was swept out of
-the source tests once already; don't reintroduce it.
-
-Before each test, answer: _what could break at runtime that this catches?_ If the answer restates the
-source file, don't write it. See `/writing-tests` for the general gate.
-
-The line is whether the thing under test can vary at runtime, not which method it sits on:
-
-- `get_schemas` that is one `build_endpoint_schemas(...)` call needs no test — the helper's filter and
- sync-mode behavior is covered in `common/test_source_schema.py`. A `get_schemas` that lists a remote
- directory, resolves per-version endpoints, or builds qualified names needs tests for each of those.
-- `validate_credentials` that forwards to the transport helper needs no test at the source-class level.
- One that maps a probe result to a message, rejects an unknown schema, or accepts a missing scope at
- create time needs one per branch.
-- `source_for_pipeline` that forwards its config needs no test. One that raises on an unknown schema,
- picks between transports, or resolves anything from schema metadata needs one per branch. The
- `db_incremental_field_last_value if inputs.should_use_incremental_field else None` ternary is not a
- branch worth its own source-level test — cover it with the transport's full-refresh test below,
- which asserts the request actually goes out without a watermark.
-- Any `raise`, any curated error message a user reads, and any value derived rather than declared —
- test it. A source whose `SourceResponse.name` comes from a storage key rather than the schema name
- is a naming branch, and getting it wrong writes data where nothing reads it.
-
-Two test modules:
-
-- `tests/test_<source>_source.py` — the source class's own decisions, per the branches above, plus
- for webhook sources `create_webhook` / `delete_webhook` / `get_external_webhook_info` behavior and
- `webhook_resource_map` correctness.
-- `tests/test_<source>.py` — the transport, where most bugs live:
- - paginator behavior from response headers and body, including the terminal page
- - incremental vs full-refresh request shaping, and that a full refresh omits the watermark
- - credential validation status mapping: each status the API returns to the message users read
- - retry classification: which statuses are retryable and which are terminal
- - mapper and normalization helpers, fan-out row shaping, parent-field injection
- - for resumable sources: resuming from saved state, and state saved after each batch
- - for incremental cursor pagination: stopping once a page predates the watermark, and walking on
- when no watermark is set
-
-When an error pattern comes from a real API response, keep the verbatim string in the test. That
-wording is field knowledge — it records what the vendor actually emits, which the pattern in
-`get_non_retryable_errors` alone does not tell a reader.
-
-Parameterize status codes and edge cases rather than copying test bodies. Cover the paths that can
-break; do not pad the count.
+**Read [references/testing.md](references/testing.md) before you write a source test.**
+Source tests were about a fifth of the monorepo's test cases.
+[#113325](https://github.com/PostHog/posthog/pull/113325) deleted most of them after per-test coverage showed they caught nothing.
+
+The gate: before each test, name the runtime failure it catches that no other test catches.
+If you cannot, do not write it. See `/writing-tests` for the general gate.
+
+Do not write:
+
+- **Declaration read-backs:** `lists_tables_without_credentials`, `connection_host_fields`, `api_docs_url`, versions, the `get_source_config` category, release status, fields or secret flags, endpoint names, primary keys, page sizes. The shared invariants in `sources/tests/` already check the ones that matter for every source.
+- **Shared framework behavior:** `get_non_retryable_errors()` key membership, `get_schemas(names=...)` filtering, `get_documented_tables()`, a `source_for_pipeline` that only forwards, and the `rest_source` paginators, auth and retries.
+- **Duplicate coverage:** first-page or single-page tests next to a multi-page walk, extra resume-state variants, and separate tests for statuses or modes that share a code path. Parameterize instead.
+
+Write, in `tests/test_<source>.py`: a multi-page walk to the terminal page, incremental against full-refresh request shaping, the order the source yields when it sorts or windows rows itself, error mapping driven by a real vendor response through the transport, resume from saved state, and row shaping on edge-case inputs.
+Add `tests/test_<source>_source.py` only for branches the source class takes itself.
+A typical REST source needs one transport module of 10 to 25 parameterized cases.
## Implementation checklist
@@ -878,6 +847,7 @@ Release status (a finished source has NO unreleasedSource flag — it hides the
- [ ] featureFlag="dwh-{source_name}" ONLY if you want a controlled rollout instead of releasing to all
Tests & handoff:
+- [ ] Tests follow references/testing.md: no declaration read-backs, no re-tested framework code, no duplicate coverage
- [ ] Source tests (test_<source>_source.py) — branches only, no declaration restatements
- [ ] Transport tests (test_<source>.py) — paginators, error mapping, request shaping
- [ ] User-facing doc written/updated per /documenting-warehouse-sources (docsUrl matches filename; `audit_source_docs` passes)
diff --git a/.agents/skills/implementing-warehouse-sources/references/testing.md b/.agents/skills/implementing-warehouse-sources/references/testing.md
new file mode 100644
index 000000000000..5a923cb54b29
--- /dev/null
+++ b/.agents/skills/implementing-warehouse-sources/references/testing.md
@@ -0,0 +1,84 @@
+# Testing a warehouse source
+
+The per-source test directories once held about a fifth of the test cases in the monorepo, and every backend PR waited on them.
+Most of those cases restated a declaration, re-ran a shared helper, or covered lines another test already covered.
+[#113325](https://github.com/PostHog/posthog/pull/113325) deleted them after measuring per-test line coverage.
+Write the tests that would have survived that cut, and nothing else.
+
+## The gate
+
+Before you write a test, name the runtime failure it catches that no other test catches.
+"Someone edits the constant" is not a runtime failure: the edit changes both halves of the test.
+If you cannot name one, do not write the test.
+
+## Do not write these
+
+### Tests that read back a declaration
+
+The assertion compares a value with the line that declares it.
+The test can only fail when someone edits both, so it catches nothing.
+
+| Do not assert | Why it is already safe |
+| ----------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------- |
+| `lists_tables_without_credentials is True` | `test_static_catalog_sources_can_list_tables_without_credentials` checks it for every source |
+| `connection_host_fields == [...]`, or that a host field rejects an internal host | `test_sources_with_a_host_field_refuse_an_internal_host` checks every source with a host field |
+| `api_docs_url` is https, `supported_versions`, `default_version` | `test_every_source_declares_valid_versions` checks every source |
+| `get_source_config` shape: category, `releaseStatus`, `docsUrl`, field names, which fields are secret | `test_every_source_has_a_valid_category` and `test_credential_fields_are_marked_secret` check every source; the rest is a declaration |
+| The set of endpoint names, or each endpoint's primary keys, incremental fields, partition key or page size from `settings.py` | A declaration. Test what the transport does with it instead |
+| That `canonical_descriptions.py` covers each endpoint, or that `get_documented_tables()` lists them | `test_canonical_descriptions_are_keyed_by_schema_name` and `test_documented_tables_match_the_schemas_they_describe` check every source |
+| `unreleasedSource is True` | This locks a finished source hidden. Never write it |
+
+The shared tests live in `sources/tests/test_source_catalog_invariants.py`, `test_source_versions.py` and `test_source_categories.py`.
+
+### Tests that re-run shared framework code
+
+| Do not test | Why |
+| ------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------- |
+| That an error string matches a key of `get_non_retryable_errors()`, e.g. `assert any(p in msg for p in source.get_non_retryable_errors())` | The test copies the pattern into the message, so it restates the dict |
+| `get_schemas(names=[...])` filtering, or the sync modes it builds, when `get_schemas` is one `build_endpoint_schemas(...)` call | `common/test_source_schema.py` covers the helper |
+| `get_documented_tables()` output | The catalog invariants cover it |
+| A `source_for_pipeline` that forwards its config to the transport | The forwarding has no branch |
+| The `rest_source` paginators, auth classes, retries or `Retry-After` handling | `common/rest_source/` tests cover them. Test your source's own paginator subclass only |
+
+### Tests that cover lines another test already covers
+
+A second test that walks the same lines adds runtime cost and no protection.
+These were the most common duplicates:
+
+- A "first page" or "single page" paginator test next to a test that already walks two pages to the terminal page.
+- A "resume state while there is a next page" test next to a resume test that already saves and loads state.
+- A full-refresh test and an incremental test that share every line except one parameter. Use one parameterized test.
+- One test per HTTP status when the statuses take the same code path. Parameterize them in one test.
+
+Before you add a case, name the branch or line that only it reaches.
+
+## Write these
+
+### `tests/test_<source>.py`: the transport
+
+This is where most bugs live.
+Use parameterized cases and mock HTTP (`responses`) or the vendor client. Never use the network.
+
+- **Pagination:** one test that walks at least two pages to the terminal page, and asserts the request parameters of each page. Add the cases where the vendor's termination signal is unusual (an empty page with a cursor, a `has_more` flag that lies).
+- **Request shaping:** an incremental request sends the watermark in the vendor's filter and sort; a full refresh sends no watermark. One parameterized test.
+- **Ordering:** if the source sorts or windows rows itself, assert the order it yields, because `sort_mode` trusts it.
+- **Error mapping:** drive a real vendor response through the transport (status code and body) and assert the error it raises, or the message `validate_credentials` returns. Keep the vendor's verbatim error text in the fixture: it records what the vendor actually sends.
+- **Resume:** for a `ResumableSource`, one test that resumes from saved state and one that asserts state is saved before the batch it covers is yielded.
+- **Row shaping:** mappers, type conversions, fan-out parent fields, flattening. Use edge-case inputs such as nulls, nested objects, and timezones.
+- **Incremental cursor pagination:** the walk stops once a page predates the watermark, and continues when there is no watermark.
+
+### `tests/test_<source>_source.py`: the source class
+
+Only for branches the source class itself takes.
+Many sources need no file here.
+
+- `validate_credentials` that maps a probe result to a message, rejects an unknown schema, or accepts a missing scope at create time: one case per branch.
+- `get_schemas` that lists a remote directory, resolves per-version endpoints, or builds qualified names.
+- `source_for_pipeline` that raises on an unknown schema, picks between transports, or reads schema metadata.
+- A derived value such as a `SourceResponse.name` taken from a storage key. A wrong name writes data where nothing reads it.
+- Webhook sources: `create_webhook`, `delete_webhook`, `get_external_webhook_info`, and `webhook_resource_map`.
+
+## Size check
+
+A typical REST source needs one transport module of 10 to 25 parameterized cases and no source-class module.
+If a new source's tests are longer than its implementation, look for the patterns above before you push.
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/AGENTS.md b/products/warehouse_sources/backend/temporal/data_imports/sources/AGENTS.md
new file mode 100644
index 000000000000..dcb17b01888f
--- /dev/null
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/AGENTS.md
@@ -0,0 +1,13 @@
+# Warehouse sources agent guide
+
+Read the `implementing-warehouse-sources` skill (`.agents/skills/implementing-warehouse-sources/SKILL.md`) before you build or extend a source here.
+
+## Source tests
+
+Every test under `<source>/tests/` must catch a runtime failure that no other test catches.
+Source tests were about a fifth of the monorepo's test cases until [#113325](https://github.com/PostHog/posthog/pull/113325) cut the ones that caught nothing.
+Read [the testing reference](../../../../../../.agents/skills/implementing-warehouse-sources/references/testing.md) when you add or change a source test: it lists the cut patterns and what to write instead.
+
+- A source test never reads back a declaration: `lists_tables_without_credentials`, `connection_host_fields`, `api_docs_url`, versions, `get_source_config` fields, endpoint names, primary keys or page sizes. The shared invariants in `tests/` cover the ones that matter.
+- A source test never re-tests shared code: `get_non_retryable_errors()` key membership, `get_schemas(names=...)` filtering, `get_documented_tables()`, a forwarding `source_for_pipeline`, or the `rest_source` paginators, auth and retries.
+- A new case reaches a branch or line that no other case reaches. Parameterize statuses and modes that share a code path.
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/CLAUDE.md b/products/warehouse_sources/backend/temporal/data_imports/sources/CLAUDE.md
new file mode 120000
index 000000000000..47dc3e3d863c
--- /dev/null
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/CLAUDE.md
@@ -0,0 +1 @@
+AGENTS.md
\ No newline at end of file