DBD-libsql
view release on metacpan or search on metacpan
xt/01_integration.t view on Meta::CPAN
$turso_bin = `which turso 2>/dev/null`;
chomp $turso_bin if $turso_bin;
unless ($turso_bin && -x $turso_bin) {
plan skip_all => 'turso CLI not found. Install Alien::Turso::CLI or system turso CLI';
}
} else {
$turso_bin = Alien::Turso::CLI->bin_dir . '/turso';
}
}
sub check_turso_dev_running {
my $ua = LWP::UserAgent->new(timeout => 5);
my $response = $ua->get('http://127.0.0.1:8080/health');
return $response->is_success;
}
sub start_turso_dev_if_needed {
return 1 if check_turso_dev_running();
print "# Starting turso dev server...\n";
my $pid = fork();
if ($pid == 0) {
# Child process - start turso dev
exec($turso_bin, 'dev', '--port', '8080') or die "Failed to start turso dev: $!";
} elsif (defined $pid) {
# Parent process - wait for server to start
sleep(3);
for my $i (1..10) {
if (check_turso_dev_running()) {
print "# turso dev server started successfully\n";
return 1;
}
sleep(1);
}
kill('TERM', $pid);
return 0;
} else {
return 0;
}
}
# Try to start turso dev server if not running
unless (start_turso_dev_if_needed()) {
plan skip_all => 'Could not start turso dev server';
}
plan tests => 7;
# Test 1: Basic Hrana Protocol Communication
subtest 'Hrana Protocol Direct Test' => sub {
plan tests => 4;
my $ua = LWP::UserAgent->new(timeout => 10);
my $json = JSON->new->utf8;
# Test health endpoint
my $health_response = $ua->get('http://127.0.0.1:8080/health');
ok($health_response->is_success, 'Health endpoint accessible');
# Test Hrana pipeline endpoint
my $request = HTTP::Request->new('POST', 'http://127.0.0.1:8080/v2/pipeline');
$request->header('Content-Type' => 'application/json');
my $pipeline_data = {
requests => [
{
type => 'execute',
stmt => {
sql => 'SELECT 1 as test_value',
args => []
}
}
]
};
$request->content($json->encode($pipeline_data));
my $response = $ua->request($request);
ok($response->is_success, 'Hrana pipeline request successful');
if ($response->is_success) {
my $result = eval { $json->decode($response->content) };
ok(!$@, 'Response is valid JSON');
ok($result && $result->{results}, 'Response contains results');
} else {
fail('Response is valid JSON');
fail('Response contains results');
diag("Response: " . $response->content);
}
};
# Test 2: DBI Connection via HTTP
subtest 'DBI Connection Test' => sub {
plan tests => 4;
# Connect using local turso dev server
my $dbh = DBI->connect("dbi:libsql:127.0.0.1?schema=http&port=8080", "", "");
ok($dbh, 'Successfully connected via HTTP');
isa_ok($dbh, 'DBI::db');
# Test database handle attributes
ok(defined $dbh->{Name}, 'Database handle has Name attribute');
# Test disconnection
ok($dbh->disconnect(), 'Successfully disconnected');
};
# Test 3: Basic SQL Operations
subtest 'SQL Operations Test' => sub {
plan tests => 6;
my $dbh = DBI->connect("dbi:libsql:127.0.0.1?schema=http&port=8080", "", "");
ok($dbh, 'Connected to database');
# Test CREATE TABLE
my $sth = $dbh->prepare("CREATE TABLE IF NOT EXISTS test_users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)");
ok($sth, 'Prepared CREATE TABLE statement');
ok($sth->execute(), 'Executed CREATE TABLE');
# Test INSERT
$sth = $dbh->prepare("INSERT INTO test_users (name, email) VALUES (?, ?)");
( run in 1.207 second using v1.01-cache-2.11-cpan-13bb782fe5a )