Skip to content

Commit 2437c08

Browse files
authored
test: add ruby client test (#14859)
1 parent ded73af commit 2437c08

File tree

6 files changed

+198
-2
lines changed

6 files changed

+198
-2
lines changed

integration_tests/client-library/client_test.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,22 @@ def check_nodejs():
2525

2626

2727
def check_php():
28-
subprocess.run(["docker", "compose", "exec", "php", "bash", "-c", "cd /php-client && phpunit tests/RWClientTest.php"], check=True)
28+
subprocess.run(
29+
["docker", "compose", "exec", "php", "bash", "-c", "cd /php-client && phpunit tests/RWClientTest.php"],
30+
check=True)
31+
32+
33+
def check_ruby():
34+
subprocess.run(["docker", "compose", "exec", "ruby", "bash", "-c", "cd /ruby-client && ruby test/crud_test.rb"],
35+
check=True)
2936

3037

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

3441
failed_cases = []
3542

36-
for client in ['go', 'python', 'java', 'nodejs', 'php']:
43+
for client in ['go', 'python', 'java', 'nodejs', 'php', 'ruby']:
3744
print(f"--- {client} client test")
3845
try:
3946
if client == 'go':
@@ -46,6 +53,8 @@ def check_php():
4653
check_nodejs()
4754
elif client == 'php':
4855
check_php()
56+
elif client == 'ruby':
57+
check_ruby()
4958
except Exception as e:
5059
print(e)
5160
failed_cases.append(f"{client} client failed")

integration_tests/client-library/docker-compose.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ services:
4848
command: tail -f /dev/null
4949
volumes:
5050
- ./php:/php-client
51+
ruby:
52+
image: ruby-library
53+
build: ./ruby
54+
command: tail -f /dev/null
55+
volumes:
56+
- ./ruby:/ruby-client
5157

5258
volumes:
5359
risingwave-standalone:
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM ruby:3.2-slim-bullseye
2+
3+
# install ruby-pg
4+
RUN apt-get update && \
5+
apt-get install -y libpq-dev && \
6+
apt-get install -y build-essential && \
7+
gem install pg
8+
9+
CMD ["irb"]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env ruby
2+
require 'pg'
3+
4+
def get_rw_conn(host: 'localhost', port: 4566, options: '', tty: '', dbname: 'dev', user: 'root', password: '')
5+
conn = PG.connect(host: host, port: port, options: options, tty: tty, dbname: dbname, user: user, password: password)
6+
# https://github.com/risingwavelabs/risingwave/issues/14682
7+
# conn.type_map_for_results = PG::BasicTypeMapForResults.new(conn)
8+
conn
9+
end
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/usr/bin/env ruby
2+
require_relative '../rw_conn'
3+
require_relative 'util'
4+
require 'date'
5+
require 'test/unit'
6+
7+
class SQLTest < Test::Unit::TestCase
8+
def setup
9+
@conn = get_rw_conn(host: 'risingwave-standalone')
10+
end
11+
12+
def test_crud
13+
create_table(@conn)
14+
15+
name = 'John Doe'
16+
age = 30
17+
salary = 50000
18+
tripIDs = ['12345', '67890']
19+
fareData = {
20+
"initial_charge" => 3.0,
21+
"subsequent_charge" => 1.5,
22+
"surcharge" => 0.5,
23+
"tolls" => 2.0,
24+
}
25+
deci = 2.14159
26+
birthdate = Date.parse('1993-01-02')
27+
starttime = "20:00:00"
28+
timest = DateTime.now()
29+
timestz = DateTime.now()
30+
timegap = '02:00:00'
31+
32+
insert_data(@conn, name, age, salary, tripIDs, birthdate, deci, fareData, starttime, timest, timestz, timegap)
33+
check_data(name, age, salary, tripIDs, birthdate, deci, fareData, starttime, timest, timestz, timegap)
34+
35+
# Insert data with null values
36+
nullName = "Null Person";
37+
nullAge = 0
38+
nullSalary = 0
39+
nullTripIDs = []
40+
nullFareData = {}
41+
nullBirthdate = Date.parse('0001-01-01')
42+
nullStarttime = '00:00:00'
43+
nullTimest = DateTime.parse('0001-01-01 00:00:00')
44+
nullTimestz = DateTime.parse('1970-01-01T00:00:00Z')
45+
nullTimegap = '00:00:00';
46+
nullDeci = 0.0;
47+
48+
insert_data(@conn, nullName, nullAge, nullSalary, nullTripIDs, nullBirthdate, nullDeci, nullFareData, nullStarttime, nullTimest, nullTimestz, nullTimegap)
49+
check_data(nullName, nullAge, nullSalary, nullTripIDs, nullBirthdate, nullDeci, nullFareData, nullStarttime, nullTimest, nullTimestz, nullTimegap)
50+
51+
update_data(@conn, name, 60000)
52+
delete_data(@conn, name)
53+
drop_table(@conn)
54+
end
55+
56+
def check_data(name, age, salary, tripIDs, birthdate, deci, fareData, starttime, timest, timestz, timegap)
57+
@conn.exec('FLUSH;')
58+
59+
select_query = "SELECT name, age, salary, trip_id, birthdate, deci, fare, starttime, timest, timestz, timegap FROM sample_table_ruby WHERE name=$1"
60+
61+
res = @conn.exec_params(select_query, [name])
62+
63+
res.each do |row|
64+
retrievedName = row['name']
65+
assert_equal(name, retrievedName)
66+
67+
retrievedAge = row['age'].to_i
68+
assert_equal(age, retrievedAge)
69+
70+
retrievedSalary = row['salary'].to_i
71+
assert_equal(salary, retrievedSalary)
72+
73+
retrievedTripIDs = row['trip_id']
74+
assert_equal('{' + tripIDs.join(',') + '}', retrievedTripIDs)
75+
76+
retrievedBirthdate = row['birthdate']
77+
assert_equal(birthdate, Date.parse(retrievedBirthdate))
78+
79+
retrievedDeci = row['deci']
80+
assert_equal(deci, retrievedDeci.to_f)
81+
82+
retrievedFareData = row['fare']
83+
84+
retrievedStarttime = row['starttime']
85+
assert_equal(starttime, retrievedStarttime)
86+
87+
retrievedTimest = row['timest']
88+
assert_equal(timest.strftime('%Y-%m-%d %H:%M:%S%z'), DateTime.parse(retrievedTimest).strftime('%Y-%m-%d %H:%M:%S%z'))
89+
90+
retrievedTimestz = row['timestz']
91+
assert_equal(timestz.strftime('%Y-%m-%dT%H:%M:%S%zZ'), DateTime.parse(retrievedTimestz).strftime('%Y-%m-%dT%H:%M:%S%zZ'))
92+
93+
retrievedTimegap = row['timegap']
94+
assert_equal(timegap, retrievedTimegap)
95+
end
96+
puts "Data checked successfully."
97+
end
98+
end
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env ruby
2+
3+
def create_table(conn)
4+
query = <<-EOF
5+
CREATE TABLE sample_table_ruby
6+
(
7+
name VARCHAR,
8+
age INTEGER,
9+
salary BIGINT,
10+
trip_id VARCHAR[],
11+
birthdate DATE,
12+
deci DOUBLE PRECISION,
13+
fare STRUCT < initial_charge DOUBLE PRECISION,
14+
subsequent_charge DOUBLE PRECISION,
15+
surcharge DOUBLE PRECISION,
16+
tolls DOUBLE PRECISION >,
17+
starttime TIME,
18+
timest TIMESTAMP,
19+
timestz TIMESTAMPTZ,
20+
timegap INTERVAL
21+
)
22+
EOF
23+
conn.exec(query)
24+
puts "Table created successfully."
25+
end
26+
27+
def drop_table(conn)
28+
conn.exec('DROP TABLE sample_table_ruby;')
29+
puts "Table dropped successfully."
30+
end
31+
32+
def insert_data(conn, name, age, salary, tripIDs, birthdate, deci, fareData, starttime, timest, timestz, timegap)
33+
insert_query = <<-EOF
34+
INSERT INTO sample_table_ruby (name, age, salary, trip_id, birthdate, deci, fare, starttime, timest, timestz, timegap)
35+
VALUES ($1, $2, $3, $4, $5, $6, ROW($7, $8, $9, $10), $11, $12, $13, $14);
36+
EOF
37+
conn.exec_params(insert_query, [name, age, salary,
38+
"{#{tripIDs.join(',')}}",
39+
birthdate,
40+
deci,
41+
fareData["initial_charge"],
42+
fareData["subsequent_charge"],
43+
fareData["surcharge"],
44+
fareData["tolls"],
45+
starttime,
46+
timest.strftime('%Y-%m-%d %H:%M:%S'),
47+
timestz,
48+
timegap
49+
])
50+
51+
puts "Data inserted successfully."
52+
end
53+
54+
def update_data(conn, name, salary)
55+
update_query = "UPDATE sample_table_ruby SET salary=$1 WHERE name=$2"
56+
57+
conn.exec_params(update_query, [salary, name])
58+
puts "Data updated successfully."
59+
end
60+
61+
def delete_data(conn, name)
62+
conn.exec_params('DELETE FROM sample_table_ruby WHERE name=$1;', [name])
63+
64+
puts "Data deletion successfully."
65+
end

0 commit comments

Comments
 (0)