Skip to content

test(cache,server): assert row counts via Ent - #1288

Merged
kalbasit merged 2 commits into
mainfrom
refactor/trim-test-raw-sql-counts
May 29, 2026
Merged

kalbasit merged 2 commits into
mainfrom
refactor/trim-test-raw-sql-counts

Conversation

@kalbasit

Copy link
Copy Markdown
Owner

Summary

Client.DB() is a discouraged raw-*sql.DB escape hatch, yet the test suites reach for it ~110 times. Most uses are legitimate (migration verification, schema probes, adversarial setup, pool tuning). One pattern is a clean swap that loses no verification independence: standalone SELECT COUNT(*) row-count assertions in the cache and server behavior tests.

  • Convert the 5 bare table-count assertions in cache_test.go and all 5 by-hash count assertions in server_test.go to the Ent API (dbClient.Ent().<Entity>.Query()[.Where(entnarinfo.HashEQ(h))].Count(ctx)).
  • Retain raw SQL everywhere it is the right tool, including the cache_test.go by-hash counts interleaved with raw-SQL idioms (rebind helper, ExecContext mutations, Eventually polling, concurrent wg flows) and all pkg/ncps migration-outcome checks (ORM-independent by design).
  • The production pkg/ncps/migrate.go use of DB() is unchanged.
  • Records a database-orm spec requirement scoping the Ent-count convention to standalone behavior-test assertions.

Test plan

  • task fmt / task lint (0 issues) / task test (full suite green; pkg/cache + pkg/server)
  • Converted assertions produce identical pass/fail outcomes

@kalbasit

kalbasit commented May 29, 2026 •

Copy link
Copy Markdown
Owner Author

@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. go Pull requests that update go code labels May 29, 2026
@coderabbitai

coderabbitai Bot commented May 29, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: dc3eb6af-b16d-4011-97a1-afb1e8603c7d

📥 Commits

Reviewing files that changed from the base of the PR and between 678d5e0 and 52f977e.

📒 Files selected for processing (8)
  • openspec/changes/archive/2026-05-29-trim-test-raw-sql-counts/.openspec.yaml
  • openspec/changes/archive/2026-05-29-trim-test-raw-sql-counts/design.md
  • openspec/changes/archive/2026-05-29-trim-test-raw-sql-counts/proposal.md
  • openspec/changes/archive/2026-05-29-trim-test-raw-sql-counts/specs/database-orm/spec.md
  • openspec/changes/archive/2026-05-29-trim-test-raw-sql-counts/tasks.md
  • openspec/specs/database-orm/spec.md
  • pkg/cache/cache_test.go
  • pkg/server/server_test.go
✅ Files skipped from review due to trivial changes (3)
  • openspec/changes/archive/2026-05-29-trim-test-raw-sql-counts/design.md
  • openspec/changes/archive/2026-05-29-trim-test-raw-sql-counts/.openspec.yaml
  • openspec/changes/archive/2026-05-29-trim-test-raw-sql-counts/tasks.md
🚧 Files skipped from review as they are similar to previous changes (2)
  • openspec/changes/archive/2026-05-29-trim-test-raw-sql-counts/specs/database-orm/spec.md
  • pkg/server/server_test.go

📝 Walkthrough

Summary by CodeRabbit

  • Tests

    • Converted test row-count assertions from raw SQL to ORM-based counts in cache and server suites, preserving behavior for interleaved/raw-SQL scenarios.
  • Documentation

    • Added design, proposal, and spec guidance defining when ORM vs raw SQL should be used in tests.
  • Chores

    • Added OpenSpec archive metadata for tracking.

Walkthrough

Converts standalone test raw SQL COUNT(*) assertions to Ent ORM Count(...) in cache and server tests; adds spec and task documents defining scope and exceptions, and implements replacements in two test files.

Changes

Test SQL-to-ORM Count Refactor

Layer / File(s) Summary
Specification and requirements
openspec/changes/archive/2026-05-29-trim-test-raw-sql-counts/.openspec.yaml, .../design.md, .../proposal.md, .../specs/database-orm/spec.md, .../tasks.md, openspec/specs/database-orm/spec.md
Add archived and main spec, design, proposal, and task documents defining conversion scope (standalone count assertions to Ent), explicit exceptions (migration/mutation/admin raw SQL), and rollout checklist.
Cache test count conversions
pkg/cache/cache_test.go
Replace three standalone table-count assertions with dbClient.Ent().NarInfo.Query().Count(ctx) and dbClient.Ent().NarFile.Query().Count(ctx) in GetNarInfo, PutNarInfo, and GetNar tests.
Server test count conversions
pkg/server/server_test.go
Add entnarinfo import and replace five by-hash COUNT(*) assertions with dbClient.Ent().NarInfo.Query().Where(entnarinfo.HashEQ(...)).Count(ctx) across DELETE, PUT, and TestGetNarInfo_Head flows.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Suggested labels

