Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions backend/RESTful API/Service/ServicoInterno.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,17 @@ public async Task Executar()

var multas = await _context.Infracoes
.Include(a => a.AluguerIdaluguerNavigation)
.ThenInclude(a => a.ClienteIdclienteNavigation)
.ThenInclude(c => c.LoginIdloginNavigation)
.Where(a => a.EstadoInfracao != "Paga" && a.EstadoInfracao != "Contestação Aceite" && a.EstadoInfracao != "Em Falta")
.ToListAsync();

foreach (var multa in multas)
{
var alugM = await _context.Aluguers
.Include(a => a.ClienteIdclienteNavigation)
.Where(a => a.Idaluguer == multa.AluguerIdaluguer)/*retorna apenas 1 aluguer*/
.FirstAsync();
var alugM = multa.AluguerIdaluguerNavigation;
var clieM = alugM?.ClienteIdclienteNavigation;

var clieM = await _context.Clientes
.Include(c => c.LoginIdloginNavigation)
.Where(c => c.Idcliente == alugM.ClienteIdcliente)
.FirstAsync();

if (clieM != null)
if (clieM != null && clieM.LoginIdloginNavigation != null)
{
var dataLimite = multa.DataInfracao.HasValue ? multa.DataInfracao.Value.AddDays(14).ToString("dd/MM/yyyy") : "N/A";
var dataPag = multa.DataInfracao.HasValue ? multa.DataInfracao.Value.ToString("dd/MM/yyyy") : "N/A";
Expand Down
66 changes: 66 additions & 0 deletions backend/Tests/CarXPress Unit Tests/ServicoInternoTests.cs
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

Copilot AI Apr 6, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test project's RootNamespace is Unit_Tests, and the other test files in this project use namespace 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.

Suggested change
namespace CarXPress_Unit_Tests
namespace Unit_Tests

Copilot uses AI. Check for mistakes.
{
public class ServicoInternoTests
{
[Fact]
public async Task Test_Executar_Performance_Multas()
{
Comment on lines +18 to +20

Copilot AI Apr 6, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is named as a performance test but has no assertions/verification, so it will pass even if Executar() stops processing multas (e.g., query filter changes, email sending stops, etc.). Add an assertion that observes behavior (for example, verify IEmailService.EnviarEmail was called the expected number of times, and/or verify that infrações were updated to the expected state). If the intent is benchmarking only, consider skipping it by default so CI doesn't run it as a unit test.

Copilot uses AI. Check for mistakes.
// 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

Copilot AI Apr 6, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Measuring elapsed time with Stopwatch and printing to console makes the test non-deterministic and noisy; timing varies heavily across machines/CI and doesn't validate the N+1 regression reliably (EF InMemory also doesn't exercise real SQL/network latency). Prefer asserting on DB command count (e.g., via DbCommandInterceptor with SQLite in-memory) or move this into a dedicated benchmark (BenchmarkDotNet) / skipped test.

Copilot uses AI. Check for mistakes.
}
}
}
}