Skip to content
This repository has been archived by the owner on Apr 27, 2024. It is now read-only.

Commit

Permalink
Revert "refactor: make the client owned"
Browse files Browse the repository at this point in the history
This reverts commit 69ed1a9.
  • Loading branch information
gadomski committed Jun 16, 2023
1 parent 69ed1a9 commit 18b0a5f
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 104 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Expand Up @@ -14,8 +14,8 @@ categories = ["database", "data-structures", "science"]
geojson = "0.24"
serde = "1"
serde_json = "1"
stac = "0.4"
stac-api = "0.2"
stac = { version = "0.4", git = "https://github.com/gadomski/stac-rs" }
stac-api = { version = "0.2", git = "https://github.com/gadomski/stac-rs" }
thiserror = "1"
tokio-postgres = { version = "0.7", features = ["with-serde_json-1"] }

Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Expand Up @@ -2,7 +2,7 @@ version: '3'
services:
database:
container_name: pgstac-rs
image: ghcr.io/stac-utils/pgstac:v0.7.9
image: ghcr.io/stac-utils/pgstac:v0.7.1
environment:
- POSTGRES_USER=username
- POSTGRES_PASSWORD=password
Expand Down
2 changes: 1 addition & 1 deletion pgstac-test/Cargo.toml
Expand Up @@ -10,5 +10,5 @@ doctest = false

[dependencies]
quote = "1"
syn = { version = "2", features = ["full", "extra-traits"] }
syn = { version = "1", features = ["full", "extra-traits"] }
tokio-postgres = { version = "0.7" }
3 changes: 1 addition & 2 deletions pgstac-test/src/lib.rs
Expand Up @@ -20,10 +20,9 @@ fn impl_pgstac_test(ast: ItemFn) -> TokenStream {
connection.await.unwrap()
});
let transaction = client.transaction().await.unwrap();
let client = Client::new(transaction);
let client = Client::new(&transaction);
#ast
#ident(&client).await;
let transaction = client.into_inner();
transaction.rollback().await.unwrap();
}
};
Expand Down
93 changes: 33 additions & 60 deletions src/client.rs
Expand Up @@ -9,46 +9,19 @@ use tokio_postgres::{

/// A **pgstac** client.
///
/// Not every **pgstac** function is provided, and some names are changed to match Rust conventions.
/// Not every **pgstac** function is provided, and some names are changed to
/// match Rust conventions.
#[derive(Debug)]
pub struct Client<C>(C)
pub struct Client<'a, C>(&'a C)
where
C: GenericClient;

