Skip to content

Commit 020ac00

Browse files
Add logging (#72)
* Add additional settings config check * Add helper class for getting underlying http handler * Add logging to Connection, Command, and BulkCopy * Add logging example
1 parent 8a97b6f commit 020ac00

26 files changed

+1507
-31
lines changed

.editorconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ dotnet_diagnostic.SA1602.severity = none # SA1602: Enumeration items should be d
2626
dotnet_diagnostic.SA1629.severity = none # SA1629: Documentation text should end with a period
2727
dotnet_diagnostic.SA1633.severity = none # SA1633: File should have header
2828

29+
dotnet_diagnostic.CA1848.severity = suggestion # CA1848: Use the LoggerMessage delegates
2930
dotnet_diagnostic.SA1201.severity = suggestion # SA1201: Elements should appear in the correct order
3031
dotnet_diagnostic.SA1202.severity = suggestion # SA1202: Elements should be ordered by access
3132
dotnet_diagnostic.SA1204.severity = suggestion # SA1204: Static elements should appear before instance elements

ClickHouse.Driver.Tests/ADO/ClickHouseClientSettingsTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,19 @@ public void Validate_WithUseSessionAndHttpClient_ShouldThrow()
265265
Assert.That(ex.Message, Does.Contain("UseSession cannot be combined with a custom HttpClient"));
266266
}
267267

