Add SQLite Patch integration tests

- Use multiple-class use statements
This commit is contained in:
Daniel J. Summers 2024-06-07 23:06:31 -04:00
parent 1ab961e35a
commit 9fba3781d6
21 changed files with 143 additions and 67 deletions

View File

@ -27,7 +27,9 @@ class Custom
$stmt = Configuration::dbConn()->prepare($query);
} catch (PDOException $ex) {
$keyword = explode(' ', $query, 2)[0];
throw new DocumentException("Error executing $keyword statement: " . Configuration::dbConn()->errorCode(),
throw new DocumentException(
sprintf("Error executing %s statement: [%s] %s", $keyword, Configuration::dbConn()->errorCode(),
Configuration::dbConn()->errorInfo()[2]),
previous: $ex);
}
foreach ($parameters as $key => $value) {

View File

@ -2,9 +2,7 @@
namespace BitBadger\PDODocument\Mapper;
use BitBadger\PDODocument\Configuration;
use BitBadger\PDODocument\DocumentException;
use BitBadger\PDODocument\Mode;
use BitBadger\PDODocument\{Configuration, DocumentException, Mode};
/**
* Map an EXISTS result to a boolean value

View File

@ -2,8 +2,7 @@
namespace BitBadger\PDODocument\Query;
use BitBadger\PDODocument\Field;
use BitBadger\PDODocument\Query;
use BitBadger\PDODocument\{Field, Query};
/**
* Queries for counting documents

View File

@ -2,9 +2,7 @@
namespace BitBadger\PDODocument\Query;
use BitBadger\PDODocument\Configuration;
use BitBadger\PDODocument\DocumentException;
use BitBadger\PDODocument\Mode;
use BitBadger\PDODocument\{Configuration, DocumentException, Mode};
/**
* Queries to define tables and indexes

View File

@ -2,8 +2,7 @@
namespace BitBadger\PDODocument\Query;
use BitBadger\PDODocument\Field;
use BitBadger\PDODocument\Query;
use BitBadger\PDODocument\{Field, Query};
/**
* Queries to delete documents

View File

@ -2,8 +2,7 @@
namespace BitBadger\PDODocument\Query;
use BitBadger\PDODocument\Field;
use BitBadger\PDODocument\Query;
use BitBadger\PDODocument\{Field, Query};
/**
* Queries to determine document existence

View File

@ -2,8 +2,7 @@
namespace BitBadger\PDODocument\Query;
use BitBadger\PDODocument\Field;
use BitBadger\PDODocument\Query;
use BitBadger\PDODocument\{Field, Query};
/**
* Queries for retrieving documents

View File

@ -2,11 +2,7 @@
namespace BitBadger\PDODocument\Query;
use BitBadger\PDODocument\Configuration;
use BitBadger\PDODocument\DocumentException;
use BitBadger\PDODocument\Field;
use BitBadger\PDODocument\Mode;
use BitBadger\PDODocument\Query;
use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode, Query};
/**
* Queries to perform partial updates on documents

View File

@ -2,11 +2,7 @@
namespace BitBadger\PDODocument\Query;
use BitBadger\PDODocument\Configuration;
use BitBadger\PDODocument\DocumentException;
use BitBadger\PDODocument\Field;
use BitBadger\PDODocument\Mode;
use BitBadger\PDODocument\Query;
use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode, Query};
/**
* Queries to remove fields from documents

View File

@ -2,13 +2,8 @@
namespace Test\Integration\SQLite;
use BitBadger\PDODocument\Count;
use BitBadger\PDODocument\Custom;
use BitBadger\PDODocument\DocumentException;
use BitBadger\PDODocument\Mapper\CountMapper;
use BitBadger\PDODocument\Mapper\DocumentMapper;
use BitBadger\PDODocument\Mapper\StringMapper;
use BitBadger\PDODocument\Query;
use BitBadger\PDODocument\{Count, Custom, DocumentException, Query};
use BitBadger\PDODocument\Mapper\{CountMapper, DocumentMapper};
use PHPUnit\Framework\TestCase;
use Test\Integration\TestDocument;
@ -105,12 +100,8 @@ class CustomTest extends TestCase
public function testNonQuerySucceedsWhenOperatingOnData()
{
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
try {
$remaining = Count::all(ThrowawayDb::TABLE);
$this->assertEquals(0, $remaining, 'There should be no documents remaining in the table');
} finally {
$this->dbName = ThrowawayDb::exchange($this->dbName);
}
$remaining = Count::all(ThrowawayDb::TABLE);
$this->assertEquals(0, $remaining, 'There should be no documents remaining in the table');
}
public function testNonQuerySucceedsWhenNoDataMatchesWhereClause()

View File

@ -0,0 +1,58 @@
<?php
namespace Test\Integration\SQLite;
use BitBadger\PDODocument\{Custom, Definition, DocumentException};
use BitBadger\PDODocument\Mapper\ExistsMapper;
use PHPUnit\Framework\TestCase;
/**
* SQLite integration tests for the Definition class
*/
class DefinitionTest extends TestCase
{
/** @var string Database name for throwaway database */
private string $dbName;
protected function setUp(): void
{
parent::setUp();
$this->dbName = ThrowawayDb::create(withData: false);
}
protected function tearDown(): void
{
ThrowawayDb::destroy($this->dbName);
parent::tearDown();
}
/**
* Does the given named object exist in the database?
*
* @param string $name The name of the object whose existence should be verified
* @return bool True if the object exists, false if not
* @throws DocumentException If any is encountered
*/
private function itExists(string $name): bool
{
return Custom::scalar('SELECT EXISTS (SELECT 1 FROM sqlite_master WHERE name = :name)',
[':name' => $name], new ExistsMapper());
}
public function testEnsureTableSucceeds()
{
$this->assertFalse($this->itExists('ensured'), 'The table should not exist already');
$this->assertFalse($this->itExists('idx_ensured_key'), 'The key index should not exist already');
Definition::ensureTable('ensured');
$this->assertTrue($this->itExists('ensured'), 'The table should now exist');
$this->assertTrue($this->itExists('idx_ensured_key'), 'The key index should now exist');
}
public function testEnsureFieldIndexSucceeds()
{
$this->assertFalse($this->itExists('idx_ensured_test'), 'The index should not exist already');
Definition::ensureTable('ensured');
Definition::ensureFieldIndex('ensured', 'test', ['name', 'age']);
$this->assertTrue($this->itExists('idx_ensured_test'), 'The index should now exist');
}
}

View File

@ -0,0 +1,59 @@
<?php declare(strict_types=1);
namespace Test\Integration\SQLite;
use BitBadger\PDODocument\{Count, Field, Find, Patch};
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
use Test\Integration\TestDocument;
/**
* SQLite integration tests for the Patch class
*/
class PatchTest extends TestCase
{
/** @var string Database name for throwaway database */
private string $dbName;
protected function setUp(): void
{
parent::setUp();
$this->dbName = ThrowawayDb::create();
}
protected function tearDown(): void
{
ThrowawayDb::destroy($this->dbName);
parent::tearDown();
}
#[TestDox('By ID succeeds when a document is updated')]
public function testByIdSucceedsWhenADocumentIsUpdated(): void
{
Patch::byId(ThrowawayDb::TABLE, 'one', ['num_value' => 44]);
$doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
$this->assertNotFalse($doc, 'There should have been a document returned');
$this->assertEquals(44, $doc->num_value, 'The updated document is not correct');
}
#[TestDox('By ID succeeds when no document is updated')]
public function testByIdSucceedsWhenNoDocumentIsUpdated(): void
{
Patch::byId(ThrowawayDb::TABLE, 'forty-seven', ['foo' => 'green']);
$this->assertTrue(true, 'The above not throwing an exception is the test');
}
public function testByFieldsSucceedsWhenADocumentIsUpdated(): void
{
Patch::byFields(ThrowawayDb::TABLE, [Field::EQ('value', 'purple')], ['num_value' => 77]);
$after = Count::byFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 77)]);
$this->assertEquals(2, $after, 'There should have been 2 documents updated');
}
public function testByFieldsSucceedsWhenNoDocumentIsUpdated(): void
{
Patch::byFields(ThrowawayDb::TABLE, [Field::EQ('value', 'burgundy')], ['foo' => 'green']);
$this->assertTrue(true, 'The above not throwing an exception is the test');
}
}

