Skip to main content

Testing an Adapter

@sqb/adapter-tests (since v6.5.0) is the test suite every built-in adapter runs, published with the test data it needs. If you write an adapter of your own, run it too: an adapter that passes it behaves the way @sqb/connect expects.

The suite covers connecting, the working schema, selects with array and object rows, cursors, fetchRows, fetchAsString, inserts and updates with returning, named parameters in raw SQL, and transactions (commit, rollback, autoCommit). Tests for an optional feature run only when the adapter declares it in adapter.features.

Install​

npm install --save-dev @sqb/adapter-tests mocha

The suite registers its tests with mocha, so the spec file must run under mocha.

Usage​

import { initAdapterTests } from '@sqb/adapter-tests';
import { MyAdapter } from '../src/my-adapter.js';
import { createTestSchema } from './create-test-schema.js';

describe('MyAdapter', () => {
const adapter = new MyAdapter();

before(() => createTestSchema('test_sqb'));

initAdapterTests(adapter, {
host: 'localhost',
database: 'test',
user: 'test',
password: 'test',
schema: 'test_sqb',
});
});

initAdapterTests(adapter, config) registers the adapter, connects with config and unregisters the adapter when the suite is done. The suite sets driver and pool itself.

The test database​

The suite does not create tables. The database it connects to must already hold the test schema, with the test data loaded. Create the tables in your database's own SQL and load the data with the helpers below.

The suite reads and writes customers, and needs at least these columns:

ColumnType
idinteger primary key, generated on insert
given_nametext
family_nametext
citytext
birth_datedate
created_attimestamp, defaulting to the current time

The test data has customers with ids 1 to 100, and the suite relies on that. Some tests insert and update customers, so give the suite a schema or database of its own.

The full schema the built-in adapters create (continents, countries, customers and their details, tags, and a parent/child/grandchild chain) is in each adapter's test/_support folder in the SQB repository. The SQLite one, db_schema.ts, is the shortest to start from.

Loading the test data​

import { getInsertSQLsForTestData } from '@sqb/adapter-tests';

const files = getInsertSQLsForTestData({ dialect: 'postgres', schema: 'test_sqb' });
for (const file of files) {
for (const sql of file.scripts) await connection.execute(sql);
}

getInsertSQLsForTestData() returns one entry per table, in an order where every foreign key finds its parent row. It builds the SQL with @sqb/builder, so the dialect's serializer must be registered (an SQB adapter registers its own when imported). Pass tables to load only some tables, for example ['continents', 'countries', 'customers'] for the suite alone.

Other exports:

  • testDataTables: the table names, in insert order.
  • getTestData(table): one table's name, field descriptions and rows.
  • stringifyValueForSQL(value): renders a value as an SQL literal, for scripts that inline values.

See also​