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
Cloud Firestore(の、スタブ)を作ってみた / Stubbing Cloud Firestore
Search
Hiron
December 08, 2017
Technology
1
680
Cloud Firestore(の、スタブ)を作ってみた / Stubbing Cloud Firestore
Mobile Act OSAKA #2で発表したスライドです。
Hiron
December 08, 2017
Tweet
Share
More Decks by Hiron
See All by Hiron
try! Swift Tokyo 2024 参加報告 / try! Swift Tokyo 2024 Report
hironytic
0
200
Swift on Windows ファーストインプレッション / Swift on Windows First Impression
hironytic
0
530
iPadOSDC: Multiple Windows
hironytic
4
2.7k
だったら、俺が代わりにやってやる / Swift Zoomin' Challenge
hironytic
0
120
SwiftのNeverとボトム型 / Never as a Bottom Type
hironytic
2
710
4000のワーニングと戦え!これは警告だ! / 4000 Warnings
hironytic
2
710
シンボリケート / Ore-con 2018 Summer
hironytic
3
960
全部iOSにしゃべらせちゃえ! / iOSDC 2018 LT
hironytic
3
1.3k
ペダルのある生活 / The Pedal
hironytic
0
370
Other Decks in Technology
See All in Technology
データベース02: データベースの概念
trycycle
0
130
20240416_devopsdaystokyo
kzkmaeda
1
200
HEXA OSINT CTF V3 作戦会議
meow_noisy
0
120
EMとして2023年度に頑張ったこと / What we did well in FY2023 as a EM
pauli
1
130
Compose Compiler Metricsを使った実践的なコードレビュー
tomorrowkey
1
210
LLM とプロンプトエンジニアリング/チューターをビルドする / LLM and Prompt Engineering and Building Tutors
ks91
PRO
0
250
マルチアカウント環境への発見的統制の導入
ch1aki
1
1.3k
Azure犬駆動開発の記録/GlobalAzureFukuoka2024_20240420
nina01
1
150
Oracle Cloud Infrastructure:2024年4月度サービス・アップデート
oracle4engineer
PRO
1
160
反実仮想機械学習とは何か
usaito
PRO
8
2.9k
よく聞くけど使ったことないソフトウェアNo.1 KafkaとSnowflake
foursue
3
270
ここが嬉しいABAC ここが辛いよABAC #再解説+補足編
masahirokawahara
1
260
Featured
See All Featured
Creatively Recalculating Your Daily Design Routine
revolveconf
209
11k
Thoughts on Productivity
jonyablonski
57
3.8k
Web development in the modern age
philhawksworth
202
10k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
1
1.3k
From Idea to $5000 a Month in 5 Months
shpigford
377
45k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
18
1.7k
Infographics Made Easy
chrislema
237
18k
How STYLIGHT went responsive
nonsquared
92
4.8k
Visualization
eitanlees
135
14k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
34
8.9k
Testing 201, or: Great Expectations
jmmastey
27
6.3k
The Cost Of JavaScript in 2023
addyosmani
14
3.8k
Transcript
Cloud Firestore を 作ってみた 2017/12/08 Mobile Act OSAKA #2 の、スタブ
About Me • Twitter、GitHub、Qiita、 SlideShare、Speaker Deck: ➡ hironytic • At
work: ➡ iOS (Objective-C)、 ときどき Android (Java)、 Windows UWP(C#) • At home: ➡ iOS (Swift) ひろん (一宮 浩教)
Firebase •Remote Config •App Indexing •Dynamic Links •Invites •AdWords •AdMob
•Analytics Develop Grow Earn •Cloud Messaging •Authentication •Realtime Database •Cloud Firestore (beta) •Storage •Hosting •Test Lab •Crash Reporting Oct.4, 2017 Mobile Act OSAKA #1
Cloud Firestore リアルタイムなデータ取得 サーバーとの通信も全部面倒見てくれる 使っている部分のテストどうしよう
そうだ スタブ、作ろう
スタブろう • スタブ (stub) とは、コンピュータプログラム のモジュールをテストする際、そのモジュール が呼び出す下位モジュールの代わりに用いる代 用品のこと。 引用元:Wikipedia 【スタブ】
ドキュメント • JSONライクなKey-Value • Swiftでは [String: Any] { "name": "Nobita",
"skill": "Hirune" }
コレクション • ドキュメントを含むコン テナ • コレクション内でユニー クなドキュメントIDで管 理される { "name":
"Nobita", "skill": "Hirune" } nvGvwR5 { "name": "Suneo", "skill": "Jiman" } zzD7UAv users
サブコレクション { "name": "general" } CSP36ah { "name": "random" }
CSP4CQz channels messages { "from": "AAd7UZv", "message": "何してますか?忙しいですか..." } MV5ahcO { "from": "nvGvwR5", "message": "昼寝してた" } MV5yaC0 { "from": "AAd7UZv", "message": "近くのコンビニエンスストアで..." } MV6Almn { "from": "nvGvwR5", "message": "まかせといてよ" } MV6L38V messages ...
データのリアルタイム取得 Firestore.firestore() .collection("users") .addSnapshotListener{ snapshot, error in // snapshot?.documents[0].documentID ...
String // snapshot?.documents[0].data() ... [String: Any] } コレクション
データのリアルタイム取得 Firestore.firestore() .collection("channels").document("CSP36ah") .collection("messages") .addSnapshotListener{ snapshot, error in // snapshot?.documents[0].documentID
... String // snapshot?.documents[0].data() ... [String: Any] } サブコレクション
データのリアルタイム取得 Firestore.firestore() .collection("users") .whereField("age", isGreaterThan: "18") .order(by: "age") .order(by: "name")
.addSnapshotListener{ snapshot, error in // snapshot?.documents[0].documentID ... String // snapshot?.documents[0].data() ... [String: Any] } クエリ
スタブりにくそう なので、 • すべての機能のスタブは作らない • 自分が必要なものだけ(なるべく妥協する) • Cloud Firestoreのインタフェースそのままでは やらない
どうやってスタブるか? Firestore App
DataStore どうやってスタブるか? Firestore App ある程度、操作を制限
どうやってスタブるか? DefaultDataStore Firestore StubDataStore App 通常時 テスト時 DataStore
コレクションの指定 protocol CollectionPath • コレクションを指す概念を抽象化 collection("channels") .document("CSP36ah") .collection("messages")
クエリ protocol DataStoreQuery • クエリを指定する概念を抽象化 • CollectionPathもDataStoreQuery(プロトコルを継承) collection("channels") .whereField("age", isGreaterThan:
"18") .order(by: "age") .order(by: "name")
クエリ=フィルタ+ソート collection("channels") .whereField("age", isGreaterThan: "18") .order(by: "age") [Entity] .filter(...) .sorted(...)
documentID: String data: [String: Any]
リアルタイムなデータ取得 DataStoreQueryにマッチするEntityの配列が リアルタイムに流れてくる
リアルタイムなデータ取得 DataStoreQueryにマッチするEntityの配列が リアルタイムに流れてくる RxSwiftのObservable func observeCollection(matches query: DataStoreQuery) -> Observable<[Entity]>
スタブるぞ • CollectionPathごとに Observableを保持しておく StubDataStore /channels /channels/CSP36ah/messages Observable<[Entity]> Observable<[Entity]>
/channels /channels/CSP36ah/messages Observable<[Entity]> Observable<[Entity]> スタブるぞ • DataStoreQueryに応じて、 フィルタ、ソートを行う Observableをmapで変換し て返す
StubDataStore filter(...) sorted(...) Observable<[Entity]> func observeCollection(matches query: DataStoreQuery) -> Observable<[Entity]> map
/channels /channels/CSP36ah/messages Observable<[Entity]> Observable<[Entity]> filter(...) sorted(...) Observable<[Entity]> map スタブるぞ •
元のObservableに更新が 流れれば、さっき返した Observableにも自動的に (リアルタイムに)更新 が流れる StubDataStore ✨ ✨
更新系メソッドも まるっとやれば
ほら、スタブれた ⭐ https://github.com/hironytic/CloudFirstoreStubPOC
まとめ • Cloud Firestoreのスタブを作った(スタブった) • 必要な操作にしぼってスタブりやすくした • RxSwiftのおかげでリアルタイムなデータ取得の部 分がうまいこといった •
作ったとは言ったが、使いものになるとは言って いない