1
0
mirror of https://github.com/danog/postgres.git synced 2024-11-30 04:29:12 +01:00

Update tests

This commit is contained in:
Aaron Piotrowski 2019-09-26 22:41:47 -05:00
parent 170266227a
commit d5f70b2f4f
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB
9 changed files with 407 additions and 455 deletions

View File

@ -1,6 +1,6 @@
The MIT License (MIT)
Copyright (c) 2016 amphp
Copyright (c) 2016-2019 amphp
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -25,8 +25,8 @@
"amphp/sql-common": "^1"
},
"require-dev": {
"amphp/phpunit-util": "^1",
"phpunit/phpunit": "^6",
"amphp/phpunit-util": "^1.1.2",
"phpunit/phpunit": "^7 || ^6",
"amphp/php-cs-fixer-config": "dev-master",
"phpstan/phpstan": "^0.9"
},

View File

@ -22,7 +22,4 @@
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<listeners>
<listener class="Amp\PHPUnit\LoopReset"/>
</listeners>
</phpunit>

View File

@ -4,15 +4,16 @@ namespace Amp\Postgres\Test;
use Amp\CancellationToken;
use Amp\CancellationTokenSource;
use Amp\Loop;
use Amp\CancelledException;
use Amp\PHPUnit\AsyncTestCase;
use Amp\Postgres\Connection;
use Amp\Postgres\ConnectionConfig as PostgresConnectionConfig;
use Amp\Promise;
use Amp\Sql\ConnectionConfig;
use Amp\Sql\FailureException;
use Amp\TimeoutCancellationToken;
use PHPUnit\Framework\TestCase;
abstract class AbstractConnectTest extends TestCase
abstract class AbstractConnectTest extends AsyncTestCase
{
/**
* @param ConnectionConfig $connectionConfig
@ -22,53 +23,47 @@ abstract class AbstractConnectTest extends TestCase
*/
abstract public function connect(ConnectionConfig $connectionConfig, CancellationToken $token = null): Promise;
public function testConnect()
public function testConnect(): \Generator
{
Loop::run(function () {
$connection = yield $this->connect(
PostgresConnectionConfig::fromString('host=localhost user=postgres'),
new TimeoutCancellationToken(100)
);
$this->assertInstanceOf(Connection::class, $connection);
});
}
/**
* @depends testConnect
* @expectedException \Amp\CancelledException
*/
public function testConnectCancellationBeforeConnect()
public function testConnectCancellationBeforeConnect(): Promise
{
Loop::run(function () {
$this->expectException(CancelledException::class);
$source = new CancellationTokenSource;
$token = $source->getToken();
$source->cancel();
$connection = yield $this->connect(PostgresConnectionConfig::fromString('host=localhost user=postgres'), $token);
});
return $this->connect(PostgresConnectionConfig::fromString('host=localhost user=postgres'), $token);
}
/**
* @depends testConnectCancellationBeforeConnect
*/
public function testConnectCancellationAfterConnect()
public function testConnectCancellationAfterConnect(): \Generator
{
Loop::run(function () {
$source = new CancellationTokenSource;
$token = $source->getToken();
$connection = yield $this->connect(PostgresConnectionConfig::fromString('host=localhost user=postgres'), $token);
$this->assertInstanceOf(Connection::class, $connection);
$source->cancel();
});
}
/**
* @depends testConnectCancellationBeforeConnect
* @expectedException \Amp\Sql\FailureException
*/
public function testConnectInvalidUser()
public function testConnectInvalidUser(): Promise
{
Loop::run(function () {
$connection = yield $this->connect(PostgresConnectionConfig::fromString('host=localhost user=invalid'), new TimeoutCancellationToken(100));
});
$this->expectException(FailureException::class);
return $this->connect(PostgresConnectionConfig::fromString('host=localhost user=invalid'), new TimeoutCancellationToken(100));
}
}

View File

