Database
Basic Transaction
QueryValidateHard
What you will learn
- Wrap mutations in BEGIN / COMMIT; on exception ROLLBACK โ test both happy and failure paths with a seeded failure
- Isolation levels (READ COMMITTED / REPEATABLE READ / SERIALIZABLE) produce different results under races โ pin explicitly
- SAVEPOINT allows partial rollback within a transaction โ useful when batch ops partially succeed and partially fail
Description
Transfer stock between products atomically.
Test Scenario
-- Transfer 10 units from product 1 to product 2
BEGIN TRANSACTION;
UPDATE products SET stock = stock - 10 WHERE id = 1;
UPDATE products SET stock = stock + 10 WHERE id = 2;
-- Verify before commit
SELECT id, name, stock FROM products WHERE id IN (1, 2);
COMMIT;
-- If error occurred, use ROLLBACK insteadTesting Tips
- โขBEGIN starts a transaction
- โขCOMMIT saves changes
- โขROLLBACK undoes changes
- โขAll or nothing execution
// Playwright API Testing - Databaseimport { test, expect } from '@playwright/test';test('Database query via API', async ({ request }) => {const response = await request.post('/api/database/query', {data: {sql: 'SELECT * FROM users WHERE id = ?',params: [1]}});expect(response.ok()).toBeTruthy();const { rows } = await response.json();expect(rows.length).toBeGreaterThan(0);});
Challenge ID: sql-transaction-basic
2 frameworks available