Which data types should QA know?
QA should know column types because a type decides which values the database accepts and gives a ready list of boundary values. The main ones are integers (INTEGER ends at 2,147,483,647), NUMERIC for money, REAL and DOUBLE PRECISION with rounding error, strings, BOOLEAN, date and time (TIMESTAMP or TIMESTAMPTZ), UUID and JSONB. A mismatch of types between UI, API and database is itself a source of bugs.
SQL · Basics
The "Amount" field accepts 0.1 + 0.2, but the database holds 0.30000000000000004. Which type was chosen?
A bug in a report: the sum of two payments of 0.10 and 0.20 does not equal 0.30. The tester looks at the schema and sees that the column has a floating-point type. A column type decides which values the database accepts, how much space they take, how they compare and where the boundary it will not cross lies. So the type is a source of boundary values for tests. Below: which types are worth knowing, what breaks with them and how to check it.
How it works and why. Every column has a type, and the database checks it on write and uses it in comparison, sorting and calculations. That is why values that look identical behave differently: the string '10' sorts before '9', the number 10 after 9. The type's limits are a ready list of boundary values: the largest number, the longest string, the number of decimal places.
| Type | Used for | What to watch |
|---|---|---|
INTEGER, BIGINT | Integers, IDs | INTEGER ranges from −2,147,483,648 to 2,147,483,647; BIGINT is much larger |
NUMERIC(p,s) | Exact decimals, money | p is total digits, s after the point; NUMERIC(14,2) allows up to 12 digits before the point |
REAL, DOUBLE PRECISION | Approximate decimals | They carry an error, not for money |
VARCHAR(n), TEXT | Strings | What happens to a string longer than n |
BOOLEAN | Yes / no | Can also be NULL |
DATE, TIMESTAMP, TIMESTAMPTZ | Dates and time | With or without a time zone |
UUID, JSONB | Unique IDs, flexible data | Format; nested fields are not checked by the schema |
Example: money in the "wrong" type. The amount column is declared as DOUBLE PRECISION. Binary floating point cannot store 0.1 exactly, so sums accumulate an error:
SELECT 0.1::double precision + 0.2::double precision AS f,
0.1::numeric + 0.2::numeric AS n;
-- f = 0.30000000000000004 n = 0.3
SELECT 0.1::double precision + 0.2::double precision
= 0.3::double precision; -- false
Money: two types
DOUBLE PRECISION
An approximate value. 0.1 + 0.2 is not 0.3, and reconciling a balance with the history drifts by cents.
NUMERIC(14,2)
Exact decimal arithmetic. The schema sets how many decimal places are kept, extra ones are rounded.
The schema-level fix is NUMERIC(14,2). But it has limits too. Here is what happens to input values:
NUMERIC(14,2) on input
| value |
|---|
| 99.50 |
| 1.239 |
| 999999999999.99 |
| 1000000000000 rejected |
| value |
|---|
| 99.50 |
| 1.24 |
| 999999999999.99 |
Other typical boundaries for tests:
SELECT 2147483647 + 1;
-- ERROR: integer out of range
INSERT INTO users (email) VALUES (repeat('a', 300));
-- for varchar(255): ERROR: value too long
-- for type character varying(255)
Strings: in PostgreSQL inserting a value longer than varchar(n) is an error. In MySQL it depends on the mode: in strict mode (the default since 5.7) it is also an error, while in non-strict mode the value is silently truncated. That is a separate test case, because truncation is invisible in both the UI and the logs. Dates: TIMESTAMP stores time as is, without a zone, while TIMESTAMPTZ in PostgreSQL stores the moment in UTC and shows it in the session time zone, so different clients see a different "wall clock" for the same record.
Limits and special cases. A database type is not always an API type: JSON has no separate type for dates or for exact numbers, so a NUMERIC sent as a JSON number becomes a floating-point number in JavaScript and may lose precision. So a mismatch of types between UI, API and database is itself a source of bugs: the UI allows 300 characters and the column holds 255. For statuses people often use text with a constraint or an enum, and then values outside the list must be rejected.
Common mistakes.
- Storing money in
REALorDOUBLE PRECISION: an error accumulates. - Not checking the upper bound of
INTEGERfor counters and IDs that grow for years. - Forgetting that the length in the UI and in the database can differ.
- Comparing
TIMESTAMPandTIMESTAMPTZas if they were the same: the difference shows in another time zone. - Assuming
JSONBvalidates structure: the schema does not know which fields are inside.
Check yourself
The amount column has type NUMERIC(14,2). What happens to the input values 12.345 and 1000000000000?
Answer
12.345 is rounded to two places: 12.35 in PostgreSQL, because numeric rounds ties away from zero, while 1000000000000 does not fit into 12 digits before the point, so the database returns an overflow error. Both values are worth checking through the API and the UI.
Why is BIGINT often chosen for IDs although INTEGER looks sufficient?
Answer
INTEGER ends at 2,147,483,647. In a table with many inserts over years of work this limit can be reached, and then inserts fail with an error. BIGINT removes the problem for decades.