Skip to content

Commit

Permalink
Npgsql: Fix test cases to use async, stop relevant warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Dec 12, 2024
1 parent b62907a commit b9b9a8d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 48 deletions.
69 changes: 33 additions & 36 deletions by-language/csharp-npgsql/DemoProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,51 +18,47 @@ await Parser.Default.ParseArguments<Options>(args)
var connString = $"Host={options.Host};Port={options.Port};SSL Mode={options.SslMode};" +
$"Username={options.Username};Password={options.Password};Database={options.Database}";
Console.WriteLine($"Connecting to {connString}\n");
using (var conn = new NpgsqlConnection(connString))
{
conn.Open();
var program = new DatabaseWorkloads();
await program.SystemQueryExample(conn);
await program.BasicConversationExample(conn);
await program.AsyncUnnestExample(conn);
conn.Close();
}

await using var conn = new NpgsqlConnection(connString);
conn.Open();
await DatabaseWorkloads.SystemQueryExample(conn);
await DatabaseWorkloads.BasicConversationExample(conn);
await DatabaseWorkloads.UnnestExample(conn);
conn.Close();
});

}

public class Options
{
[Option('h', "host", Required = false, HelpText = "Host name to connect to", Default = "localhost")]
public string Host { get; set; }
public string? Host { get; set; }

[Option('p', "port", Required = false, HelpText = "Port number to connect to", Default = 5432)]
public int Port { get; set; }

// Controls whether SSL is used, depending on server support. Can be Require, Disable, or Prefer.
// https://www.npgsql.org/doc/connection-string-parameters.html#security-and-encryption
[Option('s', "ssl-mode", Required = false, HelpText = "Which SSL mode to use", Default = "Disable")]
public string SslMode { get; set; }
public string? SslMode { get; set; }

[Option('u', "username", Required = false, HelpText = "Username to authenticate with", Default = "crate")]
public string Username { get; set; }
public string? Username { get; set; }
[Option('w', "password", Required = false, HelpText = "Password to authenticate with", Default = "")]
public string Password { get; set; }
public string? Password { get; set; }
[Option('d', "database", Required = false, HelpText = "Database to use", Default = "testdrive")]
public string Database { get; set; }
public string? Database { get; set; }
}

}

