QA Interview Hub · Knowledge base · Quizzes · Українська

SQL › SQL in QA work

How do you verify in the database the result of an action in the UI?

A UI action is verified in the database as a before-and-after difference: all new and changed rows, not just the latest one. A marker such as the maximum id or a balance is recorded before the action, then the user's rows newer than it are read, so a duplicate shows up and other people's records do not interfere. An asynchronous write is awaited by polling with a timeout, not a pause.

SQL · QA practice

The UI said "Top-up successful", but the account in the database holds 1250 instead of 750. What to check?

A test tops up an account by 500 PLN. The screen shows "Success" and the amount 500, and the API response check is green too. Yet after one click the database has two deposit rows and the balance grew by 1000. The user does not see it yet (the screen took the amount from the response), but accounting will. This lesson is about what exactly to check in the database after a UI action and how to make that check precise even on a shared environment where others write too.

How it works and why

The UI shows what the application decided to show, and the database holds what actually happened. A check must not assume the table is empty or that "the last row" belongs to this test: other tests and people work on the environment at the same time. A reliable technique has four steps. First remember a mark: the current maximum id of the table and the account balance. Then perform the action. Then wait for the result: the record may appear with a delay if processing goes through a queue or after the response is committed. Finally compare: all rows with an id above the mark for this account, and the balance difference. This shows everything the action created and nothing that existed before it.

A database check around one action

1. A mark before the actionthe maximum id and the balance2. The UI actionthe test clicks the button once3. Waitingpolling with a timeout, not sleep4. The differencenew rows, the delta, side effects
Figure 1. The mark makes the check independent of what was in the database before the test.

Example: from "Success" to a duplicate

Before the action the test reads the mark for account 10:

-- before the action: the balance and the largest id
SELECT balance,
       (SELECT COALESCE(MAX(id), 0) FROM transactions) AS mark
FROM accounts
WHERE id = 10;
-- result: balance 250.00, mark 100

After the click on the 500 PLN button, a query takes everything new for this account:

SELECT id, type, amount, status
FROM transactions
WHERE account_id = 10 AND id > 100
ORDER BY id;

What the query found and the balance

transactions (id > 100)
idtypeamountstatus
101deposit500.00completed
102deposit500.00completed
accounts, id = 10
momentbalance
before250.00
expected750.00
after1250.00
Figure 2. Two identical deposits instead of one: the balance is 500 above the expectation.

One row and a balance of 750 were expected. Two identical rows with the same time point to a double submit: either the button is not disabled after the click, or the server repeated the request without an idempotency key. The source of the defect is looked for in the code, and the bug report already has everything needed: the mark, the query and the result. The check the test keeps is short and does not depend on order:

SELECT COUNT(*) AS n, COALESCE(SUM(amount), 0) AS total
FROM transactions
WHERE account_id = 10 AND id > 100 AND type = 'deposit';
-- expected: n = 1, total = 500.00

What else is checked after such an action besides the row:

  • Status matches the stage: completed or pending by the requirement, not stuck between them.
  • Side effects: exactly one fee, if there is one; an audit entry with who performed the action.
  • Nothing extra: other accounts did not change, no duplicates.
  • Service fields: created_at in the right time zone (more in the lesson on dates).

Limits and alternatives

A mark by id works while identifiers grow: for UUIDs take the creation time or a separate unique marker in the action (for example, a unique note on the payment). If another test writes to the same account in parallel, a filter by account does not help: each test needs its own account. Replication lag also gives an "empty" result, so read from the primary node. If the record really appears asynchronously, wait by polling with a timeout, not with a fixed pause: a pause is either too long or too short. The same can often be checked without the database by reading the account through the API. That is cheaper and does not tie the test to the schema, so the database is needed only where the client sees nothing: side effects, service states, the consequences of a failure.

Common mistakes

  • Checking only the last row (ORDER BY created_at DESC LIMIT 1): a duplicate goes unnoticed.
  • Comparing the balance with an absolute number instead of a difference from the starting value: the test breaks as soon as the account holds other data.
  • A fixed pause before the query: a slow environment gives a false bug, a fast one waits for nothing.
  • A query with no filter by account or mark: other people's rows get into the result.
  • Treating the stored amount as proof that the UI shows it correctly: these are different checks.

Check yourself

After a top-up a test takes SELECT amount FROM transactions ORDER BY created_at DESC LIMIT 1 and checks 500. Why does it pass on a double top-up and what replaces it?

Answer

The query sees only one, the newest row, and both duplicates have the amount 500. Everything new must be counted: a mark before the action, then COUNT(*) and SUM(amount) for the account with an id above the mark, plus the balance difference.

A query right after the click sometimes returns 0 rows and one a second later. Is it a bug?

Answer

Not necessarily: processing may be asynchronous or the response may be returned before the commit. The test should poll the database with a timeout that matches the requirement. It is a bug if the record is missing even after the expected time, or if the UI already showed "Success" where the requirement promises "Processing".

In an interview. Junior: After a UI action I check in the database that the record was created with the right amount, type and status, that the balance changed and that there is no duplicate or change in other people's records. Middle/Senior: I make a mark before the action (the maximum id and the balance) and compare the difference afterwards, so the check does not depend on other people's data. I wait for an asynchronous write by polling with a timeout, not with a pause, look at side effects (fee, audit) and know that part of the same can be checked more cheaply through the API, leaving the database for what the client cannot see.

More articles in this chapter