Skip to content

Commit 87fec9c

Browse files
feat: add more integration tests for demo use cases
1 parent 3adcf29 commit 87fec9c

4 files changed

Lines changed: 143 additions & 1 deletion

File tree

src/3-Tests/Application.Tests.Integration/Fixtures/TestsBaseFixture.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using CanBeYours.Infrastructure;
44
using CodeBlock.DevKit.Application.Srvices;
55
using CodeBlock.DevKit.Clients.AdminPanel;
6+
using CodeBlock.DevKit.Contracts.Services;
67
using CodeBlock.DevKit.Test.TestBase;
78
using Microsoft.AspNetCore.Builder;
89
using Microsoft.Extensions.Configuration;
@@ -18,14 +19,15 @@ public abstract class TestsBaseFixture : IntegrationTestsBase
1819
public readonly IMapper _mapper;
1920
public readonly IDemoThingRepository _demoThingRepository;
2021
public readonly ICurrentUser _currentUser;
22+
public readonly IUserAccessorService _userAccessorService;
2123

2224
protected TestsBaseFixture(string dbNameSuffix)
2325
: base(dbNameSuffix)
2426
{
2527
_demoThingRepository = GetRequiredService<IDemoThingRepository>();
28+
_userAccessorService = GetRequiredService<IUserAccessorService>();
2629
_mapper = GetRequiredService<IMapper>();
2730
_requestDispatcher = Substitute.For<IRequestDispatcher>();
28-
2931
_currentUser = Substitute.For<ICurrentUser>();
3032
_currentUser.GetUserId().Returns(Guid.NewGuid().ToString());
3133
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using CanBeYours.Application.Tests.Integration.Fixtures;
2+
using CanBeYours.Application.UseCases.DemoThings.GetDemoThing;
3+
using CanBeYours.Core.Domain.DemoThings;
4+
using FluentAssertions;
5+
using Microsoft.Extensions.Logging;
6+
7+
namespace CanBeYours.Application.Tests.Integration.UseCases.DemoThings;
8+
9+
[Collection(nameof(DemoThingsCollectionFixture))]
10+
public class GetDemoThingTests
11+
{
12+
private readonly DemoThingsCollectionFixture _fixture;
13+
private readonly GetDemoThingUseCase _getDemoThingUseCase;
14+
15+
public GetDemoThingTests(DemoThingsCollectionFixture fixture)
16+
{
17+
_fixture = fixture;
18+
_getDemoThingUseCase = GetGetDemoThingUseCase();
19+
}
20+
21+
[Fact]
22+
public async Task DemoThing_is_retrieved()
23+
{
24+
//Arrange
25+
var demoThing = DemoThing.Create("Test Name", "Test Description", DemoThingType.DemoType1, _fixture._currentUser.GetUserId());
26+
await _fixture.SeedDemoThingAsync(demoThing);
27+
var request = new GetDemoThingRequest(demoThing.Id);
28+
29+
//Act
30+
var result = await _getDemoThingUseCase.Handle(request, CancellationToken.None);
31+
32+
//Assert
33+
result.Should().NotBeNull();
34+
result.Id.Should().Be(demoThing.Id);
35+
result.Name.Should().Be(demoThing.Name);
36+
}
37+
38+
private GetDemoThingUseCase GetGetDemoThingUseCase()
39+
{
40+
var logger = _fixture.GetRequiredService<ILogger<GetDemoThingUseCase>>();
41+
return new GetDemoThingUseCase(_fixture._demoThingRepository, _fixture._mapper, logger, _fixture._currentUser, _fixture._userAccessorService);
42+
}
43+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using CanBeYours.Application.Tests.Integration.Fixtures;
2+
using CanBeYours.Application.UseCases.DemoThings.SearchDemoThings;
3+
using CanBeYours.Core.Domain.DemoThings;
4+
using CodeBlock.DevKit.Core.Helpers;
5+
using FluentAssertions;
6+
using Microsoft.Extensions.Logging;
7+
8+
namespace CanBeYours.Application.Tests.Integration.UseCases.DemoThings;
9+
10+
[Collection(nameof(DemoThingsCollectionFixture))]
11+
public class SearchDemoThingsTests
12+
{
13+
private readonly DemoThingsCollectionFixture _fixture;
14+
private readonly SearchDemoThingsUseCase _searchDemoThingsUseCase;
15+
16+
public SearchDemoThingsTests(DemoThingsCollectionFixture fixture)
17+
{
18+
_fixture = fixture;
19+
_searchDemoThingsUseCase = GetSearchDemoThingsUseCase();
20+
}
21+
22+
[Fact]
23+
public async Task DemoThings_are_searched()
24+
{
25+
//Arrange
26+
var demoThing = DemoThing.Create("Test Name", "Test Description", DemoThingType.DemoType1, _fixture._currentUser.GetUserId());
27+
await _fixture.SeedDemoThingAsync(demoThing);
28+
var request = new SearchDemoThingsRequest(
29+
term: "Test",
30+
type: DemoThingType.DemoType1,
31+
pageNumber: 1,
32+
recordsPerPage: 10,
33+
sortOrder: SortOrder.Desc,
34+
fromDateTime: null,
35+
toDateTime: null
36+
);
37+
38+
//Act
39+
var result = await _searchDemoThingsUseCase.Handle(request, CancellationToken.None);
40+
41+
//Assert
42+
result.Should().NotBeNull();
43+
result.Items.Should().NotBeEmpty();
44+
result.Items.Should().Contain(x => x.Id == demoThing.Id);
45+
}
46+
47+
private SearchDemoThingsUseCase GetSearchDemoThingsUseCase()
48+
{
49+
var logger = _fixture.GetRequiredService<ILogger<SearchDemoThingsUseCase>>();
50+
return new SearchDemoThingsUseCase(_fixture._demoThingRepository, _fixture._mapper, logger, _fixture._userAccessorService);
51+
}
52+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using CanBeYours.Application.Tests.Integration.Fixtures;
2+
using CanBeYours.Application.UseCases.DemoThings.UpdateDemoThing;
3+
using CanBeYours.Core.Domain.DemoThings;
4+
using FluentAssertions;
5+
using Microsoft.Extensions.Logging;
6+
7+
namespace CanBeYours.Application.Tests.Integration.UseCases.DemoThings;
8+
9+
[Collection(nameof(DemoThingsCollectionFixture))]
10+
public class UpdateDemoThingTests
11+
{
12+
private readonly DemoThingsCollectionFixture _fixture;
13+
private readonly UpdateDemoThingUseCase _updateDemoThingUseCase;
14+
15+
public UpdateDemoThingTests(DemoThingsCollectionFixture fixture)
16+
{
17+
_fixture = fixture;
18+
_updateDemoThingUseCase = GetUpdateDemoThingUseCase();
19+
}
20+
21+
[Fact]
22+
public async Task DemoThing_is_updated()
23+
{
24+
//Arrange
25+
var demoThing = DemoThing.Create("Test Name", "Test Description", DemoThingType.DemoType1, _fixture._currentUser.GetUserId());
26+
await _fixture.SeedDemoThingAsync(demoThing);
27+
var request = new UpdateDemoThingRequest(demoThing.Id, "Updated Name", "Updated Description", DemoThingType.DemoType2);
28+
29+
//Act
30+
var result = await _updateDemoThingUseCase.Handle(request, CancellationToken.None);
31+
32+
//Assert
33+
result.EntityId.Should().NotBeNull();
34+
var updatedDemoThing = await _fixture.GetDemoThingAsync(result.EntityId);
35+
updatedDemoThing.Name.Should().Be("Updated Name");
36+
updatedDemoThing.Description.Should().Be("Updated Description");
37+
updatedDemoThing.Type.Should().Be(DemoThingType.DemoType2);
38+
}
39+
40+
private UpdateDemoThingUseCase GetUpdateDemoThingUseCase()
41+
{
42+
var logger = _fixture.GetRequiredService<ILogger<UpdateDemoThingUseCase>>();
43+
return new UpdateDemoThingUseCase(_fixture._demoThingRepository, _fixture._requestDispatcher, logger);
44+
}
45+
}

0 commit comments

Comments
 (0)