size:S, merge-stack

Poem

🐰 I hopped through tests both short and long,
Swapped raw COUNTs for Ent's counting song,
Hashes filtered, assertions kept true,
Specs and tasks wrote the how and who,
A tidy hop — tests pass anew.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: converting row-count assertions from raw SQL to Ent ORM in cache and server tests.
Description check ✅ Passed The description is directly related to the changeset, providing context about the conversion from raw SQL to Ent API and explaining what raw SQL patterns are intentionally retained.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors standalone raw SQL SELECT COUNT(*) assertions in pkg/cache/cache_test.go and pkg/server/server_test.go to use the Ent ORM client's .Count(ctx) API, aligning with a new requirement in the database-orm specification. The review feedback suggests replacing context.Background() with the local helper newContext() in pkg/cache/cache_test.go to maintain consistency with the rest of the test suite and ensure proper test logger configuration.

Comment thread pkg/cache/cache_test.go Resolved
Comment thread pkg/cache/cache_test.go Resolved
Comment thread pkg/cache/cache_test.go Resolved
Comment thread pkg/cache/cache_test.go Resolved
Comment thread pkg/cache/cache_test.go Resolved
@kalbasit
kalbasit force-pushed the refactor/standardize-notfound-checks branch from 895160e to 106d5e9 Compare May 29, 2026 17:42
kalbasit added a commit that referenced this pull request May 29, 2026
Switch the five converted Ent count assertions from context.Background()
to the cache_test.go newContext() helper (a discard-logger context),
matching the surrounding suite convention.

Addresses review comments on PR #1288 (threads PRRT_kwDONV6tZs6Fv0I4,
PRRT_kwDONV6tZs6Fv0JC, PRRT_kwDONV6tZs6Fv0JK, PRRT_kwDONV6tZs6Fv0JT,
PRRT_kwDONV6tZs6Fv0Jb).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@kalbasit
kalbasit force-pushed the refactor/trim-test-raw-sql-counts branch from 1f40f94 to b1088d5 Compare May 29, 2026 17:42

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
openspec/changes/archive/2026-05-29-trim-test-raw-sql-counts/proposal.md (1)

17-25: ⚡ Quick win

Consider explicitly listing both spec requirements in Modified Capabilities.

The spec file (lines 148-168) adds a distinct requirement about using database.IsNotFoundError as the unified not-found predicate, in addition to the count-assertion requirement (lines 170-184). Line 25's phrasing "row presence/counts" is ambiguous—it could be read as covering both, but the not-found predicate change is a separate API-surface requirement that deserves explicit mention.

Consider splitting line 25 into two bullets to match the spec's two new requirements:

  • One for the count-assertion Ent requirement
  • One for the database.IsNotFoundError predicate requirement

