pdo-document/tests/Unit/Query/RemoveFieldsTest.php

87 lines
4.1 KiB
PHP

<?php
/**
* @author Daniel J. Summers <daniel@bitbadger.solutions>
* @license MIT
*/
declare(strict_types=1);
use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode, Parameters};
use BitBadger\PDODocument\Query\RemoveFields;
pest()->group('unit');
afterEach(function () { Configuration::overrideMode(null); });
describe('::update()', function () {
test('generates correct SQL [PostgreSQL]', function () {
Configuration::overrideMode(Mode::PgSQL);
expect(RemoveFields::update('taco', [':names' => "{one,two}"], 'it = true'))
->toBe('UPDATE taco SET data = data - :names::text[] WHERE it = true');
})->group('postgresql');
test('generates correct SQL [SQLite]', function () {
Configuration::overrideMode(Mode::SQLite);
expect(RemoveFields::update('burrito', Parameters::fieldNames(':name', ['one', 'two', 'ten']), 'a = b'))
->toBe('UPDATE burrito SET data = json_remove(data, :name0, :name1, :name2) WHERE a = b');
})->group('sqlite');
test('throws an exception [mode not set]', function () {
expect(fn () => RemoveFields::update('', [], ''))->toThrow(DocumentException::class);
});
});
describe('::byId()', function () {
test('generates correct SQL [PostgreSQL]', function () {
Configuration::overrideMode(Mode::PgSQL);
expect(RemoveFields::byId('churro', Parameters::fieldNames(':bite', ['byte'])))
->toBe("UPDATE churro SET data = data - :bite::text[] WHERE data->>'id' = :id");
})->group('postgresql');
test('generates correct SQL [SQLite]', function () {
Configuration::overrideMode(Mode::SQLite);
expect(RemoveFields::byId('quesadilla', Parameters::fieldNames(':bite', ['byte'])))
->toBe("UPDATE quesadilla SET data = json_remove(data, :bite0) WHERE data->>'id' = :id");
})->group('sqlite');
test('throws an exception [mode not set]', function () {
expect(fn () => RemoveFields::byId('', []))->toThrow(DocumentException::class);
});
});
describe('::byFields()', function () {
test('generates correct SQL [PostgreSQL]', function () {
Configuration::overrideMode(Mode::PgSQL);
expect(RemoveFields::byFields('enchilada', [Field::equal('cheese', 'jack', ':queso')],
Parameters::fieldNames(':sauce', ['white'])))
->toBe("UPDATE enchilada SET data = data - :sauce::text[] WHERE data->>'cheese' = :queso");
})->group('postgresql');
test('generates correct SQL [SQLite]', function () {
Configuration::overrideMode(Mode::SQLite);
expect(RemoveFields::byFields('chimichanga', [Field::equal('side', 'beans', ':rice')],
Parameters::fieldNames(':filling', ['beef'])))
->toBe("UPDATE chimichanga SET data = json_remove(data, :filling0) WHERE data->>'side' = :rice");
})->group('sqlite');
test('throws an exception [mode not set]', function () {
expect(fn () => RemoveFields::byFields('', [], []))->toThrow(DocumentException::class);
});
});
describe('::byContains()', function () {
test('generates correct SQL [PostgreSQL]', function () {
Configuration::overrideMode(Mode::PgSQL);
expect(RemoveFields::byContains('food', Parameters::fieldNames(':drink', ['a', 'b'])))
->toBe('UPDATE food SET data = data - :drink::text[] WHERE data @> :criteria');
})->group('postgresql');
test('throws an exception [SQLite]', function () {
expect(fn () => RemoveFields::byContains('', []))->toThrow(DocumentException::class);
})->group('sqlite');
});
describe('::byJsonPath()', function () {
test('generates correct SQL [PostgreSQL]', function () {
Configuration::overrideMode(Mode::PgSQL);
expect(RemoveFields::byJsonPath('dessert', Parameters::fieldNames(':cake', ['b', 'c'])))
->toBe('UPDATE dessert SET data = data - :cake::text[] WHERE jsonb_path_exists(data, :path::jsonpath)');
});
test('throws an exception [SQLite]', function () {
expect(fn () => RemoveFields::byJsonPath('', []))->toThrow(DocumentException::class);
})->group('sqlite');
});