Skip to content

Commit 60b2805

Browse files
committed
Update to 3.1
- Fix test to work on Catalog.API controller, using in-memory database
1 parent b5ab54a commit 60b2805

5 files changed

Lines changed: 129 additions & 89 deletions

File tree

src/Services/Catalog/Catalog.API/Catalog.API.csproj

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.0</TargetFramework>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
55
<DebugType>portable</DebugType>
66
<PreserveCompilationContext>true</PreserveCompilationContext>
77
<AssemblyName>Catalog.API</AssemblyName>
@@ -42,22 +42,22 @@
4242
</ItemGroup>
4343

4444
<ItemGroup>
45-
<PackageReference Include="AspNetCore.HealthChecks.AzureServiceBus" Version="2.2.5" />
46-
<PackageReference Include="AspNetCore.HealthChecks.AzureStorage" Version="2.2.2" />
47-
<PackageReference Include="AspNetCore.HealthChecks.Rabbitmq" Version="2.2.1" />
48-
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="2.2.1" />
49-
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="2.2.4" />
50-
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="5.0.0-rc1" />
51-
<PackageReference Include="Google.Protobuf" Version="3.10.0-rc1" />
52-
<PackageReference Include="Grpc.AspNetCore.Server" Version="0.2.23-pre2" />
53-
<PackageReference Include="Grpc.Tools" Version="2.24.0-pre1" PrivateAssets="All" />
54-
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.8.0-beta3" />
55-
<PackageReference Include="Microsoft.ApplicationInsights.DependencyCollector" Version="2.11.0-beta2" />
45+
<PackageReference Include="AspNetCore.HealthChecks.AzureServiceBus" Version="3.0.0" />
46+
<PackageReference Include="AspNetCore.HealthChecks.AzureStorage" Version="3.0.1" />
47+
<PackageReference Include="AspNetCore.HealthChecks.Rabbitmq" Version="3.0.3" />
48+
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="3.0.0" />
49+
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="3.0.0" />
50+
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="5.0.1" />
51+
<PackageReference Include="Google.Protobuf" Version="3.11.2" />
52+
<PackageReference Include="Grpc.AspNetCore.Server" Version="2.25.0" />
53+
<PackageReference Include="Grpc.Tools" Version="2.25.0" PrivateAssets="All" />
54+
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.12.0" />
55+
<PackageReference Include="Microsoft.ApplicationInsights.DependencyCollector" Version="2.12.0" />
5656
<PackageReference Include="Microsoft.ApplicationInsights.Kubernetes" Version="1.1.1" />
5757
<PackageReference Include="Microsoft.AspNetCore.HealthChecks" Version="1.0.0" />
58-
<PackageReference Include="Microsoft.Extensions.Configuration.AzureKeyVault" Version="3.0.0-rc1.19456.10" />
59-
<PackageReference Include="Microsoft.Extensions.Logging.AzureAppServices" Version="3.0.0-rc1.19456.10" />
60-
<PackageReference Include="Serilog.AspNetCore" Version="3.0.1-dev-00099" />
58+
<PackageReference Include="Microsoft.Extensions.Configuration.AzureKeyVault" Version="3.1.0" />
59+
<PackageReference Include="Microsoft.Extensions.Logging.AzureAppServices" Version="3.1.0" />
60+
<PackageReference Include="Serilog.AspNetCore" Version="3.2.0" />
6161
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.1.3" />
6262
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.1-dev-00209" />
6363
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.2-dev-00824" />

src/Services/Catalog/Catalog.FunctionalTests/Catalog.FunctionalTests.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.0</TargetFramework>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
55

66
<IsPackable>false</IsPackable>
77
</PropertyGroup>
@@ -33,9 +33,9 @@
3333
</ItemGroup>
3434

3535
<ItemGroup>
36-
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.0.0-rc1.19457.4" />
37-
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="3.0.0-rc1.19457.4" />
38-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0-preview-20190828-03" />
36+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.0" />
37+
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="3.1.0" />
38+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
3939
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
4040
<PrivateAssets>all</PrivateAssets>
4141
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
Lines changed: 100 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,132 @@
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;
610
using Moq;
711
using System.Collections.Generic;
12+
using System.Linq;
813
using System.Threading.Tasks;
914
using Xunit;
10-
using CatalogModel = Microsoft.eShopOnContainers.WebMVC.ViewModels.Catalog;
1115

1216
namespace UnitTest.Catalog.Application
1317
{
1418
public class CatalogControllerTest
1519
{
16-
private readonly Mock<ICatalogService> _catalogServiceMock;
20+
private readonly DbContextOptions<CatalogContext> _dbOptions;
1721

1822
public CatalogControllerTest()
1923
{
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+
}
2133
}
2234

2335
[Fact]
2436
public async Task Get_catalog_items_success()
2537
{
2638
//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;
3143

32-
var expectedNumberOfPages = 5;
33-
var expectedTotalPages = 50;
34-
var expectedCurrentPage = 2;
44+
var expectedItemsInPage = 2;
45+
var expectedTotalItems = 6;
3546

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>();
4451

4552
//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);
4855

4956
//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()
6066
{
61-
return new CatalogModel()
67+
return new List<CatalogItem>()
6268
{
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()
6794
{
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"
86116
}
87117
};
88118
}
89119
}
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+
90132
}

src/Services/Catalog/Catalog.UnitTests/Catalog.UnitTests.csproj

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.0</TargetFramework>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
55
<GenerateErrorForMissingTargetingPacks>false</GenerateErrorForMissingTargetingPacks>
66
<IsPackable>false</IsPackable>
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="3.0.0-preview4-19123-01" />
11-
<PackageReference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" Version="3.0.0-preview4-19123-01" />
12-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0-preview-20190828-03" />
13-
<PackageReference Include="Microsoft.NETCore.Platforms" Version="3.0.0-rc1.19456.4" />
14-
<PackageReference Include="Moq" Version="4.13.0" />
10+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.0" />
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
12+
<PackageReference Include="Microsoft.NETCore.Platforms" Version="3.1.0" />
13+
<PackageReference Include="Moq" Version="4.13.1" />
1514
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
1615
<PrivateAssets>all</PrivateAssets>
1716
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
@@ -20,7 +19,6 @@
2019
</ItemGroup>
2120

2221
<ItemGroup>
23-
<ProjectReference Include="..\..\..\Web\WebMVC\WebMVC.csproj" />
2422
<ProjectReference Include="..\Catalog.API\Catalog.API.csproj" />
2523
</ItemGroup>
2624

src/Tests/Services/Application.FunctionalTests/Application.FunctionalTests.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.0</TargetFramework>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
55
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
66
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
77
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
@@ -67,10 +67,10 @@
6767
</ItemGroup>
6868

6969
<ItemGroup>
70-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
71-
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="3.0.0-preview8.19405.7" />
72-
<PackageReference Include="xunit" Version="2.4.0" />
73-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0">
70+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
71+
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="3.1.0" />
72+
<PackageReference Include="xunit" Version="2.4.1" />
73+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
7474
<PrivateAssets>all</PrivateAssets>
7575
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
7676
</PackageReference>

0 commit comments

Comments
 (0)