268+
[Test]
269+
public void Validate_WithUseSessionAndHttpClientFactory_ShouldThrow()
270+
{
271+
var settings = new ClickHouseClientSettings
272+
{
273+
UseSession = true,
274+
HttpClientFactory = new TestHttpClientFactory()
275+
};
276+
277+
var ex = Assert.Throws<InvalidOperationException>(() => settings.Validate());
278+
Assert.That(ex.Message, Does.Contain("UseSession cannot be combined with a custom HttpClientFactory"));
279+
}
280+
268281
[Test]
269282
public void Validate_WithBothHttpClientAndFactory_ShouldThrow()
270283
{
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using ClickHouse.Driver.ADO;
3+
namespace ClickHouse.Driver.Tests.ADO;
4+
5+
public class ClickHouseCommandTests
6+
{
7+
[Test]
8+
public void ExecuteReaderAsync_WithoutConnection_ThrowsInvalidOperationException()
9+
{
10+
// Arrange
11+
var command = new ClickHouseCommand();
12+
command.CommandText = "SELECT 1";
13+
14+
// Act & Assert
15+
var ex = Assert.ThrowsAsync<InvalidOperationException>(async () =>
16+
{
17+
using var reader = await command.ExecuteReaderAsync();
18+
});
19+
20+
Assert.That(ex.Message, Is.EqualTo("Connection is not set"));
21+
}
22+
23+
[Test]
24+
public void ExecuteNonQueryAsync_WithoutConnection_ThrowsInvalidOperationException()
25+
{
26+
// Arrange
27+
var command = new ClickHouseCommand();
28+
command.CommandText = "INSERT INTO test VALUES (1)";
29+
30+
// Act & Assert
31+
var ex = Assert.ThrowsAsync<InvalidOperationException>(async () =>
32+
{
33+
await command.ExecuteNonQueryAsync();
34+
});
35+
36+
Assert.That(ex.Message, Is.EqualTo("Connection is not set"));
37+
}
38+
39+
[Test]
40+
public void ExecuteScalarAsync_WithoutConnection_ThrowsInvalidOperationException()
41+
{
42+
// Arrange
43+
var command = new ClickHouseCommand();
44+
command.CommandText = "SELECT 1";
45+
46+
// Act & Assert
47+
var ex = Assert.ThrowsAsync<InvalidOperationException>(async () =>
48+
{
49+
await command.ExecuteScalarAsync();
50+
});
51+
52+
Assert.That(ex.Message, Is.EqualTo("Connection is not set"));
53+
}
54+
55+
[Test]
56+
public void ExecuteRawResultAsync_WithoutConnection_ThrowsInvalidOperationException()
57+
{
58+
// Arrange
59+
var command = new ClickHouseCommand();
60+
command.CommandText = "SELECT 1 FORMAT JSON";
61+
62+
// Act & Assert
63+
var ex = Assert.ThrowsAsync<InvalidOperationException>(async () =>
64+
{
65+
await command.ExecuteRawResultAsync(default);
66+
});
67+
68+
Assert.That(ex.Message, Is.EqualTo("Connection is not set"));
69+
}
70+
}

ClickHouse.Driver.Tests/BulkCopy/BulkCopyTests.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,5 +529,49 @@ data Tuple(name String, status Enum8('Active' = 0, 'Inactive' = 1))
529529

530530
Assert.That(bulkCopy.RowsWritten, Is.EqualTo(2));
531531
}
532+
533+
[Test]
534+
public void InitAsync_WithNullDestinationTableName_ThrowsInvalidOperationException()
535+
{
536+
// Arrange
537+
var settings = new ClickHouseClientSettings(TestUtilities.GetConnectionStringBuilder());
538+
539+
using var connection = new ClickHouseConnection(settings);
540+
var bulkCopy = new ClickHouseBulkCopy(connection)
541+
{
542+
DestinationTableName = null, // Not set
543+
};
544+
545+
// Act & Assert
546+
var ex = Assert.ThrowsAsync<InvalidOperationException>(async () =>
547+
{
548+
await bulkCopy.InitAsync();
549+
});
550+
551+
Assert.That(ex.Message, Does.Contain("DestinationTableName"));
552+
}
553+
554+
[Test]
555+
public void WriteToServerAsync_WithNullDestinationTableName_ThrowsInvalidOperationException()
556+
{
557+
// Arrange
558+
var settings = new ClickHouseClientSettings(TestUtilities.GetConnectionStringBuilder());
559+
560+
using var connection = new ClickHouseConnection(settings);
561+
var bulkCopy = new ClickHouseBulkCopy(connection)
562+
{
563+
DestinationTableName = null, // Not set
564+
};
565+
566+
var rows = new List<object[]> { new object[] { 1, "test" } };
567+
568+
// Act & Assert
569+
var ex = Assert.ThrowsAsync<InvalidOperationException>(async () =>
570+
{
571+
await bulkCopy.WriteToServerAsync(rows);
572+
});
573+
574+
Assert.That(ex.Message, Does.Contain("Destination table not set"));
575+
}
532576
}
533577

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System.Net.Http;
2+
using ClickHouse.Driver.Http;
3+
using NUnit.Framework;
4+
5+
namespace ClickHouse.Driver.Tests.Http;
6+
7+
public class HttpClientExtensionsTests
8+
{
9+
[Test]
10+
public void GetHandler_ReturnsHandlerForDefaultHttpClient()
11+
{
12+
using var httpClient = new HttpClient();
13+
14+
var handler = httpClient.GetHandler();
15+
16+
Assert.That(handler, Is.Not.Null);
17+
// Default HttpClient always creates SocketsHttpHandler on .NET 5+
18+
#if NET5_0_OR_GREATER
19+
Assert.That(handler, Is.InstanceOf<SocketsHttpHandler>());
20+
#else
21+
Assert.That(handler, Is.InstanceOf<HttpClientHandler>());
22+
#endif
23+
}
24+
25+
#if NET5_0_OR_GREATER
26+
[Test]
27+
public void GetHandler_ReturnsProvidedSocketsHttpHandler()
28+
{
29+
var handler = new SocketsHttpHandler();
30+
using var httpClient = new HttpClient(handler, disposeHandler: false);
31+
32+
var retrievedHandler = httpClient.GetHandler();
33+
34+
Assert.That(retrievedHandler, Is.SameAs(handler));
35+
Assert.That(retrievedHandler, Is.InstanceOf<SocketsHttpHandler>());
36+
}
37+
38+
39+
[Test]
40+
public void GetHandler_RetrievesHttpClientHandlerConfiguration()
41+
{
42+
var handler = new HttpClientHandler
43+
{
44+
MaxConnectionsPerServer = 42,
45+
AllowAutoRedirect = false,
46+
UseCookies = false
47+
};
48+
using var httpClient = new HttpClient(handler, disposeHandler: false);
49+
50+
var retrievedHandler = httpClient.GetHandler() as SocketsHttpHandler;
51+
52+
Assert.That(retrievedHandler, Is.Not.Null);
53+
Assert.That(retrievedHandler.MaxConnectionsPerServer, Is.EqualTo(42));
54+
Assert.That(retrievedHandler.AllowAutoRedirect, Is.False);
55+
Assert.That(retrievedHandler.UseCookies, Is.False);
56+
}
57+
58+
59+
[Test]
60+
public void GetHandler_RetrievesSocketsHttpHandlerConfiguration()
61+
{
62+
var handler = new SocketsHttpHandler
63+
{
64+
MaxConnectionsPerServer = 100,
65+
AllowAutoRedirect = true,
66+
UseCookies = false
67+
};
68+
using var httpClient = new HttpClient(handler, disposeHandler: false);
69+
70+
var retrievedHandler = httpClient.GetHandler() as SocketsHttpHandler;
71+
72+
Assert.That(retrievedHandler, Is.Not.Null);
73+
Assert.That(retrievedHandler.MaxConnectionsPerServer, Is.EqualTo(100));
74+
Assert.That(retrievedHandler.AllowAutoRedirect, Is.True);
75+
Assert.That(retrievedHandler.UseCookies, Is.False);
76+
}
77+
#endif
78+
}

0 commit comments

Comments
 (0)