Modern QA2026DynamoDB Testing — tiles
Log inJoin
54 / 65 · 15 SQL & Database Testing · NoSQL Basics← prev⊞ allnext →☰ Read as one page

7.5DynamoDB Testing

import boto3
from moto import mock_dynamodb  # Mock for testing

@mock_dynamodb
def test_dynamodb_crud():
    """Test CRUD operations on DynamoDB."""
    dynamodb = boto3.resource("dynamodb", region_name="us-east-1")

    # Create table
    table = dynamodb.create_table(
        TableName="users",
        KeySchema=[{"AttributeName": "id", "KeyType": "HASH"}],
        AttributeDefinitions=[{"AttributeName": "id", "AttributeType": "S"}],
        BillingMode="PAY_PER_REQUEST"
    )

    # Insert
    table.put_item(Item={"id": "user-1", "name": "Alice", "email": "alice@test.com"})

    # Read
    response = table.get_item(Key={"id": "user-1"})
    assert response["Item"]["name"] == "Alice"

    # Query non-existent
    response = table.get_item(Key={"id": "nonexistent"})
    assert "Item" not in response