Skip to content

Commit 91078cb

Browse files
author
Jani Giannoudis
committed
refactored test runners
payroll test result: adde failed properties updated version to 0.6.0-beta.5
1 parent 4df8fcb commit 91078cb

10 files changed

+152
-247
lines changed

Client.Test/Case/CaseTestRunner.cs

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Globalization;
4-
using System.IO;
54
using System.Linq;
65
using System.Reflection;
76
using System.Threading.Tasks;
87
using PayrollEngine.Client.Model;
98
using PayrollEngine.Client.Service;
109
using PayrollEngine.Client.Service.Api;
11-
using PayrollEngine.Serialization;
1210
using Task = System.Threading.Tasks.Task;
1311

1412
namespace PayrollEngine.Client.Test.Case;
1513

1614
/// <summary>Case test runner</summary>
17-
public class CaseTestRunner : FileTestRunner
15+
public class CaseTestRunner : TestRunnerBase
1816
{
1917

2018
/// <summary>Parse case custom test class</summary>
@@ -36,21 +34,17 @@ protected override List<Type> GetDefaultReferences()
3634
}
3735
}
3836

39-
/// <summary>Initializes a new instance of the <see cref="CaseTestRunner"/> class</summary>
37+
/// <summary>Initializes a new instance of the class</summary>
4038
/// <param name="httpClient">The payroll engine http client</param>
41-
/// <param name="fileName">Name of the file</param>
42-
public CaseTestRunner(PayrollHttpClient httpClient, string fileName) :
43-
base(httpClient, fileName)
39+
public CaseTestRunner(PayrollHttpClient httpClient) :
40+
base(httpClient)
4441
{
4542
}
4643