impl<C: GenericClient> Client<C> {
impl<'a, C: GenericClient> Client<'a, C> {
/// Creates a new client.
///
/// # Examples
///
/// ```
/// use pgstac::Client;
/// # tokio_test::block_on(async {
/// let config = "postgresql://username:password@localhost:5432/postgis";
/// let (client, connection) = tokio_postgres::connect(config, tokio_postgres::NoTls).await.unwrap();
/// let client = Client::new(client);
/// # })
/// ```
pub fn new(client: C) -> Client<C> {
pub fn new(client: &C) -> Client<C> {
Client(client)
}

/// Consumes this client and returns is inner `GenericClient`.
///
/// # Examples
///
/// ```
/// use pgstac::Client;
/// # tokio_test::block_on(async {
/// let config = "postgresql://username:password@localhost:5432/postgis";
/// let (client, connection) = tokio_postgres::connect(config, tokio_postgres::NoTls).await.unwrap();
/// let client = Client::new(client);
/// let tokio_client = client.into_inner();
/// # })
/// ```
pub fn into_inner(self) -> C {
self.0
}

/// Returns the **pgstac** version.
pub async fn version(&self) -> Result<String> {
self.string("get_version", &[]).await
Expand Down Expand Up @@ -215,17 +188,17 @@ mod tests {
}

#[pgstac_test]
async fn version(client: &Client<Transaction<'_>>) {
async fn version(client: &Client<'_, Transaction<'_>>) {
let _ = client.version().await.unwrap();
}

#[pgstac_test]
async fn setting(client: &Client<Transaction<'_>>) {
async fn setting(client: &Client<'_, Transaction<'_>>) {
assert_eq!(client.setting("context").await.unwrap(), "off");
}

#[pgstac_test]
async fn collections(client: &Client<Transaction<'_>>) {
async fn collections(client: &Client<'_, Transaction<'_>>) {
assert!(client.collections().await.unwrap().is_empty());
client
.add_collection(Collection::new("an-id", "a description"))
Expand All @@ -235,15 +208,15 @@ mod tests {
}

#[pgstac_test]
async fn add_collection_duplicate(client: &Client<Transaction<'_>>) {
async fn add_collection_duplicate(client: &Client<'_, Transaction<'_>>) {
assert!(client.collections().await.unwrap().is_empty());
let collection = Collection::new("an-id", "a description");
client.add_collection(collection.clone()).await.unwrap();
assert!(client.add_collection(collection).await.is_err());
}

#[pgstac_test]
async fn upsert_collection(client: &Client<Transaction<'_>>) {
async fn upsert_collection(client: &Client<'_, Transaction<'_>>) {
assert!(client.collections().await.unwrap().is_empty());
let mut collection = Collection::new("an-id", "a description");
client.upsert_collection(collection.clone()).await.unwrap();
Expand All @@ -262,7 +235,7 @@ mod tests {
}

#[pgstac_test]
async fn update_collection(client: &Client<Transaction<'_>>) {
async fn update_collection(client: &Client<'_, Transaction<'_>>) {
let mut collection = Collection::new("an-id", "a description");
client.add_collection(collection.clone()).await.unwrap();
assert!(client
Expand All @@ -288,18 +261,18 @@ mod tests {
}

#[pgstac_test]
async fn update_collection_does_not_exit(client: &Client<Transaction<'_>>) {
async fn update_collection_does_not_exit(client: &Client<'_, Transaction<'_>>) {
let collection = Collection::new("an-id", "a description");
assert!(client.update_collection(collection).await.is_err());
}

#[pgstac_test]
async fn collection_not_found(client: &Client<Transaction<'_>>) {
async fn collection_not_found(client: &Client<'_, Transaction<'_>>) {
assert!(client.collection("not-an-id").await.unwrap().is_none());
}

#[pgstac_test]
async fn delete_collection(client: &Client<Transaction<'_>>) {
async fn delete_collection(client: &Client<'_, Transaction<'_>>) {
let collection = Collection::new("an-id", "a description");
client.add_collection(collection.clone()).await.unwrap();
assert!(client.collection("an-id").await.unwrap().is_some());
Expand All @@ -308,12 +281,12 @@ mod tests {
}

#[pgstac_test]
async fn delete_collection_does_not_exist(client: &Client<Transaction<'_>>) {
async fn delete_collection_does_not_exist(client: &Client<'_, Transaction<'_>>) {
assert!(client.delete_collection("not-an-id").await.is_err());
}

#[pgstac_test]
async fn item(client: &Client<Transaction<'_>>) {
async fn item(client: &Client<'_, Transaction<'_>>) {
assert!(client
.item("an-id", "collection-id")
.await
Expand All @@ -336,13 +309,13 @@ mod tests {
}

#[pgstac_test]
async fn item_without_collection(client: &Client<Transaction<'_>>) {
async fn item_without_collection(client: &Client<'_, Transaction<'_>>) {
let item = Item::new("an-id");
assert!(client.add_item(item.clone()).await.is_err());
}

#[pgstac_test]
async fn update_item(client: &Client<Transaction<'_>>) {
async fn update_item(client: &Client<'_, Transaction<'_>>) {
let collection = Collection::new("collection-id", "a description");
client.add_collection(collection).await.unwrap();
let mut item = Item::new("an-id");
Expand All @@ -366,7 +339,7 @@ mod tests {
}

#[pgstac_test]
async fn upsert_item(client: &Client<Transaction<'_>>) {
async fn upsert_item(client: &Client<'_, Transaction<'_>>) {
let collection = Collection::new("collection-id", "a description");
client.add_collection(collection).await.unwrap();
let mut item = Item::new("an-id");
Expand All @@ -377,7 +350,7 @@ mod tests {
}

#[pgstac_test]
async fn add_items(client: &Client<Transaction<'_>>) {
async fn add_items(client: &Client<'_, Transaction<'_>>) {
let collection = Collection::new("collection-id", "a description");
client.add_collection(collection).await.unwrap();
let mut item = Item::new("an-id");
Expand All @@ -399,7 +372,7 @@ mod tests {
}

#[pgstac_test]
async fn upsert_items(client: &Client<Transaction<'_>>) {
async fn upsert_items(client: &Client<'_, Transaction<'_>>) {
let collection = Collection::new("collection-id", "a description");
client.add_collection(collection).await.unwrap();
let mut item = Item::new("an-id");
Expand All @@ -413,7 +386,7 @@ mod tests {
}

#[pgstac_test]
async fn search_everything(client: &Client<Transaction<'_>>) {
async fn search_everything(client: &Client<'_, Transaction<'_>>) {
assert!(client
.search(Search::default())
.await
Expand All @@ -433,7 +406,7 @@ mod tests {
}

#[pgstac_test]
async fn search_ids(client: &Client<Transaction<'_>>) {
async fn search_ids(client: &Client<'_, Transaction<'_>>) {
let collection = Collection::new("collection-id", "a description");
client.add_collection(collection).await.unwrap();
let mut item = Item::new("an-id");
Expand All @@ -453,7 +426,7 @@ mod tests {
}

#[pgstac_test]
async fn search_collections(client: &Client<Transaction<'_>>) {
async fn search_collections(client: &Client<'_, Transaction<'_>>) {
let collection = Collection::new("collection-id", "a description");
client.add_collection(collection).await.unwrap();
let mut item = Item::new("an-id");
Expand All @@ -473,7 +446,7 @@ mod tests {
}

#[pgstac_test]
async fn search_limit(client: &Client<Transaction<'_>>) {
async fn search_limit(client: &Client<'_, Transaction<'_>>) {
let collection = Collection::new("collection-id", "a description");
client.add_collection(collection).await.unwrap();
let mut item = Item::new("an-id");
Expand All @@ -492,7 +465,7 @@ mod tests {
}

#[pgstac_test]
async fn search_bbox(client: &Client<Transaction<'_>>) {
async fn search_bbox(client: &Client<'_, Transaction<'_>>) {
let collection = Collection::new("collection-id", "a description");
client.add_collection(collection).await.unwrap();
let mut item = Item::new("an-id");
Expand All @@ -512,7 +485,7 @@ mod tests {
}

#[pgstac_test]
async fn search_datetime(client: &Client<Transaction<'_>>) {
async fn search_datetime(client: &Client<'_, Transaction<'_>>) {
let collection = Collection::new("collection-id", "a description");
client.add_collection(collection).await.unwrap();
let mut item = Item::new("an-id");
Expand All @@ -533,7 +506,7 @@ mod tests {
}

#[pgstac_test]
async fn search_intersects(client: &Client<Transaction<'_>>) {
async fn search_intersects(client: &Client<'_, Transaction<'_>>) {
let collection = Collection::new("collection-id", "a description");
client.add_collection(collection).await.unwrap();
let mut item = Item::new("an-id");
Expand Down Expand Up @@ -565,7 +538,7 @@ mod tests {
}

#[pgstac_test]
async fn pagination(client: &Client<Transaction<'_>>) {
async fn pagination(client: &Client<'_, Transaction<'_>>) {
let collection = Collection::new("collection-id", "a description");
client.add_collection(collection).await.unwrap();
let mut item = Item::new("an-id");
Expand Down Expand Up @@ -595,7 +568,7 @@ mod tests {
}

#[pgstac_test]
async fn fields(client: &Client<Transaction<'_>>) {
async fn fields(client: &Client<'_, Transaction<'_>>) {
let collection = Collection::new("collection-id", "a description");
client.add_collection(collection).await.unwrap();
let mut item = Item::new("an-id");
Expand All @@ -622,7 +595,7 @@ mod tests {
}

#[pgstac_test]
async fn sortby(client: &Client<Transaction<'_>>) {
async fn sortby(client: &Client<'_, Transaction<'_>>) {
let collection = Collection::new("collection-id", "a description");
client.add_collection(collection).await.unwrap();
let mut item = Item::new("a");
Expand All @@ -649,7 +622,7 @@ mod tests {
}

#[pgstac_test]
async fn filter(client: &Client<Transaction<'_>>) {
async fn filter(client: &Client<'_, Transaction<'_>>) {
let collection = Collection::new("collection-id", "a description");
client.add_collection(collection).await.unwrap();
let mut item = Item::new("a");
Expand All @@ -676,7 +649,7 @@ mod tests {
}

#[pgstac_test]
async fn query(client: &Client<Transaction<'_>>) {
async fn query(client: &Client<'_, Transaction<'_>>) {
let collection = Collection::new("collection-id", "a description");
client.add_collection(collection).await.unwrap();
let mut item = Item::new("a");
Expand Down

0 comments on commit 18b0a5f

Please sign in to comment.