public class DatabaseWorkloads
{
public async Task<List<string>> SystemQueryExample(NpgsqlConnection conn)
public static async Task<List<string>> SystemQueryExample(NpgsqlConnection conn)
{
Console.WriteLine("Running SystemQueryExample");
var mountains = new List<string>();
using (var cmd = new NpgsqlCommand("SELECT mountain FROM sys.summits ORDER BY 1 LIMIT 25", conn))
using (var reader = cmd.ExecuteReader())
await using (var cmd = new NpgsqlCommand("SELECT mountain FROM sys.summits ORDER BY 1 LIMIT 25", conn))
await using (var reader = cmd.ExecuteReader())
{
while (await reader.ReadAsync())
{
Expand All @@ -76,31 +72,32 @@ public async Task<List<string>> SystemQueryExample(NpgsqlConnection conn)
return mountains;
}

public async Task<List<int>> BasicConversationExample(NpgsqlConnection conn)
public static async Task<List<int>> BasicConversationExample(NpgsqlConnection conn)
{
Console.WriteLine("Running BasicConversationExample");

// Submit DDL, create database schema.
using (var cmd = new NpgsqlCommand("DROP TABLE IF EXISTS testdrive.basic", conn))
await using (var cmd = new NpgsqlCommand("DROP TABLE IF EXISTS testdrive.basic", conn))
{
cmd.ExecuteNonQuery();
}
using (var cmd = new NpgsqlCommand("CREATE TABLE testdrive.basic (x int)", conn))

await using (var cmd = new NpgsqlCommand("CREATE TABLE testdrive.basic (x int)", conn))
{
cmd.ExecuteNonQuery();
}

// Insert single data point.
using (var cmd = new NpgsqlCommand("INSERT INTO testdrive.basic (x) VALUES (@x)", conn))
await using (var cmd = new NpgsqlCommand("INSERT INTO testdrive.basic (x) VALUES (@x)", conn))
{
cmd.Parameters.AddWithValue("x", -999);
cmd.ExecuteNonQuery();
}

// Insert multiple data points.
using (var cmd = new NpgsqlCommand("INSERT INTO testdrive.basic (x) VALUES (@x)", conn))
await using (var cmd = new NpgsqlCommand("INSERT INTO testdrive.basic (x) VALUES (@x)", conn))
{
using (var transaction = conn.BeginTransaction())
await using (var transaction = conn.BeginTransaction())
{
cmd.Transaction = transaction;
cmd.Parameters.Add("@x", NpgsqlDbType.Integer);
Expand All @@ -116,15 +113,15 @@ public async Task<List<int>> BasicConversationExample(NpgsqlConnection conn)
}

// Flush data.
using (var cmd = new NpgsqlCommand("REFRESH TABLE testdrive.basic", conn))
await using (var cmd = new NpgsqlCommand("REFRESH TABLE testdrive.basic", conn))
{
cmd.ExecuteNonQuery();
}

// Query back data.
var data = new List<int>();
using (var cmd = new NpgsqlCommand("SELECT x FROM testdrive.basic ORDER BY 1 ASC LIMIT 10", conn))
using (var reader = cmd.ExecuteReader())
await using (var cmd = new NpgsqlCommand("SELECT x FROM testdrive.basic ORDER BY 1 ASC LIMIT 10", conn))
await using (var reader = cmd.ExecuteReader())
{
while (await reader.ReadAsync())
{
Expand All @@ -139,20 +136,20 @@ public async Task<List<int>> BasicConversationExample(NpgsqlConnection conn)
return data;
}

public async Task<int> AsyncUnnestExample(NpgsqlConnection conn)
public static async Task<int> UnnestExample(NpgsqlConnection conn)
{
Console.WriteLine("Running AsyncUnnestExample");

// Submit DDL, create database schema.
using (var cmd = new NpgsqlCommand("DROP TABLE IF EXISTS testdrive.unnest", conn))
await using (var cmd = new NpgsqlCommand("DROP TABLE IF EXISTS testdrive.unnest", conn))
{
await cmd.ExecuteNonQueryAsync();
}

using (var cmd2 = new NpgsqlCommand(
connection: conn,
cmdText: "CREATE TABLE IF NOT EXISTS testdrive.unnest (id int, name text)"
))
await using (var cmd2 = new NpgsqlCommand(
connection: conn,
cmdText: "CREATE TABLE IF NOT EXISTS testdrive.unnest (id int, name text)"
))
{
await cmd2.ExecuteNonQueryAsync();
}
Expand All @@ -170,16 +167,16 @@ public async Task<int> AsyncUnnestExample(NpgsqlConnection conn)
await cmd3.ExecuteNonQueryAsync();

// Flush data.
using (var cmd = new NpgsqlCommand("REFRESH TABLE testdrive.unnest", conn))
await using (var cmd = new NpgsqlCommand("REFRESH TABLE testdrive.unnest", conn))
{
cmd.ExecuteNonQuery();
}

// Query back data.
var resultCount = -1;
using (var cmd = new NpgsqlCommand("SELECT COUNT(*) FROM testdrive.unnest", conn))
await using (var cmd = new NpgsqlCommand("SELECT COUNT(*) FROM testdrive.unnest", conn))
{
using (var reader = cmd.ExecuteReader())
await using (var reader = cmd.ExecuteReader())
{
await reader.ReadAsync();
resultCount = reader.GetInt32(0);
Expand Down
4 changes: 4 additions & 0 deletions by-language/csharp-npgsql/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ For running the tests against a remote database, use, for example::
export CRATEDB_DSN='Host=clustername.aks1.westeurope.azure.cratedb.net;Port=5432;SSL Mode=Require;Username=foobar;Password=X8F%Shn)TESTvF5ac7%eW4NM;Database=testdrive'
dotnet test --framework=net8.0

For running tests selectively, use::

dotnet test --framework=net8.0 --filter SystemQueryExample


.. _C#: https://en.wikipedia.org/wiki/C_Sharp_(programming_language)
.. _crate-npgsql: https://github.com/crate/crate-npgsql
Expand Down
1 change: 1 addition & 0 deletions by-language/csharp-npgsql/demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateProgramFile>false</GenerateProgramFile>
Expand Down
22 changes: 10 additions & 12 deletions by-language/csharp-npgsql/tests/DemoProgramTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Npgsql;
using Xunit;

Expand Down Expand Up @@ -41,44 +42,41 @@ public DemoProgramTest(DatabaseFixture fixture)
}

[Fact]
public void TestSystemQueryExample()
public async Task TestSystemQueryExample()
{
var conn = fixture.Db;

// Invoke database workload.
var task = program.SystemQueryExample(conn);
task.Wait();
var task = DatabaseWorkloads.SystemQueryExample(conn);
var mountains = await task.WaitAsync(TimeSpan.FromSeconds(0.5));

// Check results.
var mountains = task.Result;
Assert.Equal("Acherkogel", mountains[0]);
}

[Fact]
public void TestBasicConversationExample()
public async Task TestBasicConversationExample()
{
var conn = fixture.Db;

// Invoke database workload.
var task = program.BasicConversationExample(conn);
task.Wait();
var task = DatabaseWorkloads.BasicConversationExample(conn);
var results = await task.WaitAsync(TimeSpan.FromSeconds(0.5));

// Check results.
var results = task.Result;
Assert.Equal(new List<int>() { -999, 10, 20, 30, 40, 50, 60, 70, 80, 90 }, results);
}

[Fact]
public void TestAsyncUnnestExample()
public async Task TestUnnestExample()
{
var conn = fixture.Db;

// Invoke database workload.
var task = program.AsyncUnnestExample(conn);
task.Wait();
var task = DatabaseWorkloads.UnnestExample(conn);
var resultCount = await task.WaitAsync(TimeSpan.FromSeconds(0.5));

// Check results.
var resultCount = task.Result;
Assert.Equal(10, resultCount);
}

Expand Down

0 comments on commit b9b9a8d

Please sign in to comment.