Skip to content

Commit 5778e3c

Browse files
committed
Refectored integration tests and UI tests.
1 parent f45ff05 commit 5778e3c

39 files changed

+796
-753
lines changed

src/Server/Coderr.Server.App/Core/Reports/Jobs/DeleteOldReports.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ public class DeleteOldReports : IBackgroundJobAsync
2525
/// Creates a new instance of <see cref="DeleteOldReports" />.
2626
/// </summary>
2727
/// <param name="unitOfWork">Used for SQL queries</param>
28+
/// <param name="reportConfig"></param>
29+
/// <exception cref="ArgumentNullException"></exception>
2830
public DeleteOldReports(IAdoNetUnitOfWork unitOfWork, IConfiguration<ReportConfig> reportConfig)
2931
{
30-
if (unitOfWork == null) throw new ArgumentNullException("unitOfWork");
31-
_unitOfWork = unitOfWork;
32+
_unitOfWork = unitOfWork ?? throw new ArgumentNullException(nameof(unitOfWork));
3233
_reportConfig = reportConfig;
3334
}
3435

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
63
using codeRR.Server.ReportAnalyzer;
74
using codeRR.Server.ReportAnalyzer.Domain.Incidents;
85
using codeRR.Server.ReportAnalyzer.Domain.Reports;
@@ -11,44 +8,32 @@
118

129
namespace codeRR.Server.SqlServer.Tests.Analysis
1310
{
14-
[Collection(MapperInit.NAME)]
15-
public class IncidentBeingAnalyzedMapperTests : IDisposable
11+
public class IncidentBeingAnalyzedMapperTests : IntegrationTest
1612
{
17-
private readonly TestTools _testTools = new TestTools();
18-
private int _accountId;
19-
private int _applicationId;
20-
2113
public IncidentBeingAnalyzedMapperTests()
2214
{
23-
_testTools.CreateDatabase();
24-
_testTools.ToLatestVersion();
25-
_testTools.CreateUserAndApplication(out _accountId, out _applicationId);
15+
ResetDatabase();
2616
}
27-
17+
2818
[Fact]
29-
public void should_load_ignored_state_into_class_correctly()
19+
public void Should_load_ignored_state_into_class_correctly()
3020
{
31-
var report = new ErrorReportEntity(_applicationId, Guid.NewGuid().ToString("N"), DateTime.UtcNow,
21+
var report = new ErrorReportEntity(FirstApplicationId, Guid.NewGuid().ToString("N"), DateTime.UtcNow,
3222
new ErrorReportException(new Exception("mofo")),
33-
new List<ErrorReportContext> { new ErrorReportContext("Maps", new Dictionary<string, string>()) });
34-
report.Title = "Missing here";
23+
new List<ErrorReportContext> {new ErrorReportContext("Maps", new Dictionary<string, string>())})
24+
{
25+
Title = "Missing here"
26+
};
3527
report.Init(report.GenerateHashCodeIdentifier());
3628

37-
using (var uow = _testTools.CreateUnitOfWork())
29+
using (var uow = CreateUnitOfWork())
3830
{
39-
var incident = new ReportAnalyzer.Domain.Incidents.IncidentBeingAnalyzed(report);
31+
var incident = new IncidentBeingAnalyzed(report);
4032
var incRepos = new AnalyticsRepository(new AnalysisDbContext(uow), new TestConfigStore());
4133
incRepos.CreateIncident(incident);
4234
report.IncidentId = incident.Id;
4335
incRepos.CreateReport(report);
4436
}
45-
4637
}
47-
48-
public void Dispose()
49-
{
50-
_testTools?.Dispose();
51-
}
52-
5338
}
54-
}
39+
}

src/Server/Coderr.Server.SqlServer.Tests/ConnectionFactory.cs

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/Server/Coderr.Server.SqlServer.Tests/Core/ApiKeys/Commands/CreateApiKeyHandlerTests.cs

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,67 +14,80 @@
1414

