def test_get_item__given_valid_inputs__then_return_correct(self): # Arrange table_name = "fake-table" key_field = "id" db = boto3.client("dynamodb") db.create_table( TableName=table_name, KeySchema=[{ "AttributeName": key_field, "KeyType": "HASH" }], AttributeDefinitions=[ { "AttributeName": key_field, "AttributeType": "S" }, ], ProvisionedThroughput={ "ReadCapacityUnits": 10, "WriteCapacityUnits": 10 }, ) subject = DynamoDB(table_name) subject.set_ttl_seconds(10) subject.put_item({key_field: "J1K4", "value": 2000}) # Act new_item = subject.get_item({"id": "J1K4"}) print(new_item) # Assert self.assertTrue("ttl" in new_item) new_item.pop("ttl") self.assertEqual(new_item, {"id": "J1K4", "value": 2000})
def test_query_index_begins__given_index_key__then_return_correct(self): table_name = get_output_from_stack("FanProcessingPartTestTableName") subject = DynamoDB(table_name) subject.set_ttl_seconds(10) subject.put_item({ "pk": "J1K4", "sk": "TASK#03939", "value": { "subkey": "03939" }, "gs1_pk": "INDEX", "gs1_sk": "ABCDEF", "status": "created", }) # Act results = subject.query_index_begins("gs1", { "gs1_pk": "INDEX", "gs1_sk": "ABC" }) print(f"Query results: {results}") # Assert self.assertTrue("ttl" in results[0]) results[0].pop("ttl") self.assertEqual( results, [{ "value": { "subkey": "03939" }, "sk": "TASK#03939", "gs1_pk": "INDEX", "pk": "J1K4", "gs1_sk": "ABCDEF", "status": "created", }], )
def test_query_table_begins__given_valid_composite_key__then_return_correct( self): # Arrange table_name = "fake-table" db = boto3.client("dynamodb") db.create_table( TableName=table_name, KeySchema=[ { "AttributeName": "pk", "KeyType": "HASH" }, { "AttributeName": "sk", "KeyType": "RANGE" }, ], AttributeDefinitions=[ { "AttributeName": "pk", "AttributeType": "S" }, { "AttributeName": "sk", "AttributeType": "S" }, ], ProvisionedThroughput={ "ReadCapacityUnits": 10, "WriteCapacityUnits": 10 }, ) subject = DynamoDB(table_name) subject.set_ttl_seconds(10) subject.put_item({ "pk": "J1K4", "sk": "TASK#03939", "value": { "subkey": "03939" } }) subject.put_item({ "pk": "J1K4", "sk": "TASK#03940", "value": { "subkey": "03940" } }) # Act results = subject.query_table_begins({"pk": "J1K4", "sk": "T"}) print(f"Query results: {results}") # Assert self.assertTrue("ttl" in results[0]) results[0].pop("ttl") results[1].pop("ttl") self.assertEqual( results, [ { "pk": "J1K4", "sk": "TASK#03939", "value": { "subkey": "03939" }, }, { "pk": "J1K4", "sk": "TASK#03940", "value": { "subkey": "03940" }, }, ], )
def test_get_item__given_table_has_composite_key__then_return_correct( self): # Arrange table_name = "fake-table" db = boto3.client("dynamodb") db.create_table( TableName=table_name, KeySchema=[ { "AttributeName": "pk", "KeyType": "HASH" }, { "AttributeName": "sk", "KeyType": "RANGE" }, ], AttributeDefinitions=[ { "AttributeName": "pk", "AttributeType": "S" }, { "AttributeName": "sk", "AttributeType": "S" }, ], ProvisionedThroughput={ "ReadCapacityUnits": 10, "WriteCapacityUnits": 10 }, ) subject = DynamoDB(table_name) subject.set_ttl_seconds(10) subject.put_item({ "pk": "J1K4", "sk": "TASK#03939", "value": { "subkey": "subvalue" } }) # Act new_item = subject.get_item({"pk": "J1K4", "sk": "TASK#03939"}) print(f"New item: {new_item}") # Assert self.assertTrue("ttl" in new_item) new_item.pop("ttl") self.assertEqual( new_item, { "pk": "J1K4", "sk": "TASK#03939", "value": { "subkey": "subvalue" } }, )