This would make the proposal's scope match the spec additions 1:1 and improve clarity for reviewers.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@openspec/changes/archive/2026-05-29-trim-test-raw-sql-counts/proposal.md`
around lines 17 - 25, Update the "Modified Capabilities" section to explicitly
show both new requirements: keep the existing bullet that requires tests to
assert row presence/counts via the Ent client (`database-orm` behavior tests
must use Ent for presence/count assertions), and add a second bullet that states
the unified not-found predicate requirement (`database.IsNotFoundError` must be
used as the not-found predicate in tests). Make sure the two bullets reference
`database-orm`, the Ent client requirement, and the `database.IsNotFoundError`
symbol so the proposal matches the spec changes one-to-one.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@openspec/changes/archive/2026-05-29-trim-test-raw-sql-counts/proposal.md`:
- Around line 17-25: Update the "Modified Capabilities" section to explicitly
show both new requirements: keep the existing bullet that requires tests to
assert row presence/counts via the Ent client (`database-orm` behavior tests
must use Ent for presence/count assertions), and add a second bullet that states
the unified not-found predicate requirement (`database.IsNotFoundError` must be
used as the not-found predicate in tests). Make sure the two bullets reference
`database-orm`, the Ent client requirement, and the `database.IsNotFoundError`
symbol so the proposal matches the spec changes one-to-one.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 1323134f-65b8-455b-a19a-4986881780d1

📥 Commits

Reviewing files that changed from the base of the PR and between 1f40f94 and b1088d5.

📒 Files selected for processing (8)
  • openspec/changes/archive/2026-05-29-trim-test-raw-sql-counts/.openspec.yaml
  • openspec/changes/archive/2026-05-29-trim-test-raw-sql-counts/design.md
  • openspec/changes/archive/2026-05-29-trim-test-raw-sql-counts/proposal.md
  • openspec/changes/archive/2026-05-29-trim-test-raw-sql-counts/specs/database-orm/spec.md
  • openspec/changes/archive/2026-05-29-trim-test-raw-sql-counts/tasks.md
  • openspec/specs/database-orm/spec.md
  • pkg/cache/cache_test.go
  • pkg/server/server_test.go
✅ Files skipped from review due to trivial changes (4)
  • openspec/changes/archive/2026-05-29-trim-test-raw-sql-counts/.openspec.yaml
  • openspec/changes/archive/2026-05-29-trim-test-raw-sql-counts/tasks.md
  • openspec/changes/archive/2026-05-29-trim-test-raw-sql-counts/design.md
  • openspec/changes/archive/2026-05-29-trim-test-raw-sql-counts/specs/database-orm/spec.md
🚧 Files skipped from review as they are similar to previous changes (2)
  • pkg/server/server_test.go
  • pkg/cache/cache_test.go

@kalbasit
kalbasit force-pushed the refactor/standardize-notfound-checks branch from 106d5e9 to 014e8e3 Compare May 29, 2026 17:54
kalbasit added a commit that referenced this pull request May 29, 2026
Switch the five converted Ent count assertions from context.Background()
to the cache_test.go newContext() helper (a discard-logger context),
matching the surrounding suite convention.

Addresses review comments on PR #1288 (threads PRRT_kwDONV6tZs6Fv0I4,
PRRT_kwDONV6tZs6Fv0JC, PRRT_kwDONV6tZs6Fv0JK, PRRT_kwDONV6tZs6Fv0JT,
PRRT_kwDONV6tZs6Fv0Jb).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@kalbasit
kalbasit force-pushed the refactor/trim-test-raw-sql-counts branch from b1088d5 to 25088e5 Compare May 29, 2026 17:54
@kalbasit
kalbasit force-pushed the refactor/standardize-notfound-checks branch from 014e8e3 to 0230e29 Compare May 29, 2026 18:42
kalbasit added a commit that referenced this pull request May 29, 2026
Switch the five converted Ent count assertions from context.Background()
to the cache_test.go newContext() helper (a discard-logger context),
matching the surrounding suite convention.

Addresses review comments on PR #1288 (threads PRRT_kwDONV6tZs6Fv0I4,
PRRT_kwDONV6tZs6Fv0JC, PRRT_kwDONV6tZs6Fv0JK, PRRT_kwDONV6tZs6Fv0JT,
PRRT_kwDONV6tZs6Fv0Jb).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@kalbasit
kalbasit force-pushed the refactor/trim-test-raw-sql-counts branch from 25088e5 to 678d5e0 Compare May 29, 2026 18:42
Base automatically changed from refactor/standardize-notfound-checks to main May 29, 2026 19:42
kalbasit and others added 2 commits May 29, 2026 19:43
Client.DB() is a discouraged raw-*sql.DB escape hatch, yet the test
suites reach for it ~110 times. Most uses are legitimate (migration
verification, schema probes, adversarial setup, pool tuning). One
pattern is a clean swap that loses no verification independence:
standalone SELECT COUNT(*) row-count assertions in the cache and
server behavior tests.

Convert the 5 bare table-count assertions in cache_test.go and all 5
by-hash count assertions in server_test.go to the Ent API
(dbClient.Ent().<Entity>.Query()[.Where(entnarinfo.HashEQ(h))]
.Count(ctx)). Retain raw SQL everywhere it is the right tool,
including the cache_test.go by-hash counts that are interleaved with
raw-SQL idioms (rebind helper, ExecContext mutations, Eventually
polling, concurrent wg flows) and all pkg/ncps migration-outcome
checks (ORM-independent by design). The production pkg/ncps/migrate.go
use of DB() is unchanged.

Records a database-orm spec requirement scoping the Ent-count
convention to standalone behavior-test assertions.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Switch the five converted Ent count assertions from context.Background()
to the cache_test.go newContext() helper (a discard-logger context),
matching the surrounding suite convention.

Addresses review comments on PR #1288 (threads PRRT_kwDONV6tZs6Fv0I4,
PRRT_kwDONV6tZs6Fv0JC, PRRT_kwDONV6tZs6Fv0JK, PRRT_kwDONV6tZs6Fv0JT,
PRRT_kwDONV6tZs6Fv0Jb).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@kalbasit
kalbasit force-pushed the refactor/trim-test-raw-sql-counts branch from 678d5e0 to 52f977e Compare May 29, 2026 19:43
@kalbasit
kalbasit enabled auto-merge (squash) May 29, 2026 19:43
@kalbasit

Copy link
Copy Markdown
Owner Author

Automated stack merge failed during the rebase step. The merge-stack label has been removed from all PRs in the stack. Please resolve conflicts locally, push, and re-apply the label on the top-most PR to retry. Run: https://github.com/kalbasit/ncps/actions/runs/26658726391

@kalbasit
kalbasit merged commit ee5b884 into main May 29, 2026
19 checks passed
@kalbasit
kalbasit deleted the refactor/trim-test-raw-sql-counts branch May 29, 2026 20:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go Pull requests that update go code size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant