Skip to content

Commit

Permalink
Merge pull request #125 from bjchambers/patch-1
Browse files Browse the repository at this point in the history
Raise HTTP errors
  • Loading branch information
erichare authored Nov 28, 2023
2 parents 1ff3acb + a0da116 commit 31eee92
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 18 deletions.
12 changes: 7 additions & 5 deletions astrapy/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from astrapy.utils import make_request, http_methods
from astrapy.defaults import DEFAULT_DEV_OPS_API_VERSION, DEFAULT_DEV_OPS_URL
from astrapy.types import API_RESPONSE
from astrapy.types import API_RESPONSE, OPS_API_RESPONSE


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -69,27 +69,29 @@ def _json_ops_request(
path: str,
options: Optional[Dict[str, Any]] = None,
json_data: Optional[Dict[str, Any]] = None,
) -> API_RESPONSE:
) -> OPS_API_RESPONSE:
req_result = self._ops_request(
method=method,
path=path,
options=options,
json_data=json_data,
)
return cast(
API_RESPONSE,
OPS_API_RESPONSE,
req_result.json(),
)

def get_databases(self, options: Optional[Dict[str, Any]] = None) -> API_RESPONSE:
def get_databases(
self, options: Optional[Dict[str, Any]] = None
) -> OPS_API_RESPONSE:
"""
Retrieve a list of databases.
Args:
options (dict, optional): Additional options for the request.
Returns:
dict: A JSON response containing the list of databases.
list: a JSON list of dictionaries, one per database.
"""
response = self._json_ops_request(
method=http_methods.GET, path="/databases", options=options
Expand Down
6 changes: 5 additions & 1 deletion astrapy/types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import annotations
from typing import Any, Dict, Protocol
from typing import Any, Dict, Protocol, Union

# A type for:
# "dict from parsing a JSON from the API responses"
Expand All @@ -8,6 +8,10 @@
# return JSON objects with a mapping as top-level.
API_RESPONSE = Dict[str, Any]

# The DevOps API has a broader return type in that some calls return
# a list at top level (e.g. "get databases")
OPS_API_RESPONSE = Union[API_RESPONSE, Dict[str, Any]]

# A type for:
# "document stored on the collections"
# Identical to the above in its nature, but preferrably marked as
Expand Down
2 changes: 2 additions & 0 deletions astrapy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ def make_request(
if logger.isEnabledFor(logging.DEBUG):
log_request_response(r, json_data)

r.raise_for_status()

return r


Expand Down
11 changes: 6 additions & 5 deletions tests/astrapy/test_db_dml_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
"""

import logging
from typing import cast

import pytest

from astrapy.db import AstraDBCollection

from astrapy.types import API_DOC

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -268,10 +269,10 @@ def test_vector_find_one_and_replace(
)

assert document2 is not None
assert document2["_id"] == replace_response1["_id"]
assert document2["text"] == replacement1["text"]
assert "added_field" not in document2
assert document2["different_added_field"] is False
assert cast(API_DOC, document2)["_id"] == cast(API_DOC, replace_response1)["_id"]
assert cast(API_DOC, document2)["text"] == replacement1["text"]
assert "added_field" not in cast(API_DOC, document2)
assert cast(API_DOC, document2)["different_added_field"] is False

replace_response_no = disposable_vector_collection.vector_find_one_and_replace(
vector=[0.1, 0.9],
Expand Down
40 changes: 33 additions & 7 deletions tests/astrapy/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import itertools
import pytest
import logging
import os
import uuid
from typing import Any, cast, Dict, List

from dotenv import load_dotenv

Expand All @@ -39,12 +40,22 @@
ASTRA_DB_REGION = os.environ.get("ASTRA_DB_REGION", DEFAULT_REGION)


def find_new_name(existing: List[str], prefix: str) -> str:
candidate_name = prefix
for idx in itertools.count():
candidate_name = f"{prefix}{idx}"
if candidate_name not in existing:
break
return candidate_name


@pytest.fixture
def devops_client() -> AstraDBOps:
return AstraDBOps(token=ASTRA_DB_APPLICATION_TOKEN)


# For now we skip these tests due to creation of DBs
# In the regular CI we skip these Ops tests (slow and require manual care).
# To maintainers: please run them now and them while we figure out automation.
@pytest.mark.skipif(
int(os.environ.get("TEST_ASTRADBOPS", "0")) == 0,
reason="Ops tests not explicitly requested",
Expand All @@ -61,8 +72,16 @@ def test_get_databases(self, devops_client: AstraDBOps) -> None:

@pytest.mark.describe("should create a database")
def test_create_database(self, devops_client: AstraDBOps) -> None:
pre_databases = cast(List[Dict[str, Any]], devops_client.get_databases())
pre_database_names = [db_item["info"]["name"] for db_item in pre_databases]

new_database_name = find_new_name(
existing=pre_database_names,
prefix="vector_create_test_",
)

database_definition = {
"name": "vector_test_create",
"name": new_database_name,
"tier": "serverless",
"cloudProvider": "GCP",
"keyspace": ASTRA_DB_KEYSPACE,
Expand All @@ -81,15 +100,22 @@ def test_create_database(self, devops_client: AstraDBOps) -> None:
ASTRA_TEMP_DB = response["id"]

check_db = devops_client.get_database(database=ASTRA_TEMP_DB)
# actually, if we get to this (the above didn't error) we're good...
assert check_db is not None

term_response = devops_client.terminate_database(database=ASTRA_TEMP_DB)
assert term_response is None

@pytest.mark.describe("should create a keyspace")
def test_create_keyspace(self, devops_client: AstraDBOps) -> None:
target_db = devops_client.get_database(database=ASTRA_DB_ID)
pre_keyspaces = target_db["info"]["keyspaces"]

new_keyspace_name = find_new_name(
existing=pre_keyspaces,
prefix="keyspace_create_test_",
)

response = devops_client.create_keyspace(
keyspace="test_namespace", database=str(uuid.uuid4())
keyspace=new_keyspace_name, database=ASTRA_DB_ID
)

assert response is not None
assert response.status_code == 201 # Created

0 comments on commit 31eee92

Please sign in to comment.