Skip to content

build_stop_route_mapping のSQL回帰テスト基盤を整備#1519

Merged
TinyKitten merged 1 commit into
devfrom
feature/build-stop-route-mapping-tests
May 15, 2026
Merged

build_stop_route_mapping のSQL回帰テスト基盤を整備#1519
TinyKitten merged 1 commit into
devfrom
feature/build-stop-route-mapping-tests

Conversation

@TinyKitten
Copy link
Copy Markdown
Member

@TinyKitten TinyKitten commented May 15, 2026

概要

stationapi/src/import.rsbuild_stop_route_mapping() (バス経路順序を決定する SQL パイプライン) に対する回帰テスト基盤を整備した。PR #1511 / #1512 / #1515 で繰り返し調整が入っている関数だが、これまで実 GTFS に近い形で検証する単体テストが存在しなかった。

変更の種類

  • バグ修正
  • 新機能
  • データの修正・追加
  • リファクタリング
  • ドキュメント
  • CI/CD
  • その他

変更内容

  • stationapi/src/import.rs#[cfg(test)] mod testsbuild_stop_route_mapping 用の回帰テストを追加 (516 行)
  • mod stop_route_mapping_fixtures として、隔離スキーマ生成・最小 GTFS テーブル構築 (gtfs_stops / gtfs_trips / gtfs_stop_times)・fixture 投入ヘルパを実装
    • per-process カウンタとナノ秒タイムスタンプでユニークなスキーマ名を生成し、SET search_path で切り替えるため複数テストを並列実行可能
  • 以下の 6 シナリオをテスト関数として実装 (issue の 5 シナリオ +α):
    • test_build_stop_route_mapping_ike86_canonical_shape: 池86 風多バリアント系統で stop 数が最多の shape が canonical に選ばれる
    • test_build_stop_route_mapping_canonical_shape_direction_tiebreak: stops/dist が並ぶ場合 direction_id=0 がタイブレーク勝利
    • test_build_stop_route_mapping_main_trip_prefers_direction_zero: canonical shape を共有する trip 群から direction_id=0 を main trip に選ぶ
    • test_build_stop_route_mapping_variant_chain_recursive_interpolation: variant-only 連鎖を LAG/LEAD + 再帰 CTE で補間
    • test_build_stop_route_mapping_null_shape_dist_traveled: shape_dist_traveled 全 NULL でも stops 数フォールバックで動作
    • test_build_stop_route_mapping_parent_station_grouping: 同一物理停留所の複数ポールを COALESCE(parent_station, stop_id) で正しく集約
  • 全テストを #[cfg_attr(not(feature = "integration-tests"), ignore)] でゲート。既存 CI (cargo test のみ) には影響を与えない
  • ローカル実行コマンドはモジュール冒頭のコメントに記載 (TEST_DATABASE_URL=... cargo test -p stationapi --features integration-tests build_stop_route_mapping)

テスト

  • cargo fmt --all -- --check が通ること
  • cargo clippy -- -D warnings が通ること
  • cargo testSQLX_OFFLINE=true)が通ること

追加で、integration-tests feature 有効時に新規 6 テストすべてが pass することをローカル Postgres (Docker stationapi-psql-1) で確認済み:

running 6 tests
test import::tests::test_build_stop_route_mapping_canonical_shape_direction_tiebreak ... ok
test import::tests::test_build_stop_route_mapping_parent_station_grouping ... ok
test import::tests::test_build_stop_route_mapping_null_shape_dist_traveled ... ok
test import::tests::test_build_stop_route_mapping_main_trip_prefers_direction_zero ... ok
test import::tests::test_build_stop_route_mapping_variant_chain_recursive_interpolation ... ok
test import::tests::test_build_stop_route_mapping_ike86_canonical_shape ... ok

test result: ok. 6 passed; 0 failed; 0 ignored; 0 measured; 28 filtered out

関連Issue

Closes #1514

スクリーンショット(任意)

Summary by CodeRabbit

  • テスト
    • 駅ルートマッピング機能の回帰テストスイートを追加。複数のシナリオにおける正確性と動作を包括的に検証します。

Review Change Stack

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@TinyKitten TinyKitten self-assigned this May 15, 2026
@github-actions github-actions Bot added feature 要望対応や課題解決 deploy-dev and removed feature 要望対応や課題解決 labels May 15, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 15, 2026

📝 Walkthrough

Walkthrough

stationapi/src/import.rs に PostgreSQL 統合テストスイートを追加。build_stop_route_mapping() の回帰検証として、隔離スキーマ上の最小 GTFS テーブルを活用した 6 つのテストケース(canonical_shape・main_trip・再帰 CTE 補間・NULL フォールバック・親停留所集約)と支える fixture インフラを実装。

Changes

build_stop_route_mapping 回帰テストスイート

