Skip to content

Commit 08679fa

Browse files
committed
Simplify test code by using Assert.Throws return value.
1 parent 6df6490 commit 08679fa

File tree

7 files changed

+59
-221
lines changed

7 files changed

+59
-221
lines changed

tests/SideBySide/BulkLoaderSync.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -948,14 +948,8 @@ public void BulkCopyDoesNotInsertAllRows()
948948
}
949949
};
950950

951-
try
952-
{
953-
bcp.WriteToServer(dataTable);
954-
Assert.True(false, "Exception wasn't thrown");
955-
}
956-
catch (MySqlException ex) when (ex.Number == (int) MySqlErrorCode.BulkCopyFailed)
957-
{
958-
}
951+
var ex = Assert.Throws<MySqlException>(() => bcp.WriteToServer(dataTable));
952+
Assert.Equal(MySqlErrorCode.BulkCopyFailed, (MySqlErrorCode) ex.Number);
959953
}
960954
#endif
961955

tests/SideBySide/CommandTimeoutTests.cs

Lines changed: 19 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,10 @@ public void CommandTimeoutWithSleepSync()
6262
{
6363
cmd.CommandTimeout = 2;
6464
var sw = Stopwatch.StartNew();
65-
try
66-
{
67-
using var reader = cmd.ExecuteReader();
68-
69-
// shouldn't get here
70-
Assert.True(false);
71-
}
72-
catch (MySqlException ex)
73-
{
74-
sw.Stop();
75-
Assert.Contains(c_timeoutMessage, ex.Message, StringComparison.OrdinalIgnoreCase);
76-
TestUtilities.AssertDuration(sw, cmd.CommandTimeout * 1000 - 100, 500);
77-
}
65+
var ex = Assert.Throws<MySqlException>(cmd.ExecuteReader);
66+
sw.Stop();
67+
Assert.Contains(c_timeoutMessage, ex.Message, StringComparison.OrdinalIgnoreCase);
68+
TestUtilities.AssertDuration(sw, cmd.CommandTimeout * 1000 - 100, 500);
7869
}
7970

8071
Assert.Equal(ConnectionState.Closed, m_connection.State);
@@ -87,19 +78,10 @@ public async Task CommandTimeoutWithSleepAsync()
8778
{
8879
cmd.CommandTimeout = 2;
8980
var sw = Stopwatch.StartNew();
90-
try
91-
{
92-
using var reader = await cmd.ExecuteReaderAsync();
93-
94-
// shouldn't get here
95-
Assert.True(false);
96-
}
97-
catch (MySqlException ex)
98-
{
99-
sw.Stop();
100-
Assert.Contains(c_timeoutMessage, ex.Message, StringComparison.OrdinalIgnoreCase);
101-
TestUtilities.AssertDuration(sw, cmd.CommandTimeout * 1000 - 100, 700);
102-
}
81+
var exception = await Assert.ThrowsAsync<MySqlException>(cmd.ExecuteReaderAsync);
82+
sw.Stop();
83+
Assert.Contains(c_timeoutMessage, exception.Message, StringComparison.OrdinalIgnoreCase);
84+
TestUtilities.AssertDuration(sw, cmd.CommandTimeout * 1000 - 100, 700);
10385
}
10486

10587
Assert.Equal(ConnectionState.Closed, m_connection.State);
@@ -129,19 +111,10 @@ create procedure sleep_sproc(IN seconds INT)
129111
cmd.CommandTimeout = 2;
130112

131113
var sw = Stopwatch.StartNew();
132-
try
133-
{
134-
using var reader = cmd.ExecuteReader();
135-
136-
// shouldn't get here
137-
Assert.True(false);
138-
}
139-
catch (MySqlException ex)
140-
{
141-
sw.Stop();
142-
Assert.Contains(c_timeoutMessage, ex.Message, StringComparison.OrdinalIgnoreCase);
143-
TestUtilities.AssertDuration(sw, ((int) cmd.CommandTimeout) * 1000 - 100, 500);
144-
}
114+
var ex = Assert.Throws<MySqlException>(cmd.ExecuteReader);
115+
sw.Stop();
116+
Assert.Contains(c_timeoutMessage, ex.Message, StringComparison.OrdinalIgnoreCase);
117+
TestUtilities.AssertDuration(sw, ((int) cmd.CommandTimeout) * 1000 - 100, 500);
145118
}
146119

