69 lines
2.9 KiB
PHP
69 lines
2.9 KiB
PHP
|
<?php
|
||
|
/**
|
||
|
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||
|
* @license MIT
|
||
|
*/
|
||
|
|
||
|
declare(strict_types=1);
|
||
|
|
||
|
use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode};
|
||
|
use BitBadger\PDODocument\Query\Patch;
|
||
|
|
||
|
pest()->group('unit');
|
||
|
|
||
|
afterEach(function () { Configuration::overrideMode(null); });
|
||
|
|
||
|
describe('::byId()', function () {
|
||
|
test('generates correct SQL [PostgreSQL]', function () {
|
||
|
Configuration::overrideMode(Mode::PgSQL);
|
||
|
expect(Patch::byId('doc_table'))->toBe("UPDATE doc_table SET data = data || :data WHERE data->>'id' = :id");
|
||
|
})->group('postgresql');
|
||
|
test('generates correct SQL [SQLite]', function () {
|
||
|
Configuration::overrideMode(Mode::SQLite);
|
||
|
expect(Patch::byId('my_table'))
|
||
|
->toBe("UPDATE my_table SET data = json_patch(data, json(:data)) WHERE data->>'id' = :id");
|
||
|
})->group('sqlite');
|
||
|
test('throws an exception [mode not set]', function () {
|
||
|
expect(fn () => Patch::byId(''))->toThrow(DocumentException::class);
|
||
|
});
|
||
|
});
|
||
|
|
||
|
describe('::byFields()', function () {
|
||
|
test('generates correct SQL [PostgreSQL]', function () {
|
||
|
Configuration::overrideMode(Mode::PgSQL);
|
||
|
expect(Patch::byFields('that', [Field::less('something', 17, ':some')]))
|
||
|
->toBe("UPDATE that SET data = data || :data WHERE (data->>'something')::numeric < :some");
|
||
|
})->group('postgresql');
|
||
|
test('generates correct SQL [SQLite]', function () {
|
||
|
Configuration::overrideMode(Mode::SQLite);
|
||
|
expect(Patch::byFields('a_table', [Field::greater('something', 17, ':it')]))
|
||
|
->toBe("UPDATE a_table SET data = json_patch(data, json(:data)) WHERE data->>'something' > :it");
|
||
|
})->group('sqlite');
|
||
|
test('throws an exception [mode not set]', function () {
|
||
|
expect(fn () => Patch::byFields('', []))->toThrow(DocumentException::class);
|
||
|
});
|
||
|
});
|
||
|
|
||
|
describe('::byContains()', function () {
|
||
|
test('generates correct SQL [PostgreSQL]', function () {
|
||
|
Configuration::overrideMode(Mode::PgSQL);
|
||
|
expect(Patch::byContains('this'))->toBe('UPDATE this SET data = data || :data WHERE data @> :criteria');
|
||
|
})->group('postgresql');
|
||
|
test('throws an exception [SQLite]', function () {
|
||
|
Configuration::overrideMode(Mode::SQLite);
|
||
|
expect(fn () => Patch::byContains(''))->toThrow(DocumentException::class);
|
||
|
})->group('sqlite');
|
||
|
});
|
||
|
|
||
|
describe('::byJsonPath()', function () {
|
||
|
test('generates correct SQL [PostgreSQL]', function () {
|
||
|
Configuration::overrideMode(Mode::PgSQL);
|
||
|
expect(Patch::byJsonPath('that'))
|
||
|
->toBe('UPDATE that SET data = data || :data WHERE jsonb_path_exists(data, :path::jsonpath)');
|
||
|
})->group('postgresql');
|
||
|
test('throws an exception [SQLite]', function () {
|
||
|
Configuration::overrideMode(Mode::SQLite);
|
||
|
expect(fn () => Patch::byJsonPath(''))->toThrow(DocumentException::class);
|
||
|
})->group('sqlite');
|
||
|
});
|