Skip to content

Commit

Permalink
Add last_modified to DynamoDB items
Browse files Browse the repository at this point in the history
* Add last_modified to DynamoDB items to all methods that update table entries
* Update unit tests to account for last_modified
  • Loading branch information
ehanson8 committed Jan 28, 2022
1 parent c8b4e38 commit 0aa87d1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
8 changes: 8 additions & 0 deletions awd/dynamodb.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from datetime import datetime

import boto3
from boto3.dynamodb.types import TypeDeserializer
Expand All @@ -25,6 +26,7 @@ def add_doi_item_to_database(self, doi_table, doi):
"doi": {"S": doi},
"status": {"S": str(Status.PROCESSING.value)},
"attempts": {"S": "0"},
"last_modified": {"S": datetime.now().strftime("%Y-%m-%d %H:%M:%S")},
},
)
logger.debug(f"{doi} added to table")
Expand Down Expand Up @@ -87,6 +89,9 @@ def update_doi_item_attempts_in_database(self, doi_table, doi):
Key={"doi": {"S": doi}},
)
item["Item"]["attempts"]["S"] = str(int(item["Item"]["attempts"]["S"]) + 1)
item["Item"]["last_modified"]["S"] = datetime.now().strftime(
"%Y-%m-%d %H:%M:%S"
)
response = self.client.put_item(TableName=doi_table, Item=item["Item"])
logger.debug(
f'{doi} attempts updated to: {str(int(item["Item"]["attempts"]["S"]) + 1)}'
Expand All @@ -105,6 +110,9 @@ def update_doi_item_status_in_database(
Key={"doi": {"S": doi}},
)
item["Item"]["status"]["S"] = str(status_code)
item["Item"]["last_modified"]["S"] = datetime.now().strftime(
"%Y-%m-%d %H:%M:%S"
)
response = self.client.put_item(
TableName=doi_table,
Item=item["Item"],
Expand Down
2 changes: 2 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ def test_listen_success(
"doi": {"S": "111.1/1111"},
"status": {"S": str(Status.PROCESSING.value)},
"attempts": {"S": "1"},
"last_modified": {"S": "'2022-01-28 09:28:53"},
},
)
dynamodb_class.client.put_item(
Expand All @@ -325,6 +326,7 @@ def test_listen_success(
"doi": {"S": "222.2/2222"},
"status": {"S": str(Status.PROCESSING.value)},
"attempts": {"S": "1"},
"last_modified": {"S": "'2022-01-28 10:28:53"},
},
)
result = runner.invoke(
Expand Down
42 changes: 22 additions & 20 deletions tests/test_dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def test_dynamodb_retrieve_doi_items_from_database(mocked_dynamodb, dynamodb_cla
"doi": {"S": "111.1/1111"},
"status": {"S": str(Status.FAILED.value)},
"attempts": {"S": "1"},
"last_modified": {"S": "2022-01-28 10:28:53"},
},
)
dois = dynamodb_class.retrieve_doi_items_from_database("test_dois")
Expand All @@ -41,6 +42,7 @@ def test_dynamodb_retrieve_doi_items_from_database(mocked_dynamodb, dynamodb_cla
"doi": "111.1/1111",
"status": str(Status.FAILED.value),
"attempts": "1",
"last_modified": "2022-01-28 10:28:53",
}
]