147120
[SkippableFact(ServerFeatures.Timeout, Baseline = "https://bugs.mysql.com/bug.php?id=87307")]
@@ -270,19 +243,10 @@ public void TransactionCommandTimeoutWithSleepSync()
270243
{
271244
cmd.CommandTimeout = 2;
272245
var sw = Stopwatch.StartNew();
273-
try
274-
{
275-
using var reader = cmd.ExecuteReader();
276-
277-
// shouldn't get here
278-
Assert.True(false);
279-
}
280-
catch (MySqlException ex)
281-
{
282-
sw.Stop();
283-
Assert.Contains(c_timeoutMessage, ex.Message, StringComparison.OrdinalIgnoreCase);
284-
TestUtilities.AssertDuration(sw, cmd.CommandTimeout * 1000 - 100, 500);
285-
}
246+
var ex = Assert.Throws<MySqlException>(cmd.ExecuteReader);
247+
sw.Stop();
248+
Assert.Contains(c_timeoutMessage, ex.Message, StringComparison.OrdinalIgnoreCase);
249+
TestUtilities.AssertDuration(sw, cmd.CommandTimeout * 1000 - 100, 500);
286250
}
287251

288252
Assert.Equal(ConnectionState.Closed, m_connection.State);
@@ -296,18 +260,9 @@ public async Task TransactionCommandTimeoutWithSleepAsync()
296260
{
297261
cmd.CommandTimeout = 2;
298262
var sw = Stopwatch.StartNew();
299-
try
300-
{
301-
using var reader = await cmd.ExecuteReaderAsync();
302-
303-
// shouldn't get here
304-
Assert.True(false);
305-
}
306-
catch (MySqlException ex)
307-
{
308-
sw.Stop();
309-
Assert.Contains(c_timeoutMessage, ex.Message, StringComparison.OrdinalIgnoreCase);
310-
}
263+
var ex = await Assert.ThrowsAsync<MySqlException>(cmd.ExecuteReaderAsync);
264+
sw.Stop();
265+
Assert.Contains(c_timeoutMessage, ex.Message, StringComparison.OrdinalIgnoreCase);
311266
}
312267

313268
Assert.Equal(ConnectionState.Closed, m_connection.State);

tests/SideBySide/ConnectAsync.cs

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,9 @@ public async Task ConnectBadHost()
2828
};
2929
using var connection = new MySqlConnection(csb.ConnectionString);
3030
Assert.Equal(ConnectionState.Closed, connection.State);
31-
try
32-
{
33-
await connection.OpenAsync();
34-
Assert.True(false, "Exception not thrown");
35-
}
36-
catch (MySqlException ex)
37-
{
38-
Assert.Equal((int) MySqlErrorCode.UnableToConnectToHost, ex.Number);
39-
Assert.Equal((int) MySqlErrorCode.UnableToConnectToHost, ex.Data["Server Error Code"]);
40-
}
31+
var ex = await Assert.ThrowsAsync<MySqlException>(connection.OpenAsync);
32+
Assert.Equal((int) MySqlErrorCode.UnableToConnectToHost, ex.Number);
33+
Assert.Equal((int) MySqlErrorCode.UnableToConnectToHost, ex.Data["Server Error Code"]);
4134
Assert.Equal(ConnectionState.Closed, connection.State);
4235
}
4336

@@ -127,16 +120,9 @@ public async Task ConnectTimeoutAsync()
127120

128121
using var connection = new MySqlConnection(csb.ConnectionString);
129122
var stopwatch = Stopwatch.StartNew();
130-
try
131-
{
132-
await connection.OpenAsync();
133-
Assert.True(false);
134-
}
135-
catch (MySqlException ex)
136-
{
137-
Assert.Equal((int) MySqlErrorCode.UnableToConnectToHost, ex.Number);
138-
}
123+
var ex = await Assert.ThrowsAsync<MySqlException>(connection.OpenAsync);
139124
stopwatch.Stop();
125+
Assert.Equal((int) MySqlErrorCode.UnableToConnectToHost, ex.Number);
140126
TestUtilities.AssertDuration(stopwatch, 1900, 1500);
141127
}
142128