@ -4,20 +4,22 @@ namespace Amp\Postgres\Test;
use Amp\Coroutine;
use Amp\Delayed;
use Amp\Iterator;
use Amp\Loop;
use Amp\PHPUnit\AsyncTestCase;
use Amp\Postgres\Link;
use Amp\Postgres\Listener;
use Amp\Postgres\QueryExecutionError;
use Amp\Postgres\ResultSet;
use Amp\Postgres\Transaction;
use Amp\Promise;
use Amp\Sql\CommandResult;
use Amp\Sql\QueryError;
use Amp\Sql\Statement;
use Amp\Sql\Transaction as SqlTransaction;
use Amp\Sql\TransactionError;
use PHPUnit\Framework\TestCase;
abstract class AbstractLinkTest extends TestCase
abstract class AbstractLinkTest extends AsyncTestCase
{
/** @var \Amp\Postgres\Connection */
protected $connection;
@ -44,12 +46,12 @@ abstract class AbstractLinkTest extends TestCase
public function setUp()
{
parent::setUp();
$this->connection = $this->createLink('host=localhost user=postgres');
}
public function testQueryWithTupleResult()
public function testQueryWithTupleResult(): \Generator
{
Loop::run(function () {
/** @var \Amp\Postgres\ResultSet $result */
$result = yield $this->connection->query("SELECT * FROM test");
@ -64,17 +66,17 @@ abstract class AbstractLinkTest extends TestCase
$this->assertSame($data[$i][0], $row['domain']);
$this->assertSame($data[$i][1], $row['tld']);
}
});
}
public function testQueryWithUnconsumedTupleResult()
public function testQueryWithUnconsumedTupleResult(): \Generator
{
Loop::run(function () {
/** @var \Amp\Postgres\ResultSet $result */
$result = yield $this->connection->query("SELECT * FROM test");
$this->assertInstanceOf(ResultSet::class, $result);
unset($result); // Force destruction of result object.
/** @var \Amp\Postgres\ResultSet $result */
$result = yield $this->connection->query("SELECT * FROM test");
@ -87,34 +89,28 @@ abstract class AbstractLinkTest extends TestCase
$this->assertSame($data[$i][0], $row['domain']);
$this->assertSame($data[$i][1], $row['tld']);
}
});
}
public function testQueryWithCommandResult()
public function testQueryWithCommandResult(): \Generator
{
Loop::run(function () {
/** @var CommandResult $result */
$result = yield $this->connection->query("INSERT INTO test VALUES ('canon', 'jp')");
$this->assertInstanceOf(CommandResult::class, $result);
$this->assertSame(1, $result->getAffectedRowCount());
});
}
/**
* @expectedException \Amp\Sql\QueryError
*/
public function testQueryWithEmptyQuery()
public function testQueryWithEmptyQuery(): Promise
{
Loop::run(function () {
/** @var \Amp\Sql\CommandResult $result */
$result = yield $this->connection->query('');
});
return $this->connection->query('');
}
public function testQueryWithSyntaxError()
public function testQueryWithSyntaxError(): \Generator
{
Loop::run(function () {
/** @var \Amp\Sql\CommandResult $result */
try {
$result = yield $this->connection->query("SELECT & FROM test");
@ -123,12 +119,10 @@ abstract class AbstractLinkTest extends TestCase
$diagnostics = $exception->getDiagnostics();
$this->assertArrayHasKey("sqlstate", $diagnostics);
}
});
}
public function testPrepare()
public function testPrepare(): \Generator
{
Loop::run(function () {
$query = "SELECT * FROM test WHERE domain=\$1";
/** @var Statement $statement */
@ -150,15 +144,13 @@ abstract class AbstractLinkTest extends TestCase
$this->assertSame($data[0], $row['domain']);
$this->assertSame($data[1], $row['tld']);
}
});
}
/**
* @depends testPrepare
*/
public function testPrepareWithNamedParams()
public function testPrepareWithNamedParams(): \Generator
{
Loop::run(function () {
$query = "SELECT * FROM test WHERE domain=:domain AND tld=:tld";
/** @var Statement $statement */
@ -180,15 +172,13 @@ abstract class AbstractLinkTest extends TestCase
$this->assertSame($data[0], $row['domain']);
$this->assertSame($data[1], $row['tld']);
}
});
}
/**
* @depends testPrepare
*/
public function testPrepareWithUnnamedParams()
public function testPrepareWithUnnamedParams(): \Generator
{
Loop::run(function () {
$query = "SELECT * FROM test WHERE domain=? AND tld=?";
/** @var Statement $statement */
@ -210,15 +200,13 @@ abstract class AbstractLinkTest extends TestCase
$this->assertSame($data[0], $row['domain']);
$this->assertSame($data[1], $row['tld']);
}
});
}
/**
* @depends testPrepare
*/
public function testPrepareWithNamedParamsWithDataAppearingAsNamedParam()
public function testPrepareWithNamedParamsWithDataAppearingAsNamedParam(): \Generator
{
Loop::run(function () {
$query = "SELECT * FROM test WHERE domain=:domain OR domain=':domain'";
/** @var Statement $statement */
@ -240,30 +228,27 @@ abstract class AbstractLinkTest extends TestCase
$this->assertSame($data[0], $row['domain']);
$this->assertSame($data[1], $row['tld']);
}
});
}
/**
* @depends testPrepare
* @expectedException \Amp\Postgres\QueryExecutionError
* @expectedExceptionMessage column "invalid" does not exist
*/
public function testPrepareInvalidQuery()
public function testPrepareInvalidQuery(): Promise
{
Loop::run(function () {
$this->expectException(QueryExecutionError::class);
$this->expectExceptionMessage('column "invalid" does not exist');
$query = "SELECT * FROM test WHERE invalid=\$1";
/** @var Statement $statement */
$statement = yield $this->connection->prepare($query);
});
return $this->connection->prepare($query);
}
/**
* @depends testPrepare
*/
public function testPrepareSameQuery()
public function testPrepareSameQuery(): \Generator
{
Loop::run(function () {
$sql = "SELECT * FROM test WHERE domain=\$1";
/** @var Statement $statement1 */
@ -291,15 +276,13 @@ abstract class AbstractLinkTest extends TestCase
$this->assertSame($data[0], $row['domain']);
$this->assertSame($data[1], $row['tld']);
}
});
}
/**
* @depends testPrepareSameQuery
*/
public function testSimultaneousPrepareSameQuery()
public function testSimultaneousPrepareSameQuery(): \Generator
{
Loop::run(function () {
$sql = "SELECT * FROM test WHERE domain=\$1";
$statement1 = $this->connection->prepare($sql);
@ -343,12 +326,10 @@ abstract class AbstractLinkTest extends TestCase
$this->assertSame($data[0], $row['domain']);
$this->assertSame($data[1], $row['tld']);
}
});
}
public function testPrepareSimilarQueryReturnsDifferentStatements()
public function testPrepareSimilarQueryReturnsDifferentStatements(): \Generator
{
Loop::run(function () {
/** @var Statement $statement1 */
$statement1 = $this->connection->prepare("SELECT * FROM test WHERE domain=\$1");
@ -364,22 +345,22 @@ abstract class AbstractLinkTest extends TestCase
$data = $this->getData()[0];
$results = yield [$statement1->execute([$data[0]]), $statement2->execute(['domain' => $data[0]])];
$results = [];
$results[] = yield Iterator\toArray(yield $statement1->execute([$data[0]]));
$results[] = yield Iterator\toArray(yield $statement2->execute(['domain' => $data[0]]));
foreach ($results as $result) {
/** @var \Amp\Postgres\ResultSet $result */
while (yield $result->advance()) {
$row = $result->getCurrent();
foreach ($result as $row) {
$this->assertSame($data[0], $row['domain']);
$this->assertSame($data[1], $row['tld']);
}
}
});
}
public function testPrepareThenExecuteWithUnconsumedTupleResult()
public function testPrepareThenExecuteWithUnconsumedTupleResult(): \Generator
{
Loop::run(function () {
/** @var Statement $statement */
$statement = yield $this->connection->prepare("SELECT * FROM test");
@ -388,6 +369,8 @@ abstract class AbstractLinkTest extends TestCase
$this->assertInstanceOf(ResultSet::class, $result);
unset($result); // Force destruction of result object.
/** @var \Amp\Postgres\ResultSet $result */
$result = yield $statement->execute();
@ -400,12 +383,10 @@ abstract class AbstractLinkTest extends TestCase
$this->assertSame($data[$i][0], $row['domain']);
$this->assertSame($data[$i][1], $row['tld']);
}
});
}
public function testExecute()
public function testExecute(): \Generator
{
Loop::run(function () {
$data = $this->getData()[0];
/** @var \Amp\Postgres\ResultSet $result */
@ -420,15 +401,13 @@ abstract class AbstractLinkTest extends TestCase
$this->assertSame($data[0], $row['domain']);
$this->assertSame($data[1], $row['tld']);
}
});
}
/**
* @depends testExecute
*/
public function testExecuteWithNamedParams()
public function testExecuteWithNamedParams(): \Generator
{
Loop::run(function () {
$data = $this->getData()[0];
/** @var \Amp\Postgres\ResultSet $result */
@ -446,36 +425,33 @@ abstract class AbstractLinkTest extends TestCase
$this->assertSame($data[0], $row['domain']);
$this->assertSame($data[1], $row['tld']);
}
});
}
/**
* @depends testExecute
* @expectedException \Error
* @expectedExceptionMessage Value for unnamed parameter at position 0 missing
*/
public function testExecuteWithInvalidParams()
public function testExecuteWithInvalidParams(): Promise
{
Loop::run(function () {
$result = yield $this->connection->execute("SELECT * FROM test WHERE domain=\$1");
});
$this->expectException(\Error::class);
$this->expectExceptionMessage("Value for unnamed parameter at position 0 missing");
return $this->connection->execute("SELECT * FROM test WHERE domain=\$1");
}
/**
* @depends testExecute
* @expectedException \Error
* @expectedExceptionMessage Value for named parameter 'domain' missing
*/
public function testExecuteWithInvalidNamedParams()
public function testExecuteWithInvalidNamedParams(): Promise
{
Loop::run(function () {
$result = yield $this->connection->execute("SELECT * FROM test WHERE domain=:domain", ['tld' => 'com']);
});
$this->expectException(\Error::class);
$this->expectExceptionMessage("Value for named parameter 'domain' missing");
return $this->connection->execute("SELECT * FROM test WHERE domain=:domain", ['tld' => 'com']);
}
/**
* @depends testQueryWithTupleResult
*/
public function testSimultaneousQuery()
public function testSimultaneousQuery(): Promise
{
$callback = \Amp\coroutine(function ($value) {
/** @var \Amp\Postgres\ResultSet $result */
@ -491,15 +467,13 @@ abstract class AbstractLinkTest extends TestCase
}
});
Loop::run(function () use ($callback) {
yield [$callback(0), $callback(1)];
});
return Promise\all([$callback(0), $callback(1)]);
}
/**
* @depends testSimultaneousQuery
*/
public function testSimultaneousQueryWithOneFailing()
public function testSimultaneousQueryWithOneFailing(): \Generator
{
$callback = \Amp\coroutine(function ($query) {
/** @var \Amp\Postgres\ResultSet $result */
@ -516,14 +490,14 @@ abstract class AbstractLinkTest extends TestCase
return $result;
});
$result = null;
try {
Loop::run(function () use (&$result, $callback) {
$successful = $callback("SELECT * FROM test");
$failing = $callback("SELECT & FROM test");
$result = yield $successful;
yield $failing;
});
} catch (QueryError $exception) {
$this->assertInstanceOf(ResultSet::class, $result);
return;
@ -532,7 +506,7 @@ abstract class AbstractLinkTest extends TestCase
$this->fail(\sprintf("Test did not throw an instance of %s", QueryError::class));
}
public function testSimultaneousQueryAndPrepare()
public function testSimultaneousQueryAndPrepare(): Promise
{
$promises = [];
$promises[] = new Coroutine((function () {
@ -564,12 +538,10 @@ abstract class AbstractLinkTest extends TestCase
}
})());
Loop::run(function () use ($promises) {
yield $promises;
});
return Promise\all($promises);
}
public function testSimultaneousPrepareAndExecute()
public function testSimultaneousPrepareAndExecute(): Promise
{
$promises[] = new Coroutine((function () {
/** @var Statement $statement */
@ -600,14 +572,11 @@ abstract class AbstractLinkTest extends TestCase
}
})());
Loop::run(function () use ($promises) {
yield $promises;
});
return Promise\all($promises);
}
public function testTransaction()
public function testTransaction(): \Generator
{
Loop::run(function () {
$isolation = SqlTransaction::ISOLATION_COMMITTED;
/** @var \Amp\Postgres\Transaction $transaction */
@ -628,10 +597,14 @@ abstract class AbstractLinkTest extends TestCase
$this->assertInstanceOf(ResultSet::class, $result);
unset($result); // Force destruction of result object.
$result = yield $transaction->execute("SELECT * FROM test WHERE domain=\$1 FOR UPDATE", [$data[0]]);
$this->assertInstanceOf(ResultSet::class, $result);
unset($result); // Force destruction of result object.
yield $transaction->rollbackTo('test');
yield $transaction->commit();
@ -645,12 +618,10 @@ abstract class AbstractLinkTest extends TestCase
} catch (TransactionError $exception) {
// Exception expected.
}
});
}
public function testListen()
public function testListen(): \Generator
{
Loop::run(function () {
$channel = "test";
/** @var \Amp\Postgres\Listener $listener */
$listener = yield $this->connection->listen($channel);
@ -673,15 +644,13 @@ abstract class AbstractLinkTest extends TestCase
}
$this->assertSame(2, $count);
});
}
/**
* @depends testListen
*/
public function testNotify()
public function testNotify(): \Generator
{
Loop::run(function () {
$channel = "test";
/** @var \Amp\Postgres\Listener $listener */
$listener = yield $this->connection->listen($channel);
@ -701,20 +670,17 @@ abstract class AbstractLinkTest extends TestCase
}
$this->assertSame(2, $count);
});
}
/**
* @depends testListen
* @expectedException \Amp\Sql\QueryError
* @expectedExceptionMessage Already listening on channel
*/
public function testListenOnSameChannel()
public function testListenOnSameChannel(): Promise
{
Loop::run(function () {
$this->expectException(QueryError::class);
$this->expectExceptionMessage('Already listening on channel');
$channel = "test";
$listener = yield $this->connection->listen($channel);
$listener = yield $this->connection->listen($channel);
});
return Promise\all([$this->connection->listen($channel), $this->connection->listen($channel)]);
}
}

