Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion src/firebolt/model/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import logging
from datetime import datetime
from typing import TYPE_CHECKING, Any, List, Optional
from typing import TYPE_CHECKING, Any, List, Optional, Sequence

from pydantic import Field, PrivateAttr

Expand All @@ -25,6 +25,10 @@ class DatabaseKey(FireboltBaseModel):
database_id: str


class FieldMask(FireboltBaseModel):
paths: Sequence[str] = Field(alias="paths")


class Database(FireboltBaseModel):
"""
A Firebolt database.
Expand Down Expand Up @@ -121,3 +125,42 @@ def delete(self) -> Database:
return Database.parse_obj_with_service(
response.json()["database"], self._service
)

def update(self, description: str) -> Database:
"""
Updates a database description.
"""

class _DatabaseUpdateRequest(FireboltBaseModel):
"""Helper model for sending Database creation requests."""

account_id: str
database: Database
database_id: str
update_mask: FieldMask

self.description = description

logger.info(
f"Updating Database (database_id={self.database_id}, "
f"name={self.name}, description={self.description})"
)

payload = _DatabaseUpdateRequest(
account_id=self._service.account_id,
database=self,
database_id=self.database_id,
update_mask=FieldMask(paths=["description"]),
).jsonable_dict(by_alias=True)

response = self._service.client.patch(
url=ACCOUNT_DATABASE_URL.format(
account_id=self._service.account_id, database_id=self.database_id
),
headers={"Content-type": "application/json"},
json=payload,
)

return Database.parse_obj_with_service(
response.json()["database"], self._service
)
17 changes: 17 additions & 0 deletions tests/unit/service/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,23 @@ def database_get_by_name_url(settings: Settings, account_id: str, mock_database)
)


@pytest.fixture
def database_update_callback(database_get_url, mock_database) -> Callable:
def do_mock(
request: httpx.Request = None,
**kwargs,
) -> Response:
database_properties = json.loads(request.read().decode("utf-8"))["database"]

assert request.url == database_get_url
return Response(
status_code=httpx.codes.OK,
json={"database": database_properties},
)

return do_mock


@pytest.fixture
def database_get_callback(database_get_url, mock_database) -> Callable:
def do_mock(
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/service/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,30 @@ def test_database_get_many(

assert len(databases) == 1
assert databases[0].name == mock_database.name


def test_database_update(
httpx_mock: HTTPXMock,
auth_callback: Callable,
auth_url: str,
provider_callback: Callable,
provider_url: str,
settings: Settings,
account_id_callback: Callable,
account_id_url: str,
database_update_callback: Callable,
database_url: str,
mock_database: Database,
):
httpx_mock.add_callback(auth_callback, url=auth_url)
httpx_mock.add_callback(provider_callback, url=provider_url)
httpx_mock.add_callback(account_id_callback, url=account_id_url)

httpx_mock.add_callback(database_update_callback, url=database_url, method="PATCH")

manager = ResourceManager(settings=settings)

mock_database._service = manager
database = mock_database.update(description="new description")

assert database.description == "new description"