|
1 | | -using Microsoft.AspNetCore.Mvc; |
2 | | -using Microsoft.eShopOnContainers.WebMVC.Controllers; |
3 | | -using Microsoft.eShopOnContainers.WebMVC.Services; |
4 | | -using Microsoft.eShopOnContainers.WebMVC.ViewModels; |
5 | | -using Microsoft.eShopOnContainers.WebMVC.ViewModels.CatalogViewModels; |
| 1 | +using Catalog.API.IntegrationEvents; |
| 2 | +using Microsoft.AspNetCore.Mvc; |
| 3 | +using Microsoft.EntityFrameworkCore; |
| 4 | +using Microsoft.eShopOnContainers.Services.Catalog.API; |
| 5 | +using Microsoft.eShopOnContainers.Services.Catalog.API.Controllers; |
| 6 | +using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure; |
| 7 | +using Microsoft.eShopOnContainers.Services.Catalog.API.Model; |
| 8 | +using Microsoft.eShopOnContainers.Services.Catalog.API.ViewModel; |
| 9 | +using Microsoft.Extensions.Options; |
6 | 10 | using Moq; |
7 | 11 | using System.Collections.Generic; |
| 12 | +using System.Linq; |
8 | 13 | using System.Threading.Tasks; |
9 | 14 | using Xunit; |
10 | | -using CatalogModel = Microsoft.eShopOnContainers.WebMVC.ViewModels.Catalog; |
11 | 15 |
|
12 | 16 | namespace UnitTest.Catalog.Application |
13 | 17 | { |
14 | 18 | public class CatalogControllerTest |
15 | 19 | { |
16 | | - private readonly Mock<ICatalogService> _catalogServiceMock; |
| 20 | + private readonly DbContextOptions<CatalogContext> _dbOptions; |
17 | 21 |
|
18 | 22 | public CatalogControllerTest() |
19 | 23 | { |
20 | | - _catalogServiceMock = new Mock<ICatalogService>(); |
| 24 | + _dbOptions = new DbContextOptionsBuilder<CatalogContext>() |
| 25 | + .UseInMemoryDatabase(databaseName: "in-memory") |
| 26 | + .Options; |
| 27 | + |
| 28 | + using (var dbContext = new CatalogContext(_dbOptions)) |
| 29 | + { |
| 30 | + dbContext.AddRange(GetFakeCatalog()); |
| 31 | + dbContext.SaveChanges(); |
| 32 | + } |
21 | 33 | } |
22 | 34 |
|
23 | 35 | [Fact] |
24 | 36 | public async Task Get_catalog_items_success() |
25 | 37 | { |
26 | 38 | //Arrange |
27 | | - var fakeBrandFilterApplied = 1; |
28 | | - var fakeTypesFilterApplied = 2; |
29 | | - var fakePage = 2; |
30 | | - var fakeCatalog = GetFakeCatalog(); |
| 39 | + var brandFilterApplied = 1; |
| 40 | + var typesFilterApplied = 2; |
| 41 | + var pageSize = 4; |
| 42 | + var pageIndex = 1; |
31 | 43 |
|
32 | | - var expectedNumberOfPages = 5; |
33 | | - var expectedTotalPages = 50; |
34 | | - var expectedCurrentPage = 2; |
| 44 | + var expectedItemsInPage = 2; |
| 45 | + var expectedTotalItems = 6; |
35 | 46 |
|
36 | | - _catalogServiceMock.Setup(x => x.GetCatalogItems |
37 | | - ( |
38 | | - It.Is<int>(y => y == fakePage), |
39 | | - It.IsAny<int>(), |
40 | | - It.Is<int?>(y => y == fakeBrandFilterApplied), |
41 | | - It.Is<int?>(y => y == fakeTypesFilterApplied) |
42 | | - )) |
43 | | - .Returns(Task.FromResult(fakeCatalog)); |
| 47 | + var catalogContext = new CatalogContext(_dbOptions); |
| 48 | + var catalogSettings = new TestCatalogSettings(); |
| 49 | + |
| 50 | + var integrationServicesMock = new Mock<ICatalogIntegrationEventService>(); |
44 | 51 |
|
45 | 52 | //Act |
46 | | - var orderController = new CatalogController(_catalogServiceMock.Object); |
47 | | - var actionResult = await orderController.Index(fakeBrandFilterApplied, fakeTypesFilterApplied, fakePage, null); |
| 53 | + var orderController = new CatalogController(catalogContext, catalogSettings, integrationServicesMock.Object); |
| 54 | + var actionResult = await orderController.ItemsByTypeIdAndBrandIdAsync(typesFilterApplied, brandFilterApplied, pageSize, pageIndex); |
48 | 55 |
|
49 | 56 | //Assert |
50 | | - var viewResult = Assert.IsType<ViewResult>(actionResult); |
51 | | - var model = Assert.IsAssignableFrom<IndexViewModel>(viewResult.ViewData.Model); |
52 | | - Assert.Equal(model.PaginationInfo.TotalPages, expectedNumberOfPages); |
53 | | - Assert.Equal(model.PaginationInfo.TotalItems, expectedTotalPages); |
54 | | - Assert.Equal(model.PaginationInfo.ActualPage, expectedCurrentPage); |
55 | | - Assert.Empty(model.PaginationInfo.Next); |
56 | | - Assert.Empty(model.PaginationInfo.Previous); |
57 | | - } |
58 | | - |
59 | | - private CatalogModel GetFakeCatalog() |
| 57 | + Assert.IsType<ActionResult<PaginatedItemsViewModel<CatalogItem>>>(actionResult); |
| 58 | + var page = Assert.IsAssignableFrom<PaginatedItemsViewModel<CatalogItem>>(actionResult.Value); |
| 59 | + Assert.Equal(expectedTotalItems, page.Count); |
| 60 | + Assert.Equal(pageIndex, page.PageIndex); |
| 61 | + Assert.Equal(pageSize, page.PageSize); |
| 62 | + Assert.Equal(expectedItemsInPage, page.Data.Count()); |
| 63 | + } |
| 64 | + |
| 65 | + private List<CatalogItem> GetFakeCatalog() |
60 | 66 | { |
61 | | - return new CatalogModel() |
| 67 | + return new List<CatalogItem>() |
62 | 68 | { |
63 | | - PageSize = 10, |
64 | | - Count = 50, |
65 | | - PageIndex = 2, |
66 | | - Data = new List<CatalogItem>() |
| 69 | + new CatalogItem() |
| 70 | + { |
| 71 | + Id = 1, |
| 72 | + Name = "fakeItemA", |
| 73 | + CatalogTypeId = 2, |
| 74 | + CatalogBrandId = 1, |
| 75 | + PictureFileName = "fakeItemA.png" |
| 76 | + }, |
| 77 | + new CatalogItem() |
| 78 | + { |
| 79 | + Id = 2, |
| 80 | + Name = "fakeItemB", |
| 81 | + CatalogTypeId = 2, |
| 82 | + CatalogBrandId = 1, |
| 83 | + PictureFileName = "fakeItemB.png" |
| 84 | + }, |
| 85 | + new CatalogItem() |
| 86 | + { |
| 87 | + Id = 3, |
| 88 | + Name = "fakeItemC", |
| 89 | + CatalogTypeId = 2, |
| 90 | + CatalogBrandId = 1, |
| 91 | + PictureFileName = "fakeItemC.png" |
| 92 | + }, |
| 93 | + new CatalogItem() |
67 | 94 | { |
68 | | - new CatalogItem() |
69 | | - { |
70 | | - Id = 1, |
71 | | - Name = "fakeItemA", |
72 | | - CatalogTypeId = 1 |
73 | | - }, |
74 | | - new CatalogItem() |
75 | | - { |
76 | | - Id = 2, |
77 | | - Name = "fakeItemB", |
78 | | - CatalogTypeId = 1 |
79 | | - }, |
80 | | - new CatalogItem() |
81 | | - { |
82 | | - Id = 3, |
83 | | - Name = "fakeItemC", |
84 | | - CatalogTypeId = 1 |
85 | | - } |
| 95 | + Id = 4, |
| 96 | + Name = "fakeItemD", |
| 97 | + CatalogTypeId = 2, |
| 98 | + CatalogBrandId = 1, |
| 99 | + PictureFileName = "fakeItemD.png" |
| 100 | + }, |
| 101 | + new CatalogItem() |
| 102 | + { |
| 103 | + Id = 5, |
| 104 | + Name = "fakeItemE", |
| 105 | + CatalogTypeId = 2, |
| 106 | + CatalogBrandId = 1, |
| 107 | + PictureFileName = "fakeItemE.png" |
| 108 | + }, |
| 109 | + new CatalogItem() |
| 110 | + { |
| 111 | + Id = 6, |
| 112 | + Name = "fakeItemF", |
| 113 | + CatalogTypeId = 2, |
| 114 | + CatalogBrandId = 1, |
| 115 | + PictureFileName = "fakeItemF.png" |
86 | 116 | } |
87 | 117 | }; |
88 | 118 | } |
89 | 119 | } |
| 120 | + |
| 121 | + public class TestCatalogSettings : IOptionsSnapshot<CatalogSettings> |
| 122 | + { |
| 123 | + public CatalogSettings Value => new CatalogSettings |
| 124 | + { |
| 125 | + PicBaseUrl = "http://image-server.com/", |
| 126 | + AzureStorageEnabled = true |
| 127 | + }; |
| 128 | + |
| 129 | + public CatalogSettings Get(string name) => Value; |
| 130 | + } |
| 131 | + |
90 | 132 | } |
0 commit comments