Skip to content

Commit 4d6cf66

Browse files
committed
optimized unit tests
1 parent f2977b5 commit 4d6cf66

File tree

5 files changed

+33
-50
lines changed

5 files changed

+33
-50
lines changed

tests/SuperSocket.MySQL.Test/MainTest.cs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,12 @@ namespace SuperSocket.MySQL.Test
88
public class MainTest
99
{
1010
// Test configuration - these should be set via environment variables or test configuration
11-
private const string TestHost = "localhost";
12-
private const int TestPort = 3306;
13-
private const string TestUsername = "root";
14-
private const string TestPassword = "root";
15-
private const string TestDatabase = "test";
1611

1712
[Fact]
1813
public async Task ConnectAsync_WithValidCredentials_ShouldAuthenticateSuccessfully()
1914
{
2015
// Arrange
21-
var connection = new MySQLConnection(TestHost, TestPort, TestUsername, TestPassword);
16+
var connection = new MySQLConnection(TestConst.Host, TestConst.DefaultPort, TestConst.Username, TestConst.Password);
2217

2318
try
2419
{
@@ -39,7 +34,7 @@ public async Task ConnectAsync_WithValidCredentials_ShouldAuthenticateSuccessful
3934
public async Task ConnectAsync_WithInvalidCredentials_ShouldThrowException()
4035
{
4136
// Arrange
42-
var connection = new MySQLConnection(TestHost, TestPort, "invalid_user", "invalid_password");
37+
var connection = new MySQLConnection(TestConst.Host, TestConst.DefaultPort, "invalid_user", "invalid_password");
4338

4439
// Act & Assert
4540
var exception = await Assert.ThrowsAsync<InvalidOperationException>(
@@ -54,7 +49,7 @@ public async Task ConnectAsync_WithInvalidCredentials_ShouldThrowException()
5449
public async Task ConnectAsync_WithEmptyPassword_ShouldHandleCorrectly()
5550
{
5651
// Arrange
57-
var connection = new MySQLConnection(TestHost, TestPort, TestUsername, "");
52+
var connection = new MySQLConnection(TestConst.Host, TestConst.DefaultPort, TestConst.Username, "");
5853

5954
try
6055
{
@@ -83,8 +78,8 @@ public async Task ConnectAsync_WithEmptyPassword_ShouldHandleCorrectly()
8378
public async Task ConnectAsync_MultipleConnections_ShouldWorkIndependently()
8479
{
8580
// Arrange
86-
var connection1 = new MySQLConnection(TestHost, TestPort, TestUsername, TestPassword);
87-
var connection2 = new MySQLConnection(TestHost, TestPort, TestUsername, TestPassword);
81+
var connection1 = new MySQLConnection(TestConst.Host, TestConst.DefaultPort, TestConst.Username, TestConst.Password);
82+
var connection2 = new MySQLConnection(TestConst.Host, TestConst.DefaultPort, TestConst.Username, TestConst.Password);
8883

8984
try
9085
{
@@ -108,7 +103,7 @@ public async Task ConnectAsync_MultipleConnections_ShouldWorkIndependently()
108103
public async Task DisconnectAsync_AfterSuccessfulConnection_ShouldResetAuthenticationState()
109104
{
110105
// Arrange
111-
var connection = new MySQLConnection(TestHost, TestPort, TestUsername, TestPassword);
106+
var connection = new MySQLConnection(TestConst.Host, TestConst.DefaultPort, TestConst.Username, TestConst.Password);
112107
await connection.ConnectAsync();
113108
Assert.True(connection.IsAuthenticated, "Precondition: Connection should be authenticated");
114109

@@ -124,7 +119,7 @@ public void Constructor_WithNullHost_ShouldThrowArgumentNullException()
124119
{
125120
// Act & Assert
126121
Assert.Throws<ArgumentNullException>(() =>
127-
new MySQLConnection(null, TestPort, TestUsername, TestPassword)
122+
new MySQLConnection(null, TestConst.DefaultPort, TestConst.Username, TestConst.Password)
128123
);
129124
}
130125

@@ -133,7 +128,7 @@ public void Constructor_WithNullUsername_ShouldThrowArgumentNullException()
133128
{
134129
// Act & Assert
135130
Assert.Throws<ArgumentNullException>(() =>
136-
new MySQLConnection(TestHost, TestPort, null, TestPassword)
131+
new MySQLConnection(TestConst.Host, TestConst.DefaultPort, null, TestConst.Password)
137132
);
138133
}
139134

@@ -142,15 +137,15 @@ public void Constructor_WithNullPassword_ShouldThrowArgumentNullException()
142137
{
143138
// Act & Assert
144139
Assert.Throws<ArgumentNullException>(() =>
145-
new MySQLConnection(TestHost, TestPort, TestUsername, null)
140+
new MySQLConnection(TestConst.Host, TestConst.DefaultPort, TestConst.Username, null)
146141
);
147142
}
148143

149144
[Fact]
150145
public async Task ConnectAsync_WithInvalidHost_ShouldThrowException()
151146
{
152147
// Arrange
153-
var connection = new MySQLConnection("invalid-host-that-does-not-exist", TestPort, TestUsername, TestPassword);
148+
var connection = new MySQLConnection("invalid-host-that-does-not-exist", TestConst.DefaultPort, TestConst.Username, TestConst.Password);
154149

155150
// Act & Assert
156151
await Assert.ThrowsAnyAsync<Exception>(async () => await connection.ConnectAsync());
@@ -161,7 +156,7 @@ public async Task ConnectAsync_WithInvalidHost_ShouldThrowException()
161156
public async Task ConnectAsync_WithInvalidPort_ShouldThrowException()
162157
{
163158
// Arrange
164-
var connection = new MySQLConnection(TestHost, 12345, TestUsername, TestPassword);
159+
var connection = new MySQLConnection(TestConst.Host, 12345, TestConst.Username, TestConst.Password);
165160

166161
// Act & Assert
167162
await Assert.ThrowsAnyAsync<Exception>(async () => await connection.ConnectAsync());
@@ -172,7 +167,7 @@ public async Task ConnectAsync_WithInvalidPort_ShouldThrowException()
172167
public async Task ExecuteQueryAsync_WithoutAuthentication_ShouldThrowException()
173168
{
174169
// Arrange
175-
var connection = new MySQLConnection(TestHost, TestPort, TestUsername, TestPassword);
170+
var connection = new MySQLConnection(TestConst.Host, TestConst.DefaultPort, TestConst.Username, TestConst.Password);
176171

177172
// Act & Assert
178173
var exception = await Assert.ThrowsAsync<InvalidOperationException>(
@@ -186,7 +181,7 @@ public async Task ExecuteQueryAsync_WithoutAuthentication_ShouldThrowException()
186181
public async Task ExecuteQueryAsync_WithAuthentication_ShouldNotThrow()
187182
{
188183
// Arrange
189-
var connection = new MySQLConnection(TestHost, TestPort, TestUsername, TestPassword);
184+
var connection = new MySQLConnection(TestConst.Host, TestConst.DefaultPort, TestConst.Username, TestConst.Password);
190185
await connection.ConnectAsync();
191186

192187
try

tests/SuperSocket.MySQL.Test/MySQLIntegrationTest.cs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,12 @@ namespace SuperSocket.MySQL.Test
1212
/// </summary>
1313
public class MySQLIntegrationTest
1414
{
15-
private readonly string _host;
16-
private readonly int _port;
17-
private readonly string _username;
18-
private readonly string _password;
19-
private readonly string _database;
20-
21-
public MySQLIntegrationTest()
22-
{
23-
// Configuration can be overridden by environment variables for CI/CD
24-
_host = Environment.GetEnvironmentVariable("MYSQL_HOST") ?? "localhost";
25-
_port = int.Parse(Environment.GetEnvironmentVariable("MYSQL_PORT") ?? "3306");
26-
_username = Environment.GetEnvironmentVariable("MYSQL_USERNAME") ?? "root";
27-
_password = Environment.GetEnvironmentVariable("MYSQL_PASSWORD") ?? "password";
28-
_database = Environment.GetEnvironmentVariable("MYSQL_DATABASE") ?? "test";
29-
}
30-
3115
[Fact]
3216
[Trait("Category", "Integration")]
3317
public async Task MySQLConnection_CompleteHandshakeFlow_ShouldAuthenticate()
3418
{
3519
// Arrange
36-
var connection = new MySQLConnection(_host, _port, _username, _password);
20+
var connection = new MySQLConnection(TestConst.Host, TestConst.DefaultPort, TestConst.Username, TestConst.Password);
3721

3822
try
3923
{
@@ -56,7 +40,7 @@ public async Task MySQLConnection_CompleteHandshakeFlow_ShouldAuthenticate()
5640
public async Task MySQLConnection_InvalidCredentials_ShouldFailHandshake()
5741
{
5842
// Arrange
59-
var connection = new MySQLConnection(_host, _port, "nonexistent_user", "wrong_password");
43+
var connection = new MySQLConnection(TestConst.Host, TestConst.DefaultPort, "nonexistent_user", "wrong_password");
6044

6145
// Act & Assert
6246
var exception = await Assert.ThrowsAsync<InvalidOperationException>(
@@ -82,7 +66,7 @@ public async Task MySQLConnection_ConcurrentConnections_ShouldWork()
8266
// Act - Create multiple concurrent connections
8367
for (int i = 0; i < connectionCount; i++)
8468
{
85-
connections[i] = new MySQLConnection(_host, _port, _username, _password);
69+
connections[i] = new MySQLConnection(TestConst.Host, TestConst.DefaultPort, TestConst.Username, TestConst.Password);
8670
tasks[i] = connections[i].ConnectAsync();
8771
}
8872

@@ -147,7 +131,7 @@ public async Task MySQLConnection_ReconnectAfterDisconnect_ShouldWork()
147131
public async Task MySQLConnection_HandshakeTimeout_ShouldBeHandled()
148132
{
149133
// Skip test if MySQL is not available // Arrange
150-
var connection = new MySQLConnection(_host, _port, _username, _password);
134+
var connection = new MySQLConnection(TestConst.Host, TestConst.DefaultPort, TestConst.Username, TestConst.Password);
151135
using var cts = new System.Threading.CancellationTokenSource(TimeSpan.FromSeconds(10));
152136

153137
try

tests/SuperSocket.MySQL.Test/QueryTest.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,9 @@ public QueryTest(ITestOutputHelper output)
1717
[Fact]
1818
public async Task TestQueryFunctionality()
1919
{
20-
// Check if MySQL is available
21-
var host = Environment.GetEnvironmentVariable("MYSQL_HOST") ?? "localhost";
22-
var portStr = Environment.GetEnvironmentVariable("MYSQL_PORT") ?? "3306";
23-
var username = Environment.GetEnvironmentVariable("MYSQL_USER") ?? "root";
24-
var password = Environment.GetEnvironmentVariable("MYSQL_PASSWORD") ?? "root";
20+
_output.WriteLine($"Testing MySQL connection to {TestConst.Host}:{TestConst.DefaultPort} with user '{TestConst.Username}'");
2521

26-
if (!int.TryParse(portStr, out int port))
27-
port = 3306;
28-
29-
_output.WriteLine($"Testing MySQL connection to {host}:{port} with user '{username}'");
30-
31-
32-
var connection = new MySQLConnection(host, port, username, password);
22+
var connection = new MySQLConnection(TestConst.Host, TestConst.DefaultPort, TestConst.Username, TestConst.Password);
3323

3424
// Test connection and authentication
3525
await connection.ConnectAsync();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace SuperSocket.MySQL.Test
2+
{
3+
public static class TestConst
4+
{
5+
public const string Host = "localhost";
6+
7+
public const string Username = "root";
8+
9+
public const string Password = "root";
10+
11+
public const int DefaultPort = 3306;
12+
}
13+
}

tests/SuperSocket.MySQL.Test/docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ services:
88
restart: always
99
environment:
1010
MYSQL_ROOT_PASSWORD: root
11+
MYSQL_ROOT_HOST: "%"
1112
ports:
1213
- 3306:3306
1314
command: ["mysqld", "--log-bin=mysql-bin", "--server-id=1", "--default-authentication-plugin=mysql_native_password"]

0 commit comments

Comments
 (0)