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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
sontixyou
September 11, 2024
0
470
DBを使ったテストで詰まった点とその対策
Rustを使ったWebアプリケーションでのDBを用いたテストに関する発表です
sontixyou
September 11, 2024
Tweet
Share
More Decks by sontixyou
See All by sontixyou
開発メンバーが語るFindy Conferenceの裏側とこれから
sontixyou
2
820
Featured
See All Featured
Abbi's Birthday
coloredviolet
1
4.8k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
260
Exploring anti-patterns in Rails
aemeredith
2
250
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
80
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
230
Documentation Writing (for coders)
carmenintech
77
5.3k
Embracing the Ebb and Flow
colly
88
5k
The Pragmatic Product Professional
lauravandoore
37
7.1k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
8.7k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.3k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
410
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
0
140
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 はじめました!!