View File

@ -2,8 +2,9 @@
namespace Amp\Postgres\Test;
use Amp\PHPUnit\TestCase;
use Amp\Postgres\Internal\ArrayParser;
use Amp\Postgres\ParseException;
use PHPUnit\Framework\TestCase;
class ArrayParserTest extends TestCase
{
@ -127,42 +128,38 @@ class ArrayParserTest extends TestCase
$this->assertSame($array, $this->parser->parse($string));
}
/**
* @expectedException \Amp\Postgres\ParseException
* @expectedExceptionMessage Missing opening or closing brackets
*/
public function testNoClosingBracket()
{
$this->expectException(ParseException::class);
$this->expectExceptionMessage('Missing opening or closing brackets');
$string = '{"one", "two"';
$this->parser->parse($string);
}
/**
* @expectedException \Amp\Postgres\ParseException
* @expectedExceptionMessage Data left in buffer after parsing
*/
public function testTrailingData()
{
$this->expectException(ParseException::class);
$this->expectExceptionMessage('Data left in buffer after parsing');
$string = '{"one", "two"} data}';
$this->parser->parse($string);
}
/**
* @expectedException \Amp\Postgres\ParseException
* @expectedExceptionMessage Could not find matching quote in quoted value
*/
public function testMissingQuote()
{
$this->expectException(ParseException::class);
$this->expectExceptionMessage('Could not find matching quote in quoted value');
$string = '{"one", "two}';
$this->parser->parse($string);
}
/**
* @expectedException \Amp\Postgres\ParseException
* @expectedExceptionMessage Invalid delimiter
*/
public function testInvalidDelimiter()
{
$this->expectException(ParseException::class);
$this->expectExceptionMessage('Invalid delimiter');
$string = '{"one"; "two"}';
$this->parser->parse($string);
}

