Migrate tests to Pest (#8)
Reviewed-on: #8
This commit was merged in pull request #8.
This commit is contained in:
45
tests/Integration/PostgreSQL/CountTest.php
Normal file
45
tests/Integration/PostgreSQL/CountTest.php
Normal 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);
|
||||
});
|
||||
});
|
||||
98
tests/Integration/PostgreSQL/CustomTest.php
Normal file
98
tests/Integration/PostgreSQL/CustomTest.php
Normal 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);
|
||||
});
|
||||
});
|
||||
47
tests/Integration/PostgreSQL/DefinitionTest.php
Normal file
47
tests/Integration/PostgreSQL/DefinitionTest.php
Normal 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();
|
||||
});
|
||||
});
|
||||
64
tests/Integration/PostgreSQL/DeleteTest.php
Normal file
64
tests/Integration/PostgreSQL/DeleteTest.php
Normal 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);
|
||||
});
|
||||
});
|
||||
96
tests/Integration/PostgreSQL/DocumentListTest.php
Normal file
96
tests/Integration/PostgreSQL/DocumentListTest.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?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);
|
||||
});
|
||||
});
|
||||
|
||||
describe('->hasItems()', function () {
|
||||
test('returns false when no items are in the list', function () {
|
||||
expect(DocumentList::create(
|
||||
Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE (data->>'num_value')::numeric < 0", [],
|
||||
new DocumentMapper(TestDocument::class)))
|
||||
->not->toBeNull()
|
||||
->hasItems()->toBeFalse();
|
||||
});
|
||||
test('returns true when items are in the list', function () {
|
||||
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
||||
new DocumentMapper(TestDocument::class));
|
||||
expect($list)->not->toBeNull()->hasItems()->toBeTrue();
|
||||
foreach ($list->items() as $ignored) {
|
||||
expect($list)->hasItems()->toBeTrue();
|
||||
}
|
||||
expect($list)->hasItems()->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
describe('->map()', function () {
|
||||
test('transforms the list', function () {
|
||||
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
||||
new DocumentMapper(TestDocument::class));
|
||||
expect($list)->not->toBeNull()->hasItems()->toBeTrue();
|
||||
foreach ($list->map(fn($doc) => strrev($doc->id)) as $mapped) {
|
||||
expect(['eno', 'owt', 'eerht', 'ruof', 'evif'])->toContain($mapped);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('->iter()', function () {
|
||||
test('walks the list', function () {
|
||||
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
||||
new DocumentMapper(TestDocument::class));
|
||||
expect($list)->not->toBeNull()->hasItems()->toBeTrue();
|
||||
$splats = [];
|
||||
$list->iter(function ($doc) use (&$splats) { $splats[] = str_repeat('*', strlen($doc->id)); });
|
||||
expect(implode(' ', $splats))->toBe('*** *** ***** **** ****');
|
||||
});
|
||||
});
|
||||
|
||||
describe('->mapToArray()', function () {
|
||||
test('creates an associative array', function () {
|
||||
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
||||
new DocumentMapper(TestDocument::class));
|
||||
expect($list)->not->toBeNull()->hasItems()->toBeTrue()
|
||||
->and($list->mapToArray(fn($it) => $it->id, fn($it) => $it->value))
|
||||
->toBe(['one' => 'FIRST!', 'two' => 'another', 'three' => '', 'four' => 'purple', 'five' => 'purple']);
|
||||
});
|
||||
});
|
||||
249
tests/Integration/PostgreSQL/DocumentTest.php
Normal file
249
tests/Integration/PostgreSQL/DocumentTest.php
Normal file
@@ -0,0 +1,249 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\{AutoId, Configuration, Custom, Document, DocumentException, Field, Find, Query};
|
||||
use BitBadger\PDODocument\Mapper\ArrayMapper;
|
||||
use Test\Integration\{NumDocument, SubDocument, TestDocument};
|
||||
use Test\Integration\PostgreSQL\ThrowawayDb;
|
||||
|
||||
pest()->group('integration', 'postgresql');
|
||||
|
||||
describe('::insert()', function () {
|
||||
test('inserts an array with no automatic ID', function () {
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => 'turkey', 'sub' => ['foo' => 'gobble', 'bar' => 'gobble']]);
|
||||
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class);
|
||||
expect($tryDoc)
|
||||
->isSome()->toBeTrue()
|
||||
->and($tryDoc->get())
|
||||
->id->toBe('turkey')
|
||||
->value->toBe('')
|
||||
->num_value->toBe(0)
|
||||
->sub->not->toBeNull()
|
||||
->sub->foo->toBe('gobble')
|
||||
->sub->bar->toBe('gobble');
|
||||
});
|
||||
test('inserts an array with auto-number ID, not provided', function () {
|
||||
Configuration::$autoId = AutoId::Number;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'new', 'num_value' => 8]);
|
||||
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper());
|
||||
expect($doc)->isSome()->toBeTrue()
|
||||
->and(json_decode($doc->get()['data']))
|
||||
->id->toBe(1);
|
||||
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'again', 'num_value' => 7]);
|
||||
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE " . Query::whereById(docId: 2),
|
||||
[':id' => 2], new ArrayMapper());
|
||||
expect($doc)->isSome()->toBeTrue()
|
||||
->and(json_decode($doc->get()['data']))
|
||||
->id->toBe(2);
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
});
|
||||
test('inserts an array with auto-number ID, provided', function () {
|
||||
Configuration::$autoId = AutoId::Number;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => 7, 'value' => 'new', 'num_value' => 8]);
|
||||
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper());
|
||||
expect($doc)->isSome()->toBeTrue()
|
||||
->and(json_decode($doc->get()['data']))
|
||||
->id->toBe(7);
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
|
||||
});
|
||||
test('inserts an array with auto-UUID ID, not provided', function () {
|
||||
Configuration::$autoId = AutoId::UUID;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => '', 'num_value' => 5]);
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 5)], TestDocument::class);
|
||||
expect($doc)
|
||||
->isSome()->toBeTrue()
|
||||
->and($doc->get())->id->not->toBeEmpty();
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
});
|
||||
test('inserts an array with auto-UUID ID, provided', function () {
|
||||
Configuration::$autoId = AutoId::UUID;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
$uuid = AutoId::generateUUID();
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => $uuid, 'value' => 'uuid', 'num_value' => 12]);
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 12)], TestDocument::class);
|
||||
expect($doc)->isSome()->toBeTrue()
|
||||
->and($doc->get())->id->toBe($uuid);
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
});
|
||||
test('inserts an array with auto-string ID, not provided', function () {
|
||||
Configuration::$autoId = AutoId::RandomString;
|
||||
Configuration::$idStringLength = 6;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => '', 'value' => 'new', 'num_value' => 8]);
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 8)], TestDocument::class);
|
||||
expect($doc)->isSome()->toBeTrue()
|
||||
->and($doc->get())->id->toHaveLength(6);
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
Configuration::$idStringLength = 16;
|
||||
}
|
||||
});
|
||||
test('inserts an array with auto-string ID, provided', function () {
|
||||
Configuration::$autoId = AutoId::RandomString;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => 'my-key', 'value' => 'old', 'num_value' => 3]);
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 3)], TestDocument::class);
|
||||
expect($doc)->isSome()->toBeTrue()
|
||||
->and($doc->get())->id->toBe('my-key');
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
});
|
||||
test('inserts an object with no automatic ID', function () {
|
||||
Document::insert(ThrowawayDb::TABLE, new TestDocument('turkey', sub: new SubDocument('gobble', 'gobble')));
|
||||
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class);
|
||||
expect($tryDoc)->isSome()->toBeTrue();
|
||||
$doc = $tryDoc->get();
|
||||
expect($doc)
|
||||
->id->toBe('turkey')
|
||||
->num_value->toBe(0)
|
||||
->sub->not->toBeNull()
|
||||
->sub->foo->toBe('gobble')
|
||||
->sub->bar->toBe('gobble')
|
||||
->and($doc->value)->toBe('');
|
||||
});
|
||||
test('inserts an object with auto-number ID, not provided', function () {
|
||||
Configuration::$autoId = AutoId::Number;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
|
||||
Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'taco'));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'taco')], NumDocument::class);
|
||||
expect($doc)->isSome()->toBeTrue()
|
||||
->and($doc->get())->id->toBe(1);
|
||||
|
||||
Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'burrito'));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'burrito')], NumDocument::class);
|
||||
expect($doc)->isSome()->toBeTrue()
|
||||
->and($doc->get())->id->toBe(2);
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
});
|
||||
test('inserts an object with auto-number ID, provided', function () {
|
||||
Configuration::$autoId = AutoId::Number;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, new NumDocument(64, 'large'));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'large')], NumDocument::class);
|
||||
expect($doc)->isSome()->toBeTrue()
|
||||
->and($doc->get())->id->toBe(64);
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
});
|
||||
test('inserts an object with auto-UUID ID, not provided', function () {
|
||||
Configuration::$autoId = AutoId::UUID;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, new TestDocument(value: 'something', num_value: 9));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::exists('value')], TestDocument::class);
|
||||
expect($doc)->isSome()->toBeTrue()
|
||||
->and($doc->get())->id->not->toBeEmpty();
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
});
|
||||
test('inserts an object with auto-UUID ID, provided', function () {
|
||||
Configuration::$autoId = AutoId::UUID;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
$uuid = AutoId::generateUUID();
|
||||
Document::insert(ThrowawayDb::TABLE, new TestDocument($uuid, num_value: 14));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 14)], TestDocument::class);
|
||||
expect($doc)->isSome()->toBeTrue()
|
||||
->and($doc->get())->id->toBe($uuid);
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
});
|
||||
test('inserts an object with auto-string ID, not provided', function () {
|
||||
Configuration::$autoId = AutoId::RandomString;
|
||||
Configuration::$idStringLength = 40;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, new TestDocument(num_value: 55));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 55)], TestDocument::class);
|
||||
expect($doc)->isSome()->toBeTrue()
|
||||
->and($doc->get())->id->toHaveLength(40);
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
Configuration::$idStringLength = 16;
|
||||
}
|
||||
});
|
||||
test('inserts an object with auto-string ID, provided', function () {
|
||||
Configuration::$autoId = AutoId::RandomString;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, new TestDocument('my-key', num_value: 3));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 3)], TestDocument::class);
|
||||
expect($doc)->isSome()->toBeTrue()
|
||||
->and($doc->get())->id->toBe('my-key');
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
});
|
||||
test('throws an exception for duplicate key', function () {
|
||||
expect(fn () => Document::insert(ThrowawayDb::TABLE, new TestDocument('one')))
|
||||
->toThrow(DocumentException::class);
|
||||
});
|
||||
});
|
||||
|
||||
describe('::save()', function () {
|
||||
test('inserts a new document', function () {
|
||||
Document::save(ThrowawayDb::TABLE, new TestDocument('test', sub: new SubDocument('a', 'b')));
|
||||
expect(Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class))->isSome()->toBeTrue();
|
||||
});
|
||||
test('updates an existing document', function () {
|
||||
Document::save(ThrowawayDb::TABLE, new TestDocument('two', num_value: 44));
|
||||
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
|
||||
expect($tryDoc)->isSome()->toBeTrue()
|
||||
->and($tryDoc->get())
|
||||
->num_value->toBe(44)
|
||||
->sub->toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::update()', function () {
|
||||
test('replaces an existing document', function () {
|
||||
Document::update(ThrowawayDb::TABLE, 'one', new TestDocument('one', 'howdy', 8, new SubDocument('y', 'z')));
|
||||
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
|
||||
expect($tryDoc)->isSome()->toBeTrue();
|
||||
$doc = $tryDoc->get();
|
||||
expect($doc)
|
||||
->num_value->toBe(8)
|
||||
->sub->not->toBeNull()
|
||||
->sub->foo->toBe('y')
|
||||
->sub->bar->toBe('z')
|
||||
->and($doc->value)->toBe('howdy');
|
||||
});
|
||||
test('does nothing for a non-existent document', function () {
|
||||
Document::update(ThrowawayDb::TABLE, 'two-hundred', new TestDocument('200'));
|
||||
expect(Find::byId(ThrowawayDb::TABLE, 'two-hundred', TestDocument::class))->isNone()->toBeTrue();
|
||||
});
|
||||
});
|
||||
48
tests/Integration/PostgreSQL/ExistsTest.php
Normal file
48
tests/Integration/PostgreSQL/ExistsTest.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\{Exists, Field};
|
||||
use Test\Integration\PostgreSQL\ThrowawayDb;
|
||||
|
||||
pest()->group('integration', 'postgresql');
|
||||
|
||||
describe('::byId()', function () {
|
||||
test('returns true when a document exists', function () {
|
||||
expect(Exists::byId(ThrowawayDb::TABLE, 'three'))->toBeTrue();
|
||||
});
|
||||
test('returns false when a document does not exist', function () {
|
||||
expect(Exists::byId(ThrowawayDb::TABLE, 'seven'))->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byFields()', function () {
|
||||
test('returns true when matching documents exist', function () {
|
||||
expect(Exists::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 10)]))->toBeTrue();
|
||||
});
|
||||
test('returns false when no matching documents exist', function () {
|
||||
expect(Exists::byFields(ThrowawayDb::TABLE, [Field::less('nothing', 'none')]))->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byContains()', function () {
|
||||
test('returns true when matching documents exist', function () {
|
||||
expect(Exists::byContains(ThrowawayDb::TABLE, ['value' => 'purple']))->toBeTrue();
|
||||
});
|
||||
test('returns false when no matching documents exist', function () {
|
||||
expect(Exists::byContains(ThrowawayDb::TABLE, ['value' => 'violet']))->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byJsonPath()', function () {
|
||||
test('returns true when matching documents exist', function () {
|
||||
expect(Exists::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ == 10)'))->toBeTrue();
|
||||
});
|
||||
test('returns false when no matching documents exist', function () {
|
||||
expect(Exists::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ == 10.1)'))->toBeFalse();
|
||||
});
|
||||
});
|
||||
216
tests/Integration/PostgreSQL/FindTest.php
Normal file
216
tests/Integration/PostgreSQL/FindTest.php
Normal file
@@ -0,0 +1,216 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\{Custom, Delete, Document, Field, FieldMatch, Find};
|
||||
use Test\Integration\{ArrayDocument, NumDocument, TestDocument};
|
||||
use Test\Integration\PostgreSQL\ThrowawayDb;
|
||||
|
||||
pest()->group('integration', 'postgresql');
|
||||
|
||||
describe('::all()', function () {
|
||||
test('retrieves data', function () {
|
||||
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class);
|
||||
expect($docs)->not->toBeNull();
|
||||
$count = 0;
|
||||
foreach ($docs->items() as $ignored) $count++;
|
||||
expect($count)->toBe(5);
|
||||
});
|
||||
test('sorts data ascending', function () {
|
||||
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class, [Field::named('id')]);
|
||||
expect($docs)->not->toBeNull()
|
||||
->and(iterator_to_array($docs->map(fn ($it) => $it->id), false))
|
||||
->toBe(['five', 'four', 'one', 'three', 'two']);
|
||||
});
|
||||
test('sorts data descending', function () {
|
||||
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class, [Field::named('id DESC')]);
|
||||
expect($docs)->not->toBeNull()
|
||||
->and(iterator_to_array($docs->map(fn ($it) => $it->id), false))
|
||||
->toBe(['two', 'three', 'one', 'four', 'five']);
|
||||
});
|
||||
test('sorts data numerically', function () {
|
||||
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class,
|
||||
[Field::named('sub.foo NULLS LAST'), Field::named('n:num_value')]);
|
||||
expect($docs)->not->toBeNull()
|
||||
->and(iterator_to_array($docs->map(fn ($it) => $it->id), false))
|
||||
->toBe(['two', 'four', 'one', 'three', 'five']);
|
||||
});
|
||||
test('retrieves empty results', function () {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
expect(Find::all(ThrowawayDb::TABLE, TestDocument::class))
|
||||
->not->toBeNull()
|
||||
->hasItems()->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byId()', function () {
|
||||
test('retrieves a document via string ID', function () {
|
||||
expect(Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class))
|
||||
->isSome()->toBeTrue()
|
||||
->get()->id->toBe('two');
|
||||
});
|
||||
test('retrieves a document via numeric ID', function () {
|
||||
Delete::byFields(ThrowawayDb::TABLE, [Field::notExists('absent')]);
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => 18, 'value' => 'howdy']);
|
||||
expect(Find::byId(ThrowawayDb::TABLE, 18, NumDocument::class))
|
||||
->isSome()->toBeTrue()
|
||||
->get()->id->toBe(18);
|
||||
});
|
||||
test('returns None when a document is not found', function () {
|
||||
expect(Find::byId(ThrowawayDb::TABLE, 'seventy-five', TestDocument::class))->isNone()->toBeTrue();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byFields()', function () {
|
||||
test('retrieves matching documents', function () {
|
||||
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::in('value', ['blue', 'purple']), Field::exists('sub')],
|
||||
TestDocument::class, FieldMatch::All);
|
||||
expect($docs)->not->toBeNull();
|
||||
$count = 0;
|
||||
foreach ($docs->items() as $ignored) $count++;
|
||||
expect($count)->toBe(1);
|
||||
});
|
||||
test('retrieves ordered matching documents', function () {
|
||||
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'purple')], TestDocument::class,
|
||||
FieldMatch::All, [Field::named('id')]);
|
||||
expect($docs)->not->toBeNull()
|
||||
->and(iterator_to_array($docs->map(fn ($it) => $it->id), false))->toBe(['five', 'four']);
|
||||
});
|
||||
test('retrieves documents matching a numeric IN clause', function () {
|
||||
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::in('num_value', [2, 4, 6, 8])], TestDocument::class);
|
||||
expect($docs)->not->toBeNull();
|
||||
$count = 0;
|
||||
foreach ($docs->items() as $ignored) $count++;
|
||||
expect($count)->toBe(1);
|
||||
});
|
||||
test('returns an empty list when no matching documents are found', function () {
|
||||
expect(Find::byFields(ThrowawayDb::TABLE, [Field::greater('num_value', 100)], TestDocument::class))
|
||||
->not->toBeNull()
|
||||
->hasItems()->toBeFalse();
|
||||
});
|
||||
test('retrieves documents matching an inArray condition', function () {
|
||||
Delete::byFields(ThrowawayDb::TABLE, [Field::notExists('absentField')]);
|
||||
foreach (ArrayDocument::testDocuments() as $doc) Document::insert(ThrowawayDb::TABLE, $doc);
|
||||
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::inArray('values', ThrowawayDb::TABLE, ['c'])],
|
||||
ArrayDocument::class);
|
||||
expect($docs)->not->toBeNull();
|
||||
$count = 0;
|
||||
foreach ($docs->items() as $ignored) $count++;
|
||||
expect($count)->toBe(2);
|
||||
});
|
||||
test('returns an empty list when no documents match an inArray condition', function () {
|
||||
Delete::byFields(ThrowawayDb::TABLE, [Field::notExists('absentField')]);
|
||||
foreach (ArrayDocument::testDocuments() as $doc) Document::insert(ThrowawayDb::TABLE, $doc);
|
||||
expect(Find::byFields(ThrowawayDb::TABLE, [Field::inArray('values', ThrowawayDb::TABLE, ['j'])],
|
||||
ArrayDocument::class))
|
||||
->not->toBeNull()
|
||||
->hasItems()->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byContains()', function () {
|
||||
test('retrieves matching documents', function () {
|
||||
$docs = Find::byContains(ThrowawayDb::TABLE, ['value' => 'purple'], TestDocument::class);
|
||||
expect($docs)->not->toBeNull();
|
||||
$count = 0;
|
||||
foreach ($docs->items() as $ignored) $count++;
|
||||
expect($count)->toBe(2);
|
||||
});
|
||||
test('retrieves ordered matching documents', function () {
|
||||
$docs = Find::byContains(ThrowawayDb::TABLE, ['sub' => ['foo' => 'green']], TestDocument::class,
|
||||
[Field::named('value')]);
|
||||
expect($docs)
|
||||
->not->toBeNull()
|
||||
->and(iterator_to_array($docs->map(fn ($it) => $it->id), false))->toBe(['two', 'four']);
|
||||
});
|
||||
test('returns an empty list when no documents match', function () {
|
||||
expect(Find::byContains(ThrowawayDb::TABLE, ['value' => 'indigo'], TestDocument::class))
|
||||
->not->toBeNull()
|
||||
->hasItems()->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byJsonPath()', function () {
|
||||
test('retrieves matching documents', function () {
|
||||
$docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class);
|
||||
expect($docs)->not->toBeNull();
|
||||
$count = 0;
|
||||
foreach ($docs->items() as $ignored) $count++;
|
||||
expect($count)->toBe(2);
|
||||
});
|
||||
test('retrieves ordered matching documents', function () {
|
||||
$docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class,
|
||||
[Field::named('id')]);
|
||||
expect($docs)->not->toBeNull()
|
||||
->and(iterator_to_array($docs->map(fn ($it) => $it->id), false))->toBe(['five', 'four']);
|
||||
});
|
||||
test('returns an empty list when no documents match', function () {
|
||||
expect(Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 100)', TestDocument::class))
|
||||
->not->toBeNull()
|
||||
->hasItems()->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::firstByFields()', function () {
|
||||
test('retrieves a matching document', function () {
|
||||
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'another')], TestDocument::class))
|
||||
->isSome()->toBeTrue()->get()->id->toBe('two');
|
||||
});
|
||||
test('retrieves a document for multiple results', function () {
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class);
|
||||
expect($doc)->isSome()->toBeTrue()->and(['two', 'four'])->toContain($doc->get()->id);
|
||||
});
|
||||
test('retrieves a document for multiple ordered results', function () {
|
||||
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class,
|
||||
orderBy: [Field::named('n:num_value DESC')]))
|
||||
->isSome()->toBeTrue()->get()->id->toBe('four');
|
||||
});
|
||||
test('returns None when no documents match', function () {
|
||||
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'absent')], TestDocument::class))
|
||||
->isNone()->toBeTrue();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::firstByContains()', function () {
|
||||
test('retrieves a matching document', function () {
|
||||
expect(Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'FIRST!'], TestDocument::class))
|
||||
->isSome()->toBeTrue()->get()->id->toBe('one');
|
||||
});
|
||||
test('retrieves a document for multiple results', function () {
|
||||
$doc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'purple'], TestDocument::class);
|
||||
expect($doc)->isSome()->toBeTrue()->and(['four', 'five'])->toContain($doc->get()->id);
|
||||
});
|
||||
test('retrieves a document for multiple ordered results', function () {
|
||||
expect(Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'purple'], TestDocument::class,
|
||||
[Field::named('sub.bar NULLS FIRST')]))
|
||||
->isSome()->toBeTrue()->get()->id->toBe('five');
|
||||
});
|
||||
test('returns None when no documents match', function () {
|
||||
expect(Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'indigo'], TestDocument::class))
|
||||
->isNone()->toBeTrue();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::firstByJsonPath()', function () {
|
||||
test('retrieves a matching document', function () {
|
||||
expect(Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ == 10)', TestDocument::class))
|
||||
->isSome()->toBeTrue()->get()->id->toBe('two');
|
||||
});
|
||||
test('retrieves a document for multiple results', function () {
|
||||
$doc = Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class);
|
||||
expect($doc)->isSome()->toBeTrue()->and(['four', 'five'])->toContain($doc->get()->id);
|
||||
});
|
||||
test('retrieves a document for multiple ordered results', function () {
|
||||
expect(Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class,
|
||||
[Field::named('id DESC')]))
|
||||
->isSome()->toBeTrue()->get()->id->toBe('four');
|
||||
});
|
||||
test('returns None when no documents match', function () {
|
||||
expect(Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 100)', TestDocument::class))
|
||||
->isNone()->toBeTrue();
|
||||
});
|
||||
});
|
||||
72
tests/Integration/PostgreSQL/PatchTest.php
Normal file
72
tests/Integration/PostgreSQL/PatchTest.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\{Count, Exists, Field, Find, Patch};
|
||||
use Test\Integration\PostgreSQL\ThrowawayDb;
|
||||
use Test\Integration\TestDocument;
|
||||
|
||||
pest()->group('integration', 'postgresql');
|
||||
|
||||
describe('::byId()', function () {
|
||||
test('updates an existing document', function () {
|
||||
Patch::byId(ThrowawayDb::TABLE, 'one', ['num_value' => 44]);
|
||||
expect(Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class))
|
||||
->isSome()->toBeTrue()
|
||||
->get()->num_value->toBe(44);
|
||||
});
|
||||
test('does nothing when a document does not exist', function () {
|
||||
$id = 'forty-seven';
|
||||
expect(Exists::byId(ThrowawayDb::TABLE, $id))->toBeFalse();
|
||||
Patch::byId(ThrowawayDb::TABLE, $id, ['foo' => 'green']); // no exception = pass
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byFields()', function () {
|
||||
test('updates existing documents', function () {
|
||||
Patch::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'purple')], ['num_value' => 77]);
|
||||
expect(Count::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 77)]))->toBe(2);
|
||||
});
|
||||
test('does nothing when no matching documents exist', function () {
|
||||
$fields = [Field::equal('value', 'burgundy')];
|
||||
expect(Count::byFields(ThrowawayDb::TABLE, $fields))->toBe(0);
|
||||
Patch::byFields(ThrowawayDb::TABLE, $fields, ['foo' => 'green']); // no exception = pass
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byContains()', function () {
|
||||
test('updates existing documents', function () {
|
||||
Patch::byContains(ThrowawayDb::TABLE, ['value' => 'another'], ['num_value' => 12]);
|
||||
$tryDoc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'another'], TestDocument::class);
|
||||
expect($tryDoc)->isSome()->toBeTrue()
|
||||
->and($tryDoc->get())
|
||||
->id->toBe('two')
|
||||
->num_value->toBe(12);
|
||||
});
|
||||
test('does nothing when no matching documents exist', function () {
|
||||
$criteria = ['value' => 'updated'];
|
||||
expect(Count::byContains(ThrowawayDb::TABLE, $criteria))->toBe(0);
|
||||
Patch::byContains(ThrowawayDb::TABLE, $criteria, ['sub.foo' => 'green']); // no exception = pass
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byJsonPath()', function () {
|
||||
test('updates existing documents', function () {
|
||||
Patch::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', ['value' => 'blue']);
|
||||
$docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class);
|
||||
expect($docs)->not->toBeNull()->hasItems()->toBeTrue();
|
||||
foreach ($docs->items() as $item) {
|
||||
expect(['four', 'five'])->toContain($item->id)
|
||||
->and($item->value)->toBe('blue');
|
||||
}
|
||||
});
|
||||
test('does nothing when no matching documents exist', function () {
|
||||
$path = '$.num_value ? (@ > 100)';
|
||||
expect(Count::byJsonPath(ThrowawayDb::TABLE, $path))->toBe(0);
|
||||
Patch::byJsonPath(ThrowawayDb::TABLE, $path, ['value' => 'blue']); // no exception = pass
|
||||
});
|
||||
});
|
||||
91
tests/Integration/PostgreSQL/RemoveFieldsTest.php
Normal file
91
tests/Integration/PostgreSQL/RemoveFieldsTest.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\{Exists, Field, Find, RemoveFields};
|
||||
use Test\Integration\PostgreSQL\ThrowawayDb;
|
||||
use Test\Integration\TestDocument;
|
||||
|
||||
pest()->group('integration', 'postgresql');
|
||||
|
||||
describe('::byId()', function () {
|
||||
test('removes fields', function () {
|
||||
RemoveFields::byId(ThrowawayDb::TABLE, 'two', ['sub', 'value']);
|
||||
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
|
||||
expect($tryDoc)->isSome()->toBeTrue();
|
||||
$doc = $tryDoc->get();
|
||||
expect($doc)->sub->toBeNull()
|
||||
->and($doc->value)->toBeEmpty();
|
||||
});
|
||||
test('does nothing when the field to remove does not exist', function () {
|
||||
expect(Exists::byFields(ThrowawayDb::TABLE, [Field::exists('a_field_that_does_not_exist')]))->toBeFalse();
|
||||
RemoveFields::byId(ThrowawayDb::TABLE, 'one', ['a_field_that_does_not_exist']); // no exception = pass
|
||||
});
|
||||
test('does nothing when the document does not exist', function () {
|
||||
expect(Exists::byId(ThrowawayDb::TABLE, 'fifty'))->toBeFalse();
|
||||
RemoveFields::byId(ThrowawayDb::TABLE, 'fifty', ['sub']); // no exception = pass
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byFields()', function () {
|
||||
test('removes fields from matching documents', function () {
|
||||
RemoveFields::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], ['sub']);
|
||||
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], TestDocument::class))
|
||||
->isSome()->toBeTrue()
|
||||
->get()->sub->toBeNull();
|
||||
});
|
||||
test('does nothing when the field to remove does not exist', function () {
|
||||
expect(Exists::byFields(ThrowawayDb::TABLE, [Field::exists('nada')]))->toBeFalse();
|
||||
RemoveFields::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], ['nada']); // no exception = pass
|
||||
});
|
||||
test('does nothing when no documents match', function () {
|
||||
expect(Exists::byFields(ThrowawayDb::TABLE, [Field::notEqual('missing', 'nope')]))->toBeFalse();
|
||||
RemoveFields::byFields(ThrowawayDb::TABLE, [Field::notEqual('missing', 'nope')], ['value']); // no exn = pass
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byContains()', function () {
|
||||
test('removes fields from matching documents', function () {
|
||||
$criteria = ['sub' => ['foo' => 'green']];
|
||||
RemoveFields::byContains(ThrowawayDb::TABLE, $criteria, ['value']);
|
||||
$docs = Find::byContains(ThrowawayDb::TABLE, $criteria, TestDocument::class);
|
||||
expect($docs)->not->toBeNull()->hasItems()->toBeTrue();
|
||||
foreach ($docs->items() as $item) {
|
||||
expect(['two', 'four'])->toContain($item->id)
|
||||
->and($item->value)->toBeEmpty();
|
||||
}
|
||||
});
|
||||
test('does nothing when the field to remove does not exist', function () {
|
||||
expect(Exists::byFields(ThrowawayDb::TABLE, [Field::exists('invalid_field')]))->toBeFalse();
|
||||
RemoveFields::byContains(ThrowawayDb::TABLE, ['sub' => ['foo' => 'green']], ['invalid_field']); // no exn = pass
|
||||
});
|
||||
test('does nothing when no documents match', function () {
|
||||
expect(Exists::byContains(ThrowawayDb::TABLE, ['value' => 'substantial']))->toBeFalse();
|
||||
RemoveFields::byContains(ThrowawayDb::TABLE, ['value' => 'substantial'], ['num_value']); // no exception = pass
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byJsonPath()', function () {
|
||||
test('removes fields from matching documents', function () {
|
||||
$path = '$.value ? (@ == "purple")';
|
||||
RemoveFields::byJsonPath(ThrowawayDb::TABLE, $path, ['sub']);
|
||||
$docs = Find::byJsonPath(ThrowawayDb::TABLE, $path, TestDocument::class);
|
||||
expect($docs)->not->toBeNull()->hasItems()->toBeTrue();
|
||||
foreach ($docs->items() as $item) {
|
||||
expect(['four', 'five'])->toContain($item->id)
|
||||
->and($item->sub)->toBeNull();
|
||||
}
|
||||
});
|
||||
test('does nothing when the field to remove does not exist', function () {
|
||||
expect(Exists::byFields(ThrowawayDb::TABLE, [Field::exists('submarine')]))->toBeFalse();
|
||||
RemoveFields::byJsonPath(ThrowawayDb::TABLE, '$.value ? (@ == "purple")', ['submarine']); // no exception = pass
|
||||
});
|
||||
test('does nothing when no documents match', function () {
|
||||
expect(Exists::byJsonPath(ThrowawayDb::TABLE, '$.value ? (@ == "mauve")'))->toBeFalse();
|
||||
RemoveFields::byJsonPath(ThrowawayDb::TABLE, '$.value ? (@ == "mauve")', ['value']); // no exception = pass
|
||||
});
|
||||
});
|
||||
92
tests/Integration/PostgreSQL/ThrowawayDb.php
Normal file
92
tests/Integration/PostgreSQL/ThrowawayDb.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
* @see https://github.com/Zaid-Ajaj/ThrowawayDb The origin concept
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Test\Integration\PostgreSQL;
|
||||
|
||||
use BitBadger\PDODocument\{AutoId, Configuration, Custom, Definition, Document, DocumentException};
|
||||
use Random\RandomException;
|
||||
use Test\Integration\{SubDocument, TestDocument};
|
||||
|
||||
/**
|
||||
* Utilities to create and destroy a throwaway PostgreSQL database to use for testing
|
||||
*/
|
||||
class ThrowawayDb
|
||||
{
|
||||
/** @var string The table used for document manipulation */
|
||||
public const TABLE = 'test_table';
|
||||
|
||||
/**
|
||||
* Configure the document library for the given database (or the main PostgreSQL connection, if the database name
|
||||
* is not provided; this is used for creating and dropping databases)
|
||||
*
|
||||
* @param string|null $dbName The name of the database to configure (optional, defaults to env or "postgres")
|
||||
* @throws DocumentException If any is encountered
|
||||
*/
|
||||
private static function configure(?string $dbName = null): void
|
||||
{
|
||||
Configuration::useDSN(sprintf("pgsql:host=%s;dbname=%s", $_ENV['PDO_DOC_PGSQL_HOST'] ?? 'localhost',
|
||||
$dbName ?? $_ENV['PDO_DOC_PGSQL_DB'] ?? 'postgres'));
|
||||
Configuration::$username = $_ENV['PDO_DOC_PGSQL_USER'] ?? 'postgres';
|
||||
Configuration::$password = $_ENV['PDO_DOC_PGSQL_PASS'] ?? 'postgres';
|
||||
Configuration::resetPDO();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load data into the test table
|
||||
*
|
||||
* @throws DocumentException If any is encountered
|
||||
*/
|
||||
public static function loadData(): void
|
||||
{
|
||||
Document::insert(self::TABLE, new TestDocument('one', 'FIRST!', 0));
|
||||
Document::insert(self::TABLE, new TestDocument('two', 'another', 10, new SubDocument('green', 'blue')));
|
||||
Document::insert(self::TABLE, new TestDocument('three', '', 4));
|
||||
Document::insert(self::TABLE, new TestDocument('four', 'purple', 17, new SubDocument('green', 'red')));
|
||||
Document::insert(self::TABLE, new TestDocument('five', 'purple', 18));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a throwaway PostgreSQL database
|
||||
*
|
||||
* @param bool $withData Whether to initialize this database with data (optional; defaults to `true`)
|
||||
* @return string The name of the database (use to pass to `destroy` function at end of test)
|
||||
* @throws DocumentException|RandomException If any is encountered
|
||||
*/
|
||||
public static function create(bool $withData = true): string
|
||||
{
|
||||
$dbName = 'throwaway_' . AutoId::generateRandom(10);
|
||||
self::configure();
|
||||
Custom::nonQuery("CREATE DATABASE $dbName WITH OWNER " . Configuration::$username, []);
|
||||
self::configure($dbName);
|
||||
|
||||
Definition::ensureTable(self::TABLE);
|
||||
|
||||
if ($withData) {
|
||||
self::loadData();
|
||||
}
|
||||
|
||||
return $dbName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy a throwaway PostgreSQL database
|
||||
*
|
||||
* @param string $dbName The name of the PostgreSQL database to be dropped
|
||||
* @throws DocumentException If any is encountered
|
||||
*/
|
||||
public static function destroy(string $dbName): void
|
||||
{
|
||||
self::configure();
|
||||
Custom::nonQuery("DROP DATABASE IF EXISTS $dbName WITH (FORCE)", []);
|
||||
Configuration::useDSN('');
|
||||
Configuration::$username = null;
|
||||
Configuration::$password = null;
|
||||
Configuration::resetPDO();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user