Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
DBを使ったテストで詰まった点とその対策
Search
sontixyou
September 11, 2024
550
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
DBを使ったテストで詰まった点とその対策
Rustを使ったWebアプリケーションでのDBを用いたテストに関する発表です
sontixyou
September 11, 2024
More Decks by sontixyou
See All by sontixyou
SkillがSkillを生む:QA観点出しを自動化した
sontixyou
7
4.1k
Claude Codeと駆け抜ける 情報収集と実践録
sontixyou
2
2k
開発メンバーが語るFindy Conferenceの裏側とこれから
sontixyou
2
1.8k
Featured
See All Featured
Being A Developer After 40
akosma
91
590k
Building Flexible Design Systems
yeseniaperezcruz
330
40k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.5k
Unsuck your backbone
ammeep
672
58k
Building Applications with DynamoDB
mza
96
7.2k
Technical Leadership for Architectural Decision Making
baasie
3
470
Chasing Engaging Ingredients in Design
codingconduct
0
270
Rails Girls Zürich Keynote
gr2m
96
14k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.2k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
650
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
320
Transcript
2024/09/11 RustのWebアプリ開発 LT道場 〜テスト編〜 DBを使ったテストで詰まった点とその対策 01 1
02 2 西村 憲悟 [にしむら けんご] ファインディ株式会社 / バックエンド @sontixyou
• Webアプリケーションのテストで直面した課題 • 問題に対しての解決アプローチ • まとめ 今日話すこと 03 3
今日話さないこと 04 • テストデータの作成を楽にする方法 • どこの月見バーガーがうまいのか 4
• 実行を短時間で終わらせたい • テストケース間で干渉しないようにテス トしたい テストに対してのモチベーション 05 5
アプリケーションの構成 06 6 アプリケーション DB ORM: sea-orm https://github.com/SeaQL/sea-orm
アプリケーションコード 07 7 DBコネクションしてから postテーブルにレコードを作成 DBコネクションしてから
postテーブルにあるレコードを更新
テストコード 08 8
各テストがひとつのDBを使う状態で cargo testするとどうなるのか? テストの問題点 09 9
• 複数のテストが同じDBにデータを読み書きすると、 テスト同士で影響が出る ◦ テストが失敗したり、flakyになる テストの問題点 10 10
ユニーク制約ぐぬぬ DBのユニーク制約にひっかかり、自分が作りたいデータ を作成できない テストの問題点 11 11
• △ 実行を短時間で終わらせたい • ✗ テストケース間で干渉する テストの問題点 12 12
cargo testをすると並列でテストが実行されるため https://doc.rust-lang.org/book/ch11-02-running-tests.html#running-tests-in-parallel-or-consecutively DBへCRUDする場合での難しさ 13 13
• テストケースごとにDBを分ける • 並列数を1にする ◦ cargo testのオプションで可能 • モックDBを使う ◦
sea-ormでサポートされている 対応方法候補 14 14
• テストケースごとにDBを分ける • 並列数を1にする • モックDBを使う 対応方法候補 15 15
テスト実行前にDBを作成。 実行後にDBを削除する。 ↓ test_contextを使用することで実現可能になる。 https://docs.rs/test-context/latest/test_context/ テストケースごとにDBを分ける 準備編 16 16
テストケースごとにDBを分ける 実装編 17 17
テストケースごとにDBを分ける 実装編 18 18 setupとteardown関数を呼び出す下準備
テストケースごとにDBを分ける 実装編 19 19 テストに対してのモチベーション • ✅ テスト実行を短時間で終わる • ✅ テストケース間で干渉しない
テストケースごとにDBを分ける 実装編 20 20 実装の詳細はこちらを見てほしい🦀 https://github.com/sontixyou/sea_orm_practice_db2024
まとめ 21 21 • 並列でテスト実行をするときはDBの接続先に気 をつけよう • sea-ormを使う際は一工夫が必要 ◦ 自前実装が手間ならsqlxを使うのも手
宣伝 開催日時: 10月 8日 (火曜日) 19:30~ ↓詳細はこちら↓ 22 22 🦀Rust地域コミュニティ🦀
Osaki.rs はじめました!!