View File

@ -2,8 +2,7 @@
namespace Test\Unit;
use BitBadger\PDODocument\Configuration;
use BitBadger\PDODocument\DocumentException;
use BitBadger\PDODocument\{Configuration, DocumentException};
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
@ -34,6 +33,7 @@ class ConfigurationTest extends TestCase
public function testDbConnFailsWhenNoDSNSpecified(): void
{
$this->expectException(DocumentException::class);
Configuration::$pdoDSN = '';
Configuration::dbConn();
}
}

View File

@ -2,8 +2,7 @@
namespace Test\Unit;
use BitBadger\PDODocument\Field;
use BitBadger\PDODocument\Op;
use BitBadger\PDODocument\{Field, Op};
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;

View File

@ -2,8 +2,7 @@
namespace Test\Unit\Mapper;
use BitBadger\PDODocument\DocumentException;
use BitBadger\PDODocument\Field;
use BitBadger\PDODocument\{DocumentException, Field};
use BitBadger\PDODocument\Mapper\DocumentMapper;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;

View File

@ -2,10 +2,8 @@
namespace Test\Unit\Mapper;
use BitBadger\PDODocument\Configuration;
use BitBadger\PDODocument\DocumentException;
use BitBadger\PDODocument\{Configuration, DocumentException, Mode};
use BitBadger\PDODocument\Mapper\ExistsMapper;
use BitBadger\PDODocument\Mode;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;

View File

@ -1,12 +1,8 @@
<?php declare(strict_types=1);
namespace Tests\Unit;
namespace Test\Unit;
use BitBadger\PDODocument\Configuration;
use BitBadger\PDODocument\DocumentException;
use BitBadger\PDODocument\Field;
use BitBadger\PDODocument\Mode;
use BitBadger\PDODocument\Parameters;
use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode, Parameters};
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;

View File

@ -2,9 +2,7 @@
namespace Test\Unit\Query;
use BitBadger\PDODocument\Configuration;
use BitBadger\PDODocument\DocumentException;
use BitBadger\PDODocument\Mode;
use BitBadger\PDODocument\{Configuration, DocumentException, Mode};
use BitBadger\PDODocument\Query\Definition;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;

View File

@ -2,10 +2,7 @@
namespace Test\Unit\Query;
use BitBadger\PDODocument\Configuration;
use BitBadger\PDODocument\DocumentException;
use BitBadger\PDODocument\Field;
use BitBadger\PDODocument\Mode;
use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode};
use BitBadger\PDODocument\Query\Patch;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;

View File

@ -2,11 +2,7 @@
namespace Test\Unit\Query;
use BitBadger\PDODocument\Configuration;
use BitBadger\PDODocument\DocumentException;
use BitBadger\PDODocument\Field;
use BitBadger\PDODocument\Mode;
use BitBadger\PDODocument\Parameters;
use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode, Parameters};
use BitBadger\PDODocument\Query\RemoveFields;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;

View File

@ -2,8 +2,7 @@
namespace Test\Unit;
use BitBadger\PDODocument\Field;
use BitBadger\PDODocument\Query;
use BitBadger\PDODocument\{Field, Query};
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;