Skip to content

Commit 6b7cafd

Browse files
Update for tests and general improvements
* Rewire with tests * Added tests for comparision behaviour * Updated installation * Updated readme together * Updated to latest version (not running tests) * Remove Warnings - APITestRunner * Remove Warnings - APITestRunner.Unit.Tests * Updated definition * Fixed processing of file result to display only when should be added * Matching on simple file fixed * Added support for different request types & enhanced logger output for request type * Updated implementation to latest version * Added initial command line binding parameters * Updated logger to be passed through constructor +added command line config * Updated startup settings added additional logging to show issues to users for database connection string debugging * Pushed logger down to data access clean up structure * Updated implemenation * Updated implementation and sample --------- Co-authored-by: Benjamin Marshalsea <marshalsea@hotmail.co.uk>
1 parent 169ad23 commit 6b7cafd

50 files changed

Lines changed: 2658 additions & 1060 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

APITestingRunner.Unit.Tests/APITestingRunner.Unit.Tests.csproj

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>net7.0</TargetFramework>
@@ -7,13 +7,29 @@
77

88
<IsPackable>false</IsPackable>
99
<IsTestProject>true</IsTestProject>
10+
<IsPublishable>False</IsPublishable>
1011
</PropertyGroup>
1112

1213
<ItemGroup>
13-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
14-
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
15-
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
16-
<PackageReference Include="coverlet.collector" Version="3.2.0" />
14+
<Resource Include="SampleDb.mdf">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</Resource>
17+
<Resource Include="SampleDb_log.ldf">
18+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19+
</Resource>
20+
</ItemGroup>
21+
22+
<ItemGroup>
23+
<PackageReference Include="FluentAssertions" Version="6.12.0" />
24+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
25+
<PackageReference Include="Moq" Version="4.20.69" />
26+
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
27+
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
28+
<PackageReference Include="coverlet.collector" Version="6.0.0">
29+
<PrivateAssets>all</PrivateAssets>
30+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
31+
</PackageReference>
32+
<PackageReference Include="WireMock.Net" Version="1.5.40" />
1733
</ItemGroup>
1834

1935
<ItemGroup>
Lines changed: 127 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,150 @@
1-
using static ConfigurationManager;
1+
using APITestingRunner.Database;
2+
using APITestingRunner.Excetions;
3+
using FluentAssertions;
4+
using Microsoft.Extensions.Logging;
5+
using Moq;
6+
using static ConfigurationManager;
27

