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

Fix update item without get first resetting version to 1 (#1247) #1248

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
58 changes: 58 additions & 0 deletions tests/integration/model_integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,61 @@ class Meta:

version_invalid = VersionAttribute()
assert str(e.value) == 'The model has more than one Version attribute: version, version_invalid'


@pytest.mark.ddblocal
def test_update_model_with_version_attribute_without_get(ddb_url):
class TestModel(Model):
"""
A model for testing
"""
class Meta:
region = 'us-east-1'
table_name = 'pynamodb-ci'
host = ddb_url
forum = UnicodeAttribute(hash_key=True)
score = NumberAttribute(null=True)
version = VersionAttribute()

if TestModel.exists():
TestModel.delete_table()
TestModel.create_table(read_capacity_units=1, write_capacity_units=1, wait=True)

obj = TestModel('1')
obj.save()
assert TestModel.get('1').version == 1
obj.score = 1
obj.save()
assert TestModel.get('1').version == 2

obj_by_key = TestModel('1') # try to update item without getting it first
obj_by_key.update(
actions=[
TestModel.score.set(2), # no version increment
],
add_version_condition=False
)
updated_obj = TestModel.get('1')
assert updated_obj.score == 2
assert updated_obj.version == 2

obj_2 = TestModel('2')
obj_2.save()
assert TestModel.get('2').version == 1
obj_2.score = 1
obj_2.save()
assert TestModel.get('2').version == 2

obj_2_by_key = TestModel('2') # try to update item without getting it first
obj_2_by_key.update(
actions=[
TestModel.score.set(2),
TestModel.version.set(TestModel.version + 1) # increment version manually
],
add_version_condition=False
)
updated_obj_2 = TestModel.get('2')
assert updated_obj_2.score == 2
assert updated_obj_2.version == 3

TestModel.delete_table()
48 changes: 48 additions & 0 deletions tests/integration/test_transaction_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,51 @@ def test_transaction_write_with_version_attribute_condition_failure(connection):
assert len(exc_info.value.cancellation_reasons) == 1
assert exc_info.value.cancellation_reasons[0].code == 'ConditionalCheckFailed'
assert Foo.Meta.table_name in exc_info.value.cause.MSG_TEMPLATE


@pytest.mark.ddblocal
def test_transaction_write_without_version_attribute_condition(connection):
foo = Foo(22)
foo.save()
assert Foo.get(22).version == 1
foo.star = 'initial_update'
foo.save()
assert Foo.get(22).version == 2

foo2 = Foo(22) # try to update a field without getting the item first

with TransactWrite(connection=connection) as transaction:
transaction.update(
foo2,
actions=[
Foo.star.set('birdistheword'),
],
add_version_condition=False,
)

foo_updated = Foo.get(22)
assert foo_updated.version == 2 # should not modify the version
assert foo_updated.star == 'birdistheword'


@pytest.mark.ddblocal
def test_transaction_write_increment_version_without_version_attribute_condition(connection):
foo = Foo(23)
foo.save()
assert Foo.get(23).version == 1

foo2 = Foo(23)

with TransactWrite(connection=connection) as transaction:
transaction.update(
foo2,
actions=[
Foo.star.set('birdistheword'),
Foo.version.set(Foo.version + 1),
],
add_version_condition=False,
)

foo_updated = Foo.get(23)
assert foo_updated.star == 'birdistheword'
assert foo_updated.version == 3