Exemplo n.º 1
0
    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})
Exemplo n.º 2
0
 def then_process_in_repo(self, process_id):
     db = DynamoDB("fake-table")
     process_json = db.get_item({
         "pk": f"PROCESS#{process_id}",
         "sk": f"PROCESS#{process_id}"
     })
     assert "pk" in process_json, f"'pk' not in {process_json}"
Exemplo n.º 3
0
    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"
                }
            },
        )