4744
/// <summary>Test case</summary>
4845
/// <returns>The case test results</returns>
49-
public virtual async Task<CaseTestResult> TestAsync()
46+
public virtual async Task<CaseTestResult> TestAsync(CaseTest caseTest)
5047
{
51-
// load test
52-
var caseTest = await LoadCaseTest();
53-
5448
// test context
5549
var context = await CreateTestContext(caseTest);
5650

@@ -422,38 +416,6 @@ protected virtual async Task<CaseTestContext> CreateTestContext(CaseTest caseTes
422416
return context;
423417
}
424418

425-
/// <summary>Loads the case test</summary>
426-
protected virtual async Task<CaseTest> LoadCaseTest()
427-
{
428-
// import file
429-
if (!File.Exists(FileName))
430-
{
431-
throw new PayrollException($"Missing import file {FileName}");
432-
}
433-
var json = await File.ReadAllTextAsync(FileName);
434-
if (string.IsNullOrWhiteSpace(json))
435-
{
436-
throw new PayrollException($"Invalid import file {FileName}");
437-
}
438-
439-
// import from JSON
440-
var caseTest = DefaultJsonSerializer.Deserialize<CaseTest>(json);
441-
if (string.IsNullOrWhiteSpace(caseTest.TenantIdentifier))
442-
{
443-
throw new PayrollException($"Missing tenant {caseTest.TenantIdentifier}");
444-
}
445-
if (string.IsNullOrWhiteSpace(caseTest.UserIdentifier))
446-
{
447-
throw new PayrollException($"Missing user {caseTest.UserIdentifier}");
448-
}
449-
if (string.IsNullOrWhiteSpace(caseTest.PayrollName))
450-
{
451-
throw new PayrollException($"Missing payroll {caseTest.PayrollName}");
452-
}
453-
454-
return caseTest;
455-
}
456-
457419
/// <summary>Get division by id</summary>
458420
/// <param name="tenantId">The tenant id</param>
459421
/// <param name="divisionId">The division id</param>

Client.Test/PayrollEngine.Client.Test.csproj

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
<ItemGroup>
1818
<PackageReference Include="Microsoft.CodeAnalysis.Compilers" Version="4.6.0" />
19-
<PackageReference Include="PayrollEngine.Client.Core" Version="0.6.0-beta.4" />
19+
<PackageReference Include="PayrollEngine.Client.Core" Version="0.6.0-beta.5" />
2020
</ItemGroup>
2121

2222
<!-- include xml documention files and json schemas to the nuget package -->
@@ -28,11 +28,27 @@
2828
<PackageCopyToOutput>true</PackageCopyToOutput>
2929
</None>
3030
</ItemGroup>
31+
<ItemGroup>
32+
<Compile Remove="ExchangeFileTestDataProvider.cs" />
33+
<Compile Remove="FileTestDataProvider.cs" />
34+
<Compile Remove="FileTestRunner.cs" />
35+
<Compile Remove="ITestDataProvider.cs" />
36+
<Compile Remove="ResourceTestDataProvider.cs" />
37+
<Compile Remove="StreamTestDataProvider.cs" />
38+
<Compile Remove="StringTestDataProvider.cs" />
39+
</ItemGroup>
3140
<ItemGroup>
3241
<None Include="..\README.md">
3342
<Pack>True</Pack>
3443
<PackagePath>\</PackagePath>
3544
</None>
45+
<None Include="ExchangeFileTestDataProvider.cs" />
46+
<None Include="FileTestDataProvider.cs" />
47+
<None Include="FileTestRunner.cs" />
48+
<None Include="ITestDataProvider.cs" />
49+
<None Include="ResourceTestDataProvider.cs" />
50+
<None Include="StreamTestDataProvider.cs" />
51+
<None Include="StringTestDataProvider.cs" />
3652
</ItemGroup>
3753

3854
<!-- build json schemas -->

Client.Test/PayrollEngine.Client.Test.xml

Lines changed: 85 additions & 108 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Client.Test/Payrun/PayrollTestResult.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@ public class PayrollTestResult
3030
public int TotalResultCount =>
3131
WageTypeResults.Count + CollectorResults.Count + PayrunResults.Count;
3232

33+
/// <summary>Test if wage type test is failed</summary>
34+
public bool FailedWageTypeResult =>
35+
WageTypeResults.Any(x => x.IsFailed());
36+
37+
/// <summary>Test if collector test is failed</summary>
38+
public bool FailedCollectorResult =>
39+
CollectorResults.Any(x => x.IsFailed());
40+
41+
/// <summary>Test if payrun test is failed</summary>
42+
public bool FailedPayrunResult =>
43+
PayrunResults.Any(x => x.IsFailed());
44+
45+
/// <summary>Test if payroll test is failed</summary>
46+
public bool Failed =>
47+
FailedWageTypeResult ||
48+
FailedCollectorResult ||
49+
FailedPayrunResult;
50+
3351
/// <summary>Initializes a new instance of the <see cref="PayrollTestResult"/> class</summary>
3452
/// <param name="tenant">The tenant</param>
3553
/// <param name="payrunJob">The payrun job</param>
@@ -40,10 +58,4 @@ public PayrollTestResult(Tenant tenant, Employee employee, PayrunJob payrunJob)
4058
Employee = employee ?? throw new ArgumentNullException(nameof(employee));
4159
PayrunJob = payrunJob ?? throw new ArgumentNullException(nameof(payrunJob));
4260
}
43-
44-
/// <summary>Test if payrun test is failed</summary>
45-
public bool IsFailed() =>
46-
WageTypeResults.Any(x => x.IsFailed()) ||
47-
CollectorResults.Any(x => x.IsFailed()) ||
48-
PayrunResults.Any(x => x.IsFailed());
4961
}

Client.Test/Payrun/PayrunEmployeeTestRunner.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace PayrollEngine.Client.Test.Payrun;
1111

