Skip to content

Commit

Permalink
Sl collateral commands (#242)
Browse files Browse the repository at this point in the history
* error message against db.nonmethod() and getattr/getitem of async are non-async functions

* command() method for raw POSTs

* with_options as sugar for copy() methods

* format on test
  • Loading branch information
hemidactylus committed Mar 7, 2024
1 parent 6570a2e commit 85cc7cc
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 0 deletions.
26 changes: 26 additions & 0 deletions astrapy/idiomatic/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,19 @@ def copy(
caller_version=caller_version or self._astra_db_collection.caller_version,
)

def with_options(
self,
*,
name: Optional[str] = None,
caller_name: Optional[str] = None,
caller_version: Optional[str] = None,
) -> Collection:
return self.copy(
name=name,
caller_name=caller_name,
caller_version=caller_version,
)

def to_async(
self,
*,
Expand Down Expand Up @@ -603,6 +616,19 @@ def copy(
caller_version=caller_version or self._astra_db_collection.caller_version,
)

def with_options(
self,
*,
name: Optional[str] = None,
caller_name: Optional[str] = None,
caller_version: Optional[str] = None,
) -> AsyncCollection:
return self.copy(
name=name,
caller_name=caller_name,
caller_version=caller_version,
)

def to_sync(
self,
*,
Expand Down
26 changes: 26 additions & 0 deletions astrapy/idiomatic/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,19 @@ def copy(
api_version=api_version or self._astra_db.api_version,
)

def with_options(
self,
*,
namespace: Optional[str] = None,
caller_name: Optional[str] = None,
caller_version: Optional[str] = None,
) -> Database:
return self.copy(
namespace=namespace,
caller_name=caller_name,
caller_version=caller_version,
)

def to_async(
self,
*,
Expand Down Expand Up @@ -377,6 +390,19 @@ def copy(
api_version=api_version or self._astra_db.api_version,
)

def with_options(
self,
*,
namespace: Optional[str] = None,
caller_name: Optional[str] = None,
caller_version: Optional[str] = None,
) -> AsyncDatabase:
return self.copy(
namespace=namespace,
caller_name=caller_name,
caller_version=caller_version,
)

def to_sync(
self,
*,
Expand Down
15 changes: 15 additions & 0 deletions tests/idiomatic/unit/test_collections_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ async def test_convert_collection_async(
caller_version="c_v",
)
assert col1 == col1.copy()
assert col1 == col1.with_options()
assert col1 == col1.to_sync().to_async()

@pytest.mark.describe("test of Collection rich copy, async")
Expand Down Expand Up @@ -89,6 +90,20 @@ async def test_rich_copy_collection_async(
)
assert col3 == col1

assert col1.with_options(name="x") != col1
assert (
col1.with_options(name="x").with_options(name="id_test_collection") == col1
)
assert col1.with_options(caller_name="x") != col1
assert (
col1.with_options(caller_name="x").with_options(caller_name="c_n") == col1
)
assert col1.with_options(caller_version="x") != col1
assert (
col1.with_options(caller_version="x").with_options(caller_version="c_v")
== col1
)

@pytest.mark.describe("test of Collection rich conversions, async")
async def test_rich_convert_collection_async(
self,
Expand Down
15 changes: 15 additions & 0 deletions tests/idiomatic/unit/test_collections_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def test_convert_collection_sync(
caller_version="c_v",
)
assert col1 == col1.copy()
assert col1 == col1.with_options()
assert col1 == col1.to_async().to_sync()

@pytest.mark.describe("test of Collection rich copy, sync")
Expand Down Expand Up @@ -89,6 +90,20 @@ def test_rich_copy_collection_sync(
)
assert col3 == col1

assert col1.with_options(name="x") != col1
assert (
col1.with_options(name="x").with_options(name="id_test_collection") == col1
)
assert col1.with_options(caller_name="x") != col1
assert (
col1.with_options(caller_name="x").with_options(caller_name="c_n") == col1
)
assert col1.with_options(caller_version="x") != col1
assert (
col1.with_options(caller_version="x").with_options(caller_version="c_v")
== col1
)

@pytest.mark.describe("test of Collection rich conversions, sync")
def test_rich_convert_collection_sync(
self,
Expand Down
13 changes: 13 additions & 0 deletions tests/idiomatic/unit/test_databases_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ async def test_convert_database_async(
**astra_db_credentials_kwargs,
)
assert db1 == db1.copy()
assert db1 == db1.with_options()
assert db1 == db1.to_sync().to_async()

@pytest.mark.describe("test of Database rich copy, async")
Expand Down Expand Up @@ -99,6 +100,18 @@ async def test_rich_copy_database_async(
)
assert db3 == db1

assert db1.with_options(namespace="x") != db1
assert (
db1.with_options(namespace="x").with_options(namespace="namespace") == db1
)
assert db1.with_options(caller_name="x") != db1
assert db1.with_options(caller_name="x").with_options(caller_name="c_n") == db1
assert db1.with_options(caller_version="x") != db1
assert (
db1.with_options(caller_version="x").with_options(caller_version="c_v")
== db1
)

@pytest.mark.describe("test of Database rich conversions, async")
async def test_rich_convert_database_async(
self,
Expand Down
13 changes: 13 additions & 0 deletions tests/idiomatic/unit/test_databases_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def test_convert_database_sync(
**astra_db_credentials_kwargs,
)
assert db1 == db1.copy()
assert db1 == db1.with_options()
assert db1 == db1.to_async().to_sync()

@pytest.mark.describe("test of Database rich copy, sync")
Expand Down Expand Up @@ -99,6 +100,18 @@ def test_rich_copy_database_sync(
)
assert db3 == db1

assert db1.with_options(namespace="x") != db1
assert (
db1.with_options(namespace="x").with_options(namespace="namespace") == db1
)
assert db1.with_options(caller_name="x") != db1
assert db1.with_options(caller_name="x").with_options(caller_name="c_n") == db1
assert db1.with_options(caller_version="x") != db1
assert (
db1.with_options(caller_version="x").with_options(caller_version="c_v")
== db1
)

@pytest.mark.describe("test of Database rich conversions, sync")
def test_rich_convert_database_sync(
self,
Expand Down

0 comments on commit 85cc7cc

Please sign in to comment.