Skip to content

Commit

Permalink
feat: add all_collections (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
gadomski authored Nov 26, 2024
1 parent e490ae5 commit 564f801
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pgstacrs.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,12 @@ class Client:
id: The collection id
"""

async def all_collections(self) -> None:
"""Returns all collections.
Returns:
All collections in the database
"""

class PgstacError:
"""An exception returned from pgstac"""
17 changes: 17 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ macro_rules! pgstac {
})
};

(json $client:expr,$py:expr,$function:expr) => {
let function = $function.to_string();
$client.run($py, |pool: PgstacPool| async move {
let query = format!("SELECT * FROM pgstac.{}()", function);
let connection = pool.get().await?;
let row = connection.query_one(&query, &[]).await?;
let value: Value = row.try_get(function.as_str())?;
Ok(Json(value))
})
};

(option $client:expr,$py:expr,$function:expr,$params:expr) => {
let function = $function.to_string();
$client.run($py, |pool: PgstacPool| async move {
Expand Down Expand Up @@ -139,6 +150,12 @@ impl Client {
void self,py,"delete_collection",[&id]
}
}

fn all_collections<'a>(&self, py: Python<'a>) -> PyResult<Bound<'a, PyAny>> {
pgstac! {
json self,py,"all_collections"
}
}
}

impl Client {
Expand Down
11 changes: 11 additions & 0 deletions tests/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,14 @@ async def test_delete_collection(client: Client, collection: dict[str, Any]) ->
await client.create_collection(collection)
await client.delete_collection("simple-collection")
assert await client.get_collection("simple-id") is None


async def test_all_collections(client: Client, collection: dict[str, Any]) -> None:
assert len(await client.all_collections()) == 0
await client.create_collection(collection)
assert len(await client.all_collections()) == 1
collection["id"] = "just-as-simple-collection"
await client.create_collection(collection)
assert len(await client.all_collections()) == 2
await client.delete_collection("simple-collection")
assert len(await client.all_collections()) == 1

0 comments on commit 564f801

Please sign in to comment.