From db7b8eb19ecac87fd8c4b7b4a6af42f895bd17c2 Mon Sep 17 00:00:00 2001 From: xfz <73645462+xuefengze@users.noreply.github.com> Date: Thu, 18 Jan 2024 16:09:43 +0800 Subject: [PATCH] test: add php client test into ci pipeline (#14649) --- .../client-library/client_test.py | 9 +- .../client-library/docker-compose.yml | 6 ++ .../client-library/php/Dockerfile | 12 +++ .../client-library/php/RWPDO.php | 9 ++ .../client-library/php/tests/RWClientTest.php | 98 +++++++++++++++++++ integration_tests/client-library/php/util.php | 66 +++++++++++++ 6 files changed, 199 insertions(+), 1 deletion(-) create mode 100644 integration_tests/client-library/php/Dockerfile create mode 100644 integration_tests/client-library/php/RWPDO.php create mode 100644 integration_tests/client-library/php/tests/RWClientTest.php create mode 100644 integration_tests/client-library/php/util.php diff --git a/integration_tests/client-library/client_test.py b/integration_tests/client-library/client_test.py index 8654d6501e45..40ffd04747c9 100644 --- a/integration_tests/client-library/client_test.py +++ b/integration_tests/client-library/client_test.py @@ -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': @@ -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") @@ -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) diff --git a/integration_tests/client-library/docker-compose.yml b/integration_tests/client-library/docker-compose.yml index fe19f52b4282..3567269fceb7 100644 --- a/integration_tests/client-library/docker-compose.yml +++ b/integration_tests/client-library/docker-compose.yml @@ -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: diff --git a/integration_tests/client-library/php/Dockerfile b/integration_tests/client-library/php/Dockerfile new file mode 100644 index 000000000000..ddf382671259 --- /dev/null +++ b/integration_tests/client-library/php/Dockerfile @@ -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"] diff --git a/integration_tests/client-library/php/RWPDO.php b/integration_tests/client-library/php/RWPDO.php new file mode 100644 index 000000000000..9b7fe6a1d9e3 --- /dev/null +++ b/integration_tests/client-library/php/RWPDO.php @@ -0,0 +1,9 @@ + diff --git a/integration_tests/client-library/php/tests/RWClientTest.php b/integration_tests/client-library/php/tests/RWClientTest.php new file mode 100644 index 000000000000..996da241394d --- /dev/null +++ b/integration_tests/client-library/php/tests/RWClientTest.php @@ -0,0 +1,98 @@ + 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"); + } +} + +?> diff --git a/integration_tests/client-library/php/util.php b/integration_tests/client-library/php/util.php new file mode 100644 index 000000000000..5ce81db6d68f --- /dev/null +++ b/integration_tests/client-library/php/util.php @@ -0,0 +1,66 @@ +, + 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 = <<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"); +} + +?>