From ae3d72bf29f3126883709dd6667aeea9bce484ba Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Sun, 19 Nov 2023 00:37:06 -0600 Subject: [PATCH] WIP --- src/Internal/AbstractHandle.php | 6 ++ src/PostgresConnectionPool.php | 10 ++- src/PostgresExecutor.php | 8 +-- test/AbstractConnectionTest.php | 24 ++++---- test/AbstractLinkTest.php | 94 ++++++++++++++--------------- test/PgSqlConnectionTest.php | 13 +++- test/PgSqlNestedTransactionTest.php | 7 +-- test/PgSqlPoolTest.php | 16 +++-- test/PqConnectionTest.php | 29 ++++----- test/PqNestedTransactionTest.php | 7 +-- test/PqPoolTest.php | 10 +-- 11 files changed, 120 insertions(+), 104 deletions(-) diff --git a/src/Internal/AbstractHandle.php b/src/Internal/AbstractHandle.php index ed1d499..0981329 100644 --- a/src/Internal/AbstractHandle.php +++ b/src/Internal/AbstractHandle.php @@ -7,6 +7,7 @@ use Amp\Postgres\ByteA; use Amp\Postgres\PostgresConfig; use Amp\Sql\ConnectionException; +use Amp\Sql\Transaction; use Revolt\EventLoop; /** @@ -37,6 +38,11 @@ public function __destruct() } } + public function beginTransaction(): Transaction + { + // TODO: Implement beginTransaction() method. + } + public function getConfig(): PostgresConfig { return $this->config; diff --git a/src/PostgresConnectionPool.php b/src/PostgresConnectionPool.php index e5076a4..a1f1868 100644 --- a/src/PostgresConnectionPool.php +++ b/src/PostgresConnectionPool.php @@ -9,8 +9,6 @@ use Amp\Sql\SqlConnector; use Amp\Sql\Statement; use Amp\Sql\Transaction; -use Amp\Sql\TransactionIsolation; -use Amp\Sql\TransactionIsolationLevel; use function Amp\async; /** @@ -69,10 +67,10 @@ protected function createTransaction(Transaction $transaction, \Closure $release /** * Changes return type to this library's Transaction type. */ - public function beginTransaction( - TransactionIsolation $isolation = TransactionIsolationLevel::Committed - ): PostgresTransaction { - return parent::beginTransaction($isolation); + + public function beginTransaction(): PostgresTransaction + { + return parent::beginTransaction(); } protected function pop(): PostgresHandleConnection diff --git a/src/PostgresExecutor.php b/src/PostgresExecutor.php index 41b81d5..a9eadf1 100644 --- a/src/PostgresExecutor.php +++ b/src/PostgresExecutor.php @@ -14,22 +14,22 @@ interface PostgresExecutor extends Executor, PostgresQuoter /** * @return PostgresResult Result object specific to this library. */ - public function query(string $sql): PostgresResult; +// public function query(string $sql): PostgresResult; /** * @return PostgresStatement Statement object specific to this library. */ - public function prepare(string $sql): PostgresStatement; +// public function prepare(string $sql): PostgresStatement; /** * @return PostgresResult Result object specific to this library. */ - public function execute(string $sql, array $params = []): PostgresResult; +// public function execute(string $sql, array $params = []): PostgresResult; /** * @return PostgresTransaction Transaction object specific to this library. */ - public function beginTransaction(): PostgresTransaction; +// public function beginTransaction(): PostgresTransaction; /** * @param non-empty-string $channel Channel name. diff --git a/test/AbstractConnectionTest.php b/test/AbstractConnectionTest.php index 3426966..b860310 100644 --- a/test/AbstractConnectionTest.php +++ b/test/AbstractConnectionTest.php @@ -16,13 +16,13 @@ abstract class AbstractConnectionTest extends AbstractLinkTest { public function testIsClosed() { - $this->assertFalse($this->link->isClosed()); + $this->assertFalse($this->executor->isClosed()); } public function testConnectionCloseDuringQuery(): void { - $query = async($this->link->execute(...), 'SELECT pg_sleep(10)'); - $close = async($this->link->close(...)); + $query = async($this->executor->execute(...), 'SELECT pg_sleep(10)'); + $close = async($this->executor->close(...)); $start = \microtime(true); @@ -41,14 +41,14 @@ public function testConnectionCloseDuringQuery(): void public function testListen() { $channel = "test"; - $listener = $this->link->listen($channel); + $listener = $this->executor->listen($channel); $this->assertInstanceOf(PostgresListener::class, $listener); $this->assertSame($channel, $listener->getChannel()); EventLoop::delay(0.1, function () use ($channel): void { - $this->link->query(\sprintf("NOTIFY %s, '%s'", $channel, '0')); - $this->link->query(\sprintf("NOTIFY %s, '%s'", $channel, '1')); + $this->executor->query(\sprintf("NOTIFY %s, '%s'", $channel, '0')); + $this->executor->query(\sprintf("NOTIFY %s, '%s'", $channel, '1')); }); $count = 0; @@ -68,11 +68,11 @@ public function testListen() public function testNotify() { $channel = "test"; - $listener = $this->link->listen($channel); + $listener = $this->executor->listen($channel); EventLoop::delay(0.1, function () use ($channel) { - $this->link->notify($channel, '0'); - $this->link->notify($channel, '1'); + $this->executor->notify($channel, '0'); + $this->executor->notify($channel, '1'); }); $count = 0; @@ -95,18 +95,18 @@ public function testListenOnSameChannel() $this->expectExceptionMessage('Already listening on channel'); $channel = "test"; - Future\await([$this->link->listen($channel), $this->link->listen($channel)]); + Future\await([$this->executor->listen($channel), $this->executor->listen($channel)]); } public function testQueryAfterErroredQuery() { try { - $result = $this->link->query("INSERT INTO test VALUES ('github', 'com', '{1, 2, 3}', true, 4.2)"); + $result = $this->executor->query("INSERT INTO test VALUES ('github', 'com', '{1, 2, 3}', true, 4.2)"); } catch (QueryExecutionError $exception) { // Expected exception due to duplicate key. } - $result = $this->link->query("INSERT INTO test VALUES ('gitlab', 'com', '{1, 2, 3}', true, 4.2)"); + $result = $this->executor->query("INSERT INTO test VALUES ('gitlab', 'com', '{1, 2, 3}', true, 4.2)"); $this->assertSame(1, $result->getRowCount()); } diff --git a/test/AbstractLinkTest.php b/test/AbstractLinkTest.php index 8d3a5c8..f6a626c 100644 --- a/test/AbstractLinkTest.php +++ b/test/AbstractLinkTest.php @@ -6,14 +6,13 @@ use Amp\PHPUnit\AsyncTestCase; use Amp\Postgres\ByteA; use Amp\Postgres\Internal\PostgresHandleConnection; -use Amp\Postgres\PostgresLink; +use Amp\Postgres\PostgresExecutor; use Amp\Postgres\PostgresTransaction; use Amp\Postgres\QueryExecutionError; use Amp\Sql\QueryError; use Amp\Sql\Result; use Amp\Sql\Statement; use Amp\Sql\TransactionError; -use Amp\Sql\TransactionIsolationLevel; use function Amp\async; abstract class AbstractLinkTest extends AsyncTestCase @@ -31,7 +30,7 @@ abstract class AbstractLinkTest extends AsyncTestCase protected const INSERT_QUERY = 'INSERT INTO test VALUES ($1, $2, $3, $4, $5, $6, $7, $8)'; protected const FIELD_COUNT = 8; - protected PostgresLink $link; + protected PostgresExecutor $executor; private ?array $data = null; @@ -74,9 +73,9 @@ protected function verifyResult(Result $result, array $data): void } /** - * @return PostgresLink Connection or Link object to be tested. + * @return PostgresExecutor Executor object to be tested. */ - abstract public function createLink(string $connectionString): PostgresLink; + abstract public function createExecutor(string $connectionString): PostgresExecutor; /** * Helper method to invoke the protected constructor of classes extending {@see PostgresHandleConnection}. @@ -99,18 +98,18 @@ protected function newConnection(string $className, mixed ...$args): PostgresHan public function setUp(): void { parent::setUp(); - $this->link = $this->createLink('host=localhost user=postgres password=postgres'); + $this->executor = $this->createExecutor('host=localhost user=postgres password=postgres'); } public function tearDown(): void { parent::tearDown(); - $this->link->close(); + $this->executor->close(); } public function testQueryFetchRow(): void { - $result = $this->link->query("SELECT * FROM test"); + $result = $this->executor->query("SELECT * FROM test"); $data = $this->getData(); while ($row = $result->fetchRow()) { @@ -122,7 +121,7 @@ public function testQueryFetchRow(): void public function testQueryWithTupleResult() { - $result = $this->link->query("SELECT * FROM test"); + $result = $this->executor->query("SELECT * FROM test"); $data = $this->getData(); @@ -133,7 +132,7 @@ public function testQueryWithTupleResult() public function testMultipleQueryWithTupleResult() { - $result = $this->link->query("SELECT * FROM test; SELECT * FROM test"); + $result = $this->executor->query("SELECT * FROM test; SELECT * FROM test"); $data = $this->getData(); @@ -150,7 +149,7 @@ public function testMultipleQueryWithTupleResult() public function testMultipleQueryWithCommandResultFirst() { - $result = $this->link->query("INSERT INTO test VALUES ('canon', 'jp', '{1}', true, 4.2, null, null, '3.1415926'); SELECT * FROM test"); + $result = $this->executor->query("INSERT INTO test VALUES ('canon', 'jp', '{1}', true, 4.2, null, null, '3.1415926'); SELECT * FROM test"); $this->assertSame(1, $result->getRowCount()); @@ -168,7 +167,7 @@ public function testMultipleQueryWithCommandResultFirst() public function testMultipleQueryWithCommandResultSecond() { - $result = $this->link->query("SELECT * FROM test; INSERT INTO test VALUES ('canon', 'jp', '{1}', true, 4.2)"); + $result = $this->executor->query("SELECT * FROM test; INSERT INTO test VALUES ('canon', 'jp', '{1}', true, 4.2)"); $data = $this->getData(); @@ -183,13 +182,13 @@ public function testMultipleQueryWithCommandResultSecond() public function testQueryWithUnconsumedTupleResult() { - $result = $this->link->query("SELECT * FROM test"); + $result = $this->executor->query("SELECT * FROM test"); $this->assertInstanceOf(Result::class, $result); unset($result); // Force destruction of result object. - $result = $this->link->query("SELECT * FROM test"); + $result = $this->executor->query("SELECT * FROM test"); $this->assertInstanceOf(Result::class, $result); @@ -200,7 +199,7 @@ public function testQueryWithUnconsumedTupleResult() public function testQueryWithCommandResult(): void { - $result = $this->link->query("INSERT INTO test VALUES ('canon', 'jp', '{1}', true, 4.2)"); + $result = $this->executor->query("INSERT INTO test VALUES ('canon', 'jp', '{1}', true, 4.2)"); $this->assertSame(1, $result->getRowCount()); } @@ -208,14 +207,14 @@ public function testQueryWithCommandResult(): void public function testQueryWithEmptyQuery(): void { $this->expectException(QueryError::class); - $this->link->query(''); + $this->executor->query(''); } public function testQueryWithSyntaxError() { /** @var Result $result */ try { - $result = $this->link->query("SELECT & FROM test"); + $result = $this->executor->query("SELECT & FROM test"); $this->fail(\sprintf("An instance of %s was expected to be thrown", QueryExecutionError::class)); } catch (QueryExecutionError $exception) { $diagnostics = $exception->getDiagnostics(); @@ -227,7 +226,7 @@ public function testPrepare() { $query = "SELECT * FROM test WHERE domain=\$1"; - $statement = $this->link->prepare($query); + $statement = $this->executor->prepare($query); $this->assertSame($query, $statement->getQuery()); @@ -242,7 +241,7 @@ public function testPrepareWithCommandResult() { $query = "INSERT INTO test (domain, tld, keys, enabled, number, json) VALUES (:domain, :tld, :keys, :enabled, :number, :json)"; - $statement = $this->link->prepare($query); + $statement = $this->executor->prepare($query); $fields = [ 'domain' => 'canon', @@ -259,7 +258,7 @@ public function testPrepareWithCommandResult() $this->assertSame(1, $result->getRowCount()); - $result = $this->link->execute('SELECT * FROM test WHERE domain=? AND tld=?', ['canon', 'jp']); + $result = $this->executor->execute('SELECT * FROM test WHERE domain=? AND tld=?', ['canon', 'jp']); $this->verifyResult($result, [\array_values($fields)]); } @@ -271,7 +270,7 @@ public function testPrepareWithNamedParams() { $query = "SELECT * FROM test WHERE domain=:domain AND tld=:tld"; - $statement = $this->link->prepare($query); + $statement = $this->executor->prepare($query); $data = $this->getData()[0]; @@ -289,7 +288,7 @@ public function testPrepareWithUnnamedParams() { $query = "SELECT * FROM test WHERE domain=? AND tld=?"; - $statement = $this->link->prepare($query); + $statement = $this->executor->prepare($query); $data = $this->getData()[0]; @@ -307,7 +306,7 @@ public function testPrepareWithNamedParamsWithDataAppearingAsNamedParam() { $query = "SELECT * FROM test WHERE domain=:domain OR domain=':domain'"; - $statement = $this->link->prepare($query); + $statement = $this->executor->prepare($query); $data = $this->getData()[0]; @@ -328,7 +327,7 @@ public function testPrepareInvalidQuery() $query = "SELECT * FROM test WHERE invalid=\$1"; - $statement = $this->link->prepare($query); + $statement = $this->executor->prepare($query); $statement->execute(['param']); } @@ -340,9 +339,9 @@ public function testPrepareSameQuery() { $sql = "SELECT * FROM test WHERE domain=\$1"; - $statement1 = $this->link->prepare($sql); + $statement1 = $this->executor->prepare($sql); - $statement2 = $this->link->prepare($sql); + $statement2 = $this->executor->prepare($sql); $this->assertInstanceOf(Statement::class, $statement1); $this->assertInstanceOf(Statement::class, $statement2); @@ -363,8 +362,8 @@ public function testSimultaneousPrepareSameQuery() { $sql = "SELECT * FROM test WHERE domain=\$1"; - $statement1 = async(fn () => $this->link->prepare($sql)); - $statement2 = async(fn () => $this->link->prepare($sql)); + $statement1 = async(fn () => $this->executor->prepare($sql)); + $statement2 = async(fn () => $this->executor->prepare($sql)); [$statement1, $statement2] = Future\await([$statement1, $statement2]); @@ -383,9 +382,9 @@ public function testSimultaneousPrepareSameQuery() public function testPrepareSimilarQueryReturnsDifferentStatements() { - $statement1 = async(fn () => $this->link->prepare("SELECT * FROM test WHERE domain=\$1")); + $statement1 = async(fn () => $this->executor->prepare("SELECT * FROM test WHERE domain=\$1")); - $statement2 = async(fn () => $this->link->prepare("SELECT * FROM test WHERE domain=:domain")); + $statement2 = async(fn () => $this->executor->prepare("SELECT * FROM test WHERE domain=:domain")); [$statement1, $statement2] = Future\await([$statement1, $statement2]); @@ -412,7 +411,7 @@ public function testPrepareSimilarQueryReturnsDifferentStatements() public function testPrepareThenExecuteWithUnconsumedTupleResult() { - $statement = $this->link->prepare("SELECT * FROM test"); + $statement = $this->executor->prepare("SELECT * FROM test"); $result = $statement->execute(); @@ -429,7 +428,7 @@ public function testExecute() { $data = $this->getData()[0]; - $result = $this->link->execute("SELECT * FROM test WHERE domain=\$1", [$data[0]]); + $result = $this->executor->execute("SELECT * FROM test WHERE domain=\$1", [$data[0]]); $this->verifyResult($result, [$data]); } @@ -441,7 +440,7 @@ public function testExecuteWithNamedParams() { $data = $this->getData()[0]; - $result = $this->link->execute( + $result = $this->executor->execute( "SELECT * FROM test WHERE domain=:domain", ['domain' => $data[0]] ); @@ -457,7 +456,7 @@ public function testExecuteWithInvalidParams() $this->expectException(\Error::class); $this->expectExceptionMessage("Value for unnamed parameter at position 0 missing"); - $this->link->execute("SELECT * FROM test WHERE domain=\$1"); + $this->executor->execute("SELECT * FROM test WHERE domain=\$1"); } /** @@ -468,7 +467,7 @@ public function testExecuteWithInvalidNamedParams() $this->expectException(\Error::class); $this->expectExceptionMessage("Value for named parameter 'domain' missing"); - $this->link->execute("SELECT * FROM test WHERE domain=:domain", ['tld' => 'com']); + $this->executor->execute("SELECT * FROM test WHERE domain=:domain", ['tld' => 'com']); } /** @@ -477,7 +476,7 @@ public function testExecuteWithInvalidNamedParams() public function testSimultaneousQuery() { $callback = fn (int $value) => async(function () use ($value): void { - $result = $this->link->query("SELECT {$value} as value"); + $result = $this->executor->query("SELECT {$value} as value"); foreach ($result as $row) { $this->assertEquals($value, $row['value']); @@ -493,7 +492,7 @@ public function testSimultaneousQuery() public function testSimultaneousQueryWithOneFailing() { $callback = fn (string $query) => async(function () use ($query): Result { - $result = $this->link->query($query); + $result = $this->executor->query($query); $data = $this->getData(); @@ -527,13 +526,13 @@ public function testSimultaneousQueryAndPrepare() { $promises = []; $promises[] = async(function () { - $result = $this->link->query("SELECT * FROM test"); + $result = $this->executor->query("SELECT * FROM test"); $data = $this->getData(); $this->verifyResult($result, $data); }); $promises[] = async(function () { - $statement = ($this->link->prepare("SELECT * FROM test")); + $statement = ($this->executor->prepare("SELECT * FROM test")); $result = $statement->execute(); $data = $this->getData(); $this->verifyResult($result, $data); @@ -545,14 +544,14 @@ public function testSimultaneousQueryAndPrepare() public function testSimultaneousPrepareAndExecute() { $promises[] = async(function () { - $statement = $this->link->prepare("SELECT * FROM test"); + $statement = $this->executor->prepare("SELECT * FROM test"); $result = $statement->execute(); $data = $this->getData(); $this->verifyResult($result, $data); }); $promises[] = async(function () { - $result = $this->link->execute("SELECT * FROM test"); + $result = $this->executor->execute("SELECT * FROM test"); $data = $this->getData(); $this->verifyResult($result, $data); }); @@ -562,9 +561,7 @@ public function testSimultaneousPrepareAndExecute() public function testTransaction() { - $isolation = TransactionIsolationLevel::Committed; - - $transaction = $this->link->beginTransaction($isolation); + $transaction = $this->executor->beginTransaction(); $this->assertInstanceOf(PostgresTransaction::class, $transaction); @@ -572,7 +569,6 @@ public function testTransaction() $this->assertFalse($transaction->isClosed()); $this->assertTrue($transaction->isActive()); - $this->assertSame($isolation, $transaction->getIsolationLevel()); $transaction->createSavepoint('test'); @@ -630,13 +626,13 @@ public function testStatementInsertByteA( array $params, array $expected ): void { - $statement = $this->link->prepare($insertSql); + $statement = $this->executor->prepare($insertSql); $result = $statement->execute($params); $this->assertSame(1, $result->getRowCount()); - $result = $this->link->execute($selectSql, $params); + $result = $this->executor->execute($selectSql, $params); $this->assertSame($expected, $result->fetchRow()); } @@ -649,11 +645,11 @@ public function testExecuteInsertByteA( array $params, array $expected ): void { - $result = $this->link->execute($insertSql, $params); + $result = $this->executor->execute($insertSql, $params); $this->assertSame(1, $result->getRowCount()); - $result = $this->link->execute($selectSql, $params); + $result = $this->executor->execute($selectSql, $params); $this->assertSame($expected, $result->fetchRow()); } } diff --git a/test/PgSqlConnectionTest.php b/test/PgSqlConnectionTest.php index 61d1657..5287866 100644 --- a/test/PgSqlConnectionTest.php +++ b/test/PgSqlConnectionTest.php @@ -4,7 +4,8 @@ use Amp\Postgres\ByteA; use Amp\Postgres\PgSqlConnection; -use Amp\Postgres\PostgresLink; +use Amp\Postgres\PostgresConfig; +use Amp\Postgres\PostgresExecutor; use Revolt\EventLoop; use function Amp\Postgres\Internal\cast; @@ -15,7 +16,7 @@ class PgSqlConnectionTest extends AbstractConnectionTest { protected ?\PgSql\Connection $handle = null; - public function createLink(string $connectionString): PostgresLink + public function createExecutor(string $connectionString): PostgresExecutor { if (EventLoop::getDriver()->getHandle() instanceof \EvLoop) { $this->markTestSkipped("ext-pgsql is not compatible with pecl-ev"); @@ -39,7 +40,13 @@ public function createLink(string $connectionString): PostgresLink } } - return $this->newConnection(PgSqlConnection::class, $this->handle, $socket, 'mock-connection'); + return $this->newConnection( + PgSqlConnection::class, + $this->handle, + $socket, + 'mock-connection', + PostgresConfig::fromString($connectionString), + ); } private function cast(mixed $param): mixed diff --git a/test/PgSqlNestedTransactionTest.php b/test/PgSqlNestedTransactionTest.php index 36fda6f..1043445 100644 --- a/test/PgSqlNestedTransactionTest.php +++ b/test/PgSqlNestedTransactionTest.php @@ -4,8 +4,7 @@ use Amp\Postgres\PgSqlConnection; use Amp\Postgres\PostgresConfig; -use Amp\Postgres\PostgresLink; -use Amp\Postgres\PostgresNestableTransaction; +use Amp\Postgres\PostgresExecutor; use Amp\Postgres\PostgresTransaction; /** @@ -16,7 +15,7 @@ class PgSqlNestedTransactionTest extends AbstractLinkTest protected PgSqlConnection $connection; protected PostgresTransaction $transaction; - public function createLink(string $connectionString): PostgresLink + public function createExecutor(string $connectionString): PostgresExecutor { $connectionConfig = PostgresConfig::fromString($connectionString); $connection = PgSqlConnection::connect($connectionConfig); @@ -32,7 +31,7 @@ public function createLink(string $connectionString): PostgresLink $this->connection = $connection; $this->transaction = $connection->beginTransaction(); - return new PostgresNestableTransaction($this->transaction); + return $this->transaction->beginTransaction(); } public function tearDown(): void diff --git a/test/PgSqlPoolTest.php b/test/PgSqlPoolTest.php index 91843a5..7004d49 100644 --- a/test/PgSqlPoolTest.php +++ b/test/PgSqlPoolTest.php @@ -6,7 +6,7 @@ use Amp\Postgres\PgSqlConnection; use Amp\Postgres\PostgresConfig; use Amp\Postgres\PostgresConnectionPool; -use Amp\Postgres\PostgresLink; +use Amp\Postgres\PostgresExecutor; use Amp\Sql\Common\ConnectionPool; use Amp\Sql\SqlConnector; use Revolt\EventLoop; @@ -22,7 +22,7 @@ class PgSqlPoolTest extends AbstractConnectionTest /** @var \PgSql\Connection[] PostgreSQL connection resources. */ protected array $handles = []; - public function createLink(string $connectionString): PostgresLink + public function createExecutor(string $connectionString): PostgresExecutor { if (EventLoop::getDriver()->getHandle() instanceof \EvLoop) { $this->markTestSkipped("ext-pgsql is not compatible with pecl-ev"); @@ -32,16 +32,24 @@ public function createLink(string $connectionString): PostgresLink $this->handles[] = \pg_connect($connectionString, \PGSQL_CONNECT_FORCE_NEW); } + $config = PostgresConfig::fromString($connectionString); + $connector = $this->createMock(SqlConnector::class); $connector->method('connect') - ->willReturnCallback(function (): PgSqlConnection { + ->willReturnCallback(function () use ($config): PgSqlConnection { static $count = 0; if (!isset($this->handles[$count])) { $this->fail("createConnection called too many times"); } $handle = $this->handles[$count]; ++$count; - return $this->newConnection(PgSqlConnection::class, $handle, \pg_socket($handle), 'mock-connection'); + return $this->newConnection( + PgSqlConnection::class, + $handle, + \pg_socket($handle), + 'mock-connection', + $config, + ); }); $pool = new PostgresConnectionPool(new PostgresConfig('localhost'), \count($this->handles), ConnectionPool::DEFAULT_IDLE_TIMEOUT, true, $connector); diff --git a/test/PqConnectionTest.php b/test/PqConnectionTest.php index 10a26e4..6eee7d7 100644 --- a/test/PqConnectionTest.php +++ b/test/PqConnectionTest.php @@ -5,7 +5,8 @@ use Amp\Postgres\ByteA; use Amp\Postgres\Internal\PqBufferedResultSet; use Amp\Postgres\Internal\PqUnbufferedResultSet; -use Amp\Postgres\PostgresLink; +use Amp\Postgres\PostgresConfig; +use Amp\Postgres\PostgresExecutor; use Amp\Postgres\PqConnection; use function Amp\Postgres\Internal\cast; @@ -17,7 +18,7 @@ class PqConnectionTest extends AbstractConnectionTest /** @var \pg\Connection|null PostgreSQL connection resource. */ protected ?\pq\Connection $handle; - public function createLink(string $connectionString): PostgresLink + public function createExecutor(string $connectionString): PostgresExecutor { $this->handle = new \pq\Connection($connectionString); $this->handle->nonblocking = true; @@ -39,7 +40,7 @@ public function createLink(string $connectionString): PostgresLink } } - return $this->newConnection(PqConnection::class, $this->handle); + return $this->newConnection(PqConnection::class, $this->handle, PostgresConfig::fromString($connectionString)); } private function cast(mixed $param): mixed @@ -60,12 +61,12 @@ public function tearDown(): void public function testBufferedResults(): void { - \assert($this->link instanceof PqConnection); - $this->link->shouldBufferResults(); + \assert($this->executor instanceof PqConnection); + $this->executor->shouldBufferResults(); - $this->assertTrue($this->link->isBufferingResults()); + $this->assertTrue($this->executor->isBufferingResults()); - $result = $this->link->query("SELECT * FROM test"); + $result = $this->executor->query("SELECT * FROM test"); \assert($result instanceof PqBufferedResultSet); $data = $this->getData(); @@ -77,12 +78,12 @@ public function testBufferedResults(): void */ public function testUnbufferedResults(): void { - \assert($this->link instanceof PqConnection); - $this->link->shouldNotBufferResults(); + \assert($this->executor instanceof PqConnection); + $this->executor->shouldNotBufferResults(); - $this->assertFalse($this->link->isBufferingResults()); + $this->assertFalse($this->executor->isBufferingResults()); - $result = $this->link->query("SELECT * FROM test"); + $result = $this->executor->query("SELECT * FROM test"); \assert($result instanceof PqUnbufferedResultSet); $data = $this->getData(); @@ -91,7 +92,7 @@ public function testUnbufferedResults(): void public function testNextResultBeforeConsumption() { - $result = $this->link->query("SELECT * FROM test; SELECT * FROM test;"); + $result = $this->executor->query("SELECT * FROM test; SELECT * FROM test;"); $result = $result->getNextResult(); @@ -100,11 +101,11 @@ public function testNextResultBeforeConsumption() public function testUnconsumedMultiResult() { - $result = $this->link->query("SELECT * FROM test; SELECT * FROM test"); + $result = $this->executor->query("SELECT * FROM test; SELECT * FROM test"); unset($result); - $result = $this->link->query("SELECT * FROM test; SELECT * FROM test"); + $result = $this->executor->query("SELECT * FROM test; SELECT * FROM test"); $this->verifyResult($result, $this->getData()); diff --git a/test/PqNestedTransactionTest.php b/test/PqNestedTransactionTest.php index edc9178..30b2b3b 100644 --- a/test/PqNestedTransactionTest.php +++ b/test/PqNestedTransactionTest.php @@ -3,8 +3,7 @@ namespace Amp\Postgres\Test; use Amp\Postgres\PostgresConfig; -use Amp\Postgres\PostgresLink; -use Amp\Postgres\PostgresNestableTransaction; +use Amp\Postgres\PostgresExecutor; use Amp\Postgres\PostgresTransaction; use Amp\Postgres\PqConnection; @@ -15,7 +14,7 @@ class PqNestedTransactionTest extends AbstractLinkTest { protected PostgresTransaction $transaction; - public function createLink(string $connectionString): PostgresLink + public function createExecutor(string $connectionString): PostgresExecutor { $connectionConfig = PostgresConfig::fromString($connectionString); $connection = PqConnection::connect($connectionConfig); @@ -30,7 +29,7 @@ public function createLink(string $connectionString): PostgresLink $this->transaction = $connection->beginTransaction(); - return new PostgresNestableTransaction($this->transaction); + return $this->transaction->beginTransaction(); } public function tearDown(): void diff --git a/test/PqPoolTest.php b/test/PqPoolTest.php index e16d307..f0d0a35 100644 --- a/test/PqPoolTest.php +++ b/test/PqPoolTest.php @@ -5,7 +5,7 @@ use Amp\Postgres\ByteA; use Amp\Postgres\PostgresConfig; use Amp\Postgres\PostgresConnectionPool; -use Amp\Postgres\PostgresLink; +use Amp\Postgres\PostgresExecutor; use Amp\Postgres\PqConnection; use Amp\Sql\Common\ConnectionPool; use Amp\Sql\SqlConnector; @@ -21,7 +21,7 @@ class PqPoolTest extends AbstractConnectionTest /** @var \pq\Connection[] */ protected array $handles = []; - public function createLink(string $connectionString): PostgresLink + public function createExecutor(string $connectionString): PostgresExecutor { for ($i = 0; $i < self::POOL_SIZE; ++$i) { $this->handles[] = $handle = new \pq\Connection($connectionString); @@ -29,16 +29,18 @@ public function createLink(string $connectionString): PostgresLink $handle->unbuffered = true; } + $config = PostgresConfig::fromString($connectionString); + $connector = $this->createMock(SqlConnector::class); $connector->method('connect') - ->willReturnCallback(function (): PqConnection { + ->willReturnCallback(function () use ($config): PqConnection { static $count = 0; if (!isset($this->handles[$count])) { $this->fail("createConnection called too many times"); } $handle = $this->handles[$count]; ++$count; - return $this->newConnection(PqConnection::class, $handle); + return $this->newConnection(PqConnection::class, $handle, $config); }); $pool = new PostgresConnectionPool(new PostgresConfig('localhost'), \count($this->handles), ConnectionPool::DEFAULT_IDLE_TIMEOUT, true, $connector);