Skip to content

Commit

Permalink
Fix issues with None data values in updat_item
Browse files Browse the repository at this point in the history
  • Loading branch information
fulder committed Oct 30, 2021
1 parent 44a0715 commit 4c43c53
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/layers/databases/python/watch_history_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,19 @@ def update_item(username, collection_name, item_id, data,
m_d = m_d.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
data["latest_watch_date"] = m_d.replace("000Z", "Z")

items = ','.join(f'#{k}=:{k}' for k in data)
update_expression = f"SET {items}"
expression_attribute_names = {f'#{k}': k for k in data}
expression_attribute_values = {f':{k}': v for k, v in data.items()}
update_expression = "SET "
expression_attribute_names = {}
expression_attribute_values = {}
for k, v in data.items():
if v is None:
continue

update_expression += f"#{k}=:{k},"
expression_attribute_names[f"#{k}"] = k
expression_attribute_values[f":{k}"] = v

# remove last comma
update_expression = update_expression[:-1]

remove_names = []
for o in OPTIONAL_FIELDS:
Expand Down
32 changes: 32 additions & 0 deletions test/unittest/test_watch_history_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,38 @@ def test_update_item_dates_watched_one_date(mocked_watch_history_db):
}


def test_update_item_none_data(mocked_watch_history_db):
mock_func = MockFunc()
mocked_watch_history_db.table.update_item = mock_func.f

mocked_watch_history_db.update_item(TEST_USERNAME, "MOVIE", "123",
{"review": "review_text", "ignore": None})

assert mock_func.update_values == {
'ExpressionAttributeNames': {
'#collection_name': 'collection_name',
'#dates_watched': 'dates_watched',
'#deleted_at': 'deleted_at',
'#overview': 'overview',
'#rating': 'rating',
'#review': 'review',
'#status': 'status',
'#updated_at': 'updated_at'
},
'ExpressionAttributeValues': {
':collection_name': 'MOVIE',
":updated_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
":review": "review_text"
},
'Key': {
'username': TEST_USERNAME,
'item_id': '123'},
'UpdateExpression': 'SET #review=:review,#collection_name=:collection_name,'
'#updated_at=:updated_at '
'REMOVE #deleted_at,#overview,#status,#rating,#dates_watched'
}


def test_delete_item(mocked_watch_history_db):
mock_func = MockFunc()
mocked_watch_history_db.table.update_item = mock_func.f
Expand Down

0 comments on commit 4c43c53

Please sign in to comment.