1515
namespace codeRR.Server.SqlServer.Tests.Core.ApiKeys.Commands
1616
{
17-
[Collection(MapperInit.NAME)]
18-
public class CreateApiKeyHandlerTests : IDisposable
17+
18+
public class CreateApiKeyHandlerTests : IntegrationTest
1919
{
2020
private int _applicationId;
21-
private readonly IAdoNetUnitOfWork _uow;
2221

2322
public CreateApiKeyHandlerTests()
2423
{
25-
_uow = ConnectionFactory.Create();
2624
GetApplicationId();
2725
}
2826

29-
public void Dispose()
30-
{
31-
_uow.Dispose();
32-
}
33-
3427
[Fact]
3528
public async Task Should_be_able_to_Create_a_key_without_applications_mapped()
3629
{
3730
var cmd = new CreateApiKey("Mofo", Guid.NewGuid().ToString("N"), Guid.NewGuid().ToString("N"));
3831
var bus = Substitute.For<IMessageBus>();
3932
var ctx = Substitute.For<IMessageContext>();
4033

41-
var sut = new CreateApiKeyHandler(_uow, bus);
42-
await sut.HandleAsync(ctx, cmd);
34+
using (var uow = CreateUnitOfWork())
35+
{
36+
var sut = new CreateApiKeyHandler(uow, bus);
37+
await sut.HandleAsync(ctx, cmd);
38+
39+
var repos = new ApiKeyRepository(uow);
40+
var generated = await repos.GetByKeyAsync(cmd.ApiKey);
41+
generated.Should().NotBeNull();
42+
generated.Claims.Should().NotBeEmpty("because keys without appIds are universal");
43+
44+
uow.SaveChanges();
45+
}
4346

44-
var repos = new ApiKeyRepository(_uow);
45-
var generated = await repos.GetByKeyAsync(cmd.ApiKey);
46-
generated.Should().NotBeNull();
47-
generated.Claims.Should().NotBeEmpty("because keys without appIds are universal");
4847
}
4948

5049
[Fact]
5150
public async Task Should_be_able_to_Create_key()
5251
{
5352
var cmd = new CreateApiKey("Mofo", Guid.NewGuid().ToString("N"), Guid.NewGuid().ToString("N"),
54-
new[] {_applicationId});
53+
new[] { _applicationId });
5554
var bus = Substitute.For<IMessageBus>();
5655
var ctx = Substitute.For<IMessageContext>();
5756

58-
var sut = new CreateApiKeyHandler(_uow, bus);
59-
await sut.HandleAsync(ctx, cmd);
57+
using (var uow = CreateUnitOfWork())
58+
{
59+
60+
var sut = new CreateApiKeyHandler(uow, bus);
61+
await sut.HandleAsync(ctx, cmd);
6062

61-
var repos = new ApiKeyRepository(_uow);
62-
var generated = await repos.GetByKeyAsync(cmd.ApiKey);
63-
generated.Should().NotBeNull();
64-
generated.Claims.First().Value.Should().BeEquivalentTo(_applicationId.ToString());
63+
var repos = new ApiKeyRepository(uow);
64+
var generated = await repos.GetByKeyAsync(cmd.ApiKey);
65+
generated.Should().NotBeNull();
66+
generated.Claims.First().Value.Should().BeEquivalentTo(_applicationId.ToString());
67+
uow.SaveChanges();
68+
}
6569
}
6670

6771
private void GetApplicationId()
6872
{
69-
var repos = new ApplicationRepository(_uow);
70-
var id = _uow.ExecuteScalar("SELECT TOP 1 Id FROM Applications");
71-
if (id is DBNull || id is null)
73+
if (_applicationId != 0)
74+
return;
75+
76+
using (var uow = CreateUnitOfWork())
7277
{
73-
repos.CreateAsync(new Application(10, "AppTen")).Wait();
74-
_applicationId = (int) _uow.ExecuteScalar("SELECT TOP 1 Id FROM Applications");
78+
79+
var repos = new ApplicationRepository(uow);
80+
var id = uow.ExecuteScalar("SELECT TOP 1 Id FROM Applications");
81+
if (id is DBNull || id is null)
82+
{
83+
repos.CreateAsync(new Application(10, "AppTen")).GetAwaiter().GetResult();
84+
_applicationId = (int)uow.ExecuteScalar("SELECT TOP 1 Id FROM Applications");
85+
}
86+
else
87+
_applicationId = (int)id;
88+
uow.SaveChanges();
7589
}
76-
else
77-
_applicationId = (int) id;
90+
7891
}
7992
}
8093
}

src/Server/Coderr.Server.SqlServer.Tests/Core/ApiKeys/Commands/DeleteApiKeyHandlerTests.cs

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,14 @@
1414

