-
Notifications
You must be signed in to change notification settings - Fork 0
⚡ [Performance Improvement] Fix N+1 Query in ServicoInterno #45
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.Logging; | ||
| using Moq; | ||
| using Xunit; | ||
| using RESTful_API.Models; | ||
| using RESTful_API.Service; | ||
| using RESTful_API.Interface; | ||
|
|
||
| namespace CarXPress_Unit_Tests | ||
| { | ||
| public class ServicoInternoTests | ||
| { | ||
| [Fact] | ||
| public async Task Test_Executar_Performance_Multas() | ||
| { | ||
|
Comment on lines
+18
to
+20
|
||
| // Arrange | ||
| var options = new DbContextOptionsBuilder<PdsContext>() | ||
| .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()) | ||
| .Options; | ||
|
|
||
| using (var context = new PdsContext(options)) | ||
| { | ||
| // Setup mock data for 1000 multas | ||
| var cliente = new Cliente { Idcliente = 1, NomeCliente = "Test User", CodigoPostalCp = 1000, LoginIdloginNavigation = new Login { Email = "test@example.com" } }; | ||
| context.Clientes.Add(cliente); | ||
|
|
||
| var veiculo = new Veiculo { Idveiculo = 1, EstadoVeiculo = "Alugado" }; | ||
| context.Veiculos.Add(veiculo); | ||
|
|
||
| for (int i = 1; i <= 1000; i++) | ||
| { | ||
| var aluguer = new Aluguer { Idaluguer = i, ClienteIdcliente = 1, VeiculoIdveiculo = 1, EstadoAluguer = "Alugado" }; | ||
| context.Aluguers.Add(aluguer); | ||
|
|
||
| var multa = new Infracao { Idinfracao = i, AluguerIdaluguer = i, EstadoInfracao = "Pendente" }; | ||
| context.Infracoes.Add(multa); | ||
| } | ||
|
|
||
| await context.SaveChangesAsync(); | ||
| } | ||
|
|
||
| using (var context = new PdsContext(options)) | ||
| { | ||
| var loggerMock = new Mock<ILogger<ServicoInterno>>(); | ||
| var emailServiceMock = new Mock<IEmailService>(); | ||
| var configMock = new Mock<IConfiguration>(); | ||
|
|
||
| var service = new ServicoInterno(loggerMock.Object, emailServiceMock.Object, configMock.Object, context); | ||
|
|
||
| var watch = System.Diagnostics.Stopwatch.StartNew(); | ||
|
|
||
| // Act | ||
| await service.Executar(); | ||
|
|
||
| watch.Stop(); | ||
|
|
||
| Console.WriteLine($"Executar took {watch.ElapsedMilliseconds} ms"); | ||
|
Comment on lines
+55
to
+62
|
||
| } | ||
| } | ||
| } | ||
| } | ||
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.
The test project's
RootNamespaceisUnit_Tests, and the other test files in this project usenamespace Unit_Tests. This file uses a different namespace (CarXPress_Unit_Tests), which is inconsistent and can make discovery/organization harder. Consider aligning the namespace with the rest of the test project.