@@ -156,17 +142,10 @@ public async Task ConnectTimeoutAsyncCancellationToken()
156142

157143
using var connection = new MySqlConnection(csb.ConnectionString);
158144
var stopwatch = Stopwatch.StartNew();
159-
try
160-
{
161-
using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(2)))
162-
await connection.OpenAsync(cts.Token);
163-
Assert.True(false);
164-
}
165-
catch (MySqlException ex)
166-
{
167-
Assert.Equal((int) MySqlErrorCode.UnableToConnectToHost, ex.Number);
168-
}
145+
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(2));
146+
var ex = await Assert.ThrowsAsync<MySqlException>(async () => await connection.OpenAsync(cts.Token));
169147
stopwatch.Stop();
148+
Assert.Equal((int) MySqlErrorCode.UnableToConnectToHost, ex.Number);
170149
TestUtilities.AssertDuration(stopwatch, 1900, 1500);
171150
}
172151

tests/SideBySide/ConnectSync.cs

Lines changed: 12 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,9 @@ public void ConnectBadHost()
2525
};
2626
using var connection = new MySqlConnection(csb.ConnectionString);
2727
Assert.Equal(ConnectionState.Closed, connection.State);
28-
try
29-
{
30-
connection.Open();
31-
Assert.True(false, "Exception not thrown");
32-
}
33-
catch (MySqlException ex)
34-
{
35-
Assert.Equal((int) MySqlErrorCode.UnableToConnectToHost, ex.Number);
36-
Assert.Equal((int) MySqlErrorCode.UnableToConnectToHost, ex.Data["Server Error Code"]);
37-
}
28+
var ex = Assert.Throws<MySqlException>(connection.Open);
29+
Assert.Equal((int) MySqlErrorCode.UnableToConnectToHost, ex.Number);
30+
Assert.Equal((int) MySqlErrorCode.UnableToConnectToHost, ex.Data["Server Error Code"]);
3831
Assert.Equal(ConnectionState.Closed, connection.State);
3932
}
4033

@@ -70,21 +63,11 @@ public void ConnectBadDatabase()
7063
var csb = AppConfig.CreateConnectionStringBuilder();
7164
csb.Database = "wrong_database";
7265
using var connection = new MySqlConnection(csb.ConnectionString);
73-
try
74-
{
75-
connection.Open();
76-
Assert.True(false);
77-
}
78-
catch (MySqlException ex)
79-
{
80-
#if BASELINE
81-
// https://bugs.mysql.com/bug.php?id=78426
82-
Assert.NotNull(ex);
83-
#else
84-
if (AppConfig.SupportedFeatures.HasFlag(ServerFeatures.ErrorCodes) || ex.Number != 0)
85-
Assert.Equal((int) MySqlErrorCode.UnknownDatabase, ex.Number);
66+
var ex = Assert.Throws<MySqlException>(connection.Open);
67+
#if !BASELINE // https://bugs.mysql.com/bug.php?id=78426
68+
if (AppConfig.SupportedFeatures.HasFlag(ServerFeatures.ErrorCodes) || ex.Number != 0)
69+
Assert.Equal((int) MySqlErrorCode.UnknownDatabase, ex.Number);
8670
#endif
87-
}
8871
Assert.Equal(ConnectionState.Closed, connection.State);
8972
}
9073

