Skip to content

Commit 64f8ca9

Browse files
Merge pull request #13 from FrendsPlatform/Issue11
Result update
2 parents 0bbbbd2 + 0540ec7 commit 64f8ca9

File tree

14 files changed

+507
-910
lines changed

14 files changed

+507
-910
lines changed

.github/workflows/BulkInsert_build_and_test_on_main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ jobs:
1313
uses: FrendsPlatform/FrendsTasks/.github/workflows/linux_build_main.yml@main
1414
with:
1515
workdir: Frends.MicrosoftSQL.BulkInsert
16-
prebuild_command: 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
16+
prebuild_command: docker-compose -f ./Frends.MicrosoftSQL.BulkInsert.Tests/docker-compose.yml up -d
1717
secrets:
1818
badge_service_api_key: ${{ secrets.BADGE_SERVICE_API_KEY }}

.github/workflows/BulkInsert_build_and_test_on_push.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
uses: FrendsPlatform/FrendsTasks/.github/workflows/linux_build_test.yml@main
1414
with:
1515
workdir: Frends.MicrosoftSQL.BulkInsert
16-
prebuild_command: 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
16+
prebuild_command: docker-compose -f ./Frends.MicrosoftSQL.BulkInsert.Tests/docker-compose.yml up -d
1717
secrets:
1818
badge_service_api_key: ${{ secrets.BADGE_SERVICE_API_KEY }}
1919
test_feed_api_key: ${{ secrets.TASKS_TEST_FEED_API_KEY }}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## [1.1.0] - 2023-01-26
4+
### Added
5+
- Options.ThrowErrorOnFailure and Result.ErrorMessage was added to let the user choose how to handle errors.
6+
37
## [1.0.0] - 2023-01-10
48
### Added
59
- Initial implementation
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using Frends.MicrosoftSQL.BulkInsert.Definitions;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
4+
namespace Frends.MicrosoftSQL.BulkInsert.Tests;
5+
6+
[TestClass]
7+
public class ExceptionUnitTests
8+
{
9+
/*
10+
docker-compose up
11+
12+
How to use via terminal:
13+
docker exec -it sql1 "bash"
14+
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "Salakala123!"
15+
SELECT * FROM TestTable
16+
GO
17+
*/
18+
19+
private static readonly string _tableName = "TestTable";
20+
private static readonly string _json = @"[
21+
{
22+
""Id"": 1,
23+
""Firstname"": ""Etu"",
24+
""Lastname"": ""Suku""
25+
}
26+
]";
27+
28+
[TestMethod]
29+
public async Task TestBulkInsert_Invalid_Creds()
30+
{
31+
var input = new Input()
32+
{
33+
ConnectionString = "Server=127.0.0.1,1433;Database=Master;User Id=SA;Password=WrongPassWord",
34+
TableName = _tableName,
35+
InputData = _json
36+
};
37+
38+
var options = new Options()
39+
{
40+
SqlTransactionIsolationLevel = SqlTransactionIsolationLevel.ReadCommitted,
41+
CommandTimeoutSeconds = 60,
42+
ThrowErrorOnFailure = true
43+
};
44+
45+
var ex = await Assert.ThrowsExceptionAsync<Exception>(() => MicrosoftSQL.BulkInsert(input, options, default));
46+
Assert.IsNotNull(ex.InnerException);
47+
Assert.IsTrue(ex.InnerException.Message.Contains("Login failed for user 'SA'."));
48+
}
49+
50+
[TestMethod]
51+
public async Task TestBulkInsert_Invalid_Creds_ReturnErrorMessage()
52+
{
53+
var input = new Input()
54+
{
55+
ConnectionString = "Server=127.0.0.1,1433;Database=Master;User Id=SA;Password=WrongPassWord",
56+
TableName = _tableName,
57+
InputData = _json
58+
};
59+
60+
var options = new Options()
61+
{
62+
SqlTransactionIsolationLevel = SqlTransactionIsolationLevel.ReadCommitted,
63+
CommandTimeoutSeconds = 2,
64+
ThrowErrorOnFailure = false
65+
};
66+
67+
var result = await MicrosoftSQL.BulkInsert(input, options, default);
68+
Assert.IsFalse(result.Success);
69+
Assert.IsTrue(result.ErrorMessage.Contains("Login failed for user 'SA'."));
70+
Assert.AreEqual(0, result.Count);
71+
}
72+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
using Frends.MicrosoftSQL.BulkInsert.Definitions;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using System.Data.SqlClient;
4+
5+
namespace Frends.MicrosoftSQL.BulkInsert.Tests;
6+
7+
[TestClass]
8+
public class ManualTests
9+
{
10+
/*
11+
docker-compose up
12+
13+
How to use via terminal:
14+
docker exec -it sql1 "bash"
15+
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "Salakala123!"
16+
SELECT * FROM TestTable
17+
GO
18+
*/
19+
20+
private static readonly string _connString = "Server=127.0.0.1,1433;Database=Master;User Id=SA;Password=Salakala123!";
21+
private static readonly string _tableName = "TestTable";
22+
private static readonly string _json = @"[
23+
{
24+
""Id"": 1,
25+
""Firstname"": ""Etu"",
26+
""Lastname"": ""Suku""
27+
},
28+
{
29+
""Id"": 2,
30+
""Firstname"": ""Suku"",
31+
""Lastname"": ""Etu""
32+
},
33+
{
34+
""Id"": 3,
35+
""Firstname"": ""Först"",
36+
""Lastname"": ""Lääst""
37+
}
38+
]";
39+
40+
readonly Input _input = new()
41+
{
42+
ConnectionString = _connString,
43+
TableName = _tableName,
44+
InputData = _json
45+
};
46+
47+
[TestInitialize]
48+
public void Init()
49+
{
50+
using var connection = new SqlConnection(_connString);
51+
connection.Open();
52+
var createTable = connection.CreateCommand();
53+
createTable.CommandText = $@"IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='{_tableName}') BEGIN CREATE TABLE {_tableName} ( Id int, LastName varchar(255), FirstName varchar(255) ); END";
54+
createTable.ExecuteNonQuery();
55+
connection.Close();
56+
connection.Dispose();
57+
}
58+
59+
[TestCleanup]
60+
public void CleanUp()
61+
{
62+
using var connection = new SqlConnection(_connString);
63+
connection.Open();
64+
var createTable = connection.CreateCommand();
65+
createTable.CommandText = $@"IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='{_tableName}') BEGIN DROP TABLE IF EXISTS {_tableName}; END";
66+
createTable.ExecuteNonQuery();
67+
connection.Close();
68+
connection.Dispose();
69+
}
70+
71+
// Add following line to BulkInsert.cs: 'throw new Exception();' before 'transaction.Commit();' (currently line 78).
72+
[Ignore("To run this test, comment this line after exception has been added to BulkInsert.cs.")]
73+
[TestMethod]
74+
public async Task TestBulkInsert_RollbackSuccess_ThrowErrorOnFailure_False()
75+
{
76+
var options = new Options()
77+
{
78+
SqlTransactionIsolationLevel = SqlTransactionIsolationLevel.Unspecified,
79+
CommandTimeoutSeconds = 2,
80+
ThrowErrorOnFailure = false
81+
};
82+
83+
84+
var result = await MicrosoftSQL.BulkInsert(_input, options, default);
85+
Assert.IsFalse(result.Success);
86+
Assert.AreEqual(0, result.Count);
87+
Assert.IsTrue(result.ErrorMessage.Contains("(If required) transaction rollback completed without exception."));
88+
Assert.AreEqual(0, GetRowCount());
89+
}
90+
91+
// Add following line to BulkInsert.cs: 'throw new Exception();' before 'transaction.Commit();' (currently line 78).
92+
[Ignore("To run this test, comment this line after exception has been added to BulkInsert.cs.")]
93+
[TestMethod]
94+
public async Task TestBulkInsert_RollbackInsert_ThrowErrorOnFailure_True()
95+
{
96+
var options = new Options()
97+
{
98+
SqlTransactionIsolationLevel = SqlTransactionIsolationLevel.Unspecified,
99+
CommandTimeoutSeconds = 2,
100+
ThrowErrorOnFailure = true
101+
};
102+
103+
var ex = await Assert.ThrowsExceptionAsync<Exception>(async () => await MicrosoftSQL.BulkInsert(_input, options, default));
104+
Assert.IsNotNull(ex.InnerException);
105+
Assert.IsTrue(ex.InnerException.Message.Contains("(If required) transaction rollback completed without exception."));
106+
Assert.Equals(0, GetRowCount());
107+
}
108+
109+
// Simple select query
110+
private static int GetRowCount()
111+
{
112+
using var connection = new SqlConnection(_connString);
113+
connection.Open();
114+
var getRows = connection.CreateCommand();
115+
getRows.CommandText = $"SELECT COUNT(*) FROM {_tableName}";
116+
var count = (int)getRows.ExecuteScalar();
117+
connection.Close();
118+
connection.Dispose();
119+
return count;
120+
}
121+
}

0 commit comments

Comments
 (0)