WIP on migrating Postgres integration tests

This commit is contained in:
2024-11-14 23:38:47 -05:00
parent 7cdbf5fcdb
commit 3ec1a05d82
12 changed files with 377 additions and 415 deletions

View File

@@ -0,0 +1,45 @@
<?php
/**
* @author Daniel J. Summers <daniel@bitbadger.solutions>
* @license MIT
*/
declare(strict_types=1);
use BitBadger\PDODocument\{Count, Field};
use Test\Integration\PostgreSQL\ThrowawayDb;
pest()->group('integration', 'postgresql');
describe('::all()', function () {
test('counts all documents', function () {
expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
});
});
describe('::byFields()', function () {
test('counts for numeric range correctly', function () {
expect(Count::byFields(ThrowawayDb::TABLE, [Field::between('num_value', 10, 20)]))->toBe(3);
});
test('counts for non-numeric range correctly', function () {
expect(Count::byFields(ThrowawayDb::TABLE, [Field::between('value', 'aardvark', 'apple')]))->toBe(1);
});
});
describe('::byContains()', function () {
test('counts matching documents', function () {
expect(Count::byContains(ThrowawayDb::TABLE, ['value' => 'purple']))->toBe(2);
});
test('returns 0 for no matching documents', function () {
expect(Count::byContains(ThrowawayDb::TABLE, ['value' => 'magenta']))->toBe(0);
});
});
describe('::byJsonPath()', function () {
test('counts matching documents', function () {
expect(Count::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ < 5)'))->toBe(2);
});
test('returns 0 for no matching documents', function () {
expect(Count::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 100)'))->toBe(0);
});
});

View File

@@ -0,0 +1,98 @@
<?php
/**
* @author Daniel J. Summers <daniel@bitbadger.solutions>
* @license MIT
*/
declare(strict_types=1);
use BitBadger\PDODocument\{Count, Custom, DocumentException, Query};
use BitBadger\PDODocument\Mapper\{CountMapper, DocumentMapper};
use Test\Integration\PostgreSQL\ThrowawayDb;
use Test\Integration\TestDocument;
pest()->group('integration', 'postgresql');
describe('::runQuery()', function () {
test('runs a valid query successfully', function () {
$stmt = &Custom::runQuery('SELECT data FROM ' . ThrowawayDb::TABLE . ' LIMIT 1', []);
try {
expect($stmt)->not->toBeNull();
} finally {
$stmt = null;
}
});
test('fails with an invalid query', function () {
$stmt = null;
try {
expect(function () use (&$stmt) { $stmt = &Custom::runQuery('GRAB stuff FROM over_there UNTIL done', []); })
->toThrow(DocumentException::class);
} finally {
$stmt = null;
}
});
});
describe('::list()', function () {
test('returns non-empty list when data found', function () {
$list = Custom::list(Query::selectFromTable(ThrowawayDb::TABLE), [], new DocumentMapper(TestDocument::class));
expect($list)->not->toBeNull();
$count = 0;
foreach ($list->items() as $ignored) $count++;
expect($count)->toBe(5);
});
test('returns empty list when no data found', function () {
expect(Custom::list(
Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE (data->>'num_value')::numeric > :value",
[':value' => 100], new DocumentMapper(TestDocument::class)))
->not->toBeNull()
->hasItems()->toBeFalse();
});
});
describe('::array()', function () {
test('returns non-empty array when data found', function () {
expect(Custom::array(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'sub' IS NOT NULL", [],
new DocumentMapper(TestDocument::class)))
->not->toBeNull()
->toHaveCount(2);
});
test('returns empty array when no data found', function () {
expect(Custom::array(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'value' = :value",
[':value' => 'not there'], new DocumentMapper(TestDocument::class)))
->not->toBeNull()
->toBeEmpty();
});
});
describe('::single()', function () {
test('returns a document when one is found', function () {
expect(Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id", [':id' => 'one'],
new DocumentMapper(TestDocument::class)))
->isSome()->toBeTrue()
->get()->id->toBe('one');
});
test('returns no document when one is not found', function () {
expect(Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id",
[':id' => 'eighty'], new DocumentMapper(TestDocument::class)))
->isNone()->toBeTrue();
});
});
describe('::nonQuery()', function () {
test('works when documents match the WHERE clause', function () {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
expect(Count::all(ThrowawayDb::TABLE))->toBe(0);
});
test('works when no documents match the WHERE clause', function () {
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE . " WHERE (data->>'num_value')::numeric > :value",
[':value' => 100]);
expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
});
});
describe('::scalar()', function () {
test('returns a scalar value', function () {
expect(Custom::scalar("SELECT 5 AS it", [], new CountMapper()))->toBe(5);
});
});

