|
|
@ -1,4 +1,4 @@ |
|
|
|
using Catalog.API.IntegrationEvents; |
|
|
|
using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents; |
|
|
|
using Microsoft.AspNetCore.Mvc; |
|
|
|
using Microsoft.EntityFrameworkCore; |
|
|
|
using Microsoft.eShopOnContainers.Services.Catalog.API; |
|
|
@ -13,120 +13,118 @@ using System.Linq; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Xunit; |
|
|
|
|
|
|
|
namespace UnitTest.Catalog.Application |
|
|
|
namespace UnitTest.Catalog.Application; |
|
|
|
|
|
|
|
public class CatalogControllerTest |
|
|
|
{ |
|
|
|
public class CatalogControllerTest |
|
|
|
private readonly DbContextOptions<CatalogContext> _dbOptions; |
|
|
|
|
|
|
|
public CatalogControllerTest() |
|
|
|
{ |
|
|
|
private readonly DbContextOptions<CatalogContext> _dbOptions; |
|
|
|
_dbOptions = new DbContextOptionsBuilder<CatalogContext>() |
|
|
|
.UseInMemoryDatabase(databaseName: "in-memory") |
|
|
|
.Options; |
|
|
|
|
|
|
|
public CatalogControllerTest() |
|
|
|
using (var dbContext = new CatalogContext(_dbOptions)) |
|
|
|
{ |
|
|
|
_dbOptions = new DbContextOptionsBuilder<CatalogContext>() |
|
|
|
.UseInMemoryDatabase(databaseName: "in-memory") |
|
|
|
.Options; |
|
|
|
|
|
|
|
using (var dbContext = new CatalogContext(_dbOptions)) |
|
|
|
{ |
|
|
|
dbContext.AddRange(GetFakeCatalog()); |
|
|
|
dbContext.SaveChanges(); |
|
|
|
} |
|
|
|
dbContext.AddRange(GetFakeCatalog()); |
|
|
|
dbContext.SaveChanges(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task Get_catalog_items_success() |
|
|
|
{ |
|
|
|
//Arrange
|
|
|
|
var brandFilterApplied = 1; |
|
|
|
var typesFilterApplied = 2; |
|
|
|
var pageSize = 4; |
|
|
|
var pageIndex = 1; |
|
|
|
|
|
|
|
var expectedItemsInPage = 2; |
|
|
|
var expectedTotalItems = 6; |
|
|
|
[Fact] |
|
|
|
public async Task Get_catalog_items_success() |
|
|
|
{ |
|
|
|
//Arrange
|
|
|
|
var brandFilterApplied = 1; |
|
|
|
var typesFilterApplied = 2; |
|
|
|
var pageSize = 4; |
|
|
|
var pageIndex = 1; |
|
|
|
|
|
|
|
var catalogContext = new CatalogContext(_dbOptions); |
|
|
|
var catalogSettings = new TestCatalogSettings(); |
|
|
|
var expectedItemsInPage = 2; |
|
|
|
var expectedTotalItems = 6; |
|
|
|
|
|
|
|
var integrationServicesMock = new Mock<ICatalogIntegrationEventService>(); |
|
|
|
var catalogContext = new CatalogContext(_dbOptions); |
|
|
|
var catalogSettings = new TestCatalogSettings(); |
|
|
|
|
|
|
|
//Act
|
|
|
|
var orderController = new CatalogController(catalogContext, catalogSettings, integrationServicesMock.Object); |
|
|
|
var actionResult = await orderController.ItemsByTypeIdAndBrandIdAsync(typesFilterApplied, brandFilterApplied, pageSize, pageIndex); |
|
|
|
var integrationServicesMock = new Mock<ICatalogIntegrationEventService>(); |
|
|
|
|
|
|
|
//Assert
|
|
|
|
Assert.IsType<ActionResult<PaginatedItemsViewModel<CatalogItem>>>(actionResult); |
|
|
|
var page = Assert.IsAssignableFrom<PaginatedItemsViewModel<CatalogItem>>(actionResult.Value); |
|
|
|
Assert.Equal(expectedTotalItems, page.Count); |
|
|
|
Assert.Equal(pageIndex, page.PageIndex); |
|
|
|
Assert.Equal(pageSize, page.PageSize); |
|
|
|
Assert.Equal(expectedItemsInPage, page.Data.Count()); |
|
|
|
} |
|
|
|
//Act
|
|
|
|
var orderController = new CatalogController(catalogContext, catalogSettings, integrationServicesMock.Object); |
|
|
|
var actionResult = await orderController.ItemsByTypeIdAndBrandIdAsync(typesFilterApplied, brandFilterApplied, pageSize, pageIndex); |
|
|
|
|
|
|
|
private List<CatalogItem> GetFakeCatalog() |
|
|
|
{ |
|
|
|
return new List<CatalogItem>() |
|
|
|
{ |
|
|
|
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() |
|
|
|
{ |
|
|
|
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" |
|
|
|
} |
|
|
|
}; |
|
|
|
} |
|
|
|
//Assert
|
|
|
|
Assert.IsType<ActionResult<PaginatedItemsViewModel<CatalogItem>>>(actionResult); |
|
|
|
var page = Assert.IsAssignableFrom<PaginatedItemsViewModel<CatalogItem>>(actionResult.Value); |
|
|
|
Assert.Equal(expectedTotalItems, page.Count); |
|
|
|
Assert.Equal(pageIndex, page.PageIndex); |
|
|
|
Assert.Equal(pageSize, page.PageSize); |
|
|
|
Assert.Equal(expectedItemsInPage, page.Data.Count()); |
|
|
|
} |
|
|
|
|
|
|
|
public class TestCatalogSettings : IOptionsSnapshot<CatalogSettings> |
|
|
|
private List<CatalogItem> GetFakeCatalog() |
|
|
|
{ |
|
|
|
public CatalogSettings Value => new CatalogSettings |
|
|
|
return new List<CatalogItem>() |
|
|
|
{ |
|
|
|
PicBaseUrl = "http://image-server.com/", |
|
|
|
AzureStorageEnabled = true |
|
|
|
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() |
|
|
|
{ |
|
|
|
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 CatalogSettings Get(string name) => Value; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public class TestCatalogSettings : IOptionsSnapshot<CatalogSettings> |
|
|
|
{ |
|
|
|
public CatalogSettings Value => new CatalogSettings |
|
|
|
{ |
|
|
|
PicBaseUrl = "http://image-server.com/", |
|
|
|
AzureStorageEnabled = true |
|
|
|
}; |
|
|
|
|
|
|
|
public CatalogSettings Get(string name) => Value; |
|
|
|
} |