Skip to content

Commit

Permalink
Allow VersionAttribute to operate on items with a version of zero (#869)
Browse files Browse the repository at this point in the history
  • Loading branch information
bobwanglyft authored Oct 14, 2020
1 parent b42c326 commit ce43a2f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pynamodb/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ def _handle_version_attribute(self, serialized_attributes, actions=None):
version_attribute = self.get_attributes()[self._version_attribute_name]
version_attribute_value = getattr(self, self._version_attribute_name)

if version_attribute_value:
if version_attribute_value is not None:
version_condition = version_attribute == version_attribute_value
if actions:
actions.append(version_attribute.add(1))
Expand Down
54 changes: 54 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3076,6 +3076,60 @@ def test_model_version_attribute_save(self):

deep_eq(args, params, _assert=True)

def test_model_version_attribute_save_with_initial_version_zero(self):
self.init_table_meta(VersionedModel, VERSIONED_TABLE_DATA)
item = VersionedModel('test_user_name', email='test_user@email.com', version=0)

with patch(PATCH_METHOD) as req:
req.return_value = {}
item.save()
args = req.call_args[0][1]
params = {
'Item': {
'name': {
'S': 'test_user_name'
},
'email': {
'S': 'test_user@email.com'
},
'version': {
'N': '1'
},
},
'ReturnConsumedCapacity': 'TOTAL',
'TableName': 'VersionedModel',
'ConditionExpression': '#0 = :0',
'ExpressionAttributeNames': {'#0': 'version'},
'ExpressionAttributeValues': {':0': {'N': '0'}}
}

deep_eq(args, params, _assert=True)
item.version = 1
item.name = "test_new_username"
item.save()
args = req.call_args[0][1]

params = {
'Item': {
'name': {
'S': 'test_new_username'
},
'email': {
'S': 'test_user@email.com'
},
'version': {
'N': '2'
},
},
'ReturnConsumedCapacity': 'TOTAL',
'TableName': 'VersionedModel',
'ConditionExpression': '#0 = :0',
'ExpressionAttributeNames': {'#0': 'version'},
'ExpressionAttributeValues': {':0': {'N': '1'}}
}

deep_eq(args, params, _assert=True)

def test_version_attribute_increments_on_update(self):
self.init_table_meta(VersionedModel, VERSIONED_TABLE_DATA)
item = VersionedModel('test_user_name', email='test_user@email.com')
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = py36,py37,pypy,pypy3
envlist = py36,py37,py38,pypy,pypy3

[testenv]
deps = -rrequirements-dev.txt
Expand Down

0 comments on commit ce43a2f

Please sign in to comment.