Skip to content

Commit

Permalink
Update for tests and general improvements
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
cpoDesign and bmarshalsea committed Nov 21, 2023
1 parent 169ad23 commit 6b7cafd
Show file tree
Hide file tree
Showing 50 changed files with 2,658 additions and 1,060 deletions.
26 changes: 21 additions & 5 deletions APITestingRunner.Unit.Tests/APITestingRunner.Unit.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
Expand All @@ -7,13 +7,29 @@

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<IsPublishable>False</IsPublishable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="coverlet.collector" Version="3.2.0" />
<Resource Include="SampleDb.mdf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
<Resource Include="SampleDb_log.ldf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.69" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="WireMock.Net" Version="1.5.40" />
</ItemGroup>

<ItemGroup>
Expand Down
178 changes: 127 additions & 51 deletions APITestingRunner.Unit.Tests/DataAccessTests.cs
Original file line number Diff line number Diff line change
@@ -1,74 +1,150 @@
using static ConfigurationManager;
using APITestingRunner.Database;
using APITestingRunner.Excetions;
using FluentAssertions;
using Microsoft.Extensions.Logging;
using Moq;
using static ConfigurationManager;

namespace APITestingRunner.Unit.Tests
{
namespace APITestingRunner.Unit.Tests {
[TestClass]
public class DataAccessTests
{
private Config? _config;
public class DataAccessTests : TestBase {
private readonly Config _config = new() {
UrlBase = "http://localhost:5152/",
CompareUrlBase = string.Empty,
CompareUrlPath = string.Empty,
UrlPath = "/Data",
UrlParam = new List<Param>
{
new Param("urlKey", "test"),
new Param("id", "sqlId")
},
HeaderParam = new List<Param> {
new Param("accept","application/json")
},
RequestBody = null,
DBConnectionString = "Server=127.0.0.1; Database=test; User Id=sa; Password=<YourStrong@Passw0rd>;TrustServerCertificate=True;",
DBQuery = "select id as sqlId from dbo.sampleTable;",
DBFields = new List<Param>
{
new Param("sqlId", "sqlId")
},
RequestType = RequestType.GET,
ResultsStoreOption = StoreResultsOption.All,
ConfigMode = TesterConfigMode.Run,
OutputLocation = DirectoryServices.AssemblyDirectory
};

[TestInitialize]
public void TestInit()
{
_config = new()
{
UrlBase = "http://localhost:5152/",
CompareUrlBase = string.Empty,
CompareUrlPath = string.Empty,
UrlPath = "/Data",
UrlParam = new List<Param>
{
new Param("urlKey", "test"),
new Param("id", "sqlId")
},
HeaderParam = new List<Param> {
new Param("accept","application/json")
},
RequestBody = null,
DBConnectionString = "Server=127.0.0.1; Database=test; User Id=sa; Password=<YourStrong@Passw0rd>;TrustServerCertificate=True;",
DBQuery = "select id as sqlId from dbo.sampleTable;",
DBFields = new List<Param>
{
new Param("sqlId", "sqlId")
},
RequestType = RequestType.GET,
ResultsStoreOption = StoreResultsOption.All,
ConfigMode = TesterConfigMode.Run,
LogLocation = DirectoryServices.AssemblyDirectory
};
public void TestInit() {

}

[TestMethod]
public void DataAccess_Tests_ConstructorShouldPass()
{
_ = new DataAccess(_config);
public void DataAccess_Tests_ConstructorShouldPass() {
_ = new DataAccess(_config, new Mock<ILogger>().Object);
}

//Type Safety takes care of this - shouldn't be needed.
[TestMethod]
public void DataAccess_Tests_MissingConfig_ConstructorShouldThrowArgumentNullException() {
_ = Assert.ThrowsException<ArgumentNullException>(() => new DataAccess(null, null));
}
[TestMethod]
public void DataAccess_Tests_ConstructorShouldThrowArgumentNullException()
{
_ = Assert.ThrowsException<ArgumentNullException>(() => new DataAccess(null));
public void DataAccess_Tests_MissingLogger_ConstructorShouldThrowArgumentNullException() {
_ = Assert.ThrowsException<ArgumentNullException>(() => new DataAccess(_config, null));
}

[TestMethod]
public async Task FetchDataForRunnerAsync_PassNullForConnectionString_shouldThrowConfigurationErrorsException()
{
public async Task FetchDataForRunnerAsync_PassNullForConnectionString_shouldThrowConfigurationErrorsException() {
Config testConfig = _config;
try
{
try {
testConfig.DBConnectionString = null;
DataAccess da = new(testConfig);
DataAccess da = new(testConfig, new Mock<Logger>().Object);
_ = await da.FetchDataForRunnerAsync();
Assert.Fail();
}
catch (TestRunnerConfigurationErrorsException ex)
{
} catch (TestRunnerConfigurationErrorsException ex) {
Assert.AreEqual("Failed to load connection string", ex.Message);
}
catch
{
} catch {
Assert.Fail();
}
}

[TestMethod]
public async Task FetchDataForRunner_GetDataFromDatabase_ShouldReturn_DataSet_withOneFieldFromDbForBinder() {
_config.DBConnectionString = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\code\\cpoDesign\\APITestingRunner\\APITestingRunner.Unit.Tests\\SampleDb.mdf;Integrated Security=True";

var data = new DataAccess(_config, new Mock<Logger>().Object);

var records = await data.FetchDataForRunnerAsync();

_ = records.Should().NotBeEmpty();
_ = records.Should().HaveCount(3);

_ = records.Last().Should().BeEquivalentTo(new DataQueryResult {
RowId = 3,
Results = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("sqlId", "3"),
}
});

_ = records.Last().Results.Should().HaveCount(1);
}

[TestMethod]
public async Task FetchDataForRunner_GetDataFromDatabase_ShouldReturn_EmptyDataSet_withOneFieldFromDbForBinder() {
_config.DBConnectionString = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\code\\cpoDesign\\APITestingRunner\\APITestingRunner.Unit.Tests\\SampleDb.mdf;Integrated Security=True";

_config.DBQuery = "select id as sqlId from dbo.sampleTable where id>5;";
var data = new DataAccess(_config, new Mock<Logger>().Object);

var records = await data.FetchDataForRunnerAsync();

_ = records.Should().BeEmpty();
}


[TestMethod]
public async Task FetchDataForRunner_GetDataFromDatabase_ShouldReturn_SingleFieldDataSet_withOneFieldFromDbForBinder() {
_config.DBConnectionString = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\code\\cpoDesign\\APITestingRunner\\APITestingRunner.Unit.Tests\\SampleDb.mdf;Integrated Security=True";

_config.DBQuery = "select id as sqlId, name as fieldName from dbo.sampleTable";
var data = new DataAccess(_config, new Mock<Logger>().Object);

var records = await data.FetchDataForRunnerAsync();

_ = records.Should().NotBeEmpty();
_ = records.Should().HaveCount(3);

_ = records.Last().Should().BeEquivalentTo(new DataQueryResult {
RowId = 3,
Results = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("sqlId", "3"),
}
});

_ = records.Last().Results.Should().HaveCount(1);
}

[TestMethod]
public async Task FetchDataForRunner_GetDataFromDatabase_ShouldReturn_TwoFieldDataSet_withOneFieldFromDbForBinder() {
_config.DBConnectionString = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\code\\cpoDesign\\APITestingRunner\\APITestingRunner.Unit.Tests\\SampleDb.mdf;Integrated Security=True";

_config.DBFields = new List<Param>
{
new Param("sqlId", "sqlId"),
new Param("fieldName", "fieldName")
};

_config.DBQuery = "select id as sqlId, name as fieldName from dbo.sampleTable";
var data = new DataAccess(_config, new Mock<Logger>().Object);

var records = await data.FetchDataForRunnerAsync();

_ = records.Should().NotBeEmpty();
_ = records.Should().HaveCount(3);

_ = records.Last().RowId.Should().Be(3);
_ = records.Last().Results.Should().HaveCount(2);
_ = records.Last().Results.First().Should().BeEquivalentTo(new KeyValuePair<string, string>("sqlId", "3"));
_ = records.Last().Results.Last().Should().BeEquivalentTo(new KeyValuePair<string, string>("fieldName", "Linux"), because: "");
}
}
}
Loading

0 comments on commit 6b7cafd

Please # to comment.