Skip to main content
Database

Constraint Validation

QueryValidateHard

Description

Test constraint violations (negative stock, invalid rating).

Test Scenario

-- Try inserting invalid data

-- This should fail (negative stock):
INSERT INTO products (name, price, stock)
VALUES ('Test', 10.00, -5);

-- This should fail (rating > 5):
INSERT INTO reviews (user_id, product_id, rating, comment)
VALUES (1, 1, 6, 'Too good!');

-- These validate constraints are working

Testing Tips

  • •CHECK constraints validate data
  • •Violations raise errors
  • •Good for data integrity
// Playwright API Testing - Database
import { 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-check-constraint

2 frameworks available