Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add new_transaction support #499

Merged
merged 47 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
d7fe72d
added begin_later flag
daniel-sanche Nov 28, 2023
14942e0
refactoring
daniel-sanche Nov 28, 2023
8ce4654
got tests passing
daniel-sanche Nov 28, 2023
18d6a34
improved docstring
daniel-sanche Nov 28, 2023
af92f68
added helper tests
daniel-sanche Nov 28, 2023
11cedac
fixed type in docstring
daniel-sanche Nov 28, 2023
27454d5
added client test
daniel-sanche Nov 28, 2023
b952b8d
handle context manager close
daniel-sanche Nov 28, 2023
81108df
removed extra indents
daniel-sanche Nov 28, 2023
defd080
added transaction tests
daniel-sanche Nov 28, 2023
2063f71
added system tests
daniel-sanche Nov 28, 2023
648204d
fixed lint
daniel-sanche Nov 28, 2023
087eaaf
refactor using wrappers
daniel-sanche Nov 28, 2023
cbb2c0a
added functools.wraps
daniel-sanche Nov 29, 2023
471ead2
fixed mypy
daniel-sanche Nov 29, 2023
439604c
Merge branch 'main' into new_transaction
daniel-sanche Nov 29, 2023
8da9e00
fixed mypy
daniel-sanche Nov 29, 2023
95797e9
added test
daniel-sanche Nov 29, 2023
09d945f
default begin_later to False
daniel-sanche Nov 30, 2023
3409196
Merge branch 'main' into new_transaction
daniel-sanche Dec 1, 2023
210c5d8
Merge branch 'main' into new_transaction
daniel-sanche Dec 1, 2023
d63cb90
Merge branch 'main' into new_transaction
daniel-sanche Dec 4, 2023
982b998
added new_transaction to query and aggregation classes
daniel-sanche Dec 15, 2023
e7b8b9a
chore(deps): update all dependencies (#505)
renovate-bot Dec 7, 2023
026c934
build: update actions/checkout and actions/setup-python (#507)
gcf-owl-bot[bot] Dec 10, 2023
a001d5f
chore(main): release 2.19.0 (#481)
release-please[bot] Dec 12, 2023
aef3bef
chore(deps): update dependency google-cloud-datastore to v2.19.0 (#508)
renovate-bot Dec 12, 2023
3c2e4a7
build: update actions/upload-artifact and actions/download-artifact (…
gcf-owl-bot[bot] Dec 15, 2023
3ac41ea
got tests passing
daniel-sanche Dec 15, 2023
7e8bb6e
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Dec 15, 2023
78bdba6
Merge branch 'main' into new_transaction
daniel-sanche Feb 9, 2024
4ad1071
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Feb 9, 2024
ce9e335
remove begin from put and delete
daniel-sanche Feb 9, 2024
28f6d10
fixed jinja version
daniel-sanche Feb 9, 2024
61237b3
fixed tests
daniel-sanche Feb 9, 2024
77e410a
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Feb 9, 2024
f4512ba
remove unused decorator
daniel-sanche Feb 9, 2024
b2012a8
removed unneeded delete override
daniel-sanche Feb 9, 2024
cfa11db
fixed commit without begin call
daniel-sanche Feb 23, 2024
efc8e8d
improved tests
daniel-sanche Feb 23, 2024
6872fcf
refactored out helper function
daniel-sanche Feb 23, 2024
19ed6e9
fixed lint
daniel-sanche Feb 23, 2024
4682186
added comment
daniel-sanche Feb 23, 2024
abf29e3
added tests for helper
daniel-sanche Feb 23, 2024
8061202
Merge branch 'main' into new_transaction
daniel-sanche Mar 5, 2024
c90da29
Merge branch 'main' into new_transaction
daniel-sanche Mar 7, 2024
4b40a87
Merge branch 'main' into new_transaction
daniel-sanche Apr 4, 2024
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
Prev Previous commit
Next Next commit
fixed tests
  • Loading branch information
daniel-sanche committed Feb 9, 2024
commit 61237b3e9a82ccae89db17e28250fe793366320a
5 changes: 3 additions & 2 deletions google/cloud/datastore/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,8 +784,9 @@ def _next_page(self):
transaction_id = None
else:
transaction_id = transaction.id
# if transaction hasn't been initialized, initialize it as part of this request
new_transaction_options = transaction._options
if transaction._begin_later and transaction._status == transaction._INITIAL:
# if transaction hasn't been initialized, initialize it as part of this request
new_transaction_options = transaction._options
read_options = helpers.get_read_options(
self._eventual, transaction_id, self._read_time, new_transaction_options
)
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/test_aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,9 @@ def _next_page_helper(txn_id=None, retry=None, timeout=None, database_id=None):
if txn_id is None:
client = _Client(project, datastore_api=ds_api, database=database_id)
else:
transaction = mock.Mock(id=txn_id, spec=["id"])
transaction = mock.Mock(
id=txn_id, _begin_later=False, spec=["id", "_begin_later"]
)
client = _Client(
project, datastore_api=ds_api, transaction=transaction, database=database_id
)
Expand Down
10 changes: 6 additions & 4 deletions tests/unit/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ def test_eventual_transaction_fails(database_id):
@pytest.mark.parametrize("database_id", [None, "somedb"])
def test_transaction_id_populated(database_id):
"""
When an aggregation is run in the context of a transaction, the transaction
When an query is run in the context of a transaction, the transaction
ID should be populated in the request.
"""
import mock
Expand Down Expand Up @@ -699,7 +699,7 @@ def test_transaction_id_populated(database_id):


@pytest.mark.parametrize("database_id", [None, "somedb"])
def test_transaction_begin_later(database_id):
def test_query_transaction_begin_later(database_id):
"""
When an aggregation is run in the context of a transaction with begin_later=True,
the new_transaction field should be populated in the request read_options.
Expand All @@ -711,7 +711,7 @@ def test_transaction_begin_later(database_id):
transaction = mock.Mock()
transaction.id = None
transaction._begin_later = True
transaction._state = transaction._INITIAL
transaction._status = transaction._INITIAL
transaction._options = TransactionOptions(read_only=TransactionOptions.ReadOnly())

mock_datastore_api = mock.Mock()
Expand Down Expand Up @@ -926,7 +926,9 @@ def _next_page_helper(
if txn_id is None:
client = _Client(project, database=database, datastore_api=ds_api)
else:
transaction = mock.Mock(id=txn_id, spec=["id"])
transaction = mock.Mock(
id=txn_id, _begin_later=False, spec=["id", "_begin_later"]
)
client = _Client(
project, database=database, datastore_api=ds_api, transaction=transaction
)
Expand Down
64 changes: 0 additions & 64 deletions tests/unit/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,70 +491,6 @@ def test_transaction_put_read_only(database_id):
xact.put(entity)


@pytest.mark.parametrize("database_id", [None, "somedb"])
def test_transaction_begin_later_with_delete(database_id):
"""
begin_later transactions should begin on delete
"""
project = "PROJECT"
id_ = 912830
ds_api = _make_datastore_api(xact_id=id_)
client = _Client(project, datastore_api=ds_api, database=database_id)
xact = _make_transaction(client, begin_later=True)

fake_key = mock.Mock()

with mock.patch("google.cloud.datastore.batch.Batch.delete") as delete:
with xact:
assert xact._status == xact._INITIAL
assert xact.id is None
xact.delete(fake_key)
# call should have started transaction
assert xact._status == xact._IN_PROGRESS
assert xact.id == id_
# super class delete should have been called
assert delete.call_count == 1
assert delete.call_args == mock.call(fake_key)
# should be finalized after context manager block
assert xact._status == xact._FINISHED
assert xact.id is None
# should have committed
assert ds_api.commit.call_count == 1
assert ds_api.rollback.call_count == 0


@pytest.mark.parametrize("database_id", [None, "somedb"])
def test_transaction_begin_later_with_put(database_id):
"""
begin_later transactions should begin on put
"""
project = "PROJECT"
id_ = 912830
ds_api = _make_datastore_api(xact_id=id_)
client = _Client(project, datastore_api=ds_api, database=database_id)
xact = _make_transaction(client, begin_later=True)

fake_entity = mock.Mock()

with mock.patch("google.cloud.datastore.batch.Batch.put") as put:
with xact:
assert xact._status == xact._INITIAL
assert xact.id is None
xact.put(fake_entity)
# call should have started transaction
assert xact._status == xact._IN_PROGRESS
assert xact.id == id_
# super class put should have been called
assert put.call_count == 1
assert put.call_args == mock.call(fake_entity)
# should be finalized after context manager block
assert xact._status == xact._FINISHED
assert xact.id is None
# should have committed
assert ds_api.commit.call_count == 1
assert ds_api.rollback.call_count == 0


def _make_key(kind, id_, project, database=None):
from google.cloud.datastore_v1.types import entity as entity_pb2

Expand Down
Loading