Layer / File(s) Summary
テストインフラストラクチャと Fixtures 基盤
stationapi/src/import.rs
テスト実行ガイドと stop_route_mapping_fixtures モジュール(隔離スキーマ管理、最小 GTFS テーブル作成、停留所・ポール・trip・stop_times 投入、マッピング抽出)を追加。integration-tests フラグ下で実行される。
Canonical shape 選択の検証
stationapi/src/import.rs
池86 風多バリアント系統で canonical_shape が停留所数最大を優先、direction_id=0 がタイブレーク勝ちする 2 つのテスト。
Main trip 選択と補間ロジックの検証
stationapi/src/import.rs
main_trip が direction_id=0 を優先し、再帰 CTE で main_trip に無い stop を中央に補間挿入する動作を検証する 2 つのテスト。
エッジケース処理の検証
stationapi/src/import.rs
shape_dist_traveled NULL 時の stops 数フォールバックと parent_station 経由の親停留所単位集約を検証する 2 つのテスト。

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

Possibly related PRs

  • TrainLCD/StationAPI#1511: canonical_shape・direction_id=0 優先・shape_dist_traveled フォールバック・親停留所集約などの停留所順序ロジックを変更した PR に対する直接的な回帰検証テストを追加。
  • TrainLCD/StationAPI#1512: canonical_shape・main_trip・再帰 CTE/LAG/LEAD による補間の順序決定ロジック変更に対応する統合テストを追加。

Suggested labels

deploy-dev

Poem

🐰 PostgreSQL の隔離スキーマで
池86 の多バリアント
順序の謎を六つ解きて
canonical と main_trip
タイブレーク・補間の輝きよ ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed PRのタイトルは、build_stop_route_mappingのSQL回帰テスト基盤整備という主な変更を簡潔かつ明確に要約しており、チェンジセット全体の内容と正確に対応している。
Description check ✅ Passed PR説明は、概要・変更の種類・変更内容・テスト・関連Issueの全必須セクションを含み、テンプレートの構成に準拠している。詳細な実装内容とテスト確認結果も記載されている。
Linked Issues check ✅ Passed 実装された6つのテストシナリオはIssue #1514で要求された5シナリオ(多バリアント系統、canonical_shape選択、main trip選択順序、variant-only連鎖、shape_dist_traveled NULL対応)をすべてカバーし、加えてparent_station集約シナリオも追加実装されている。
Out of Scope Changes check ✅ Passed 変更内容はstationapi/src/import.rsのテストモジュール内(516行追加)に限定され、テスト基盤整備というIssue #1514の要件に完全に対応しており、スコープ外の変更は認められない。
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/build-stop-route-mapping-tests

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

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
stationapi/src/import.rs (1)

3219-3221: ⚡ Quick win

失敗時でも drop_schema が必ず走る順序にしてください。

build_stop_route_mapping(...).await.unwrap() が先に失敗するとクリーンアップがスキップされ、失敗テスト時のスキーマ残骸が増えます。Result を保持して先に drop_schema を実行してから expect する形にすると防げます。

差分例(1テスト分、同パターンを他5テストにも適用)
-        let mapping = build_stop_route_mapping(&mut conn).await.unwrap();
-        let actual = collect_route(&mapping, "IKE86");
-        stop_route_mapping_fixtures::drop_schema(&mut conn, &schema).await;
+        let mapping_result = build_stop_route_mapping(&mut conn).await;
+        stop_route_mapping_fixtures::drop_schema(&mut conn, &schema).await;
+        let mapping = mapping_result.expect("build_stop_route_mapping should succeed");
+        let actual = collect_route(&mapping, "IKE86");

Also applies to: 3276-3278, 3331-3333, 3402-3404, 3452-3454, 3504-3506

🤖 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 `@stationapi/src/import.rs` around lines 3219 - 3221, The cleanup call
drop_schema must run even if build_stop_route_mapping fails: change the code to
capture the Result from build_stop_route_mapping into a variable (e.g., let
mapping_res = build_stop_route_mapping(&mut conn).await;), then call
stop_route_mapping_fixtures::drop_schema(&mut conn, &schema).await before
unwrapping or expect-ing the result, and finally unwrap/expect mapping_res to
get mapping (used by collect_route). Apply the same pattern for the other
occurrences around lines using build_stop_route_mapping, drop_schema, and
collect_route.
🤖 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 `@stationapi/src/import.rs`:
- Around line 3219-3221: The cleanup call drop_schema must run even if
build_stop_route_mapping fails: change the code to capture the Result from
build_stop_route_mapping into a variable (e.g., let mapping_res =
build_stop_route_mapping(&mut conn).await;), then call
stop_route_mapping_fixtures::drop_schema(&mut conn, &schema).await before
unwrapping or expect-ing the result, and finally unwrap/expect mapping_res to
get mapping (used by collect_route). Apply the same pattern for the other
occurrences around lines using build_stop_route_mapping, drop_schema, and
collect_route.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 6f7a3038-1d1a-4fd1-a24f-59a5f9764b26

📥 Commits

Reviewing files that changed from the base of the PR and between 102a445 and 2c50a71.

📒 Files selected for processing (1)
  • stationapi/src/import.rs

@TinyKitten TinyKitten merged commit dce816f into dev May 15, 2026
11 checks passed
@TinyKitten TinyKitten deleted the feature/build-stop-route-mapping-tests branch May 15, 2026 12:22
@TinyKitten TinyKitten mentioned this pull request May 15, 2026
10 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

バス経路順序ロジック (build_stop_route_mapping) のSQL回帰テスト基盤を整備する

1 participant