Skip to content

Commit

Permalink
test: add php client test into ci pipeline (#14649)
Browse files Browse the repository at this point in the history
  • Loading branch information
xuefengze authored Jan 18, 2024
1 parent 5df707d commit db7b8eb
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 1 deletion.
9 changes: 8 additions & 1 deletion integration_tests/client-library/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ def check_nodejs():
check=True)


def check_php():
subprocess.run(["docker", "compose", "exec", "php", "bash", "-c", "cd /php-client && phpunit tests/RWClientTest.php"], check=True)


subprocess.run(["docker", "compose", "up", "-d"], check=True)
sleep(10)

failed_cases = []

for client in ['go', 'python', 'java', 'nodejs']:
for client in ['go', 'python', 'java', 'nodejs', 'php']:
print(f"--- {client} client test")
try:
if client == 'go':
Expand All @@ -40,6 +44,8 @@ def check_nodejs():
check_java()
elif client == 'nodejs':
check_nodejs()
elif client == 'php':
check_php()
except Exception as e:
print(e)
failed_cases.append(f"{client} client failed")
Expand All @@ -48,4 +54,5 @@ def check_nodejs():
print(f"--- client check failed for case\n{failed_cases}")
sys.exit(1)

print("--- docker compose down")
subprocess.run(["docker", "compose", "down", "--remove-orphans", "-v"], check=True)
6 changes: 6 additions & 0 deletions integration_tests/client-library/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ services:
command: tail -f /dev/null
volumes:
- ./nodejs:/nodejs-client
php:
image: php-library
build: ./php
command: tail -f /dev/null
volumes:
- ./php:/php-client

volumes:
risingwave-standalone:
Expand Down
12 changes: 12 additions & 0 deletions integration_tests/client-library/php/Dockerfile
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"]
9 changes: 9 additions & 0 deletions integration_tests/client-library/php/RWPDO.php
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 integration_tests/client-library/php/tests/RWClientTest.php
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");
}
}

?>
66 changes: 66 additions & 0 deletions integration_tests/client-library/php/util.php
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");
}

?>

0 comments on commit db7b8eb

Please sign in to comment.