-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTestBulkInsert.cs
More file actions
138 lines (130 loc) · 4.94 KB
/
TestBulkInsert.cs
File metadata and controls
138 lines (130 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using NUnit.Framework;
using System;
using System.Collections.Generic;
using Microsoft.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Dapper;
namespace Frends.Community.SQL.Tests
{
[TestFixture]
public class TestBulkInsert
{
/*
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=Salakala123!" -p 1433:1433 --name sql1 --hostname sql1 -d mcr.microsoft.com/mssql/server:2019-CU18-ubuntu-20.04
with Git bash add winpty to the start of
docker exec -it sql1 "bash"
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "Salakala123!"
Check rows before CleanUp:
SELECT * FROM TestTable
GO
Optional queries:
SELECT Name FROM sys.Databases;
GO
SELECT * FROM INFORMATION_SCHEMA.TABLES;
GO
*/
private readonly string ConnectionString = "Server=127.0.0.1,1433;Database=Master;User Id=SA;Password=Salakala123!;TrustServerCertificate=True";
// The test case creates and destroys an SQL table with this name.
// Ensure that this table does not already exist in the DB! Test aborts if it does.
private readonly string TableName = "FrendsTestTable";
private readonly struct DbRow
{
public readonly int Id;
public readonly string FirstName;
public readonly string LastName;
public DbRow(int Id, string FirstName, string LastName)
{
this.Id = Id;
this.FirstName = FirstName;
this.LastName = LastName;
}
}
// Schema for the test table
private readonly DataColumn[] Columns = typeof(DbRow).GetFields().Select(field => new DataColumn(field.Name, field.FieldType)).ToArray();
[SetUp]
public void SetUp()
{
// Map C# types to SQL types
var typeMap = new Dictionary<Type, string>
{
[typeof(int)] = "int",
[typeof(string)] = "varchar(255)"
};
// Create SQL table with the test harness schema
using (var connection = new SqlConnection(ConnectionString))
{
connection.Open();
var cmd = connection.CreateCommand();
cmd.CommandText = $@"
CREATE TABLE {TableName} ({string.Join(
",",
Columns.Select(column => $"{column.ColumnName} {typeMap[column.DataType]}")
)});";
cmd.ExecuteNonQuery();
}
}
[TearDown]
public void TearDown()
{
// Drop test table
using (var connection = new SqlConnection(ConnectionString))
{
connection.Open();
var cmd = connection.CreateCommand();
cmd.CommandText = $"DROP TABLE {TableName};";
cmd.ExecuteNonQuery();
}
}
[Test]
public async Task TestBasicInsert()
{
var insertedData = new DataTable();
insertedData.Columns.AddRange(Columns);
foreach (var rowData in new[]
{
new DbRow(1, "Etu", "Suku" ),
new DbRow(2, "First", "Last"),
new DbRow(3, "Eka", "Name")
})
{
var row = insertedData.NewRow();
foreach (var (column, columnIndex) in Columns.Select((x, i) => (x, i)))
{
row[column.ColumnName] = rowData.GetType().GetField(column.ColumnName).GetValue(rowData);
}
insertedData.Rows.Add(row);
}
var result =
await
SQL.BulkInsertDataTable(
new BulkInsertInput()
{
ConnectionString = ConnectionString,
TableName = TableName,
InputData = insertedData
},
new BulkInsertOptions()
{
CommandTimeoutSeconds = 60,
FireTriggers = true,
KeepIdentity = false,
SqlTransactionIsolationLevel = SqlTransactionIsolationLevel.ReadCommitted
}, CancellationToken.None);
Assert.AreEqual(3, result);
var tableRows = GetTableRows().ToArray();
Assert.AreEqual(3, tableRows.Length);
Assert.AreEqual("Suku", tableRows[0].LastName);
}
private IEnumerable<DbRow> GetTableRows()
{
using (var connection = new SqlConnection(ConnectionString))
{
connection.Open();
return connection.Query<DbRow>($"SELECT * FROM {TableName}");
}
}
}
}