Skip to content

Commit 65aac9f

Browse files
committed
Create Issue56_AsyncEnumerable_Test.cs
1 parent c1a26ba commit 65aac9f

1 file changed

Lines changed: 87 additions & 0 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using Npgsql;
4+
using NUnit.Framework;
5+
using PostgreSQLCopyHelper.Test.Extensions;
6+
7+
namespace PostgreSQLCopyHelper.Test.Issues
8+
{
9+
[TestFixture]
10+
[Description("A Unit Test to see, if PostgreSQLCopyHelper works with AsyncEnumerable.")]
11+
public class Issue56_AsyncEnumerable_Test : TransactionalTestBase
12+
{
13+
private class User
14+
{
15+
public int Id { get; set; }
16+
17+
public string Name { get; set; }
18+
}
19+
20+
private PostgreSQLCopyHelper<User> subject;
21+
22+
protected override void OnSetupInTransaction()
23+
{
24+
CreateTable();
25+
}
26+
27+
[Test]
28+
public async Task Test_AsyncEnumerable_BulkInsert()
29+
{
30+
subject = new PostgreSQLCopyHelper<User>("sample", "TestUsers")
31+
.MapInteger("Id", x => x.Id)
32+
.MapText("Name", x => x.Name);
33+
34+
var recordsSaved = new List<User>();
35+
36+
await foreach (var user in FetchUserData())
37+
{
38+
recordsSaved.Add(user);
39+
}
40+
41+
// Try to work with the Bulk Inserter:
42+
await subject.SaveAllAsync(connection, FetchUserData());
43+
44+
var result = connection.GetAll("sample", "TestUsers");
45+
46+
// Check if we have the amount of rows:
47+
Assert.AreEqual(2, result.Count);
48+
Assert.AreEqual(2, recordsSaved.Count);
49+
50+
Assert.IsNotNull(result[0][0]);
51+
Assert.IsNotNull(result[1][0]);
52+
53+
Assert.AreEqual(recordsSaved[0].Id, (int) result[0][0]);
54+
Assert.AreEqual(recordsSaved[0].Name, (string) result[0][1]);
55+
56+
Assert.AreEqual(recordsSaved[1].Id, (int) result[1][0]);
57+
Assert.AreEqual(recordsSaved[1].Name, (string) result[1][1]);
58+
}
59+
60+
private static async IAsyncEnumerable<User> FetchUserData()
61+
{
62+
for (var i = 1; i <= 2; i++)
63+
{
64+
// Simulate waiting for data to come through.
65+
await Task.Delay(10);
66+
yield return new User
67+
{
68+
Id = i,
69+
Name = $"Username {i}"
70+
};
71+
}
72+
}
73+
74+
private int CreateTable()
75+
{
76+
var sqlStatement = @"CREATE TABLE sample.TestUsers
77+
(
78+
Id integer,
79+
Name text
80+
);";
81+
82+
var sqlCommand = new NpgsqlCommand(sqlStatement, connection);
83+
84+
return sqlCommand.ExecuteNonQuery();
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)