<?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\Delete;

pest()->group('unit');

afterEach(function () { Configuration::overrideMode(null); });

describe('::byId()', function () {
    test('generates correct SQL', function () {
        Configuration::overrideMode(Mode::SQLite);
        expect(Delete::byId('over_there'))->toBe("DELETE FROM over_there WHERE data->>'id' = :id");
    });
});

describe('::byFields()', function () {
    test('generates correct SQL', function () {
        Configuration::overrideMode(Mode::SQLite);
        expect(Delete::byFields('my_table',
                [Field::less('value', 99, ':max'), Field::greaterOrEqual('value', 18, ':min')]))
            ->toBe("DELETE FROM my_table WHERE data->>'value' < :max AND data->>'value' >= :min");
    });
});

describe('::byContains()', function () {
    test('generates correct SQL [PostgreSQL]', function () {
        Configuration::overrideMode(Mode::PgSQL);
        expect(Delete::byContains('somewhere'))->toBe('DELETE FROM somewhere WHERE data @> :criteria');
    })->group('postgresql');
    test('throws an exception [SQLite]', function () {
        Configuration::overrideMode(Mode::SQLite);
        expect(fn () => Delete::byContains(''))->toThrow(DocumentException::class);
    })->group('sqlite');
});

describe('::byJsonPath()', function () {
    test('generates correct SQL [PostgreSQL]', function () {
        Configuration::overrideMode(Mode::PgSQL);
        expect(Delete::byJsonPath('here'))->toBe('DELETE FROM here WHERE jsonb_path_exists(data, :path::jsonpath)');
    })->group('postgresql');
    test('throws an exception [SQLite]', function () {
        Configuration::overrideMode(Mode::SQLite);
        expect(fn () => Delete::byJsonPath(''))->toThrow(DocumentException::class);
    });
});