1515
namespace codeRR.Server.SqlServer.Tests.Core.ApiKeys.Commands
1616
{
17-
[Collection(MapperInit.NAME)]
18-
public class DeleteApiKeyHandlerTests : IDisposable
17+
18+
public class DeleteApiKeyHandlerTests : IntegrationTest
1919
{
2020
private int _applicationId;
2121
private readonly ApiKey _existingEntity;
22-
private readonly IAdoNetUnitOfWork _uow;
2322

2423
public DeleteApiKeyHandlerTests()
2524
{
26-
_uow = ConnectionFactory.Create();
2725
GetApplicationId();
2826

2927
_existingEntity = new ApiKey
@@ -36,27 +34,30 @@ public DeleteApiKeyHandlerTests()
3634
};
3735

3836
_existingEntity.Add(_applicationId);
39-
var repos = new ApiKeyRepository(_uow);
40-
repos.CreateAsync(_existingEntity).Wait();
41-
}
42-
43-
public void Dispose()
44-
{
45-
_uow.Dispose();
37+
using (var uow = CreateUnitOfWork())
38+
{
39+
var repos = new ApiKeyRepository(uow);
40+
repos.CreateAsync(_existingEntity).GetAwaiter().GetResult();
41+
uow.SaveChanges();
42+
}
4643
}
47-
4844
[Fact]
4945
public async Task Should_be_able_to_delete_key_by_ApiKey()
5046
{
5147
var cmd = new DeleteApiKey(_existingEntity.GeneratedKey);
5248
var context = Substitute.For<IMessageContext>();
5349

54-
var sut = new DeleteApiKeyHandler(_uow);
55-
await sut.HandleAsync(context, cmd);
50+
using (var uow = CreateUnitOfWork())
51+
{
52+
var sut = new DeleteApiKeyHandler(uow);
53+
await sut.HandleAsync(context, cmd);
54+
55+
var count = uow.ExecuteScalar("SELECT cast(count(*) as int) FROM ApiKeys WHERE Id = @id",
56+
new { id = _existingEntity.Id });
57+
count.Should().Be(0);
58+
uow.SaveChanges();
59+
}
5660

57-
var count = _uow.ExecuteScalar("SELECT cast(count(*) as int) FROM ApiKeys WHERE Id = @id",
58-
new {id = _existingEntity.Id});
59-
count.Should().Be(0);
6061
}
6162

6263
[Fact]
@@ -65,25 +66,38 @@ public async Task Should_be_able_to_delete_key_by_id()
6566
var cmd = new DeleteApiKey(_existingEntity.Id);
6667
var context = Substitute.For<IMessageContext>();
6768

68-
var sut = new DeleteApiKeyHandler(_uow);
69-
await sut.HandleAsync(context, cmd);
69+
using (var uow = CreateUnitOfWork())
70+
{
71+
var sut = new DeleteApiKeyHandler(uow);
72+
await sut.HandleAsync(context, cmd);
73+
74+
var count = uow.ExecuteScalar("SELECT cast(count(*) as int) FROM ApiKeys WHERE Id = @id",
75+
new { id = _existingEntity.Id });
76+
count.Should().Be(0);
77+
uow.SaveChanges();
78+
}
7079

71-
var count = _uow.ExecuteScalar("SELECT cast(count(*) as int) FROM ApiKeys WHERE Id = @id",
72-
new {id = _existingEntity.Id});
73-
count.Should().Be(0);
7480
}
7581

7682
private void GetApplicationId()
7783
{
78-
var repos = new ApplicationRepository(_uow);
79-
var id = _uow.ExecuteScalar("SELECT TOP 1 Id FROM Applications");
80-
if (id is DBNull || id is null)
84+
if (_applicationId != 0)
85+
return;
86+
87+
using (var uow = CreateUnitOfWork())
8188
{
82-
repos.CreateAsync(new Application(10, "AppTen")).Wait();
83-
_applicationId = (int) _uow.ExecuteScalar("SELECT TOP 1 Id FROM Applications");
89+
var repos = new ApplicationRepository(uow);
90+
var id = uow.ExecuteScalar("SELECT TOP 1 Id FROM Applications");
91+
if (id is DBNull || id is null)
92+
{
93+
repos.CreateAsync(new Application(10, "AppTen")).Wait();
94+
_applicationId = (int)uow.ExecuteScalar("SELECT TOP 1 Id FROM Applications");
95+
}
96+
else
97+
_applicationId = (int)id;
98+
uow.SaveChanges();
8499
}
85-
else
86-
_applicationId = (int) id;
100+
87101
}
88102
}
89103
}

0 commit comments

Comments
 (0)