From 60b28055ef58dd64df26390076ffff177c69332e Mon Sep 17 00:00:00 2001 From: Miguel Veloso Date: Mon, 16 Dec 2019 15:17:41 +0000 Subject: [PATCH] Update to 3.1 - Fix test to work on Catalog.API controller, using in-memory database --- .../Catalog/Catalog.API/Catalog.API.csproj | 30 ++-- .../Catalog.FunctionalTests.csproj | 8 +- .../Application/CatalogControllerTest.cs | 158 +++++++++++------- .../Catalog.UnitTests.csproj | 12 +- .../Application.FunctionalTests.csproj | 10 +- 5 files changed, 129 insertions(+), 89 deletions(-) diff --git a/src/Services/Catalog/Catalog.API/Catalog.API.csproj b/src/Services/Catalog/Catalog.API/Catalog.API.csproj index 2a08b699c..f6d3cd799 100644 --- a/src/Services/Catalog/Catalog.API/Catalog.API.csproj +++ b/src/Services/Catalog/Catalog.API/Catalog.API.csproj @@ -1,7 +1,7 @@  - netcoreapp3.0 + netcoreapp3.1 portable true Catalog.API @@ -42,22 +42,22 @@ - - - - - - - - - - - + + + + + + + + + + + - - - + + + diff --git a/src/Services/Catalog/Catalog.FunctionalTests/Catalog.FunctionalTests.csproj b/src/Services/Catalog/Catalog.FunctionalTests/Catalog.FunctionalTests.csproj index 1a2079c49..a1e04922e 100644 --- a/src/Services/Catalog/Catalog.FunctionalTests/Catalog.FunctionalTests.csproj +++ b/src/Services/Catalog/Catalog.FunctionalTests/Catalog.FunctionalTests.csproj @@ -1,7 +1,7 @@  - netcoreapp3.0 + netcoreapp3.1 false @@ -33,9 +33,9 @@ - - - + + + all runtime; build; native; contentfiles; analyzers diff --git a/src/Services/Catalog/Catalog.UnitTests/Application/CatalogControllerTest.cs b/src/Services/Catalog/Catalog.UnitTests/Application/CatalogControllerTest.cs index 7410551e4..f7529f73c 100644 --- a/src/Services/Catalog/Catalog.UnitTests/Application/CatalogControllerTest.cs +++ b/src/Services/Catalog/Catalog.UnitTests/Application/CatalogControllerTest.cs @@ -1,90 +1,132 @@ -using Microsoft.AspNetCore.Mvc; -using Microsoft.eShopOnContainers.WebMVC.Controllers; -using Microsoft.eShopOnContainers.WebMVC.Services; -using Microsoft.eShopOnContainers.WebMVC.ViewModels; -using Microsoft.eShopOnContainers.WebMVC.ViewModels.CatalogViewModels; +using Catalog.API.IntegrationEvents; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using Microsoft.eShopOnContainers.Services.Catalog.API; +using Microsoft.eShopOnContainers.Services.Catalog.API.Controllers; +using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure; +using Microsoft.eShopOnContainers.Services.Catalog.API.Model; +using Microsoft.eShopOnContainers.Services.Catalog.API.ViewModel; +using Microsoft.Extensions.Options; using Moq; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using Xunit; -using CatalogModel = Microsoft.eShopOnContainers.WebMVC.ViewModels.Catalog; namespace UnitTest.Catalog.Application { public class CatalogControllerTest { - private readonly Mock _catalogServiceMock; + private readonly DbContextOptions _dbOptions; public CatalogControllerTest() { - _catalogServiceMock = new Mock(); + _dbOptions = new DbContextOptionsBuilder() + .UseInMemoryDatabase(databaseName: "in-memory") + .Options; + + using (var dbContext = new CatalogContext(_dbOptions)) + { + dbContext.AddRange(GetFakeCatalog()); + dbContext.SaveChanges(); + } } [Fact] public async Task Get_catalog_items_success() { //Arrange - var fakeBrandFilterApplied = 1; - var fakeTypesFilterApplied = 2; - var fakePage = 2; - var fakeCatalog = GetFakeCatalog(); + var brandFilterApplied = 1; + var typesFilterApplied = 2; + var pageSize = 4; + var pageIndex = 1; - var expectedNumberOfPages = 5; - var expectedTotalPages = 50; - var expectedCurrentPage = 2; + var expectedItemsInPage = 2; + var expectedTotalItems = 6; - _catalogServiceMock.Setup(x => x.GetCatalogItems - ( - It.Is(y => y == fakePage), - It.IsAny(), - It.Is(y => y == fakeBrandFilterApplied), - It.Is(y => y == fakeTypesFilterApplied) - )) - .Returns(Task.FromResult(fakeCatalog)); + var catalogContext = new CatalogContext(_dbOptions); + var catalogSettings = new TestCatalogSettings(); + + var integrationServicesMock = new Mock(); //Act - var orderController = new CatalogController(_catalogServiceMock.Object); - var actionResult = await orderController.Index(fakeBrandFilterApplied, fakeTypesFilterApplied, fakePage, null); + var orderController = new CatalogController(catalogContext, catalogSettings, integrationServicesMock.Object); + var actionResult = await orderController.ItemsByTypeIdAndBrandIdAsync(typesFilterApplied, brandFilterApplied, pageSize, pageIndex); //Assert - var viewResult = Assert.IsType(actionResult); - var model = Assert.IsAssignableFrom(viewResult.ViewData.Model); - Assert.Equal(model.PaginationInfo.TotalPages, expectedNumberOfPages); - Assert.Equal(model.PaginationInfo.TotalItems, expectedTotalPages); - Assert.Equal(model.PaginationInfo.ActualPage, expectedCurrentPage); - Assert.Empty(model.PaginationInfo.Next); - Assert.Empty(model.PaginationInfo.Previous); - } - - private CatalogModel GetFakeCatalog() + Assert.IsType>>(actionResult); + var page = Assert.IsAssignableFrom>(actionResult.Value); + Assert.Equal(expectedTotalItems, page.Count); + Assert.Equal(pageIndex, page.PageIndex); + Assert.Equal(pageSize, page.PageSize); + Assert.Equal(expectedItemsInPage, page.Data.Count()); + } + + private List GetFakeCatalog() { - return new CatalogModel() + return new List() { - PageSize = 10, - Count = 50, - PageIndex = 2, - Data = new List() + new CatalogItem() + { + Id = 1, + Name = "fakeItemA", + CatalogTypeId = 2, + CatalogBrandId = 1, + PictureFileName = "fakeItemA.png" + }, + new CatalogItem() + { + Id = 2, + Name = "fakeItemB", + CatalogTypeId = 2, + CatalogBrandId = 1, + PictureFileName = "fakeItemB.png" + }, + new CatalogItem() + { + Id = 3, + Name = "fakeItemC", + CatalogTypeId = 2, + CatalogBrandId = 1, + PictureFileName = "fakeItemC.png" + }, + new CatalogItem() { - new CatalogItem() - { - Id = 1, - Name = "fakeItemA", - CatalogTypeId = 1 - }, - new CatalogItem() - { - Id = 2, - Name = "fakeItemB", - CatalogTypeId = 1 - }, - new CatalogItem() - { - Id = 3, - Name = "fakeItemC", - CatalogTypeId = 1 - } + Id = 4, + Name = "fakeItemD", + CatalogTypeId = 2, + CatalogBrandId = 1, + PictureFileName = "fakeItemD.png" + }, + new CatalogItem() + { + Id = 5, + Name = "fakeItemE", + CatalogTypeId = 2, + CatalogBrandId = 1, + PictureFileName = "fakeItemE.png" + }, + new CatalogItem() + { + Id = 6, + Name = "fakeItemF", + CatalogTypeId = 2, + CatalogBrandId = 1, + PictureFileName = "fakeItemF.png" } }; } } + + public class TestCatalogSettings : IOptionsSnapshot + { + public CatalogSettings Value => new CatalogSettings + { + PicBaseUrl = "http://image-server.com/", + AzureStorageEnabled = true + }; + + public CatalogSettings Get(string name) => Value; + } + } diff --git a/src/Services/Catalog/Catalog.UnitTests/Catalog.UnitTests.csproj b/src/Services/Catalog/Catalog.UnitTests/Catalog.UnitTests.csproj index 0a097242e..7c64276b4 100644 --- a/src/Services/Catalog/Catalog.UnitTests/Catalog.UnitTests.csproj +++ b/src/Services/Catalog/Catalog.UnitTests/Catalog.UnitTests.csproj @@ -1,17 +1,16 @@  - netcoreapp3.0 + netcoreapp3.1 false false - - - - - + + + + all runtime; build; native; contentfiles; analyzers @@ -20,7 +19,6 @@ - diff --git a/src/Tests/Services/Application.FunctionalTests/Application.FunctionalTests.csproj b/src/Tests/Services/Application.FunctionalTests/Application.FunctionalTests.csproj index 0ce51e413..0239c1ade 100644 --- a/src/Tests/Services/Application.FunctionalTests/Application.FunctionalTests.csproj +++ b/src/Tests/Services/Application.FunctionalTests/Application.FunctionalTests.csproj @@ -1,7 +1,7 @@  - netcoreapp3.0 + netcoreapp3.1 true false false @@ -67,10 +67,10 @@ - - - - + + + + all runtime; build; native; contentfiles; analyzers