Skip to content

Commit

Permalink
Add JSON column tests
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Sep 3, 2023
1 parent 2aca4a8 commit 26ac664
Showing 1 changed file with 36 additions and 8 deletions.
44 changes: 36 additions & 8 deletions test/AbstractLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ abstract class AbstractLinkTest extends AsyncTestCase
number DOUBLE PRECISION NOT NULL,
nullable CHAR(1) DEFAULT NULL,
bytea BYTEA DEFAULT NULL,
json JSON DEFAULT NULL,
PRIMARY KEY (domain, tld))";
protected const DROP_QUERY = "DROP TABLE IF EXISTS test";
protected const INSERT_QUERY = 'INSERT INTO test VALUES ($1, $2, $3, $4, $5, $6, $7)';
protected const FIELD_COUNT = 7;
protected const INSERT_QUERY = 'INSERT INTO test VALUES ($1, $2, $3, $4, $5, $6, $7, $8)';
protected const FIELD_COUNT = 8;

protected PostgresLink $link;

Expand All @@ -37,10 +38,10 @@ abstract class AbstractLinkTest extends AsyncTestCase
protected function getParams(): array
{
return $this->data ??= [
['amphp', 'org', [1], true, 3.14159, null, new ByteA(\random_bytes(10))],
['github', 'com', [1, 2, 3, 4, 5], false, 2.71828, null, new ByteA(\str_repeat("\0", 10))],
['google', 'com', [1, 2, 3, 4], true, 1.61803, null, new ByteA(\random_bytes(42))],
['php', 'net', [1, 2], false, 0.0, null, null],
['amphp', 'org', [1], true, 3.14159, null, new ByteA(\random_bytes(10)), \json_encode('string')],
['github', 'com', [1, 2, 3, 4, 5], false, 2.71828, null, new ByteA(\str_repeat("\0", 10)), \json_encode([1, 2, 3])],
['google', 'com', [1, 2, 3, 4], true, 1.61803, null, new ByteA(\random_bytes(42)), \json_encode(null)],
['php', 'net', [1, 2], false, 0.0, null, null, \json_encode((object) ['value' => 1])],
];
}

Expand All @@ -67,6 +68,7 @@ protected function verifyResult(Result $result, array $data): void
$this->assertSame($data[$i][3], $row['enabled']);
$this->assertEqualsWithDelta($data[$i][4], $row['number'], 0.001);
$this->assertNull($row['nullable']);
$this->assertEquals(\json_decode($data[$i][7]), \json_decode($row['json']));
++$i;
}
}
Expand Down Expand Up @@ -148,7 +150,7 @@ public function testMultipleQueryWithTupleResult()

public function testMultipleQueryWithCommandResultFirst()
{
$result = $this->link->query("INSERT INTO test VALUES ('canon', 'jp', '{1}', true, 4.2); SELECT * FROM test");
$result = $this->link->query("INSERT INTO test VALUES ('canon', 'jp', '{1}', true, 4.2, null, null, '3.1415926'); SELECT * FROM test");

$this->assertSame(1, $result->getRowCount());

Expand All @@ -157,7 +159,7 @@ public function testMultipleQueryWithCommandResultFirst()
$this->assertInstanceOf(Result::class, $result);

$data = $this->getData();
$data[] = ['canon', 'jp', [1], true, 4.2]; // Add inserted row to expected data.
$data[] = ['canon', 'jp', [1], true, 4.2, null, null, \json_encode(3.1415926)]; // Add inserted row to expected data.

$this->verifyResult($result, $data);

Expand Down Expand Up @@ -236,6 +238,32 @@ public function testPrepare()
$this->verifyResult($result, [$data]);
}

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);

$fields = [
'domain' => 'canon',
'tld' => 'jp',
'keys' => [1],
'enabled' => true,
'number' => 1,
'nullable' => null,
'bytea' => null,
'json' => '[1,2,3]',
];

$result = $statement->execute($fields);

$this->assertSame(1, $result->getRowCount());

$result = $this->link->execute('SELECT * FROM test WHERE domain=? AND tld=?', ['canon', 'jp']);

$this->verifyResult($result, [\array_values($fields)]);
}

/**
* @depends testPrepare
*/
Expand Down

0 comments on commit 26ac664

Please sign in to comment.