3-
namespace APITestingRunner.Unit.Tests
4-
{
8+
namespace APITestingRunner.Unit.Tests {
59
[TestClass]
6-
public class DataAccessTests
7-
{
8-
private Config? _config;
10+
public class DataAccessTests : TestBase {
11+
private readonly Config _config = new() {
12+
UrlBase = "http://localhost:5152/",
13+
CompareUrlBase = string.Empty,
14+
CompareUrlPath = string.Empty,
15+
UrlPath = "/Data",
16+
UrlParam = new List<Param>
17+
{
18+
new Param("urlKey", "test"),
19+
new Param("id", "sqlId")
20+
},
21+
HeaderParam = new List<Param> {
22+
new Param("accept","application/json")
23+
},
24+
RequestBody = null,
25+
DBConnectionString = "Server=127.0.0.1; Database=test; User Id=sa; Password=<YourStrong@Passw0rd>;TrustServerCertificate=True;",
26+
DBQuery = "select id as sqlId from dbo.sampleTable;",
27+
DBFields = new List<Param>
28+
{
29+
new Param("sqlId", "sqlId")
30+
},
31+
RequestType = RequestType.GET,
32+
ResultsStoreOption = StoreResultsOption.All,
33+
ConfigMode = TesterConfigMode.Run,
34+
OutputLocation = DirectoryServices.AssemblyDirectory
35+
};
936

1037
[TestInitialize]
11-
public void TestInit()
12-
{
13-
_config = new()
14-
{
15-
UrlBase = "http://localhost:5152/",
16-
CompareUrlBase = string.Empty,
17-
CompareUrlPath = string.Empty,
18-
UrlPath = "/Data",
19-
UrlParam = new List<Param>
20-
{
21-
new Param("urlKey", "test"),
22-
new Param("id", "sqlId")
23-
},
24-
HeaderParam = new List<Param> {
25-
new Param("accept","application/json")
26-
},
27-
RequestBody = null,
28-
DBConnectionString = "Server=127.0.0.1; Database=test; User Id=sa; Password=<YourStrong@Passw0rd>;TrustServerCertificate=True;",
29-
DBQuery = "select id as sqlId from dbo.sampleTable;",
30-
DBFields = new List<Param>
31-
{
32-
new Param("sqlId", "sqlId")
33-
},
34-
RequestType = RequestType.GET,
35-
ResultsStoreOption = StoreResultsOption.All,
36-
ConfigMode = TesterConfigMode.Run,
37-
LogLocation = DirectoryServices.AssemblyDirectory
38-
};
38+
public void TestInit() {
39+
3940
}
4041

4142
[TestMethod]
42-
public void DataAccess_Tests_ConstructorShouldPass()
43-
{
44-
_ = new DataAccess(_config);
43+
public void DataAccess_Tests_ConstructorShouldPass() {
44+
_ = new DataAccess(_config, new Mock<ILogger>().Object);
4545
}
4646

47+
//Type Safety takes care of this - shouldn't be needed.
48+
[TestMethod]
49+
public void DataAccess_Tests_MissingConfig_ConstructorShouldThrowArgumentNullException() {
50+
_ = Assert.ThrowsException<ArgumentNullException>(() => new DataAccess(null, null));
51+
}
4752
[TestMethod]
48-
public void DataAccess_Tests_ConstructorShouldThrowArgumentNullException()
49-
{
50-
_ = Assert.ThrowsException<ArgumentNullException>(() => new DataAccess(null));
53+
public void DataAccess_Tests_MissingLogger_ConstructorShouldThrowArgumentNullException() {
54+
_ = Assert.ThrowsException<ArgumentNullException>(() => new DataAccess(_config, null));
5155
}
5256

5357
[TestMethod]
54-
public async Task FetchDataForRunnerAsync_PassNullForConnectionString_shouldThrowConfigurationErrorsException()
55-
{
58+
public async Task FetchDataForRunnerAsync_PassNullForConnectionString_shouldThrowConfigurationErrorsException() {
5659
Config testConfig = _config;
57-
try
58-
{
60+
try {
5961
testConfig.DBConnectionString = null;
60-
DataAccess da = new(testConfig);
62+
DataAccess da = new(testConfig, new Mock<Logger>().Object);
6163
_ = await da.FetchDataForRunnerAsync();
6264
Assert.Fail();
63-
}
64-
catch (TestRunnerConfigurationErrorsException ex)
65-
{
65+
} catch (TestRunnerConfigurationErrorsException ex) {
6666
Assert.AreEqual("Failed to load connection string", ex.Message);
67-
}
68-
catch
69-
{
67+
} catch {
7068
Assert.Fail();
7169
}
7270
}
71+
72+
[TestMethod]
73+
public async Task FetchDataForRunner_GetDataFromDatabase_ShouldReturn_DataSet_withOneFieldFromDbForBinder() {
74+
_config.DBConnectionString = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\code\\cpoDesign\\APITestingRunner\\APITestingRunner.Unit.Tests\\SampleDb.mdf;Integrated Security=True";
75+
76+
var data = new DataAccess(_config, new Mock<Logger>().Object);
77+
78+
var records = await data.FetchDataForRunnerAsync();
79+
80+
_ = records.Should().NotBeEmpty();
81+
_ = records.Should().HaveCount(3);
82+
83+
_ = records.Last().Should().BeEquivalentTo(new DataQueryResult {
84+
RowId = 3,
85+
Results = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("sqlId", "3"),
86+
}
87+
});
88+
89+
_ = records.Last().Results.Should().HaveCount(1);
90+
}
91+
92+
[TestMethod]
93+
public async Task FetchDataForRunner_GetDataFromDatabase_ShouldReturn_EmptyDataSet_withOneFieldFromDbForBinder() {
94+
_config.DBConnectionString = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\code\\cpoDesign\\APITestingRunner\\APITestingRunner.Unit.Tests\\SampleDb.mdf;Integrated Security=True";
95+
96+
_config.DBQuery = "select id as sqlId from dbo.sampleTable where id>5;";
97+
var data = new DataAccess(_config, new Mock<Logger>().Object);
98+
99+
var records = await data.FetchDataForRunnerAsync();
100+
101+
_ = records.Should().BeEmpty();
102+
}
103+
104+
105+
[TestMethod]
106+
public async Task FetchDataForRunner_GetDataFromDatabase_ShouldReturn_SingleFieldDataSet_withOneFieldFromDbForBinder() {
107+
_config.DBConnectionString = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\code\\cpoDesign\\APITestingRunner\\APITestingRunner.Unit.Tests\\SampleDb.mdf;Integrated Security=True";
108+
109+
_config.DBQuery = "select id as sqlId, name as fieldName from dbo.sampleTable";
110+
var data = new DataAccess(_config, new Mock<Logger>().Object);
111+
112+
var records = await data.FetchDataForRunnerAsync();
113+
114+
_ = records.Should().NotBeEmpty();
115+
_ = records.Should().HaveCount(3);
116+
117+
_ = records.Last().Should().BeEquivalentTo(new DataQueryResult {
118+
RowId = 3,
119+
Results = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("sqlId", "3"),
120+
}
121+
});
122+
123+
_ = records.Last().Results.Should().HaveCount(1);
124+
}
125+
126+
[TestMethod]
127+
public async Task FetchDataForRunner_GetDataFromDatabase_ShouldReturn_TwoFieldDataSet_withOneFieldFromDbForBinder() {
128+
_config.DBConnectionString = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\code\\cpoDesign\\APITestingRunner\\APITestingRunner.Unit.Tests\\SampleDb.mdf;Integrated Security=True";
129+
130+
_config.DBFields = new List<Param>
131+
{
132+
new Param("sqlId", "sqlId"),
133+
new Param("fieldName", "fieldName")
134+
};
135+
136+
_config.DBQuery = "select id as sqlId, name as fieldName from dbo.sampleTable";
137+
var data = new DataAccess(_config, new Mock<Logger>().Object);
138+
139+
var records = await data.FetchDataForRunnerAsync();
140+
141+
_ = records.Should().NotBeEmpty();
142+
_ = records.Should().HaveCount(3);
143+
144+
_ = records.Last().RowId.Should().Be(3);
145+
_ = records.Last().Results.Should().HaveCount(2);
146+
_ = records.Last().Results.First().Should().BeEquivalentTo(new KeyValuePair<string, string>("sqlId", "3"));
147+
_ = records.Last().Results.Last().Should().BeEquivalentTo(new KeyValuePair<string, string>("fieldName", "Linux"), because: "");
148+
}
73149
}
74150
}

0 commit comments

Comments
 (0)