Expand All @@ -52,6 +54,7 @@ def test_dynamodb_retry_threshold_exceeded_false(mocked_dynamodb, dynamodb_class
"doi": {"S": "111.1/1111"},
"status": {"S": str(Status.FAILED.value)},
"attempts": {"S": "1"},
"last_modified": {"S": "2022-01-28 10:28:53"},
},
)
validation_status = dynamodb_class.retry_attempts_exceeded(
Expand All @@ -67,6 +70,7 @@ def test_dynamodb_retry_threshold_exceeded_true(mocked_dynamodb, dynamodb_class)
"doi": {"S": "111.1/1111"},
"status": {"S": str(Status.FAILED.value)},
"attempts": {"S": "10"},
"last_modified": {"S": "2022-01-28 10:28:53"},
},
)
validation_status = dynamodb_class.retry_attempts_exceeded(
Expand All @@ -82,17 +86,17 @@ def test_dynamodb_update_doi_item_attempts_in_database(mocked_dynamodb, dynamodb
"doi": {"S": "111.1/1111"},
"status": {"S": str(Status.FAILED.value)},
"attempts": {"S": "1"},
"last_modified": {"S": "2022-01-28 10:28:53"},
},
)
existing_item = dynamodb_class.client.get_item(
TableName="test_dois",
Key={"doi": {"S": "111.1/1111"}},
)
assert existing_item["Item"] == {
"attempts": {"S": "1"},
"doi": {"S": "111.1/1111"},
"status": {"S": str(Status.FAILED.value)},
}
assert existing_item["Item"]["attempts"]["S"] == "1"
assert existing_item["Item"]["doi"]["S"] == "111.1/1111"
assert existing_item["Item"]["status"]["S"] == str(Status.FAILED.value)
assert existing_item["Item"]["last_modified"]["S"] == "2022-01-28 10:28:53"
update_response = dynamodb_class.update_doi_item_attempts_in_database(
"test_dois", "111.1/1111"
)
Expand All @@ -101,11 +105,10 @@ def test_dynamodb_update_doi_item_attempts_in_database(mocked_dynamodb, dynamodb
TableName="test_dois",
Key={"doi": {"S": "111.1/1111"}},
)
assert updated_item["Item"] == {
"attempts": {"S": "2"},
"doi": {"S": "111.1/1111"},
"status": {"S": str(Status.FAILED.value)},
}
assert updated_item["Item"]["attempts"]["S"] == "2"
assert updated_item["Item"]["doi"]["S"] == "111.1/1111"
assert updated_item["Item"]["status"]["S"] == str(Status.FAILED.value)
assert updated_item["Item"]["last_modified"]["S"] != "2022-01-28 10:28:53"


def test_dynamodb_update_doi_item_status_in_database(mocked_dynamodb, dynamodb_class):
Expand All @@ -115,17 +118,17 @@ def test_dynamodb_update_doi_item_status_in_database(mocked_dynamodb, dynamodb_c
"doi": {"S": "111.1/1111"},
"status": {"S": str(Status.FAILED.value)},
"attempts": {"S": "1"},
"last_modified": {"S": "2022-01-28 10:28:53"},
},
)
existing_item = dynamodb_class.client.get_item(
TableName="test_dois",
Key={"doi": {"S": "111.1/1111"}},
)
assert existing_item["Item"] == {
"attempts": {"S": "1"},
"doi": {"S": "111.1/1111"},
"status": {"S": str(Status.FAILED.value)},
}
assert existing_item["Item"]["attempts"]["S"] == "1"
assert existing_item["Item"]["doi"]["S"] == "111.1/1111"
assert existing_item["Item"]["status"]["S"] == str(Status.FAILED.value)
assert existing_item["Item"]["last_modified"]["S"] == "2022-01-28 10:28:53"
update_response = dynamodb_class.update_doi_item_status_in_database(
"test_dois", "111.1/1111", Status.PROCESSING.value
)
Expand All @@ -134,8 +137,7 @@ def test_dynamodb_update_doi_item_status_in_database(mocked_dynamodb, dynamodb_c
TableName="test_dois",
Key={"doi": {"S": "111.1/1111"}},
)
assert updated_item["Item"] == {
"attempts": {"S": "1"},
"doi": {"S": "111.1/1111"},
"status": {"S": str(Status.PROCESSING.value)},
}
assert updated_item["Item"]["attempts"]["S"] == "1"
assert updated_item["Item"]["doi"]["S"] == "111.1/1111"
assert updated_item["Item"]["status"]["S"] == str(Status.PROCESSING.value)
assert updated_item["Item"]["last_modified"]["S"] != "2022-01-28 10:28:53"

0 comments on commit 0aa87d1

Please sign in to comment.