From b88b66321c0f0a5c40feb9ded1327f649fb2b68c Mon Sep 17 00:00:00 2001 From: Eduardo Kroetz Date: Wed, 26 Jun 2024 14:37:22 -0300 Subject: [PATCH] tests: ajustar os testes quebrados --- .../Repositories/InvoiceRepository.cs | 3 + .../UnitTests/Entities/InvoiceEntityTest.cs | 39 ------- .../Repositories/InvoiceRepositoryTest.cs | 107 ++---------------- .../Mock/CreateData/CreateInvoices.cs | 1 - 4 files changed, 15 insertions(+), 135 deletions(-) diff --git a/Hotel.Domain/Repositories/InvoiceRepository.cs b/Hotel.Domain/Repositories/InvoiceRepository.cs index e24cea3..4c2816a 100644 --- a/Hotel.Domain/Repositories/InvoiceRepository.cs +++ b/Hotel.Domain/Repositories/InvoiceRepository.cs @@ -49,6 +49,9 @@ public async Task> GetAsync(InvoiceQueryParameters query if (queryParameters.ServiceId.HasValue) query = query.Where(x => x.Services.Any(x => x.Id == queryParameters.ServiceId)); + if (queryParameters.TotalAmount.HasValue) + query = query.FilterByOperator(queryParameters.TotalAmountOperator, x => x.TotalAmount, queryParameters.TotalAmount); + query = query.Skip(queryParameters.Skip ?? 0).Take(queryParameters.Take ?? 1); query = query.AsNoTracking(); diff --git a/Hotel.Tests/UnitTests/Entities/InvoiceEntityTest.cs b/Hotel.Tests/UnitTests/Entities/InvoiceEntityTest.cs index 1cb4167..1ffc17d 100644 --- a/Hotel.Tests/UnitTests/Entities/InvoiceEntityTest.cs +++ b/Hotel.Tests/UnitTests/Entities/InvoiceEntityTest.cs @@ -10,30 +10,6 @@ namespace Hotel.Tests.UnitTests.Entities; [TestClass] public class InvoiceEntityTest { - - [TestMethod] - public void ValidInvoice_MustBeValid() - { - var room = new Room("Quarto",1, 50, 3, "Quarto padrão", TestParameters.Category); - var reservation = new Reservation(room, DateTime.Now.Date, DateTime.Now.AddDays(1), TestParameters.Customer, 2); - - reservation.ToCheckIn(); - reservation.Finish(); - Assert.IsTrue(reservation?.Invoice?.IsValid); - } - - [TestMethod] - [ExpectedException(typeof(ValidationException))] - public void InvoiceWithoutCheckOutStatusReservation_ExpectedException() - { - var room = new Room("Quarto",1, 50, 3, "Quarto padrão", TestParameters.Category); - var reservation = new Reservation(room, DateTime.Now.Date, DateTime.Now.AddDays(1), TestParameters.Customer, 2); - reservation.ToCheckIn(); - - new Invoice(EPaymentMethod.Pix, reservation); - Assert.Fail(); - } - [TestMethod] public void TheCustomersInReservation_MustBeSameOnInvoice() { @@ -45,19 +21,4 @@ public void TheCustomersInReservation_MustBeSameOnInvoice() Assert.AreEqual(reservation.Customer, invoice.Customer); } - [TestMethod] - public void ChangeToFinishInvoiceStatus_MustBeFinishStatus() - { - var room = new Room("Quarto",1, 50, 3, "Quarto padrão", TestParameters.Category); - var reservation = new Reservation(room, DateTime.Now.Date, DateTime.Now.AddDays(1), TestParameters.Customer, 2); - reservation.ToCheckIn(); - - var invoice = reservation.Finish(); - invoice.FinishInvoice(); - Assert.AreEqual(EStatus.Finish, invoice.Status); - } - - - - } \ No newline at end of file diff --git a/Hotel.Tests/UnitTests/Repositories/InvoiceRepositoryTest.cs b/Hotel.Tests/UnitTests/Repositories/InvoiceRepositoryTest.cs index 6942395..248cd40 100644 --- a/Hotel.Tests/UnitTests/Repositories/InvoiceRepositoryTest.cs +++ b/Hotel.Tests/UnitTests/Repositories/InvoiceRepositoryTest.cs @@ -1,5 +1,4 @@ using Hotel.Domain.DTOs.InvoiceDTOs; -using Hotel.Domain.Enums; using Hotel.Domain.Repositories; using Hotel.Tests.UnitTests.Repositories.Mock; using Microsoft.EntityFrameworkCore; @@ -24,19 +23,14 @@ public async Task GetByIdAsync_ReturnsWithCorrectParameters() Assert.IsNotNull(roomInvoice); Assert.AreEqual(BaseRepositoryTest.Invoices[0].Id, roomInvoice.Id); Assert.AreEqual(BaseRepositoryTest.Invoices[0].PaymentMethod, roomInvoice.PaymentMethod); - Assert.AreEqual(BaseRepositoryTest.Invoices[0].TaxInformation, roomInvoice.TaxInformation); Assert.AreEqual(BaseRepositoryTest.Invoices[0].ReservationId, roomInvoice.ReservationId); - Assert.AreEqual(BaseRepositoryTest.Invoices[0].Number, roomInvoice.Number); - Assert.AreEqual(BaseRepositoryTest.Invoices[0].IssueDate, roomInvoice.IssueDate); Assert.AreEqual(Math.Round(BaseRepositoryTest.Invoices[0].TotalAmount), Math.Round(roomInvoice.TotalAmount)); - Assert.AreEqual(BaseRepositoryTest.Invoices[0].Status, roomInvoice.Status); - } [TestMethod] public async Task GetAsync_ReturnWithCorrectParameters() { - var parameters = new InvoiceQueryParameters(0, 100, null, BaseRepositoryTest.Invoices[0].PaymentMethod, BaseRepositoryTest.Invoices[0].TotalAmount, "eq", null, null, null, null, null, null, null, null); + var parameters = new InvoiceQueryParameters(0, 100, BaseRepositoryTest.Invoices[0].PaymentMethod, BaseRepositoryTest.Invoices[0].TotalAmount, "eq", null, null, null); var roomInvoices = await InvoiceRepository.GetAsync(parameters); var roomInvoice = roomInvoices.ToList()[0]; @@ -44,30 +38,26 @@ public async Task GetAsync_ReturnWithCorrectParameters() Assert.IsNotNull(roomInvoice); Assert.AreEqual(BaseRepositoryTest.Invoices[0].Id, roomInvoice.Id); Assert.AreEqual(BaseRepositoryTest.Invoices[0].PaymentMethod, roomInvoice.PaymentMethod); - Assert.AreEqual(BaseRepositoryTest.Invoices[0].TaxInformation, roomInvoice.TaxInformation); Assert.AreEqual(BaseRepositoryTest.Invoices[0].ReservationId, roomInvoice.ReservationId); - Assert.AreEqual(BaseRepositoryTest.Invoices[0].Number, roomInvoice.Number); - Assert.AreEqual(BaseRepositoryTest.Invoices[0].IssueDate, roomInvoice.IssueDate); Assert.AreEqual(Math.Round(BaseRepositoryTest.Invoices[0].TotalAmount), Math.Round(roomInvoice.TotalAmount)); - Assert.AreEqual(BaseRepositoryTest.Invoices[0].Status, roomInvoice.Status); } [TestMethod] - public async Task GetAsync_WherePaymentMethodEqualsCreditCard_ReturnsInvoices() + public async Task GetAsync_WhereCardPaymentMethod_ReturnsInvoices() { - var parameters = new InvoiceQueryParameters(0, 100, null, EPaymentMethod.CreditCard, null, null, null, null, null, null, null, null, null, null); + var parameters = new InvoiceQueryParameters(0, 100, "card", null, null, null, null, null); var roomInvoices = await InvoiceRepository.GetAsync(parameters); Assert.IsTrue(roomInvoices.Any()); foreach (var roomInvoice in roomInvoices) - Assert.AreEqual(EPaymentMethod.CreditCard, roomInvoice.PaymentMethod); + Assert.AreEqual("card", roomInvoice.PaymentMethod); } [TestMethod] public async Task GetAsync_WhereTotalAmountGratherThan9_ReturnsInvoices() { - var parameters = new InvoiceQueryParameters(0, 100, null, null, 9m, "gt", null, null, null, null, null, null, null, null); + var parameters = new InvoiceQueryParameters(0, 100, null, 9m, "gt", null, null, null); var roomInvoices = await InvoiceRepository.GetAsync(parameters); Assert.IsTrue(roomInvoices.Any()); @@ -80,7 +70,7 @@ public async Task GetAsync_WhereTotalAmountGratherThan9_ReturnsInvoices() [TestMethod] public async Task GetAsync_WhereTotalAmountLessThan100_ReturnsInvoices() { - var parameters = new InvoiceQueryParameters(0, 100, null, null, 100m, "lt", null, null, null, null, null, null, null, null); + var parameters = new InvoiceQueryParameters(0, 100, null, 100m, "lt", null, null, null); var roomInvoices = await InvoiceRepository.GetAsync(parameters); Assert.IsTrue(roomInvoices.Any()); @@ -92,48 +82,24 @@ public async Task GetAsync_WhereTotalAmountLessThan100_ReturnsInvoices() [TestMethod] public async Task GetAsync_WhereTotalAmountEquals_ReturnsInvoices() { - var parameters = new InvoiceQueryParameters(0, 100, null, null, BaseRepositoryTest.Invoices[3].TotalAmount, "eq", null, null, null, null, null, null, null, null); + var totalAmount = BaseRepositoryTest.Invoices[3].TotalAmount; + var parameters = new InvoiceQueryParameters(0, 100, null, totalAmount, "eq", null, null, null); var roomInvoices = await InvoiceRepository.GetAsync(parameters); Assert.IsTrue(roomInvoices.Any()); foreach (var roomInvoice in roomInvoices) - Assert.AreEqual(Math.Round(BaseRepositoryTest.Invoices[3].TotalAmount), Math.Round(roomInvoice.TotalAmount)); + Assert.AreEqual(Math.Round(totalAmount), Math.Round(roomInvoice.TotalAmount)); } - [TestMethod] - public async Task GetAsync_WhereStatusEqualsPending_ReturnsInvoices() - { - var parameters = new InvoiceQueryParameters(0, 100, null, null, null, null, EStatus.Pending, null, null, null, null, null, null, null); - var roomInvoices = await InvoiceRepository.GetAsync(parameters); - - Assert.IsTrue(roomInvoices.Any()); - foreach (var roomInvoice in roomInvoices) - Assert.AreEqual(EStatus.Pending, roomInvoice.Status); - - - } - - [TestMethod] - public async Task GetAsync_WhereStatusEqualsFinish_ReturnsInvoices() - { - - var parameters = new InvoiceQueryParameters(0, 100, null, null, null, null, EStatus.Finish, null, null, null, null, null, null, null); - var roomInvoices = await InvoiceRepository.GetAsync(parameters); - - Assert.IsTrue(roomInvoices.Any()); - foreach (var roomInvoice in roomInvoices) - Assert.AreEqual(EStatus.Finish, roomInvoice.Status); - } - [TestMethod] public async Task GetAsync_WhereCustomerId_ReturnsInvoices() { var customerWithInvoice = await BaseRepositoryTest.MockConnection.Context.Customers .FirstOrDefaultAsync(x => x.Invoices.Count > 0); - var parameters = new InvoiceQueryParameters(0, 100, null, null, null, null, null, customerWithInvoice?.Id, null, null, null, null, null, null); + var parameters = new InvoiceQueryParameters(0, 100, null, null, null, customerWithInvoice?.Id, null, null); var roomInvoices = await InvoiceRepository.GetAsync(parameters); Assert.IsTrue(roomInvoices.Any()); @@ -144,7 +110,7 @@ public async Task GetAsync_WhereCustomerId_ReturnsInvoices() [TestMethod] public async Task GetAsync_WhereReservationId_ReturnsInvoices() { - var parameters = new InvoiceQueryParameters(0, 100, null, null, null, null, null, null, BaseRepositoryTest.Reservations[0].Id, null, null, null, null, null); + var parameters = new InvoiceQueryParameters(0, 100, null, null, null, null, BaseRepositoryTest.Reservations[0].Id, null); for (var i = 0; i < 10; i++) { var roomInvoices = await InvoiceRepository.GetAsync(parameters); @@ -163,7 +129,7 @@ public async Task GetAsync_WhereServiceId_ReturnsInvoices() { var serviceWithInvoices = await BaseRepositoryTest.MockConnection.Context.Services.FirstOrDefaultAsync(x => x.Invoices.Count > 0); - var parameters = new InvoiceQueryParameters(0, 100, null, null, null, null, null, null, null, serviceWithInvoices!.Id, null, null, null, null); + var parameters = new InvoiceQueryParameters(0, 100, null, null, null, null, null, serviceWithInvoices!.Id); var roomInvoices = await InvoiceRepository.GetAsync(parameters); Assert.IsTrue(roomInvoices.Any()); @@ -178,53 +144,4 @@ public async Task GetAsync_WhereServiceId_ReturnsInvoices() } } - [TestMethod] - public async Task GetAsync_WhereTaxInformationLessThan13_ReturnsInvoices() - { - var parameters = new InvoiceQueryParameters(0, 100, null, null, null, null, null, null, null, null, 13, "lt", null, null); - var roomInvoices = await InvoiceRepository.GetAsync(parameters); - - Assert.IsTrue(roomInvoices.Any()); - - foreach (var roomInvoice in roomInvoices) - Assert.IsTrue(13 > roomInvoice.TaxInformation); - - } - - [TestMethod] - public async Task GetAsync_WhereIssueDateGratherThanYesterday_ReturnsInvoices() - { - var parameters = new InvoiceQueryParameters(0, 100, null, null, null, null, null, null, null, null, null, null, DateTime.Now.AddDays(-1), "gt"); - var roomInvoices = await InvoiceRepository.GetAsync(parameters); - - Assert.IsTrue(roomInvoices.Any()); - foreach (var roomInvoice in roomInvoices) - Assert.IsTrue(DateTime.Now.AddDays(-1) < roomInvoice.IssueDate); - - } - - [TestMethod] - public async Task GetAsync_WhereIssueDateLessThanToday_ReturnsInvoices() - { - var parameters = new InvoiceQueryParameters(0, 100, null, null, null, null, null, null, null, null, null, null, DateTime.Now.AddDays(1), "lt"); - var roomInvoices = await InvoiceRepository.GetAsync(parameters); - - Assert.IsTrue(roomInvoices.Any()); - - foreach (var roomInvoice in roomInvoices) - Assert.IsTrue(DateTime.Now.AddDays(1) > roomInvoice.IssueDate); - - } - - [TestMethod] - public async Task GetAsync_WhereIssueDateEquals_ReturnsInvoices() - { - var parameters = new InvoiceQueryParameters(0, 100, null, null, null, null, null, null, null, null, null, null, BaseRepositoryTest.Invoices[0].IssueDate, "eq"); - var roomInvoices = await InvoiceRepository.GetAsync(parameters); - - Assert.IsTrue(roomInvoices.Any()); - foreach (var roomInvoice in roomInvoices) - Assert.AreEqual(BaseRepositoryTest.Invoices[0].IssueDate, roomInvoice.IssueDate); - } - } diff --git a/Hotel.Tests/UnitTests/Repositories/Mock/CreateData/CreateInvoices.cs b/Hotel.Tests/UnitTests/Repositories/Mock/CreateData/CreateInvoices.cs index d09fea9..f469acb 100644 --- a/Hotel.Tests/UnitTests/Repositories/Mock/CreateData/CreateInvoices.cs +++ b/Hotel.Tests/UnitTests/Repositories/Mock/CreateData/CreateInvoices.cs @@ -18,7 +18,6 @@ public static async Task Create() BaseRepositoryTest.ReservationsToFinish[1].Finish(), BaseRepositoryTest.ReservationsToFinish[2].Finish(), }; - roomInvoices[0].FinishInvoice(); await BaseRepositoryTest.MockConnection.Context.Invoices.AddRangeAsync(roomInvoices); await BaseRepositoryTest.MockConnection.Context.SaveChangesAsync();