View File

@@ -0,0 +1,47 @@
<?php
/**
* @author Daniel J. Summers <daniel@bitbadger.solutions>
* @license MIT
*/
declare(strict_types=1);
use BitBadger\PDODocument\{Definition, DocumentIndex};
pest()->group('integration', 'postgresql');
describe('::ensureTable()', function () {
test('creates a table', function () {
expect($this->dbObjectExists('ensured'))->toBeFalse()
->and($this->dbObjectExists('idx_ensured_key'))->toBeFalse();
Definition::ensureTable('ensured');
expect($this->dbObjectExists('ensured'))->toBeTrue()
->and($this->dbObjectExists('idx_ensured_key'))->toBeTrue();
});
});
describe('::ensureFieldIndex()', function () {
test('creates an index', function () {
expect($this->dbObjectExists('idx_ensured_test'))->toBeFalse();
Definition::ensureTable('ensured');
Definition::ensureFieldIndex('ensured', 'test', ['name', 'age']);
expect($this->dbObjectExists('idx_ensured_test'))->toBeTrue();
});
});
describe('::ensureDocumentIndex()', function () {
test('creates a full index', function () {
$docIdx = 'idx_doc_table_document';
Definition::ensureTable('doc_table');
expect($this->dbObjectExists($docIdx))->toBeFalse();
Definition::ensureDocumentIndex('doc_table', DocumentIndex::Full);
expect($this->dbObjectExists($docIdx))->toBeTrue();
});
test('creates an optimized index', function () {
$docIdx = 'idx_doc_tbl_document';
Definition::ensureTable('doc_tbl');
expect($this->dbObjectExists($docIdx))->toBeFalse();
Definition::ensureDocumentIndex('doc_tbl', DocumentIndex::Optimized);
expect($this->dbObjectExists($docIdx))->toBeTrue();
});
});

View File

@@ -0,0 +1,64 @@
<?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);
});
});

View File

@@ -0,0 +1,45 @@
<?php
/**
* @author Daniel J. Summers <daniel@bitbadger.solutions>
* @license MIT
*/
declare(strict_types=1);
use BitBadger\PDODocument\{DocumentException, DocumentList, Query};
use BitBadger\PDODocument\Mapper\DocumentMapper;
use Test\Integration\PostgreSQL\ThrowawayDb;
use Test\Integration\TestDocument;
pest()->group('integration', 'postgresql');
describe('::create()', function () {
test('creates a document list', function () {
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
new DocumentMapper(TestDocument::class));
expect($list)->not->toBeNull();
$list = null; // free database result
});
});
describe('::items()', function () {
test('enumerates items in the list', function () {
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
new DocumentMapper(TestDocument::class));
expect($list)->not->toBeNull();
$count = 0;
foreach ($list->items() as $item) {
expect(['one', 'two', 'three', 'four', 'five'])->toContain($item->id);
$count++;
}
expect($count)->toBe(5);
});
test('fails when the list is exhausted', function () {
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
new DocumentMapper(TestDocument::class));
expect($list)->not->toBeNull()->hasItems()->toBeTrue();
$ignored = iterator_to_array($list->items());
expect($list)->hasItems()->toBeFalse()
->and(fn () => iterator_to_array($list->items()))->toThrow(DocumentException::class);
});
});