-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Creating first few integration tests #571
Merged
raffacabofrio
merged 11 commits into
SharebookBR:develop
from
henriqueholtz:develop-integration-tests
Dec 24, 2024
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d37b39e
feat: Creating the project "ShareBook.Test.Integration"
henriqueholtz 6825243
test: Integration => Adding first Meetups test "AvailableBooks_Empty"
henriqueholtz db43bf3
test: ShareBook.Test.Integration => Adding the first test for Books (…
henriqueholtz 8cf3b15
test: Integration => Seeding data before run tests
henriqueholtz e29cacb
test: Integration => Turning MeetupTests as theory with different pag…
henriqueholtz d81e820
test: Integration/meetupTests => Adding tests for upcoming=true
henriqueholtz 8373f8c
docs: Adding docker command to run SQL Server as docker container on …
henriqueholtz 98a8503
Merge remote-tracking branch 'origin/develop' into develop-integratio…
henriqueholtz fa60269
test: Adding validations to all integration tests
henriqueholtz 1804b19
test: Adding integration test to api/category
henriqueholtz 8a8c9ac
test: Adding integration test to "api/Meetup/search?criteria={criteri…
henriqueholtz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
global using FluentAssertions; | ||
global using ShareBook.Test.Integration.Setup; |
26 changes: 26 additions & 0 deletions
26
ShareBook/ShareBook.Test.Integration/Setup/ShareBookTestsFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using ShareBook.Repository; | ||
|
||
namespace ShareBook.Test.Integration.Setup; | ||
|
||
|
||
[CollectionDefinition(nameof(ShareBookTestsFixture))] | ||
public class ShareBookTestsFixtureCollection : ICollectionFixture<ShareBookTestsFixture> | ||
{ } | ||
|
||
public class ShareBookTestsFixture | ||
{ | ||
public ShareBookWebAppFactory ShareBookWebAppFactory { get; } = new ShareBookWebAppFactory(); | ||
public HttpClient ShareBookApiClient { get; init; } | ||
public ApplicationDbContext ApplicationDbContext { get; init; } | ||
|
||
public ShareBookTestsFixture() | ||
{ | ||
ShareBookApiClient = ShareBookWebAppFactory.CreateClient(); | ||
ApplicationDbContext = ShareBookWebAppFactory.Services.GetRequiredService<ApplicationDbContext>(); | ||
|
||
// Seed data | ||
var sharebookSeeder = new ShareBookSeeder(ApplicationDbContext); | ||
sharebookSeeder.Seed(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
ShareBook/ShareBook.Test.Integration/Setup/ShareBookWebAppFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Mvc.Testing; | ||
using Microsoft.AspNetCore.TestHost; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using ShareBook.Api; | ||
using ShareBook.Repository; | ||
|
||
namespace ShareBook.Test.Integration.Setup; | ||
|
||
public class ShareBookWebAppFactory : WebApplicationFactory<Program> | ||
{ | ||
protected override IHostBuilder? CreateHostBuilder() | ||
{ | ||
return Host.CreateDefaultBuilder() | ||
.ConfigureWebHostDefaults(webBuilder => | ||
{ | ||
webBuilder.UseStartup<Startup>(); | ||
}); | ||
} | ||
|
||
protected override void ConfigureWebHost(IWebHostBuilder builder) | ||
{ | ||
base.ConfigureWebHost(builder); | ||
Startup.IgnoreMigrations = true; | ||
|
||
builder.ConfigureTestServices(services => | ||
{ | ||
var dbOptions = services.FirstOrDefault(x => x.ServiceType == typeof(DbContextOptions<ApplicationDbContext>)); | ||
if (dbOptions != null) | ||
services.Remove(dbOptions); | ||
|
||
services.AddDbContext<ApplicationDbContext>(options => | ||
{ | ||
options.UseInMemoryDatabase("ShareBookInMemoryDb"); | ||
}); | ||
}); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
ShareBook/ShareBook.Test.Integration/ShareBook.Test.Integration.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0" /> | ||
<PackageReference Include="FluentAssertions" Version="4.14.0" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.11" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.4" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageReference Include="xunit" Version="2.5.3" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\ShareBook.Api\ShareBook.Api.csproj" /> | ||
<ProjectReference Include="..\ShareBook.Repository\ShareBook.Infra.Data.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Xunit" /> | ||
</ItemGroup> | ||
|
||
</Project> |
37 changes: 37 additions & 0 deletions
37
ShareBook/ShareBook.Test.Integration/Tests/BookTests/BookTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System.Net; | ||
using Newtonsoft.Json; | ||
using ShareBook.Api.ViewModels; | ||
|
||
namespace ShareBook.Test.Integration.Tests.BookTests; | ||
|
||
[Collection(nameof(ShareBookTestsFixture))] | ||
public class BookTests | ||
{ | ||
private readonly ShareBookTestsFixture _fixture; | ||
|
||
public BookTests(ShareBookTestsFixture fixture) | ||
{ | ||
_fixture = fixture; | ||
} | ||
|
||
[Fact] | ||
public async Task AvailableBooks_All() | ||
{ | ||
var response = await _fixture.ShareBookApiClient.GetAsync("api/book/AvailableBooks"); | ||
|
||
response.Should().NotBeNull(); | ||
response.StatusCode.Should().Be(HttpStatusCode.OK); | ||
string responseAsString = await response.Content.ReadAsStringAsync(); | ||
responseAsString.Should().NotBeNullOrWhiteSpace(); | ||
IList<BookVM>? books = JsonConvert.DeserializeObject<IList<BookVM>>(responseAsString); | ||
books.Should().NotBeNullOrEmpty(); | ||
books!.Count.Should().Be(22); | ||
|
||
books!.All(i => | ||
!string.IsNullOrWhiteSpace(i.Title) | ||
&& !string.IsNullOrWhiteSpace(i.Author) | ||
&& !string.IsNullOrWhiteSpace(i.Slug) | ||
&& i.CategoryId != default | ||
).Should().BeTrue(); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
ShareBook/ShareBook.Test.Integration/Tests/CategoryTests/CategoryTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Newtonsoft.Json; | ||
using ShareBook.Domain; | ||
using ShareBook.Domain.Common; | ||
using System.Net; | ||
|
||
namespace ShareBook.Test.Integration.Tests.CategoryTests; | ||
|
||
[Collection(nameof(ShareBookTestsFixture))] | ||
public class CategoryTests | ||
{ | ||
private readonly ShareBookTestsFixture _fixture; | ||
|
||
public CategoryTests(ShareBookTestsFixture fixture) | ||
{ | ||
_fixture = fixture; | ||
} | ||
|
||
[Fact] | ||
public async Task GetCategories() | ||
{ | ||
var response = await _fixture.ShareBookApiClient.GetAsync("api/category"); | ||
|
||
response.Should().NotBeNull(); | ||
response.StatusCode.Should().Be(HttpStatusCode.OK); | ||
string responseAsString = await response.Content.ReadAsStringAsync(); | ||
responseAsString.Should().NotBeNullOrWhiteSpace(); | ||
PagedList<Category>? categories = JsonConvert.DeserializeObject<PagedList<Category>>(responseAsString); | ||
categories.Should().NotBeNull(); | ||
categories!.Items.Should().NotBeNull(); | ||
categories.Items.Count.Should().Be(10); | ||
categories.ItemsPerPage.Should().Be(50); | ||
categories.Page.Should().Be(1); | ||
|
||
categories.Items.All(i => | ||
!string.IsNullOrWhiteSpace(i.Name) | ||
&& i.Id != default | ||
).Should().BeTrue(); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
ShareBook/ShareBook.Test.Integration/Tests/MeetupTests/MeetupTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System.Net; | ||
using Newtonsoft.Json; | ||
using ShareBook.Domain; | ||
using ShareBook.Domain.Common; | ||
|
||
namespace ShareBook.Test.Integration.Tests.MeetupTests; | ||
|
||
[Collection(nameof(ShareBookTestsFixture))] | ||
public class MeetupTests | ||
{ | ||
private readonly ShareBookTestsFixture _fixture; | ||
|
||
public MeetupTests(ShareBookTestsFixture fixture) | ||
{ | ||
_fixture = fixture; | ||
} | ||
|
||
[Theory] | ||
[InlineData(1, 1, false, 1)] | ||
[InlineData(1, 10, false, 8)] | ||
[InlineData(2, 10, false, 0)] | ||
[InlineData(2, 5, false, 3)] | ||
[InlineData(3, 3, false, 2)] | ||
[InlineData(1, 5, true, 1)] | ||
[InlineData(2, 5, true, 0)] | ||
public async Task MeetupList(int page, int pageSize, bool upcoming, int expectedQuantity) | ||
{ | ||
var response = await _fixture.ShareBookApiClient.GetAsync($"api/Meetup?page={page}&pageSize={pageSize}&upcoming={upcoming}"); | ||
|
||
response.Should().NotBeNull(); | ||
response.StatusCode.Should().Be(HttpStatusCode.OK); | ||
string responseAsString = await response.Content.ReadAsStringAsync(); | ||
responseAsString.Should().NotBeNullOrWhiteSpace(); | ||
PagedList<Meetup>? meetups = JsonConvert.DeserializeObject<PagedList<Meetup>>(responseAsString); | ||
meetups.Should().NotBeNull(); | ||
meetups!.Page.Should().Be(page); | ||
meetups!.ItemsPerPage.Should().Be(pageSize); | ||
meetups!.Items.Should().HaveCount(expectedQuantity); | ||
meetups!.TotalItems.Should().Be(upcoming ? 1 : 8); | ||
|
||
meetups!.Items.All(i => | ||
!string.IsNullOrWhiteSpace(i.Title) | ||
&& !string.IsNullOrWhiteSpace(i.Description) | ||
&& !string.IsNullOrWhiteSpace(i.Cover) | ||
&& i.StartDate != default | ||
).Should().BeTrue(); | ||
} | ||
|
||
[Theory] | ||
[InlineData("Qualidade de vida", 1)] | ||
[InlineData("Azure", 2)] | ||
[InlineData("invalid-nonexist", 0)] | ||
public async Task MeetupSearch(string criteria, int totalExpected) | ||
{ | ||
var response = await _fixture.ShareBookApiClient.GetAsync($"api/Meetup/search?criteria={criteria}"); | ||
|
||
response.Should().NotBeNull(); | ||
response.StatusCode.Should().Be(HttpStatusCode.OK); | ||
string responseAsString = await response.Content.ReadAsStringAsync(); | ||
responseAsString.Should().NotBeNullOrWhiteSpace(); | ||
IList<Meetup>? meetups = JsonConvert.DeserializeObject<IList<Meetup>>(responseAsString); | ||
meetups.Should().NotBeNull(); | ||
meetups!.Count.Should().Be(totalExpected); | ||
|
||
meetups!.All(i => | ||
!string.IsNullOrWhiteSpace(i.Title) | ||
&& i.Title.Contains(criteria, StringComparison.InvariantCultureIgnoreCase) | ||
&& !string.IsNullOrWhiteSpace(i.Description) | ||
&& !string.IsNullOrWhiteSpace(i.Cover) | ||
&& i.StartDate != default | ||
).Should().BeTrue(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pode falar um pouco desse cara? Em que contexto ele fica true?