-
Notifications
You must be signed in to change notification settings - Fork 590
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add php client test into ci pipeline (#14649)
- Loading branch information
Showing
6 changed files
with
199 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
FROM php:cli-bullseye | ||
|
||
# install phpunit for test | ||
RUN curl -fsSL -o /usr/local/bin/phpunit https://phar.phpunit.de/phpunit-10.phar && \ | ||
chmod +x /usr/local/bin/phpunit | ||
|
||
# install pdo-pgsql extension | ||
RUN apt-get update && \ | ||
apt-get install -y libpq-dev && \ | ||
docker-php-ext-install pdo_pgsql | ||
|
||
CMD ["php", "-a"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php declare(strict_types=1); | ||
class RWPDO extends PDO { | ||
public function __construct($host = 'localhost', $db = 'dev', $port = 4566, $user = 'root', $password = '') { | ||
$dsn = "pgsql:host=$host;dbname=$db;port=$port"; | ||
parent::__construct($dsn, $user, $password); | ||
print("connect risingwave!\n"); | ||
} | ||
} | ||
?> |
98 changes: 98 additions & 0 deletions
98
integration_tests/client-library/php/tests/RWClientTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php declare(strict_types=1); | ||
require 'RWPDO.php'; | ||
require 'util.php'; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class RWClientTest extends TestCase { | ||
public function testCrud() { | ||
$rw = new RWPDO(host: "risingwave-standalone"); | ||
createTable($rw); | ||
$name = 'John Doe'; | ||
$age = 30; | ||
$salary = 50000; | ||
$tripIDs = ['12345', '67890']; | ||
$fareData = [ | ||
"initial_charge" => 3.0, | ||
"subsequent_charge" => 1.5, | ||
"surcharge" => 0.5, | ||
"tolls" => 2.0, | ||
]; | ||
$deci = 3.14159; | ||
$birthdate = new DateTime("1993-01-02"); | ||
$starttime = new DateTime("20:00:00"); | ||
$timest = new DateTime(); | ||
$timestz = new DateTime(timezone: new DateTimeZone('UTC')); | ||
$timegap = DateInterval::createFromDateString('2 hours'); | ||
insertData($rw, $name, $age, $salary, $tripIDs, $birthdate, $deci, $fareData, $starttime, $timest, $timestz, $timegap); | ||
$this->checkData($rw, $name, $age, $salary, $tripIDs, $birthdate, $deci, $fareData, $starttime, $timest, $timestz, $timegap); | ||
|
||
// Insert data with null values | ||
$nullName = "Null Person"; | ||
$nullAge = 0; | ||
$nullSalary = 0; | ||
$nullTripIDs = []; | ||
$nullFareData = []; | ||
$nullBirthdate = new DateTime('0001-01-01'); | ||
$nullStarttime = new DateTime('00:00:00'); | ||
$nullTimest = new DateTime('0001-01-01 00:00:00'); | ||
$nullTimestz = new DateTime('1970-01-01 00:00:00', timezone: new DateTimeZone('UTC')); | ||
$nullTimegap = DateInterval::createFromDateString('0 seconds'); | ||
$nullDeci = 0.0; | ||
insertData($rw, $nullName, $nullAge, $nullSalary, $nullTripIDs, $nullBirthdate, $nullDeci, $nullFareData, $nullStarttime, $nullTimest, $nullTimestz, $nullTimegap); | ||
$this->checkData($rw, $nullName, $nullAge, $nullSalary, $nullTripIDs, $nullBirthdate, $nullDeci, $nullFareData, $nullStarttime, $nullTimest, $nullTimestz, $nullTimegap); | ||
|
||
updateSalaryData($rw, $name, 60000); | ||
deleteDataByName($rw, $name); | ||
|
||
dropTable($rw); | ||
} | ||
|
||
function checkData(RWPDO &$rw, $name, int $age, int $salary, $tripIDs, DateTime $birthdate, $deci, $fareData, DateTime $starttime, DateTime $timest, DateTime $timestz, DateInterval $timegap) { | ||
$rw->exec('FLUSH;'); | ||
|
||
$query = 'SELECT name, age, salary, trip_id, birthdate, deci, fare, starttime, timest, timestz, timegap FROM sample_table_php WHERE name=:name'; | ||
$stmt = $rw->prepare($query); | ||
$stmt->execute([':name' => $name]); | ||
|
||
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { | ||
$retrievedName = $row['name']; | ||
$this->assertEquals($retrievedName, $name); | ||
|
||
$retrievedAge = $row['age']; | ||
$this->assertEquals($retrievedAge, $age); | ||
|
||
$retrievedSalary = $row['salary']; | ||
$this->assertEquals($retrievedSalary, $salary); | ||
|
||
$retrievedTripIDsStr = trim($row['trip_id'], '{}'); | ||
$retrievedTripIDs = []; | ||
if ($retrievedTripIDsStr !== '') { | ||
$retrievedTripIDs = explode(',', $retrievedTripIDsStr); | ||
} | ||
$this->assertEquals($retrievedTripIDs, $tripIDs); | ||
|
||
$retrievedBirthdate = new DateTime($row['birthdate']); | ||
$this->assertEquals($retrievedBirthdate, $birthdate); | ||
|
||
$retrievedDeci = (float)$row['deci']; | ||
$this->assertEquals($retrievedDeci, $deci); | ||
|
||
$retrievedFareData = $row['fare']; | ||
|
||
$retrievedStarttime = new DateTime($row['starttime']); | ||
$this->assertEquals($retrievedStarttime, $starttime); | ||
|
||
$retrievedTimest = new DateTime($row['timest']); | ||
$this->assertEquals($retrievedTimest, $timest); | ||
|
||
$retrievedTimestz = new DateTime($row['timestz']); | ||
$this->assertEquals($retrievedTimestz, $timestz); | ||
|
||
$retrievedTimegap = $row['timegap']; | ||
$this->assertEquals($retrievedTimegap, $timegap->format('%H:%I:%S')); | ||
} | ||
print("Data checked successfully.\n"); | ||
} | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php declare(strict_types=1); | ||
|
||
function createTable(RWPDO &$rw) { | ||
$query = <<<EOT | ||
CREATE TABLE sample_table_php ( | ||
name VARCHAR, | ||
age INTEGER, | ||
salary BIGINT, | ||
trip_id VARCHAR[], | ||
birthdate DATE, | ||
deci DOUBLE PRECISION, | ||
fare STRUCT < | ||
initial_charge DOUBLE PRECISION, | ||
subsequent_charge DOUBLE PRECISION, | ||
surcharge DOUBLE PRECISION, | ||
tolls DOUBLE PRECISION | ||
>, | ||
starttime TIME, | ||
timest TIMESTAMP, | ||
timestz TIMESTAMPTZ, | ||
timegap INTERVAL | ||
) | ||
EOT; | ||
$rw->exec($query); | ||
print("Create Table successfully!\n"); | ||
} | ||
|
||
function dropTable(RWPDO &$rw) { | ||
$rw->exec('DROP TABLE sample_table_php;'); | ||
print("Drop Table successfully!\n"); | ||
} | ||
|
||
function insertData(RWPDO &$rw, $name, int $age, int $salary, $tripIDs, DateTime $birthdate, $deci, $fareData, DateTime $starttime, DateTime $timest, DateTime $timestz, DateInterval $timegap) { | ||
$insertQuery = <<<EOT | ||
INSERT INTO sample_table_php (name, age, salary, trip_id, birthdate, deci, fare, starttime, timest, timestz, timegap) | ||
VALUES (?, ?, ?, ?, ?, ?, ROW(?, ?, ?, ?), ?, ?, ?, ?); | ||
EOT; | ||
$stmt = $rw->prepare($insertQuery); | ||
$stmt->execute([$name, $age, $salary, | ||
'{'.implode(',',$tripIDs).'}', | ||
$birthdate->format('Y-m-d'), | ||
$deci, | ||
$fareData['initial_charge'], $fareData['subsequent_charge'], $fareData['surcharge'], $fareData['tolls'], | ||
$starttime->format('H:i:s.u'), | ||
$timest->format('Y-m-d H:i:s.u'), | ||
$timestz->format('Y-m-d H:i:s.uP'), | ||
$timegap->format('%H:%I:%S') | ||
]); | ||
print("Data inserted successfully.\n"); | ||
} | ||
|
||
function updateSalaryData(RWPDO &$rw, $name, $salary) { | ||
$query = 'UPDATE sample_table_php SET salary=:salary WHERE name = :name;'; | ||
$stmt = $rw->prepare($query); | ||
$stmt->execute([':name' => $name, ':salary' => $salary]); | ||
print("Data updated successfully.\n"); | ||
} | ||
|
||
function deleteDataByName(RWPDO &$rw, $name) { | ||
$query = 'DELETE FROM sample_table_php WHERE name = :name;'; | ||
$stmt = $rw->prepare($query); | ||
$stmt->execute([':name' => $name]); | ||
print("Data deleted successfully.\n"); | ||
} | ||
|
||
?> |