How to use the pynamodb.transactions.TransactWrite function in pynamodb

To help you get started, we’ve selected a few pynamodb examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pynamodb / PynamoDB / tests / test_transaction.py View on Github external
def test_commit(self, mocker):
        connection = Connection()
        mock_connection_transact_write = mocker.patch.object(connection, 'transact_write_items')
        with patch(PATCH_METHOD) as req:
            req.return_value = MOCK_TABLE_DESCRIPTOR
            with TransactWrite(connection=connection) as t:
                t.condition_check(MockModel, 1, 3, condition=(MockModel.mock_hash.does_not_exist()))
                t.delete(MockModel(2, 4))
                t.save(MockModel(3, 5))
                t.update(MockModel(4, 6), actions=[MockModel.mock_toot.set('hello')], return_values='ALL_OLD')

        expected_condition_checks = [{
            'ConditionExpression': 'attribute_not_exists (#0)',
            'ExpressionAttributeNames': {'#0': 'mock_hash'},
            'Key': {'MockHash': {'N': '1'}, 'MockRange': {'N': '3'}},
            'TableName': 'mock'}
        ]
        expected_deletes = [{
            'ConditionExpression': 'attribute_not_exists (#0)',
            'ExpressionAttributeNames': {'#0': 'mock_version'},
            'Key': {'MockHash': {'N': '2'}, 'MockRange': {'N': '4'}},
            'TableName': 'mock'
github pynamodb / PynamoDB / tests / test_transaction.py View on Github external
def test_condition_check__no_condition(self):
        with pytest.raises(TypeError):
            with TransactWrite(connection=Connection()) as transaction:
                transaction.condition_check(MockModel, hash_key=1, condition=None)
github pynamodb / PynamoDB / tests / integration / test_transaction_integration.py View on Github external
def test_transaction_write_with_version_attribute_condition_failure(connection):
    foo = Foo(21)
    foo.save()

    foo2 = Foo(21)

    with pytest.raises(TransactWriteError) as exc_info:
        with TransactWrite(connection=connection) as transaction:
            transaction.save(Foo(21))
    assert get_error_code(exc_info.value) == TRANSACTION_CANCELLED
    assert 'ConditionalCheckFailed' in get_error_message(exc_info.value)

    with pytest.raises(TransactWriteError) as exc_info:
        with TransactWrite(connection=connection) as transaction:
            transaction.update(
                foo2,
                actions=[
                    Foo.star.set('birdistheword'),
                ]
            )
    assert get_error_code(exc_info.value) == TRANSACTION_CANCELLED
    assert 'ConditionalCheckFailed' in get_error_message(exc_info.value)
    # Version attribute is not updated on failure.
    assert foo2.version is None
github pynamodb / PynamoDB / tests / integration / test_transaction_integration.py View on Github external
with pytest.raises(TransactWriteError) as exc_info:
        with TransactWrite(connection=connection) as transaction:
            transaction.update(
                foo2,
                actions=[
                    Foo.star.set('birdistheword'),
                ]
            )
    assert get_error_code(exc_info.value) == TRANSACTION_CANCELLED
    assert 'ConditionalCheckFailed' in get_error_message(exc_info.value)
    # Version attribute is not updated on failure.
    assert foo2.version is None

    with pytest.raises(TransactWriteError) as exc_info:
        with TransactWrite(connection=connection) as transaction:
            transaction.delete(foo2)
    assert get_error_code(exc_info.value) == TRANSACTION_CANCELLED
    assert 'ConditionalCheckFailed' in get_error_message(exc_info.value)
github pynamodb / PynamoDB / tests / integration / test_transaction_integration.py View on Github external
def test_transaction_write_with_version_attribute(connection):
    foo1 = Foo(1)
    foo1.save()
    foo2 = Foo(2, star='bar')
    foo2.save()
    foo3 = Foo(3)
    foo3.save()

    with TransactWrite(connection=connection) as transaction:
        transaction.condition_check(Foo, 1, condition=(Foo.bar.exists()))
        transaction.delete(foo2)
        transaction.save(Foo(4))
        transaction.update(
            foo3,
            actions=[
                Foo.star.set('birdistheword'),
            ]
        )

    assert Foo.get(1).version == 1
    with pytest.raises(DoesNotExist):
        Foo.get(2)
    # Local object's version attribute is updated automatically.
    assert foo3.version == 2
    assert Foo.get(4).version == 1
github pynamodb / PynamoDB / tests / integration / test_transaction_integration.py View on Github external
def test_transact_write__error__transaction_cancelled__condition_check_failure(connection):
    # create a users and a bank statements for them
    User(1).save()
    BankStatement(1).save()

    # attempt to do this as a transaction with the condition that they don't already exist
    with pytest.raises(TransactWriteError) as exc_info:
        with TransactWrite(connection=connection) as transaction:
            transaction.save(User(1), condition=(User.user_id.does_not_exist()))
            transaction.save(BankStatement(1), condition=(BankStatement.user_id.does_not_exist()))
    assert get_error_code(exc_info.value) == TRANSACTION_CANCELLED
    assert 'ConditionalCheckFailed' in get_error_message(exc_info.value)
github pynamodb / PynamoDB / tests / integration / test_transaction_integration.py View on Github external
def test_transact_write__error__idempotent_parameter_mismatch(connection):
    client_token = str(uuid.uuid4())

    with TransactWrite(connection=connection, client_request_token=client_token) as transaction:
        transaction.save(User(1))
        transaction.save(User(2))

    with pytest.raises(TransactWriteError) as exc_info:
        # committing the first time, then adding more info and committing again
        with TransactWrite(connection=connection, client_request_token=client_token) as transaction:
            transaction.save(User(3))
    assert get_error_code(exc_info.value) == IDEMPOTENT_PARAMETER_MISMATCH

    # ensure that the first request succeeded in creating new users
    assert User.get(1)
    assert User.get(2)

    with pytest.raises(DoesNotExist):
        # ensure it did not create the user from second request
        User.get(3)
github pynamodb / PynamoDB / tests / integration / test_transaction_integration.py View on Github external
def test_transact_write__one_of_each(connection):
    User(1).save()
    User(2).save()
    statement = BankStatement(1, balance=100, active=True)
    statement.save()

    with TransactWrite(connection=connection) as transaction:
        transaction.condition_check(User, 1, condition=(User.user_id.exists()))
        transaction.delete(User(2))
        transaction.save(LineItem(4, amount=100, currency='USD'), condition=(LineItem.user_id.does_not_exist()))
        transaction.update(
            statement,
            actions=[
                BankStatement.active.set(False),
                BankStatement.balance.set(0),
            ]
        )

    # confirming transaction correct and successful
    assert User.get(1)
    with pytest.raises(DoesNotExist):
        User.get(2)
github pynamodb / PynamoDB / pynamodb / transactions.py View on Github external
def __init__(self, client_request_token=None, return_item_collection_metrics=None, **kwargs):
        super(TransactWrite, self).__init__(**kwargs)
        self._client_request_token = client_request_token
        self._return_item_collection_metrics = return_item_collection_metrics
        self._condition_check_items = []
        self._delete_items = []
        self._put_items = []
        self._update_items = []
        self._models_for_version_attribute_update = []