@@ -94,21 +77,11 @@ public void ConnectBadPassword()
9477
var csb = AppConfig.CreateConnectionStringBuilder();
9578
csb.Password = "wrong";
9679
using var connection = new MySqlConnection(csb.ConnectionString);
97-
try
98-
{
99-
connection.Open();
100-
Assert.True(false);
101-
}
102-
catch (MySqlException ex)
103-
{
104-
#if BASELINE
105-
// https://bugs.mysql.com/bug.php?id=73610
106-
Assert.NotNull(ex);
107-
#else
108-
if (AppConfig.SupportedFeatures.HasFlag(ServerFeatures.ErrorCodes) || ex.Number != 0)
109-
Assert.Equal((int) MySqlErrorCode.AccessDenied, ex.Number);
80+
var ex = Assert.Throws<MySqlException>(connection.Open);
81+
#if !BASELINE // https://bugs.mysql.com/bug.php?id=78426
82+
if (AppConfig.SupportedFeatures.HasFlag(ServerFeatures.ErrorCodes) || ex.Number != 0)
83+
Assert.Equal((int) MySqlErrorCode.AccessDenied, ex.Number);
11084
#endif
111-
}
11285
Assert.Equal(ConnectionState.Closed, connection.State);
11386
}
11487

@@ -137,14 +110,7 @@ public void NonExistentPipe()
137110

138111
var sw = Stopwatch.StartNew();
139112
using var connection = new MySqlConnection(csb.ConnectionString);
140-
try
141-
{
142-
connection.Open();
143-
Assert.False(true);
144-
}
145-
catch (MySqlException)
146-
{
147-
}
113+
Assert.Throws<MySqlException>(connection.Open);
148114
#if !BASELINE
149115
TestUtilities.AssertDuration(sw, 900, 500);
150116
#else

tests/SideBySide/PreparedCommandTests.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -313,15 +313,8 @@ public void CannotUse64KParameters()
313313
{
314314
using var connection = CreateConnectionWithTableOfIntegers();
315315
using var cmd = CreateCommandWithParameters(connection, 65536);
316-
try
317-
{
318-
cmd.Prepare();
319-
Assert.False(true, "Exception wasn't thrown");
320-
}
321-
catch (MySqlException ex)
322-
{
323-
Assert.Equal(MySqlErrorCode.PreparedStatementManyParameters, (MySqlErrorCode) ex.Number);
324-
}
316+
var ex = Assert.Throws<MySqlException>(cmd.Prepare);
317+
Assert.Equal(MySqlErrorCode.PreparedStatementManyParameters, (MySqlErrorCode) ex.Number);
325318
}
326319

327320
private static MySqlConnection CreateConnectionWithTableOfIntegers()

tests/SideBySide/QueryTests.cs

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -208,37 +208,14 @@ public async Task InvalidSql()
208208
using (var cmd = m_database.Connection.CreateCommand())
209209
{
210210
cmd.CommandText = @"select id from query_invalid_sql limit 1 where id is not null";
211-
try
212-
{
213-
await cmd.ExecuteNonQueryAsync();
214-
Assert.True(false, "Exception should have been thrown.");
215-
}
216-
catch (MySqlException ex)
217-
{
218-
Assert.Equal((int) MySqlErrorCode.ParseError, ex.Number);
219-
}
211+
var ex = await Assert.ThrowsAsync<MySqlException>(cmd.ExecuteNonQueryAsync);
212+
Assert.Equal((int) MySqlErrorCode.ParseError, ex.Number);
220213

221-
try
222-
{
223-
using (var reader = await cmd.ExecuteReaderAsync())
224-
{
225-
}
226-
Assert.True(false, "Exception should have been thrown.");
227-
}
228-
catch (MySqlException ex)
229-
{
230-
Assert.Equal((int) MySqlErrorCode.ParseError, ex.Number);
231-
}
214+
ex = await Assert.ThrowsAsync<MySqlException>(cmd.ExecuteReaderAsync);
215+
Assert.Equal((int) MySqlErrorCode.ParseError, ex.Number);
232216

233-
try
234-
{
235-
await cmd.ExecuteScalarAsync();
236-
Assert.True(false, "Exception should have been thrown.");
237-
}
238-
catch (MySqlException ex)
239-
{
240-
Assert.Equal((int) MySqlErrorCode.ParseError, ex.Number);
241-
}
217+
ex = await Assert.ThrowsAsync<MySqlException>(cmd.ExecuteScalarAsync);
218+
Assert.Equal((int) MySqlErrorCode.ParseError, ex.Number);
242219
}
243220

244221
using (var cmd = m_database.Connection.CreateCommand())

0 commit comments

Comments
 (0)