Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Djangoで「良い」Factoryを書きたい
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Yasshieeee
June 20, 2024
Technology
84
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Djangoで「良い」Factoryを書きたい
みんなのPython勉強会#105でのLT資料
Yasshieeee
June 20, 2024
Transcript
10 slides
Djangoで「良い」 Factoryを書きたい やっしー みんなのPython勉強会#105
自己紹介 名前 やっしー https://github.com/YaCpotato https://www.leadingmark.jp/ 現在業務でDjangoを使っています その他Rails, Nuxt.jsなどなど、3年ほどのWebエンジニアです :@Yasshieeee
今回の発表はトークメインで、発表しきれないことなど具体 例はQiitaに書いてあります! https://qiita.com/leadingmark_yashiro/items/0e38b0cbccd88505c9e8
「良い」Factory、書きたいですよね 問題提起:テスト導入初期、新しい viewsのテストの度にFactoryが修正される → 割れ窓的にできがちになる不完全な Factory → テストだけに集中したいのに Factoryもみなければならず、コスト増 → 絶賛改修中の機能周りのモデルだった場合、コンフリクトの可能性 ここ最近書くことが多くなり、知見をシェアさせていただきます。 ※本当にモデルに仕様変更はいったらしょうがないですコレ
「良い」Factoryとはなにか考える • 読みやすい、修正しやすい →作って終わりではない。使い続ける、変化に対応させるため • 使い方がわかりやすい →自分にしかわからないオレオレ Factoryはダメ (大体みんな同じ書き方にはなるのだが、 細かい部分で自身の想定との差異はコー
ディングが統一されない原因となる。細かいから指摘する優先度が下がる ) • データは現実に即している →自動生成してくれるFaker、便利だけれど、そのデータ、実際に入りうるの?
読みやすい、修正しやすい • 書き方(ルール)が統一されている ◦ f文字列、.format()など、文字列内の変数使用の方法 ◦ fakerの使用メソッドの統一 ◦ 変数をどう受け取り、どう作るか
使い方がわかりやすい DocStringを使おう! 引数としてカラム名で渡せば、 Factoryで参照し ていなくても使ってくれるが、極力書いてあげた い 上記でもこれができてしまう →
データは現実に即している Fakerから現実に即した出力を満たすメソッド探すの、つらいけど頑張らないとですね https://faker.readthedocs.io/en/master/locales/ja_JP.html# こういうときはぶっちゃけrandomパッケージ使ってます。 → ChoiceField:選択肢形式。データベース的に varchar
そのテストのための要件を満た すFactoryは、必ずしも他のテ ストの要件を満たすとは限らな い →新しいviewsのテストの度に 修正されがちになる
ではどうするか 新しいテスト書く度に仕様変更されないモデ ルのFactoryが修正されないために • テスト、Factoryにもコーディングルール • Factoryも詳細設計 • viewsではなくmodelのテストをまず書き、 Factoryを使い倒す
More Decks by Yasshieeee
See All by Yasshieeee
「他の人が理解できる」を掘り下げる_リーダブルコード LT会 - vol.4
yacpotato
0
98
はんなりPython 47回LT回
yacpotato
0
200
Other Decks in Technology
See All in Technology
エンタープライズIAMについて - ID・権限をどう統制するか
oidfj
1
320
Platform Engineering をベースとした Agentic Software Factory の実践
110y
3
970
IP重複は"ランディングVPC"で受け止める ― PrivateLink と Transit Gatewayによるマルチテナント接続基盤の設計
hatahata021
3
270
実測データからサービスの成長余力を測る。負荷シナリオ・テストデータ・限界値の設計
kazatohiei
0
170
業務の傍らで進める改善ループエンジニアリング
bengo4com
1
240
Is the Website Dying?
miriamschwab
0
160
コーディングできない僕にBobが舞い降りた
shumpei3
0
200
お風呂場をサーバルームにしよう
yusuke427
0
400
新規事業開発でのAWS活用成功談&失敗談! 〜新規事業開発を専門に行うRelicならではのAWS活用〜
amixedcolor
1
140
人の努力に頼らないセキュリティをエンジニアリングする在り方 / An approach to engineering security that does not rely on human effort
carta_engineering
0
240
市販IoT機器や自作IoT機器とHomeAssistantを駆使して家の電気代をできる限り見える化する方法の考案
harutiro
0
420
リリースしたで終わらない。 観測できるフロントエンド開発
yamakenji24
2
230
Featured
See All Featured
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
630
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
670
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
1.1k
RailsConf 2023
tenderlove
30
1.6k
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
330
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
1
1.7k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
For a Future-Friendly Web
brad_frost
184
11k
Making Projects Easy
brettharned
120
6.9k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.9k
Git: the NoSQL Database
bkeepers
PRO
433
67k