Skip to content

fix: statistics aggregation spec の ID ハードコードを削除#1805

Merged
yasulab merged 1 commit intomainfrom
fix/statistics-aggregation-spec-id-conflict
Feb 18, 2026
Merged

fix: statistics aggregation spec の ID ハードコードを削除#1805
yasulab merged 1 commit intomainfrom
fix/statistics-aggregation-spec-id-conflict

Conversation

@yasulab
Copy link
Copy Markdown
Member

@yasulab yasulab commented Feb 18, 2026

問題

spec/lib/statistics/aggregation_spec.rbcreate(:dojo, id: 194, ...) と ID をハードコードしていたため、テスト実行中に以下のエラーが発生していました。

ActiveRecord::RecordNotUnique:
  PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "dojos_pkey"
  DETAIL:  Key (id)=(194) already exists.

Dependabot PR #1804 の CI でこのテストが失敗していました。

原因

  • use_transactional_fixtures: true の環境では各テストのロールバックでデータは消えますが、PostgreSQL のシーケンスは巻き戻らない
  • 他のテストが繰り返し dojo を作成するたびにシーケンスが進む
  • 最終的に create(:dojo, name: 'Dojo1', ...) の自動採番が 194 になり、直後の create(:dojo, id: 194, ...) と衝突

修正

  • id: 194 のハードコードを削除し、d3 = create(:dojo, ...) で自動採番させる
  • モックデータも dojo_id: 194 の固定値から d3.id に変更

確認

bundle exec rspec spec/lib/statistics/aggregation_spec.rb
# → 15 examples, 0 failures

PostgreSQL のシーケンスはトランザクションロールバックで
巻き戻らないため、テスト実行中にシーケンスが積算されると
`create(:dojo, id: 194)` の明示的 ID と自動採番が衝突する。

dojo を ID 指定なしで作成し、生成された ID をモックデータに
渡すよう修正して、同様の衝突が将来発生しないようにする。
@yasulab yasulab merged commit c1b81cb into main Feb 18, 2026
5 checks passed
@yasulab yasulab deleted the fix/statistics-aggregation-spec-id-conflict branch February 18, 2026 07:34
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.

1 participant