66 lines
3.0 KiB
PHP
66 lines
3.0 KiB
PHP
<?php
|
|
/**
|
|
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
|
* @license MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
use BitBadger\PDODocument\{Configuration, DocumentException, DocumentIndex, Mode};
|
|
use BitBadger\PDODocument\Query\Definition;
|
|
|
|
pest()->group('unit');
|
|
|
|
describe('::ensureTable()', function () {
|
|
afterEach(function () { Configuration::overrideMode(null); });
|
|
test('generates correct SQL [PostgreSQL]', function () {
|
|
Configuration::overrideMode(Mode::PgSQL);
|
|
expect(Definition::ensureTable('documents'))
|
|
->toBe('CREATE TABLE IF NOT EXISTS documents (data JSONB NOT NULL)');
|
|
})->group('postgresql');
|
|
test('generates correct SQL [SQLite]', function () {
|
|
Configuration::overrideMode(Mode::SQLite);
|
|
expect(Definition::ensureTable('dox'))->toBe('CREATE TABLE IF NOT EXISTS dox (data TEXT NOT NULL)');
|
|
})->group('sqlite');
|
|
test('throws an exception if mode is not set', function () {
|
|
expect(fn () => Definition::ensureTable(''))->toThrow(DocumentException::class);
|
|
});
|
|
});
|
|
|
|
describe('::ensureIndexOn()', function () {
|
|
test('generates correct SQL for unqualified table, single ascending field', function () {
|
|
expect(Definition::ensureIndexOn('test', 'fields', ['details']))
|
|
->toBe("CREATE INDEX IF NOT EXISTS idx_test_fields ON test ((data->>'details'))");
|
|
});
|
|
test('generates correct SQL for qualified table, multiple fields', function () {
|
|
expect(Definition::ensureIndexOn('sch.testing', 'json', ['group', 'sub_group DESC']))
|
|
->toBe('CREATE INDEX IF NOT EXISTS idx_testing_json ON sch.testing '
|
|
. "((data->>'group'), (data->>'sub_group') DESC)");
|
|
});
|
|
});
|
|
|
|
describe('::ensureKey()', function () {
|
|
test('generates correct SQL', function () {
|
|
expect(Definition::ensureKey('tbl'))
|
|
->toBe("CREATE UNIQUE INDEX IF NOT EXISTS idx_tbl_key ON tbl ((data->>'id'))");
|
|
});
|
|
});
|
|
|
|
describe('::ensureDocumentIndexOn()', function () {
|
|
afterEach(function () { Configuration::overrideMode(null); });
|
|
test('generates correct SQL for qualified table, full index [PostgreSQL]', function () {
|
|
Configuration::overrideMode(Mode::PgSQL);
|
|
expect(Definition::ensureDocumentIndexOn('my.tbl', DocumentIndex::Full))
|
|
->toBe("CREATE INDEX IF NOT EXISTS idx_tbl_document ON my.tbl USING GIN (data)");
|
|
})->group('postgresql');
|
|
test('generates correct SQL for unqualified table, optimized index [PostgreSQL]', function () {
|
|
Configuration::overrideMode(Mode::PgSQL);
|
|
expect(Definition::ensureDocumentIndexOn('it', DocumentIndex::Optimized))
|
|
->toBe("CREATE INDEX IF NOT EXISTS idx_it_document ON it USING GIN (data jsonb_path_ops)");
|
|
})->group('postgresql');
|
|
test('throws an exception [SQLite]', function () {
|
|
Configuration::overrideMode(Mode::SQLite);
|
|
expect(fn () => Definition::ensureDocumentIndexOn('', DocumentIndex::Full))->toThrow(DocumentException::class);
|
|
})->group('sqlite');
|
|
});
|