Skip to content

Commit

Permalink
feat: complete postgres client compatibility tests (apecloud#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
NoyException committed Nov 28, 2024
1 parent bca8fbb commit 592b701
Show file tree
Hide file tree
Showing 16 changed files with 741 additions and 66 deletions.
12 changes: 8 additions & 4 deletions compatibility/pg/c/pg_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ typedef struct {
int testCount;
} PGTest;

void connectDB(PGTest *pgTest, const char *ip, int port, const char *user, const char *password);
void disconnectDB(PGTest *pgTest);
void addTest(PGTest *pgTest, const char *query, char **expectedResults, int expectedRows, int expectedCols);
int runTests(PGTest *pgTest);
void readTestsFromFile(PGTest *pgTest, const char *filename);
size_t removeNewline(char *line);

void connectDB(PGTest *pgTest, const char *ip, int port, const char *user, const char *password) {
char conninfo[256];
snprintf(conninfo, sizeof(conninfo), "host=%s port=%d dbname=postgres user=%s password=%s", ip, port, user, password);
Expand Down Expand Up @@ -81,10 +88,7 @@ int runTests(PGTest *pgTest) {

size_t removeNewline(char *line) {
size_t len = strlen(line);
if (len > 0 && (line[len - 1] == '\n' || line[len - 1] == '\r')) {
line[--len] = '\0';
}
if (len > 0 && line[len - 1] == '\r') {
while (len > 0 && (line[len - 1] == '\n' || line[len - 1] == '\r')) {
line[--len] = '\0';
}
return len;
Expand Down
7 changes: 7 additions & 0 deletions compatibility/pg/clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

rm -rf ./c/pg_test \
./csharp/bin ./csharp/obj \
./go/pg \
./java/*.class \
./rust/target ./rust/Cargo.lock
179 changes: 179 additions & 0 deletions compatibility/pg/csharp/PGTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
using System;
using System.Collections.Generic;
using System.Data;
using Microsoft.Data.SqlClient;
using System.IO;

public class PGTest
{
public class Tests
{
private SqlConnection conn;
private SqlCommand cmd;
private List<Test> tests = new List<Test>();

public void Connect(string ip, int port, string user, string password)
{
string connectionString = $"Server={ip},{port};User Id={user};Password={password};";
try
{
conn = new SqlConnection(connectionString);
conn.Open();
cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
}
catch (SqlException e)
{
throw new Exception($"Error connecting to database: {e.Message}", e);
}
}

public void Disconnect()
{
try
{
cmd.Dispose();
conn.Close();
}
catch (SqlException e)
{
throw new Exception(e.Message);
}
}

public void AddTest(string query, string[][] expectedResults)
{
tests.Add(new Test(query, expectedResults));
}

public bool RunTests()
{
foreach (var test in tests)
{
if (!test.Run(cmd))
{
return false;
}
}
return true;
}

public void ReadTestsFromFile(string filename)
{
try
{
using (var reader = new StreamReader(filename))
{
string line;
while ((line = reader.ReadLine()) != null)
{
if (string.IsNullOrWhiteSpace(line)) continue;
string query = line;
var results = new List<string[]>();
while ((line = reader.ReadLine()) != null && !string.IsNullOrWhiteSpace(line))
{
results.Add(line.Split(','));
}
string[][] expectedResults = results.ToArray();
AddTest(query, expectedResults);
}
}
}
catch (IOException e)
{
Console.Error.WriteLine(e.Message);
Environment.Exit(1);
}
}

public class Test
{
private string query;
private string[][] expectedResults;

public Test(string query, string[][] expectedResults)
{
this.query = query;
this.expectedResults = expectedResults;
}

public bool Run(SqlCommand cmd)
{
try
{
Console.WriteLine("Running test: " + query);
cmd.CommandText = query;
using (var reader = cmd.ExecuteReader())
{
if (!reader.HasRows)
{
if (expectedResults.Length != 0)
{
Console.Error.WriteLine($"Expected {expectedResults.Length} rows, got 0");
return false;
}
Console.WriteLine("Returns 0 rows");
return true;
}
if (reader.FieldCount != expectedResults[0].Length)
{
Console.Error.WriteLine($"Expected {expectedResults[0].Length} columns, got {reader.FieldCount}");
return false;
}
int rows = 0;
while (reader.Read())
{
for (int col = 0; col < expectedResults[rows].Length; col++)
{
string result = reader.GetString(col);
if (expectedResults[rows][col] != result)
{
Console.Error.WriteLine($"Expected:\n'{expectedResults[rows][col]}'");
Console.Error.WriteLine($"Result:\n'{result}'\nRest of the results:");
while (reader.Read())
{
Console.Error.WriteLine(reader.GetString(0));
}
return false;
}
}
rows++;
}
Console.WriteLine("Returns " + rows + " rows");
if (rows != expectedResults.Length)
{
Console.Error.WriteLine($"Expected {expectedResults.Length} rows");
return false;
}
return true;
}
}
catch (SqlException e)
{
Console.Error.WriteLine(e.Message);
return false;
}
}
}
}

public static void Main(string[] args)
{
if (args.Length < 5)
{
Console.Error.WriteLine("Usage: PGTest <ip> <port> <user> <password> <testFile>");
Environment.Exit(1);
}

var tests = new Tests();
tests.Connect(args[0], int.Parse(args[1]), args[2], args[3]);
tests.ReadTestsFromFile(args[4]);

if (!tests.RunTests())
{
tests.Disconnect();
Environment.Exit(1);
}
tests.Disconnect();
}
}
12 changes: 12 additions & 0 deletions compatibility/pg/csharp/PGTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2" />
</ItemGroup>

</Project>
Loading

0 comments on commit 592b701

Please sign in to comment.