View File

@ -2,8 +2,8 @@
namespace Amp\Postgres\Test;
use Amp\PHPUnit\TestCase;
use Amp\Postgres\ConnectionConfig;
use PHPUnit\Framework\TestCase;
class ConnectionConfigTest extends TestCase
{

View File

@ -2,7 +2,7 @@
namespace Amp\Postgres\Test;
use Amp\PHPUnit\TestCase;
use PHPUnit\Framework\TestCase;
use function Amp\Postgres\encode;
class EncodeTest extends TestCase
@ -79,12 +79,11 @@ class EncodeTest extends TestCase
$this->assertSame($string, encode($array));
}
/**
* @expectedException \Error
* @expectedExceptionMessage Object without a __toString() method in array
*/
public function testObjectWithoutToStringMethod()
{
$this->expectException(\Error::class);
$this->expectExceptionMessage('Object without a __toString() method in array');
encode([new \stdClass]);
}
}

View File

@ -2,36 +2,34 @@
namespace Amp\Postgres\Test;
use Amp\Loop;
use Amp\PHPUnit\AsyncTestCase;
use Amp\Postgres\Connection;
use Amp\Postgres\ConnectionConfig;
use PHPUnit\Framework\TestCase;
use Amp\Promise;
use Amp\Sql\FailureException;
use function Amp\Postgres\connect;
class FunctionsTest extends TestCase
class FunctionsTest extends AsyncTestCase
{
public function setUp()
{
parent::setUp();
if (!\extension_loaded('pgsql') && !\extension_loaded('pq')) {
$this->markTestSkipped('This test requires either ext/pgsql or pecl/pq');
}
}
public function testConnect()
public function testConnect(): \Generator
{
Loop::run(function () {
$connection = yield connect(ConnectionConfig::fromString('host=localhost user=postgres'));
$this->assertInstanceOf(Connection::class, $connection);
});
}
/**
* @expectedException \Amp\Sql\FailureException
*/
public function testConnectInvalidUser()
public function testConnectInvalidUser(): Promise
{
Loop::run(function () {
$connection = yield connect(ConnectionConfig::fromString('host=localhost user=invalid'));
});
$this->expectException(FailureException::class);
return connect(ConnectionConfig::fromString('host=localhost user=invalid'));
}
}