<?php
/**
 * @author Daniel J. Summers <daniel@bitbadger.solutions>
 * @license MIT
 */

declare(strict_types=1);

use BitBadger\PDODocument\{Count, Delete, Field};
use Test\Integration\PostgreSQL\ThrowawayDb;

pest()->group('integration', 'postgresql');

describe('::byId()', function () {
    test('deletes a document when ID is matched', function () {
        expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
        Delete::byId(ThrowawayDb::TABLE, 'four');
        expect(Count::all(ThrowawayDb::TABLE))->toBe(4);
    });
    test('does not delete a document when ID is not matched', function () {
        expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
        Delete::byId(ThrowawayDb::TABLE, 'negative four');
        expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
    });
});

describe('::byFields()', function () {
    test('deletes documents when fields match', function () {
        expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
        Delete::byFields(ThrowawayDb::TABLE, [Field::notEqual('value', 'purple')]);
        expect(Count::all(ThrowawayDb::TABLE))->toBe(2);
    });
    test('does not delete documents when fields are not matched', function () {
        expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
        Delete::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'crimson')]);
        expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
    });
});

describe('::byContains()', function () {
    test('deletes documents when containment matches', function () {
        expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
        Delete::byContains(ThrowawayDb::TABLE, ['value' => 'purple']);
        expect(Count::all(ThrowawayDb::TABLE))->toBe(3);
    });
    test('does not delete documents when containment is not matched', function () {
        expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
        Delete::byContains(ThrowawayDb::TABLE, ['target' => 'acquired']);
        expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
    });
});

describe('::byJsonPath()', function () {
    test('deletes documents when path matches', function () {
        expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
        Delete::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ <> 0)');
        expect(Count::all(ThrowawayDb::TABLE))->toBe(1);
    });
    test('does not delete documents when path is not matched', function () {
        expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
        Delete::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ < 0)');
        expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
    });
});