Skip to content

Commit 3ccfcdd

Browse files
committed
Clean Up Tests To C#8, Removed Unsed, Get Passing
1 parent e0e25e3 commit 3ccfcdd

2 files changed

Lines changed: 33 additions & 26 deletions

File tree

src/PostgreSQLCopyHelper.Test/Issues/Issue58_CancelRequest_Test.cs

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System.Collections.Generic;
2-
using System.Linq;
3-
using System.Runtime.CompilerServices;
42
using System.Threading;
53
using System.Threading.Tasks;
64
using Npgsql;
@@ -34,11 +32,11 @@ public Task Test_CanceledBulkInsertThrowsWhenCanceledBeforeStarting()
3432
.MapInteger("Id", x => x.Id)
3533
.MapText("Name", x => x.Name);
3634

37-
using (var cts = new CancellationTokenSource(1))
35+
Assert.ThrowsAsync<TaskCanceledException>(async () =>
3836
{
39-
Assert.ThrowsAsync<TaskCanceledException>(async () =>
40-
await subject.SaveAllAsync(connection, FetchUserData(5), cts.Token), $"Should Throw Exception of Type {nameof(TaskCanceledException)}!");
41-
}
37+
using var cancellationTokenSource = new CancellationTokenSource(1);
38+
await subject.SaveAllAsync(connection, FetchUserData(5), cancellationTokenSource.Token);
39+
}, $"Should Throw Exception of Type {nameof(TaskCanceledException)}!");
4240

4341
return Task.CompletedTask;
4442
}
@@ -50,11 +48,11 @@ public Task Test_CanceledBulkInsertThrowsWhenCanceled()
5048
.MapInteger("Id", x => x.Id)
5149
.MapText("Name", x => x.Name);
5250

53-
using (var cts = new CancellationTokenSource(15))
51+
Assert.ThrowsAsync<TaskCanceledException>(async () =>
5452
{
55-
Assert.ThrowsAsync<TaskCanceledException>(async () =>
56-
await subject.SaveAllAsync(connection, FetchUserData(10), cts.Token), $"Should Throw Exception of Type {nameof(TaskCanceledException)}!");
57-
}
53+
using var cancellationTokenSource = new CancellationTokenSource(15);
54+
await subject.SaveAllAsync(connection, FetchUserData(10), cancellationTokenSource.Token);
55+
}, $"Should Throw Exception of Type {nameof(TaskCanceledException)}!");
5856

5957
return Task.CompletedTask;
6058
}
@@ -66,26 +64,39 @@ public async Task Test_CanceledBulkInsertDoesNotThrowWhenCancelledAfterCompletio
6664
.MapInteger("Id", x => x.Id)
6765
.MapText("Name", x => x.Name);
6866

69-
using (var cts = new CancellationTokenSource(50))
67+
var users = new List<User>();
68+
69+
await foreach (var user in FetchUserData(0))
7070
{
71-
var recordsSaved = await subject.SaveAllAsync(connection, FetchUserData(10), cts.Token);
72-
var result = connection.GetAll("sample", "\"TestUsers\"")
73-
.Cast<User>()
74-
.OrderBy(x => x.Id)
75-
.ToList();
76-
77-
Assert.AreEqual(2, recordsSaved);
78-
Assert.AreEqual(1, result.First().Id);
79-
Assert.AreEqual(2, result.Last().Id);
71+
users.Add(user);
8072
}
73+
74+
using var cancellationTokenSource = new CancellationTokenSource(100);
75+
var recordsSaved = await subject.SaveAllAsync(connection, FetchUserData(1), cancellationTokenSource.Token);
76+
77+
var result = connection.GetAll("sample", "TestUsers");
78+
79+
Assert.AreEqual(2, result.Count);
80+
Assert.AreEqual(2, recordsSaved);
81+
82+
Assert.IsNotNull(result[0][0]);
83+
Assert.IsNotNull(result[1][0]);
84+
85+
Assert.AreEqual(users[0].Id, (int) result[0][0]);
86+
Assert.AreEqual(users[0].Name, (string) result[0][1]);
87+
88+
Assert.AreEqual(users[1].Id, (int) result[1][0]);
89+
Assert.AreEqual(users[1].Name, (string) result[1][1]);
90+
91+
Assert.AreEqual(2, recordsSaved);
8192
}
8293

83-
private static async IAsyncEnumerable<User> FetchUserData(int delayMillis, [EnumeratorCancellation] CancellationToken cancellationToken = default)
94+
private static async IAsyncEnumerable<User> FetchUserData(int delayMillis)
8495
{
8596
for (var i = 1; i <= 2; i++)
8697
{
8798
// Simulate waiting for data to come through.
88-
await Task.Delay(delayMillis, cancellationToken);
99+
await Task.Delay(delayMillis);
89100
yield return new User
90101
{
91102
Id = i,

src/PostgreSQLCopyHelper.Test/PostgreSQLCopyHelper.Test.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,4 @@
1515
<ProjectReference Include="..\PostgreSQLCopyHelper\PostgreSQLCopyHelper.csproj" />
1616
</ItemGroup>
1717

18-
<ItemGroup>
19-
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
20-
</ItemGroup>
21-
2218
</Project>

0 commit comments

Comments
 (0)