Skip to content

Commit

Permalink
add smoke tests for perl, php and ruby (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
jennifersp committed Jun 11, 2024
1 parent 820b846 commit e255e50
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 1 deletion.
12 changes: 11 additions & 1 deletion testing/PostgresDockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM --platform=linux/amd64 ubuntu:20.04

# install python, libmysqlclient-dev, java, bats, git ruby, perl, cpan
# install python, java, bats, git ruby, perl, cpan
ENV DEBIAN_FRONTEND=noninteractive
RUN apt update -y && \
apt install -y \
Expand All @@ -23,6 +23,7 @@ RUN apt update -y && \
bats \
perl \
php \
php-pgsql \
cpanminus \
cmake \
g++ \
Expand Down Expand Up @@ -66,6 +67,15 @@ COPY ./testing/postgres-client-tests/node/package-lock.json /postgres-client-tes
WORKDIR /postgres-client-tests/node
RUN npm install

# install cpan dependencies
RUN cpanm --force DBD::Pg

# install ruby dependencies
COPY ./testing/postgres-client-tests/ruby/Gemfile /postgres-client-tests/ruby/
COPY ./testing/postgres-client-tests/ruby/Gemfile.lock /postgres-client-tests/ruby/
WORKDIR /postgres-client-tests/ruby
RUN gem install bundler -v 2.1.4 && bundle install

# install postgres and psql
RUN service postgresql start

Expand Down
45 changes: 45 additions & 0 deletions testing/postgres-client-tests/perl/postgres-test.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use strict;

use DBI;

my $QUERY_RESPONSE = [
{ "create table test (pk int, value int, primary key(pk))" => '0E0' },
{ "insert into test (pk, value) values (0,0)" => 1 },
{ "select * from test" => 1 },
{"call dolt_add('-A');" => '0E0' },
{"call dolt_commit('-m', 'my commit')" => '0E0' },
{"call dolt_checkout('-b', 'mybranch')" => '0E0' },
{"insert into test (pk, value) values (1,1)" => 1 },
{"call dolt_commit('-a', '-m', 'my commit2')" => '0E0' },
{"call dolt_checkout('main')" => '0E0' },
{"call dolt_merge('mybranch')" => '0E0' },
{"select COUNT(*) FROM dolt_log" => 1 },
];

my $user = $ARGV[0];
my $port = $ARGV[1];
my $db = "doltgres";

my $dsn = "DBI:Pg:database=$db;host=127.0.0.1;port=$port";
# Connect to the database
my $dbh = DBI->connect($dsn, $user, "", { PrintError => 0, RaiseError => 1 });
die "failed to connect to database:DBI->errstr()" unless($dbh);

foreach my $query_response ( @{$QUERY_RESPONSE} ) {
my @query_keys = keys %{$query_response};
my $query = $query_keys[0];
my $exp_result = $query_response->{$query};

my $result = $dbh->do($query);
if ( $result != $exp_result ) {
print "QUERY: $query\n";
print "EXPECTED: $exp_result\n";
print "RESULT: $result\n";
exit 1
}
}

# Disconnect from the database
$dbh->disconnect();

exit 0;
44 changes: 44 additions & 0 deletions testing/postgres-client-tests/php/pdo_connector_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
$user = $argv[1];
$port = $argv[2];
$db = 'doltgres';

$conn = new PDO("pgsql:host=localhost;port={$port};dbname={$db}", $user, '');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$queries = [
"create table test (pk int, value int, d1 decimal(9, 3), f1 float, primary key(pk))" => 0,
"insert into test (pk, value, d1, f1) values (0,0,0.0,0.0)" => 1,
"select * from test" => 1,
"call dolt_add('-A');" => 0,
"call dolt_commit('-m', 'my commit')" => 0,
"call dolt_checkout('-b', 'mybranch')" => 0,
"insert into test (pk, value, d1, f1) values (1,1, 123456.789, 420.42)" => 1,
"call dolt_commit('-a', '-m', 'my commit2')" => 0,
"call dolt_checkout('main')" => 0,
"call dolt_merge('mybranch')" => 0,
"select COUNT(*) FROM dolt_log" => 1
];

foreach ($queries as $query => $expected) {
$result = $conn->query($query);
if ($result->rowCount() != $expected) {
echo "LENGTH: {$result->rowCount()}\n";
echo "QUERY: {$query}\n";
echo "EXPECTED: {$expected}\n";
echo "RESULT: {$result}";
exit(1);
}
}

$result = $conn->query("SELECT * FROM test WHERE pk = 1");
assert(1 == $result->rowCount());
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
assert(1 == $row['pk']);
assert(1 == $row['value']);
assert(123456.789 == $row['d1']);
assert(420.42 == $row['f1']);
}

exit(0)
?>
54 changes: 54 additions & 0 deletions testing/postgres-client-tests/php/pg_connect_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
$user = $argv[1];
$port = $argv[2];
$db = 'doltgres';