1212
/// <summary>Payrun employee test runner</summary>
13-
public class PayrunEmployeeTestRunner : PayrunFileTestRunner
13+
public class PayrunEmployeeTestRunner : PayrunTestRunnerBase
1414
{
1515
/// <summary>The delay between creation and test</summary>
1616
public int DelayBetweenCreateAndTest { get; set; }
@@ -23,26 +23,23 @@ public class PayrunEmployeeTestRunner : PayrunFileTestRunner
2323

2424
/// <summary>Initializes a new instance of the <see cref="PayrunEmployeeTestRunner"/> class</summary>
2525
/// <param name="httpClient">The payroll engine http client</param>
26-
/// <param name="fileName">Name of the file</param>
2726
/// <param name="scriptParser">The script parser</param>
2827
/// <param name="owner">The test owner</param>
2928
/// <param name="testPrecision">The testing precision</param>
3029
/// <param name="employeeMode">The employee test mode</param>
31-
public PayrunEmployeeTestRunner(PayrollHttpClient httpClient, string fileName, IScriptParser scriptParser,
32-
TestPrecision testPrecision, string owner = null, EmployeeTestMode employeeMode = EmployeeTestMode.InsertEmployee) :
33-
base(httpClient, fileName, testPrecision, owner)
30+
public PayrunEmployeeTestRunner(PayrollHttpClient httpClient, IScriptParser scriptParser,
31+
TestPrecision testPrecision = TestPrecision.TestPrecision2, string owner = null,
32+
EmployeeTestMode employeeMode = EmployeeTestMode.InsertEmployee) :
33+
base(httpClient, testPrecision, owner)
3434
{
3535
ScriptParser = scriptParser ?? throw new ArgumentNullException(nameof(scriptParser));
3636
EmployeeMode = employeeMode;
3737
}
3838

3939
/// <summary>Start the test</summary>
40-
/// <param name="namespace">The tenant name</param>
4140
/// <returns>A list of payrun job results</returns>
42-
public override async Task<Dictionary<Tenant, List<PayrollTestResult>>> TestAllAsync(string @namespace = null)
41+
public override async Task<Dictionary<Tenant, List<PayrollTestResult>>> TestAllAsync(Model.Exchange exchange)
4342
{
44-
// exchange
45-
var exchange = await LoadExchangeAsync(@namespace);
4643
// apply owner
4744
ApplyOwner(exchange, Owner);
4845

Client.Test/Payrun/PayrunTestRunner.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
namespace PayrollEngine.Client.Test.Payrun;
1212

1313
/// <summary>Payrun test runner</summary>
14-
public class PayrunTestRunner : PayrunFileTestRunner
14+
public class PayrunTestRunner : PayrunTestRunnerBase
1515
{
1616
/// <summary>The employee test mode</summary>
1717
public IScriptParser ScriptParser { get; }
@@ -27,29 +27,26 @@ public class PayrunTestRunner : PayrunFileTestRunner
2727

2828
/// <summary>Initializes a new instance of the <see cref="PayrunTestRunner"/> class</summary>
2929
/// <param name="httpClient">The payroll engine http client</param>
30-
/// <param name="fileName">Name of the file</param>
3130
/// <param name="scriptParser">The script parser</param>
3231
/// <param name="testPrecision">The testing precision</param>
3332
/// <param name="owner">The test owner</param>
3433
/// <param name="importMode">The data import mode (default: single)</param>
3534
/// <param name="resultMode">The test result mode (default: clean)</param>
36-
public PayrunTestRunner(PayrollHttpClient httpClient, string fileName, IScriptParser scriptParser,
37-
TestPrecision testPrecision, string owner = null, DataImportMode importMode = DataImportMode.Single,
35+
public PayrunTestRunner(PayrollHttpClient httpClient, IScriptParser scriptParser,
36+
TestPrecision testPrecision = TestPrecision.TestPrecision2,
37+
string owner = null, DataImportMode importMode = DataImportMode.Single,
3838
TestResultMode resultMode = TestResultMode.CleanTest) :
39-
base(httpClient, fileName, testPrecision, owner)
39+
base(httpClient, testPrecision, owner)
4040
{
4141
ScriptParser = scriptParser ?? throw new ArgumentNullException(nameof(scriptParser));
4242
ImportMode = importMode;
4343
ResultMode = resultMode;
4444
}
4545

4646
/// <summary>Start the test</summary>
47-
/// <param name="namespace">The tenant name</param>
4847
/// <returns>A list of payrun job results</returns>
49-
public override async Task<Dictionary<Tenant, List<PayrollTestResult>>> TestAllAsync(string @namespace = null)
48+
public override async Task<Dictionary<Tenant, List<PayrollTestResult>>> TestAllAsync(Model.Exchange exchange)
5049
{
51-
// exchange
52-
var exchange = await LoadExchangeAsync(@namespace);
5350
// apply owner
5451
ApplyOwner(exchange, Owner);
5552

Client.Test/Payrun/PayrunFileTestRunner.cs renamed to Client.Test/Payrun/PayrunTestRunnerBase.cs

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,35 @@
11
using System.Collections.Generic;
22
using System.Linq;
33
using System.Threading.Tasks;
4-
using PayrollEngine.Client.Exchange;
54
using PayrollEngine.Client.Model;
65
using PayrollEngine.Client.Service.Api;
76

87
namespace PayrollEngine.Client.Test.Payrun;
98

109
/// <summary>Base class for file based payroll tests</summary>
11-
public abstract class PayrunFileTestRunner : FileTestRunner
10+
public abstract class PayrunTestRunnerBase : TestRunnerBase
1211
{
1312
/// <summary>The testing precision</summary>
1413
public TestPrecision TestPrecision { get; }
1514

1615
/// <summary>The test owner</summary>
1716
public string Owner { get; }
1817

19-
/// <summary>Initializes a new instance of the <see cref="PayrunFileTestRunner"/> class</summary>
18+
/// <summary>Initializes a new instance of the <see cref="PayrunTestRunnerBase"/> class</summary>
2019
/// <param name="httpClient">The payroll engine http client</param>
21-
/// <param name="fileName">Name of the file</param>
2220
/// <param name="testPrecision">The testing precision</param>
2321
/// <param name="owner">The test owner</param>
24-
protected PayrunFileTestRunner(PayrollHttpClient httpClient, string fileName,
25-
TestPrecision testPrecision, string owner) :
26-
base(httpClient, fileName)
22+
protected PayrunTestRunnerBase(PayrollHttpClient httpClient, TestPrecision testPrecision, string owner = null) :
23+
base(httpClient)
2724
{
2825
TestPrecision = testPrecision;
2926
Owner = owner;
3027
}
3128

3229
/// <summary>Start the test</summary>
33-
/// <param name="namespace">The namespace</param>
30+
/// <param name="exchange">The test exchange</param>
3431
/// <returns>A list of payrun job results</returns>
35-
public virtual Task<IList<PayrollTestResult>> TestAsync(string @namespace = null) =>
36-
default;
37-
38-
/// <summary>Start the test</summary>
39-
/// <param name="namespace">The import namespace</param>
40-
/// <returns>A list of payrun job results</returns>
41-
public virtual Task<Dictionary<Tenant, List<PayrollTestResult>>> TestAllAsync(string @namespace = null) =>
42-
default;
43-
44-
/// <summary>Loads the exchange</summary>
45-
protected virtual async Task<Model.Exchange> LoadExchangeAsync(string @namespace)
46-
{
47-
// exchange import
48-
var exchange = await ExchangeReader.ReadAsync(FileName, @namespace);
49-
if (exchange.Tenants == null || !exchange.Tenants.Any())
50-
{
51-
throw new PayrollException($"Missing tenant in test file {FileName}");
52-
}
53-
return exchange;
54-
}
32+
public abstract Task<Dictionary<Tenant, List<PayrollTestResult>>> TestAllAsync(Model.Exchange exchange);
5533

5634
/// <summary>Tests the payrun job</summary>
5735
/// <param name="tenant">The tenant</param>
@@ -147,7 +125,7 @@ protected virtual async Task<IList<PayrollTestResult>> TestPayrunJobAsync(Exchan
147125
var payrunJobs = employePayrunJobs.Where(x => x.JobResult == PayrunJobResult.Full &&
148126
string.Equals(x.Name, payrollResult.PayrunJobName)).ToList();
149127
if (!payrunJobs.Any())
150-
{
128+
{
151129
throw new PayrollException($"Missing payrun job {payrollResult.PayrunJobName}");
152130
}
153131
payrunJob = payrunJobs.OrderByDescending(x => x.Created).First();

Client.Test/Report/ReportTestRunner.cs

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.IO;
43
using System.Linq;
54
using System.Reflection;
65
using System.Threading.Tasks;
76
using PayrollEngine.Client.Model;
87
using PayrollEngine.Client.Service.Api;
9-
using PayrollEngine.Serialization;
108

119
namespace PayrollEngine.Client.Test.Report;
1210

1311
/// <summary>Report test runner</summary>
14-
public class ReportTestRunner : FileTestRunner
12+
public class ReportTestRunner : TestRunnerBase
1513
{
1614
/// <summary>Parse report custom test class</summary>
1715
private sealed class ReportCustomTestParser : CustomTestParser<ReportCustomTest, ReportTestContext>
@@ -31,21 +29,17 @@ protected override List<Type> GetDefaultReferences()
3129
}
3230
}
3331

34-
/// <summary>Initializes a new instance of the <see cref="ReportTestRunner"/> class</summary>
32+
/// <summary>Initializes a new instance of the class</summary>
3533
/// <param name="httpClient">The payroll engine http client</param>
36-
/// <param name="fileName">Name of the file</param>
37-
public ReportTestRunner(PayrollHttpClient httpClient, string fileName) :
38-
base(httpClient, fileName)
34+
public ReportTestRunner(PayrollHttpClient httpClient) :
35+
base(httpClient)
3936
{
4037
}
4138

4239
/// <summary>Test case</summary>
4340
/// <returns>The report test results</returns>
44-
public virtual async Task<ReportTestResult> TestAsync()
41+
public virtual async Task<ReportTestResult> TestAsync(ReportTest reportTest)
4542
{
46-
// load test
47-
var reportTest = await LoadReportTest();
48-
4943
// test context
5044
var context = await CreateTestContext(reportTest);
5145

@@ -216,32 +210,4 @@ protected virtual async Task<ReportTestContext> CreateTestContext(ReportTest rep
216210
/// <param name="name">The regulation name</param>
217211
protected async Task<Regulation> GetRegulationAsync(int tenantId, string name) =>
218212
await new RegulationService(HttpClient).GetAsync<Regulation>(new(tenantId), name);
219-
220-
/// <summary>Loads the case test</summary>
221-
protected virtual async Task<ReportTest> LoadReportTest()
222-
{
223-
// read file
224-
if (!File.Exists(FileName))
225-
{
226-
throw new PayrollException($"Missing import file {FileName}");
227-
}
228-
var json = await File.ReadAllTextAsync(FileName);
229-
if (string.IsNullOrWhiteSpace(json))
230-
{
231-
throw new PayrollException($"Invalid import file {FileName}");
232-
}
233-
234-
// convert from json
235-
var reportTest = DefaultJsonSerializer.Deserialize<ReportTest>(json);
236-
if (string.IsNullOrWhiteSpace(reportTest.TenantIdentifier))
237-
{
238-
throw new PayrollException($"Missing tenant {reportTest.TenantIdentifier}");
239-
}
240-
if (string.IsNullOrWhiteSpace(reportTest.UserIdentifier))
241-
{
242-
throw new PayrollException($"Missing user {reportTest.UserIdentifier}");
243-
}
244-
245-
return reportTest;
246-
}
247213
}

0 commit comments

Comments
 (0)