-
Notifications
You must be signed in to change notification settings - Fork 0
/
firestore.rs
78 lines (68 loc) · 2.16 KB
/
firestore.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
pub mod client;
pub mod document;
pub mod path;
pub mod timestamp;
#[cfg(test)]
mod tests {
use crate::infra::firestore::{client::Client, document::Document};
#[tokio::test]
async fn test() -> anyhow::Result<()> {
let endpoint = "http://firebase:8080";
let mut client = Client::new(
"demo-project1".to_string(),
"(default)".to_string(),
endpoint,
)
.await?;
let collection_path = client.collection("repositories".to_string());
assert_eq!(
collection_path.path(),
"projects/demo-project1/databases/(default)/documents/repositories"
);
// reset
let (documents, _) = client.list::<V>(&collection_path).await?;
for doc in documents {
client.delete(doc.name(), doc.update_time()).await?;
}
// CREATE
#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
struct V {
k1: String,
}
let input = V {
k1: "v1".to_string(),
};
let created = client.create(&collection_path, input.clone()).await?;
assert!(created
.name()
.path()
.starts_with("projects/demo-project1/databases/(default)/documents/repositories/"),);
assert_eq!(created.clone().data(), input);
// READ (GET)
let got = client.get(created.name()).await?;
assert_eq!(got, created);
// READ (LIST)
let (documents, next_page_token) = client.list::<V>(&collection_path).await?;
assert_eq!(documents, vec![got.clone()]);
assert_eq!(next_page_token, "");
// UPDATE
let updated: Document<V> = client
.update(
got.name(),
V {
k1: "v2".to_owned(), // "v1" -> "v2
},
got.update_time(),
)
.await?;
assert_eq!(
updated.clone().data(),
V {
k1: "v2".to_string()
}
);
// DELETE
client.delete(updated.name(), updated.update_time()).await?;
Ok(())
}
}