$conn = pg_connect("host = localhost port = $port dbname = $db user = $user")
or die('Could not connect: ' . pg_result_error());

$queries = [
"create table test (pk int, value int, d1 decimal(9, 3), f1 float, primary key(pk))" => 0,
"insert into test (pk, value, d1, f1) values (0,0,0.0,0.0)" => 0,
"select * from test" => 1,
"call dolt_add('-A');" => 0,
"call dolt_commit('-m', 'my commit')" => 0,
"call dolt_checkout('-b', 'mybranch')" => 0,
"insert into test (pk, value, d1, f1) values (1,1, 123456.789, 420.42)" => 0,
"call dolt_commit('-a', '-m', 'my commit2')" => 0,
"call dolt_checkout('main')" => 0,
"call dolt_merge('mybranch')" => 0,
"select COUNT(*) FROM dolt_log" => 1
];

foreach ($queries as $query => $expected) {
$result = pg_query($conn, $query);
if (is_bool($result)) {
if (!$result) {
echo "LENGTH: {pg_num_rows($result)}\n";
echo "QUERY: {$query}\n";
echo "EXPECTED: {$expected}\n";
echo "RESULT: {$result}";
exit(1);
}
} else if (pg_num_rows($result) != $expected) {
echo "LENGTH: {pg_num_rows($result)}\n";
echo "QUERY: {$query}\n";
echo "EXPECTED: {$expected}\n";
echo "RESULT: {$result}";
exit(1);
}
}

$result = pg_query($conn, "SELECT * FROM test WHERE pk = 1");
assert(1 == pg_num_rows($result));
while($row = pg_fetch_assoc($result)) {
assert(1 == $row['pk']);
assert(1 == $row['value']);
assert(123456.789 == $row['d1']);
assert(420.42 == $row['f1']);
}

pg_close($conn);

exit(0)
?>
18 changes: 18 additions & 0 deletions testing/postgres-client-tests/postgres-client-tests.bats
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,21 @@ teardown() {
echo $DOLTGRES_VERSION
node $BATS_TEST_DIRNAME/node/knex.js $USER $PORT $DOLTGRES_VERSION
}

@test "perl DBI:Pg client" {
perl $BATS_TEST_DIRNAME/perl/postgres-test.pl $USER $PORT
}

@test "ruby pg test" {
ruby $BATS_TEST_DIRNAME/ruby/pg-test.rb $USER $PORT
}

@test "php pg_connect client" {
cd $BATS_TEST_DIRNAME/php
php pg_connect_test.php $USER $PORT
}

@test "php pdo pgsql client" {
cd $BATS_TEST_DIRNAME/php
php pdo_connector_test.php $USER $PORT
}
3 changes: 3 additions & 0 deletions testing/postgres-client-tests/ruby/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source "https://rubygems.org"
gem 'pg'
gem 'test'
18 changes: 18 additions & 0 deletions testing/postgres-client-tests/ruby/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
GEM
remote: https://rubygems.org/
specs:
pg (1.5.6)
rubytest (0.8.1)
test (1.0.0)
rubytest

PLATFORMS
ruby
x86_64-darwin-23

DEPENDENCIES
pg
test

BUNDLED WITH
2.5.9
47 changes: 47 additions & 0 deletions testing/postgres-client-tests/ruby/pg-test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/ruby

require 'pg'
require 'test/unit'

extend Test::Unit::Assertions

user = ARGV[0]
port = ARGV[1]
db = "doltgres"

queries = [
"create table test (pk int, value int, d1 decimal(9, 3), f1 float, primary key(pk))",
"select * from test",
"insert into test (pk, value, d1, f1) values (0,0,0.0,0.0)",
"select * from test",
"call dolt_add('-A');",
"call dolt_commit('-m', 'my commit')",
"select COUNT(*) FROM dolt_log",
"call dolt_checkout('-b', 'mybranch')",
"insert into test (pk, value, d1, f1) values (1,1, 123456.789, 420.42)",
"call dolt_commit('-a', '-m', 'my commit2')",
"call dolt_checkout('main')",
"call dolt_merge('mybranch')",
"select COUNT(*) FROM dolt_log",
]

# Smoke test the queries to make sure nothing blows up
conn = PG::Connection.new(:host => "localhost", :user => user, :dbname => db, :port => port)
queries.each do |query|
res = conn.query(query)
end

# Then make sure we can read some data back
res = conn.query("SELECT * from test where pk = 1;")
rowCount = 0
res.each do |row|
rowCount += 1
assert_equal 1, row["pk"].to_i
assert_equal 1, row["value"].to_i
assert_equal 123456.789, row["d1"].to_f
assert_equal 420.42, row["f1"].to_f
end
assert_equal 1, rowCount

conn.close()
exit(0)

0 comments on commit e255e50

Please sign in to comment.