Skip to content

Commit

Permalink
Replace path mod with firestore-path crate
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Dec 21, 2023
1 parent d6cbee0 commit c7d3766
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 596 deletions.
18 changes: 14 additions & 4 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rust/crates/web/Cargo.toml
Expand Up @@ -11,6 +11,7 @@ async-graphql = "6.0.5"
async-graphql-axum = "6.0.5"
axum = "0.6.20"
features = "0.10.0"
firestore-path = "0.4.0"
google-api-proto = { version = "1.415.0", features = ["google-firestore-v1"] }
google-authz = { version = "1.0.0-alpha.5", features = ["tonic"] }
hyper = { version = "0.14.27", features = ["full"] }
Expand Down
49 changes: 28 additions & 21 deletions rust/crates/web/src/infra/firestore.rs
@@ -1,10 +1,13 @@
pub mod client;
pub mod document;
pub mod path;
pub mod timestamp;

#[cfg(test)]
mod tests {
use std::str::FromStr as _;

use firestore_path::{DatabaseId, DatabaseName, ProjectId};

use crate::infra::firestore::{
client::{Client, Error},
document::Document,
Expand All @@ -14,20 +17,22 @@ mod tests {
async fn test() -> anyhow::Result<()> {
let endpoint = "http://firebase:8080";
let mut client = Client::new(
"demo-project1".to_string(),
"(default)".to_string(),
DatabaseName::new(
ProjectId::from_str("demo-project1")?,
DatabaseId::from_str("(default)")?,
),
endpoint,
)
.await?;
let collection_path = client.collection("repositories")?;
let collection_name = client.collection("repositories")?;

assert_eq!(
collection_path.path(),
collection_name.to_string(),
"projects/demo-project1/databases/(default)/documents/repositories"
);

// reset
let (documents, _) = client.list::<V>(&collection_path).await?;
let (documents, _) = client.list::<V>(&collection_name).await?;
for doc in documents {
client.delete(doc.name(), doc.update_time()).await?;
}
Expand All @@ -40,10 +45,10 @@ mod tests {
let input = V {
k1: "v1".to_string(),
};
let document_path = collection_path.clone().doc("1")?;
let document_path = collection_name.clone().doc("1")?;
let created = client.create(&document_path, input.clone()).await?;
assert_eq!(
created.name().path(),
created.name().to_string(),
"projects/demo-project1/databases/(default)/documents/repositories/1"
);
assert_eq!(created.clone().data(), input);
Expand All @@ -53,7 +58,7 @@ mod tests {
assert_eq!(got, created);

// READ (LIST)
let (documents, next_page_token) = client.list::<V>(&collection_path).await?;
let (documents, next_page_token) = client.list::<V>(&collection_name).await?;
assert_eq!(documents, vec![got.clone()]);
assert_eq!(next_page_token, None);

Expand Down Expand Up @@ -84,20 +89,22 @@ mod tests {
async fn test_transaction() -> anyhow::Result<()> {
let endpoint = "http://firebase:8080";
let mut client = Client::new(
"demo-project1".to_string(),
"(default)".to_string(),
DatabaseName::new(
ProjectId::from_str("demo-project1")?,
DatabaseId::from_str("(default)")?,
),
endpoint,
)
.await?;
let collection_path = client.collection("transactions")?;
let collection_name = client.collection("transactions")?;

// reset
let (documents, _) = client.list::<V>(&collection_path).await?;
let (documents, _) = client.list::<V>(&collection_name).await?;
for doc in documents {
client.delete(doc.name(), doc.update_time()).await?;
}

let document_path = collection_path.doc("1")?;
let document_name = collection_name.doc("1")?;

#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
struct V {
Expand All @@ -109,18 +116,18 @@ mod tests {
};
client
.run_transaction(|transaction| {
let p = document_path.clone();
let p = document_name.clone();
Box::pin(async move {
transaction.create(&p, input)?;
Ok(())
})
})
.await?;
assert!(client.get::<V>(&document_path).await.is_ok());
assert!(client.get::<V>(&document_name).await.is_ok());

let result = client
.run_transaction(|transaction| {
let p = document_path.clone();
let p = document_name.clone();
Box::pin(async move {
let got = transaction.get::<V>(&p).await?;
transaction.delete(&p, got.update_time())?;
Expand All @@ -130,20 +137,20 @@ mod tests {
.await;
assert!(result.is_err());
// Not deleted because it was rolled back
assert!(client.get::<V>(&document_path).await.is_ok());
assert!(client.get::<V>(&document_name).await.is_ok());

let got = client.get::<V>(&document_path).await?;
let got = client.get::<V>(&document_name).await?;
let current_update_time = got.update_time();
client
.run_transaction(|transaction| {
let p = document_path.clone();
let p = document_name.clone();
Box::pin(async move {
transaction.delete(&p, current_update_time)?;
Ok(())
})
})
.await?;
let err = client.get::<V>(&document_path).await.unwrap_err();
let err = client.get::<V>(&document_name).await.unwrap_err();
if let crate::infra::firestore::client::Error::Status(status) = err {
assert_eq!(status.code(), tonic::Code::NotFound);
} else {
Expand Down

0 comments on commit c7d3766

Please sign in to comment.