Shady nagy/net6 (#614)
* udated to .net6 * used the .net6 version RC2 * added editconfig. * App core new Scoped Namespaces style. * BlazorAdmin new Scoped Namespaces style. * Blazor Shared new Scoped Namespaces style. * Infra new Scoped Namespaces style. * public api new Scoped Namespaces style. * web new Scoped Namespaces style. * FunctionalTests new Scoped Namespaces style. * Integrational tests new Scoped Namespaces style. * unit tests new Scoped Namespaces style. * update github action. * update github action. * change the global.
This commit is contained in:
@@ -1,76 +1,75 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
||||
using System;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Entities.BasketTests
|
||||
{
|
||||
public class BasketAddItem
|
||||
{
|
||||
private readonly int _testCatalogItemId = 123;
|
||||
private readonly decimal _testUnitPrice = 1.23m;
|
||||
private readonly int _testQuantity = 2;
|
||||
private readonly string _buyerId = "Test buyerId";
|
||||
|
||||
[Fact]
|
||||
public void AddsBasketItemIfNotPresent()
|
||||
{
|
||||
var basket = new Basket(_buyerId);
|
||||
basket.AddItem(_testCatalogItemId, _testUnitPrice, _testQuantity);
|
||||
|
||||
var firstItem = basket.Items.Single();
|
||||
Assert.Equal(_testCatalogItemId, firstItem.CatalogItemId);
|
||||
Assert.Equal(_testUnitPrice, firstItem.UnitPrice);
|
||||
Assert.Equal(_testQuantity, firstItem.Quantity);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IncrementsQuantityOfItemIfPresent()
|
||||
{
|
||||
var basket = new Basket(_buyerId);
|
||||
basket.AddItem(_testCatalogItemId, _testUnitPrice, _testQuantity);
|
||||
basket.AddItem(_testCatalogItemId, _testUnitPrice, _testQuantity);
|
||||
|
||||
var firstItem = basket.Items.Single();
|
||||
Assert.Equal(_testQuantity * 2, firstItem.Quantity);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void KeepsOriginalUnitPriceIfMoreItemsAdded()
|
||||
{
|
||||
var basket = new Basket(_buyerId);
|
||||
basket.AddItem(_testCatalogItemId, _testUnitPrice, _testQuantity);
|
||||
basket.AddItem(_testCatalogItemId, _testUnitPrice * 2, _testQuantity);
|
||||
|
||||
var firstItem = basket.Items.Single();
|
||||
Assert.Equal(_testUnitPrice, firstItem.UnitPrice);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DefaultsToQuantityOfOne()
|
||||
{
|
||||
var basket = new Basket(_buyerId);
|
||||
basket.AddItem(_testCatalogItemId, _testUnitPrice);
|
||||
|
||||
var firstItem = basket.Items.Single();
|
||||
Assert.Equal(1, firstItem.Quantity);
|
||||
}
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Entities.BasketTests;
|
||||
|
||||
[Fact]
|
||||
public void CantAddItemWithNegativeQuantity()
|
||||
{
|
||||
var basket = new Basket(_buyerId);
|
||||
public class BasketAddItem
|
||||
{
|
||||
private readonly int _testCatalogItemId = 123;
|
||||
private readonly decimal _testUnitPrice = 1.23m;
|
||||
private readonly int _testQuantity = 2;
|
||||
private readonly string _buyerId = "Test buyerId";
|
||||
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => basket.AddItem(_testCatalogItemId, _testUnitPrice, -1));
|
||||
}
|
||||
[Fact]
|
||||
public void AddsBasketItemIfNotPresent()
|
||||
{
|
||||
var basket = new Basket(_buyerId);
|
||||
basket.AddItem(_testCatalogItemId, _testUnitPrice, _testQuantity);
|
||||
|
||||
[Fact]
|
||||
public void CantModifyQuantityToNegativeNumber()
|
||||
{
|
||||
var basket = new Basket(_buyerId);
|
||||
basket.AddItem(_testCatalogItemId, _testUnitPrice);
|
||||
var firstItem = basket.Items.Single();
|
||||
Assert.Equal(_testCatalogItemId, firstItem.CatalogItemId);
|
||||
Assert.Equal(_testUnitPrice, firstItem.UnitPrice);
|
||||
Assert.Equal(_testQuantity, firstItem.Quantity);
|
||||
}
|
||||
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => basket.AddItem(_testCatalogItemId, _testUnitPrice, -2));
|
||||
}
|
||||
}
|
||||
[Fact]
|
||||
public void IncrementsQuantityOfItemIfPresent()
|
||||
{
|
||||
var basket = new Basket(_buyerId);
|
||||
basket.AddItem(_testCatalogItemId, _testUnitPrice, _testQuantity);
|
||||
basket.AddItem(_testCatalogItemId, _testUnitPrice, _testQuantity);
|
||||
|
||||
var firstItem = basket.Items.Single();
|
||||
Assert.Equal(_testQuantity * 2, firstItem.Quantity);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void KeepsOriginalUnitPriceIfMoreItemsAdded()
|
||||
{
|
||||
var basket = new Basket(_buyerId);
|
||||
basket.AddItem(_testCatalogItemId, _testUnitPrice, _testQuantity);
|
||||
basket.AddItem(_testCatalogItemId, _testUnitPrice * 2, _testQuantity);
|
||||
|
||||
var firstItem = basket.Items.Single();
|
||||
Assert.Equal(_testUnitPrice, firstItem.UnitPrice);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DefaultsToQuantityOfOne()
|
||||
{
|
||||
var basket = new Basket(_buyerId);
|
||||
basket.AddItem(_testCatalogItemId, _testUnitPrice);
|
||||
|
||||
var firstItem = basket.Items.Single();
|
||||
Assert.Equal(1, firstItem.Quantity);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CantAddItemWithNegativeQuantity()
|
||||
{
|
||||
var basket = new Basket(_buyerId);
|
||||
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => basket.AddItem(_testCatalogItemId, _testUnitPrice, -1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CantModifyQuantityToNegativeNumber()
|
||||
{
|
||||
var basket = new Basket(_buyerId);
|
||||
basket.AddItem(_testCatalogItemId, _testUnitPrice);
|
||||
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => basket.AddItem(_testCatalogItemId, _testUnitPrice, -2));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,21 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Entities.BasketTests
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Entities.BasketTests;
|
||||
|
||||
public class BasketRemoveEmptyItems
|
||||
{
|
||||
public class BasketRemoveEmptyItems
|
||||
private readonly int _testCatalogItemId = 123;
|
||||
private readonly decimal _testUnitPrice = 1.23m;
|
||||
private readonly string _buyerId = "Test buyerId";
|
||||
|
||||
[Fact]
|
||||
public void RemovesEmptyBasketItems()
|
||||
{
|
||||
private readonly int _testCatalogItemId = 123;
|
||||
private readonly decimal _testUnitPrice = 1.23m;
|
||||
private readonly string _buyerId = "Test buyerId";
|
||||
var basket = new Basket(_buyerId);
|
||||
basket.AddItem(_testCatalogItemId, _testUnitPrice, 0);
|
||||
basket.RemoveEmptyItems();
|
||||
|
||||
[Fact]
|
||||
public void RemovesEmptyBasketItems()
|
||||
{
|
||||
var basket = new Basket(_buyerId);
|
||||
basket.AddItem(_testCatalogItemId, _testUnitPrice, 0);
|
||||
basket.RemoveEmptyItems();
|
||||
|
||||
Assert.Equal(0, basket.Items.Count);
|
||||
}
|
||||
Assert.Equal(0, basket.Items.Count);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,56 +1,55 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
using System;
|
||||
using System;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Entities.CatalogItemTests
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Entities.CatalogItemTests;
|
||||
|
||||
public class UpdateDetails
|
||||
{
|
||||
public class UpdateDetails
|
||||
private CatalogItem _testItem;
|
||||
private int _validTypeId = 1;
|
||||
private int _validBrandId = 2;
|
||||
private string _validDescription = "test description";
|
||||
private string _validName = "test name";
|
||||
private decimal _validPrice = 1.23m;
|
||||
private string _validUri = "/123";
|
||||
|
||||
public UpdateDetails()
|
||||
{
|
||||
private CatalogItem _testItem;
|
||||
private int _validTypeId = 1;
|
||||
private int _validBrandId = 2;
|
||||
private string _validDescription = "test description";
|
||||
private string _validName = "test name";
|
||||
private decimal _validPrice = 1.23m;
|
||||
private string _validUri = "/123";
|
||||
_testItem = new CatalogItem(_validTypeId, _validBrandId, _validDescription, _validName, _validPrice, _validUri);
|
||||
}
|
||||
|
||||
public UpdateDetails()
|
||||
{
|
||||
_testItem = new CatalogItem(_validTypeId, _validBrandId, _validDescription, _validName, _validPrice, _validUri);
|
||||
}
|
||||
[Fact]
|
||||
public void ThrowsArgumentExceptionGivenEmptyName()
|
||||
{
|
||||
string newValue = "";
|
||||
Assert.Throws<ArgumentException>(() => _testItem.UpdateDetails(newValue, _validDescription, _validPrice));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ThrowsArgumentExceptionGivenEmptyName()
|
||||
{
|
||||
string newValue = "";
|
||||
Assert.Throws<ArgumentException>(() => _testItem.UpdateDetails(newValue, _validDescription, _validPrice));
|
||||
}
|
||||
[Fact]
|
||||
public void ThrowsArgumentExceptionGivenEmptyDescription()
|
||||
{
|
||||
string newValue = "";
|
||||
Assert.Throws<ArgumentException>(() => _testItem.UpdateDetails(_validName, newValue, _validPrice));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ThrowsArgumentExceptionGivenEmptyDescription()
|
||||
{
|
||||
string newValue = "";
|
||||
Assert.Throws<ArgumentException>(() => _testItem.UpdateDetails(_validName, newValue, _validPrice));
|
||||
}
|
||||
[Fact]
|
||||
public void ThrowsArgumentNullExceptionGivenNullName()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() => _testItem.UpdateDetails(null, _validDescription, _validPrice));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ThrowsArgumentNullExceptionGivenNullName()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() => _testItem.UpdateDetails(null, _validDescription, _validPrice));
|
||||
}
|
||||
[Fact]
|
||||
public void ThrowsArgumentNullExceptionGivenNullDescription()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() => _testItem.UpdateDetails(_validName, null, _validPrice));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ThrowsArgumentNullExceptionGivenNullDescription()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() => _testItem.UpdateDetails(_validName, null, _validPrice));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0)]
|
||||
[InlineData(-1.23)]
|
||||
public void ThrowsArgumentExceptionGivenNonPositivePrice(decimal newPrice)
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => _testItem.UpdateDetails(_validName, _validDescription, newPrice));
|
||||
}
|
||||
[Theory]
|
||||
[InlineData(0)]
|
||||
[InlineData(-1.23)]
|
||||
public void ThrowsArgumentExceptionGivenNonPositivePrice(decimal newPrice)
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => _testItem.UpdateDetails(_validName, _validDescription, newPrice));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,41 +1,40 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
|
||||
using Microsoft.eShopWeb.UnitTests.Builders;
|
||||
using System.Collections.Generic;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Entities.OrderTests
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Entities.OrderTests;
|
||||
|
||||
public class OrderTotal
|
||||
{
|
||||
public class OrderTotal
|
||||
private decimal _testUnitPrice = 42m;
|
||||
|
||||
[Fact]
|
||||
public void IsZeroForNewOrder()
|
||||
{
|
||||
private decimal _testUnitPrice = 42m;
|
||||
var order = new OrderBuilder().WithNoItems();
|
||||
|
||||
[Fact]
|
||||
public void IsZeroForNewOrder()
|
||||
{
|
||||
var order = new OrderBuilder().WithNoItems();
|
||||
Assert.Equal(0, order.Total());
|
||||
}
|
||||
|
||||
Assert.Equal(0, order.Total());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsCorrectGiven1Item()
|
||||
{
|
||||
var builder = new OrderBuilder();
|
||||
var items = new List<OrderItem>
|
||||
[Fact]
|
||||
public void IsCorrectGiven1Item()
|
||||
{
|
||||
var builder = new OrderBuilder();
|
||||
var items = new List<OrderItem>
|
||||
{
|
||||
new OrderItem(builder.TestCatalogItemOrdered, _testUnitPrice, 1)
|
||||
};
|
||||
var order = new OrderBuilder().WithItems(items);
|
||||
Assert.Equal(_testUnitPrice, order.Total());
|
||||
}
|
||||
var order = new OrderBuilder().WithItems(items);
|
||||
Assert.Equal(_testUnitPrice, order.Total());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsCorrectGiven3Items()
|
||||
{
|
||||
var builder = new OrderBuilder();
|
||||
var order = builder.WithDefaultValues();
|
||||
[Fact]
|
||||
public void IsCorrectGiven3Items()
|
||||
{
|
||||
var builder = new OrderBuilder();
|
||||
var order = builder.WithDefaultValues();
|
||||
|
||||
Assert.Equal(builder.TestUnitPrice * builder.TestUnits, order.Total());
|
||||
}
|
||||
Assert.Equal(builder.TestUnitPrice * builder.TestUnits, order.Total());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,36 +1,35 @@
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Extensions
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Extensions;
|
||||
|
||||
public class JsonExtensions
|
||||
{
|
||||
public class JsonExtensions
|
||||
[Fact]
|
||||
public void CorrectlySerializesAndDeserializesObject()
|
||||
{
|
||||
[Fact]
|
||||
public void CorrectlySerializesAndDeserializesObject()
|
||||
var testParent = new TestParent
|
||||
{
|
||||
var testParent = new TestParent
|
||||
Id = 7,
|
||||
Name = "Test name",
|
||||
Children = new[]
|
||||
{
|
||||
Id = 7,
|
||||
Name = "Test name",
|
||||
Children = new[]
|
||||
{
|
||||
new TestChild(),
|
||||
new TestChild(),
|
||||
new TestChild()
|
||||
}
|
||||
};
|
||||
|
||||
var json = testParent.ToJson();
|
||||
var result = json.FromJson<TestParent>();
|
||||
Assert.Equal(testParent, result);
|
||||
}
|
||||
|
||||
[
|
||||
Theory,
|
||||
InlineData("{ \"id\": 9, \"name\": \"Another test\" }", 9, "Another test"),
|
||||
InlineData("{ \"id\": 3124, \"name\": \"Test Value 1\" }", 3124, "Test Value 1"),
|
||||
]
|
||||
public void CorrectlyDeserializesJson(string json, int expectedId, string expectedName) =>
|
||||
Assert.Equal(new TestParent { Id = expectedId, Name = expectedName }, json.FromJson<TestParent>());
|
||||
};
|
||||
|
||||
var json = testParent.ToJson();
|
||||
var result = json.FromJson<TestParent>();
|
||||
Assert.Equal(testParent, result);
|
||||
}
|
||||
}
|
||||
|
||||
[
|
||||
Theory,
|
||||
InlineData("{ \"id\": 9, \"name\": \"Another test\" }", 9, "Another test"),
|
||||
InlineData("{ \"id\": 3124, \"name\": \"Test Value 1\" }", 3124, "Test Value 1"),
|
||||
]
|
||||
public void CorrectlyDeserializesJson(string json, int expectedId, string expectedName) =>
|
||||
Assert.Equal(new TestParent { Id = expectedId, Name = expectedName }, json.FromJson<TestParent>());
|
||||
|
||||
}
|
||||
|
||||
@@ -2,16 +2,15 @@
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Extensions
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Extensions;
|
||||
|
||||
[DebuggerDisplay("Id={Id}, Date={Date}")]
|
||||
public class TestChild : IEquatable<TestChild>
|
||||
{
|
||||
[DebuggerDisplay("Id={Id}, Date={Date}")]
|
||||
public class TestChild : IEquatable<TestChild>
|
||||
{
|
||||
public Guid Id { get; set; } = Guid.NewGuid();
|
||||
public Guid Id { get; set; } = Guid.NewGuid();
|
||||
|
||||
public DateTime Date { get; set; } = DateTime.UtcNow;
|
||||
public DateTime Date { get; set; } = DateTime.UtcNow;
|
||||
|
||||
public bool Equals([AllowNull] TestChild other) =>
|
||||
other?.Date == Date && other?.Id == Id;
|
||||
}
|
||||
public bool Equals([AllowNull] TestChild other) =>
|
||||
other?.Date == Date && other?.Id == Id;
|
||||
}
|
||||
|
||||
@@ -3,19 +3,18 @@ using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Extensions
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Extensions;
|
||||
|
||||
public class TestParent : IEquatable<TestParent>
|
||||
{
|
||||
public class TestParent : IEquatable<TestParent>
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public int Id { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
public string Name { get; set; }
|
||||
|
||||
public IEnumerable<TestChild> Children { get; set; }
|
||||
public IEnumerable<TestChild> Children { get; set; }
|
||||
|
||||
public bool Equals([AllowNull] TestParent other) =>
|
||||
other?.Id == Id && other?.Name == Name &&
|
||||
(other?.Children is null && Children is null ||
|
||||
(other?.Children?.Zip(Children)?.All(t => t.First?.Equals(t.Second) ?? false) ?? false));
|
||||
}
|
||||
}
|
||||
public bool Equals([AllowNull] TestParent other) =>
|
||||
other?.Id == Id && other?.Name == Name &&
|
||||
(other?.Children is null && Children is null ||
|
||||
(other?.Children?.Zip(Children)?.All(t => t.First?.Equals(t.Second) ?? false) ?? false));
|
||||
}
|
||||
|
||||
@@ -1,44 +1,43 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Services;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Specifications;
|
||||
using Moq;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests;
|
||||
|
||||
public class AddItemToBasket
|
||||
{
|
||||
public class AddItemToBasket
|
||||
private readonly string _buyerId = "Test buyerId";
|
||||
private readonly Mock<IRepository<Basket>> _mockBasketRepo = new();
|
||||
|
||||
[Fact]
|
||||
public async Task InvokesBasketRepositoryGetBySpecAsyncOnce()
|
||||
{
|
||||
private readonly string _buyerId = "Test buyerId";
|
||||
private readonly Mock<IRepository<Basket>> _mockBasketRepo = new();
|
||||
var basket = new Basket(_buyerId);
|
||||
basket.AddItem(1, It.IsAny<decimal>(), It.IsAny<int>());
|
||||
_mockBasketRepo.Setup(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default)).ReturnsAsync(basket);
|
||||
|
||||
[Fact]
|
||||
public async Task InvokesBasketRepositoryGetBySpecAsyncOnce()
|
||||
{
|
||||
var basket = new Basket(_buyerId);
|
||||
basket.AddItem(1, It.IsAny<decimal>(), It.IsAny<int>());
|
||||
_mockBasketRepo.Setup(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default)).ReturnsAsync(basket);
|
||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||
|
||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||
await basketService.AddItemToBasket(basket.BuyerId, 1, 1.50m);
|
||||
|
||||
await basketService.AddItemToBasket(basket.BuyerId, 1, 1.50m);
|
||||
_mockBasketRepo.Verify(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default), Times.Once);
|
||||
}
|
||||
|
||||
_mockBasketRepo.Verify(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default), Times.Once);
|
||||
}
|
||||
[Fact]
|
||||
public async Task InvokesBasketRepositoryUpdateAsyncOnce()
|
||||
{
|
||||
var basket = new Basket(_buyerId);
|
||||
basket.AddItem(1, It.IsAny<decimal>(), It.IsAny<int>());
|
||||
_mockBasketRepo.Setup(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default)).ReturnsAsync(basket);
|
||||
|
||||
[Fact]
|
||||
public async Task InvokesBasketRepositoryUpdateAsyncOnce()
|
||||
{
|
||||
var basket = new Basket(_buyerId);
|
||||
basket.AddItem(1, It.IsAny<decimal>(), It.IsAny<int>());
|
||||
_mockBasketRepo.Setup(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default)).ReturnsAsync(basket);
|
||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||
|
||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||
await basketService.AddItemToBasket(basket.BuyerId, 1, 1.50m);
|
||||
|
||||
await basketService.AddItemToBasket(basket.BuyerId, 1, 1.50m);
|
||||
|
||||
_mockBasketRepo.Verify(x => x.UpdateAsync(basket, default), Times.Once);
|
||||
}
|
||||
_mockBasketRepo.Verify(x => x.UpdateAsync(basket, default), Times.Once);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,30 +1,29 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Services;
|
||||
using Moq;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests;
|
||||
|
||||
public class DeleteBasket
|
||||
{
|
||||
public class DeleteBasket
|
||||
private readonly string _buyerId = "Test buyerId";
|
||||
private readonly Mock<IRepository<Basket>> _mockBasketRepo = new();
|
||||
|
||||
[Fact]
|
||||
public async Task ShouldInvokeBasketRepositoryDeleteAsyncOnce()
|
||||
{
|
||||
private readonly string _buyerId = "Test buyerId";
|
||||
private readonly Mock<IRepository<Basket>> _mockBasketRepo = new();
|
||||
var basket = new Basket(_buyerId);
|
||||
basket.AddItem(1, It.IsAny<decimal>(), It.IsAny<int>());
|
||||
basket.AddItem(2, It.IsAny<decimal>(), It.IsAny<int>());
|
||||
_mockBasketRepo.Setup(x => x.GetByIdAsync(It.IsAny<int>(), default))
|
||||
.ReturnsAsync(basket);
|
||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||
|
||||
[Fact]
|
||||
public async Task ShouldInvokeBasketRepositoryDeleteAsyncOnce()
|
||||
{
|
||||
var basket = new Basket(_buyerId);
|
||||
basket.AddItem(1, It.IsAny<decimal>(), It.IsAny<int>());
|
||||
basket.AddItem(2, It.IsAny<decimal>(), It.IsAny<int>());
|
||||
_mockBasketRepo.Setup(x => x.GetByIdAsync(It.IsAny<int>(),default))
|
||||
.ReturnsAsync(basket);
|
||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||
await basketService.DeleteBasketAsync(It.IsAny<int>());
|
||||
|
||||
await basketService.DeleteBasketAsync(It.IsAny<int>());
|
||||
|
||||
_mockBasketRepo.Verify(x => x.DeleteAsync(It.IsAny<Basket>(),default), Times.Once);
|
||||
}
|
||||
_mockBasketRepo.Verify(x => x.DeleteAsync(It.IsAny<Basket>(), default), Times.Once);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,35 +1,34 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Exceptions;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Services;
|
||||
using Moq;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests;
|
||||
|
||||
public class SetQuantities
|
||||
{
|
||||
public class SetQuantities
|
||||
private readonly int _invalidId = -1;
|
||||
private readonly Mock<IRepository<Basket>> _mockBasketRepo = new();
|
||||
|
||||
[Fact]
|
||||
public async Task ThrowsGivenInvalidBasketId()
|
||||
{
|
||||
private readonly int _invalidId = -1;
|
||||
private readonly Mock<IRepository<Basket>> _mockBasketRepo = new();
|
||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||
|
||||
[Fact]
|
||||
public async Task ThrowsGivenInvalidBasketId()
|
||||
{
|
||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||
await Assert.ThrowsAsync<BasketNotFoundException>(async () =>
|
||||
await basketService.SetQuantities(_invalidId, new System.Collections.Generic.Dictionary<string, int>()));
|
||||
}
|
||||
|
||||
await Assert.ThrowsAsync<BasketNotFoundException>(async () =>
|
||||
await basketService.SetQuantities(_invalidId, new System.Collections.Generic.Dictionary<string, int>()));
|
||||
}
|
||||
[Fact]
|
||||
public async Task ThrowsGivenNullQuantities()
|
||||
{
|
||||
var basketService = new BasketService(null, null);
|
||||
|
||||
[Fact]
|
||||
public async Task ThrowsGivenNullQuantities()
|
||||
{
|
||||
var basketService = new BasketService(null, null);
|
||||
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(async () =>
|
||||
await basketService.SetQuantities(123, null));
|
||||
}
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(async () =>
|
||||
await basketService.SetQuantities(123, null));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,97 +1,96 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Services;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Specifications;
|
||||
using Moq;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests;
|
||||
|
||||
public class TransferBasket
|
||||
{
|
||||
public class TransferBasket
|
||||
private readonly string _nonexistentAnonymousBasketBuyerId = "nonexistent-anonymous-basket-buyer-id";
|
||||
private readonly string _existentAnonymousBasketBuyerId = "existent-anonymous-basket-buyer-id";
|
||||
private readonly string _nonexistentUserBasketBuyerId = "newuser@microsoft.com";
|
||||
private readonly string _existentUserBasketBuyerId = "testuser@microsoft.com";
|
||||
private readonly Mock<IRepository<Basket>> _mockBasketRepo = new();
|
||||
|
||||
[Fact]
|
||||
public async Task ThrowsGivenNullAnonymousId()
|
||||
{
|
||||
private readonly string _nonexistentAnonymousBasketBuyerId = "nonexistent-anonymous-basket-buyer-id";
|
||||
private readonly string _existentAnonymousBasketBuyerId = "existent-anonymous-basket-buyer-id";
|
||||
private readonly string _nonexistentUserBasketBuyerId = "newuser@microsoft.com";
|
||||
private readonly string _existentUserBasketBuyerId = "testuser@microsoft.com";
|
||||
private readonly Mock<IRepository<Basket>> _mockBasketRepo = new();
|
||||
var basketService = new BasketService(null, null);
|
||||
|
||||
[Fact]
|
||||
public async Task ThrowsGivenNullAnonymousId()
|
||||
{
|
||||
var basketService = new BasketService(null, null);
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(async () => await basketService.TransferBasketAsync(null, "steve"));
|
||||
}
|
||||
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(async () => await basketService.TransferBasketAsync(null, "steve"));
|
||||
}
|
||||
[Fact]
|
||||
public async Task ThrowsGivenNullUserId()
|
||||
{
|
||||
var basketService = new BasketService(null, null);
|
||||
|
||||
[Fact]
|
||||
public async Task ThrowsGivenNullUserId()
|
||||
{
|
||||
var basketService = new BasketService(null, null);
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(async () => await basketService.TransferBasketAsync("abcdefg", null));
|
||||
}
|
||||
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(async () => await basketService.TransferBasketAsync("abcdefg", null));
|
||||
}
|
||||
[Fact]
|
||||
public async Task InvokesBasketRepositoryFirstOrDefaultAsyncOnceIfAnonymousBasketNotExists()
|
||||
{
|
||||
var anonymousBasket = null as Basket;
|
||||
var userBasket = new Basket(_existentUserBasketBuyerId);
|
||||
_mockBasketRepo.SetupSequence(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default))
|
||||
.ReturnsAsync(anonymousBasket)
|
||||
.ReturnsAsync(userBasket);
|
||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||
await basketService.TransferBasketAsync(_nonexistentAnonymousBasketBuyerId, _existentUserBasketBuyerId);
|
||||
_mockBasketRepo.Verify(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task InvokesBasketRepositoryFirstOrDefaultAsyncOnceIfAnonymousBasketNotExists()
|
||||
{
|
||||
var anonymousBasket = null as Basket;
|
||||
var userBasket = new Basket(_existentUserBasketBuyerId);
|
||||
_mockBasketRepo.SetupSequence(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default))
|
||||
.ReturnsAsync(anonymousBasket)
|
||||
.ReturnsAsync(userBasket);
|
||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||
await basketService.TransferBasketAsync(_nonexistentAnonymousBasketBuyerId, _existentUserBasketBuyerId);
|
||||
_mockBasketRepo.Verify(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default), Times.Once);
|
||||
}
|
||||
[Fact]
|
||||
public async Task TransferAnonymousBasketItemsWhilePreservingExistingUserBasketItems()
|
||||
{
|
||||
var anonymousBasket = new Basket(_existentAnonymousBasketBuyerId);
|
||||
anonymousBasket.AddItem(1, 10, 1);
|
||||
anonymousBasket.AddItem(3, 55, 7);
|
||||
var userBasket = new Basket(_existentUserBasketBuyerId);
|
||||
userBasket.AddItem(1, 10, 4);
|
||||
userBasket.AddItem(2, 99, 3);
|
||||
_mockBasketRepo.SetupSequence(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default))
|
||||
.ReturnsAsync(anonymousBasket)
|
||||
.ReturnsAsync(userBasket);
|
||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||
await basketService.TransferBasketAsync(_nonexistentAnonymousBasketBuyerId, _existentUserBasketBuyerId);
|
||||
_mockBasketRepo.Verify(x => x.UpdateAsync(userBasket, default), Times.Once);
|
||||
Assert.Equal(3, userBasket.Items.Count);
|
||||
Assert.Contains(userBasket.Items, x => x.CatalogItemId == 1 && x.UnitPrice == 10 && x.Quantity == 5);
|
||||
Assert.Contains(userBasket.Items, x => x.CatalogItemId == 2 && x.UnitPrice == 99 && x.Quantity == 3);
|
||||
Assert.Contains(userBasket.Items, x => x.CatalogItemId == 3 && x.UnitPrice == 55 && x.Quantity == 7);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TransferAnonymousBasketItemsWhilePreservingExistingUserBasketItems()
|
||||
{
|
||||
var anonymousBasket = new Basket(_existentAnonymousBasketBuyerId);
|
||||
anonymousBasket.AddItem(1, 10, 1);
|
||||
anonymousBasket.AddItem(3, 55, 7);
|
||||
var userBasket = new Basket(_existentUserBasketBuyerId);
|
||||
userBasket.AddItem(1, 10, 4);
|
||||
userBasket.AddItem(2, 99, 3);
|
||||
_mockBasketRepo.SetupSequence(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default))
|
||||
.ReturnsAsync(anonymousBasket)
|
||||
.ReturnsAsync(userBasket);
|
||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||
await basketService.TransferBasketAsync(_nonexistentAnonymousBasketBuyerId, _existentUserBasketBuyerId);
|
||||
_mockBasketRepo.Verify(x => x.UpdateAsync(userBasket, default), Times.Once);
|
||||
Assert.Equal(3, userBasket.Items.Count);
|
||||
Assert.Contains(userBasket.Items, x => x.CatalogItemId == 1 && x.UnitPrice == 10 && x.Quantity == 5);
|
||||
Assert.Contains(userBasket.Items, x => x.CatalogItemId == 2 && x.UnitPrice == 99 && x.Quantity == 3);
|
||||
Assert.Contains(userBasket.Items, x => x.CatalogItemId == 3 && x.UnitPrice == 55 && x.Quantity == 7);
|
||||
}
|
||||
[Fact]
|
||||
public async Task RemovesAnonymousBasketAfterUpdatingUserBasket()
|
||||
{
|
||||
var anonymousBasket = new Basket(_existentAnonymousBasketBuyerId);
|
||||
var userBasket = new Basket(_existentUserBasketBuyerId);
|
||||
_mockBasketRepo.SetupSequence(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default))
|
||||
.ReturnsAsync(anonymousBasket)
|
||||
.ReturnsAsync(userBasket);
|
||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||
await basketService.TransferBasketAsync(_nonexistentAnonymousBasketBuyerId, _existentUserBasketBuyerId);
|
||||
_mockBasketRepo.Verify(x => x.UpdateAsync(userBasket, default), Times.Once);
|
||||
_mockBasketRepo.Verify(x => x.DeleteAsync(anonymousBasket, default), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RemovesAnonymousBasketAfterUpdatingUserBasket()
|
||||
{
|
||||
var anonymousBasket = new Basket(_existentAnonymousBasketBuyerId);
|
||||
var userBasket = new Basket(_existentUserBasketBuyerId);
|
||||
_mockBasketRepo.SetupSequence(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default))
|
||||
.ReturnsAsync(anonymousBasket)
|
||||
.ReturnsAsync(userBasket);
|
||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||
await basketService.TransferBasketAsync(_nonexistentAnonymousBasketBuyerId, _existentUserBasketBuyerId);
|
||||
_mockBasketRepo.Verify(x => x.UpdateAsync(userBasket, default), Times.Once);
|
||||
_mockBasketRepo.Verify(x => x.DeleteAsync(anonymousBasket, default), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreatesNewUserBasketIfNotExists()
|
||||
{
|
||||
var anonymousBasket = new Basket(_existentAnonymousBasketBuyerId);
|
||||
var userBasket = null as Basket;
|
||||
_mockBasketRepo.SetupSequence(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default))
|
||||
.ReturnsAsync(anonymousBasket)
|
||||
.ReturnsAsync(userBasket);
|
||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||
await basketService.TransferBasketAsync(_existentAnonymousBasketBuyerId, _nonexistentUserBasketBuyerId);
|
||||
_mockBasketRepo.Verify(x => x.AddAsync(It.Is<Basket>(x => x.BuyerId == _nonexistentUserBasketBuyerId), default), Times.Once);
|
||||
}
|
||||
[Fact]
|
||||
public async Task CreatesNewUserBasketIfNotExists()
|
||||
{
|
||||
var anonymousBasket = new Basket(_existentAnonymousBasketBuyerId);
|
||||
var userBasket = null as Basket;
|
||||
_mockBasketRepo.SetupSequence(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default))
|
||||
.ReturnsAsync(anonymousBasket)
|
||||
.ReturnsAsync(userBasket);
|
||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||
await basketService.TransferBasketAsync(_existentAnonymousBasketBuyerId, _nonexistentUserBasketBuyerId);
|
||||
_mockBasketRepo.Verify(x => x.AddAsync(It.Is<Basket>(x => x.BuyerId == _nonexistentUserBasketBuyerId), default), Times.Once);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,76 +1,75 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Specifications;
|
||||
using Moq;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications;
|
||||
|
||||
public class BasketWithItems
|
||||
{
|
||||
public class BasketWithItems
|
||||
private readonly int _testBasketId = 123;
|
||||
private readonly string _buyerId = "Test buyerId";
|
||||
|
||||
[Fact]
|
||||
public void MatchesBasketWithGivenBasketId()
|
||||
{
|
||||
private readonly int _testBasketId = 123;
|
||||
private readonly string _buyerId = "Test buyerId";
|
||||
var spec = new BasketWithItemsSpecification(_testBasketId);
|
||||
|
||||
[Fact]
|
||||
public void MatchesBasketWithGivenBasketId()
|
||||
{
|
||||
var spec = new BasketWithItemsSpecification(_testBasketId);
|
||||
var result = spec.Evaluate(GetTestBasketCollection()).FirstOrDefault();
|
||||
|
||||
var result = spec.Evaluate(GetTestBasketCollection()).FirstOrDefault();
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(_testBasketId, result.Id);
|
||||
}
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(_testBasketId, result.Id);
|
||||
}
|
||||
[Fact]
|
||||
public void MatchesNoBasketsIfBasketIdNotPresent()
|
||||
{
|
||||
int badBasketId = -1;
|
||||
var spec = new BasketWithItemsSpecification(badBasketId);
|
||||
|
||||
[Fact]
|
||||
public void MatchesNoBasketsIfBasketIdNotPresent()
|
||||
{
|
||||
int badBasketId = -1;
|
||||
var spec = new BasketWithItemsSpecification(badBasketId);
|
||||
var result = spec.Evaluate(GetTestBasketCollection()).Any();
|
||||
|
||||
var result = spec.Evaluate(GetTestBasketCollection()).Any();
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
Assert.False(result);
|
||||
}
|
||||
[Fact]
|
||||
public void MatchesBasketWithGivenBuyerId()
|
||||
{
|
||||
var spec = new BasketWithItemsSpecification(_buyerId);
|
||||
|
||||
[Fact]
|
||||
public void MatchesBasketWithGivenBuyerId()
|
||||
{
|
||||
var spec = new BasketWithItemsSpecification(_buyerId);
|
||||
var result = spec.Evaluate(GetTestBasketCollection()).FirstOrDefault();
|
||||
|
||||
var result = spec.Evaluate(GetTestBasketCollection()).FirstOrDefault();
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(_buyerId, result.BuyerId);
|
||||
}
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(_buyerId, result.BuyerId);
|
||||
}
|
||||
[Fact]
|
||||
public void MatchesNoBasketsIfBuyerIdNotPresent()
|
||||
{
|
||||
string badBuyerId = "badBuyerId";
|
||||
var spec = new BasketWithItemsSpecification(badBuyerId);
|
||||
|
||||
[Fact]
|
||||
public void MatchesNoBasketsIfBuyerIdNotPresent()
|
||||
{
|
||||
string badBuyerId = "badBuyerId";
|
||||
var spec = new BasketWithItemsSpecification(badBuyerId);
|
||||
var result = spec.Evaluate(GetTestBasketCollection()).Any();
|
||||
|
||||
var result = spec.Evaluate(GetTestBasketCollection()).Any();
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
Assert.False(result);
|
||||
}
|
||||
public List<Basket> GetTestBasketCollection()
|
||||
{
|
||||
var basket1Mock = new Mock<Basket>(_buyerId);
|
||||
basket1Mock.SetupGet(s => s.Id).Returns(1);
|
||||
var basket2Mock = new Mock<Basket>(_buyerId);
|
||||
basket2Mock.SetupGet(s => s.Id).Returns(2);
|
||||
var basket3Mock = new Mock<Basket>(_buyerId);
|
||||
basket3Mock.SetupGet(s => s.Id).Returns(_testBasketId);
|
||||
|
||||
public List<Basket> GetTestBasketCollection()
|
||||
{
|
||||
var basket1Mock = new Mock<Basket>(_buyerId);
|
||||
basket1Mock.SetupGet(s => s.Id).Returns(1);
|
||||
var basket2Mock = new Mock<Basket>(_buyerId);
|
||||
basket2Mock.SetupGet(s => s.Id).Returns(2);
|
||||
var basket3Mock = new Mock<Basket>(_buyerId);
|
||||
basket3Mock.SetupGet(s => s.Id).Returns(_testBasketId);
|
||||
|
||||
return new List<Basket>()
|
||||
return new List<Basket>()
|
||||
{
|
||||
basket1Mock.Object,
|
||||
basket2Mock.Object,
|
||||
basket3Mock.Object
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,48 +1,47 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications;
|
||||
|
||||
public class CatalogFilterPaginatedSpecification
|
||||
{
|
||||
public class CatalogFilterPaginatedSpecification
|
||||
[Fact]
|
||||
public void ReturnsAllCatalogItems()
|
||||
{
|
||||
[Fact]
|
||||
public void ReturnsAllCatalogItems()
|
||||
{
|
||||
var spec = new eShopWeb.ApplicationCore.Specifications.CatalogFilterPaginatedSpecification(0, 10, null, null);
|
||||
var spec = new eShopWeb.ApplicationCore.Specifications.CatalogFilterPaginatedSpecification(0, 10, null, null);
|
||||
|
||||
var result = GetTestCollection()
|
||||
.AsQueryable()
|
||||
.Where(spec.WhereExpressions.FirstOrDefault());
|
||||
var result = GetTestCollection()
|
||||
.AsQueryable()
|
||||
.Where(spec.WhereExpressions.FirstOrDefault());
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(4, result.ToList().Count);
|
||||
}
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(4, result.ToList().Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Returns2CatalogItemsWithSameBrandAndTypeId()
|
||||
{
|
||||
var spec = new eShopWeb.ApplicationCore.Specifications.CatalogFilterPaginatedSpecification(0, 10, 1, 1);
|
||||
[Fact]
|
||||
public void Returns2CatalogItemsWithSameBrandAndTypeId()
|
||||
{
|
||||
var spec = new eShopWeb.ApplicationCore.Specifications.CatalogFilterPaginatedSpecification(0, 10, 1, 1);
|
||||
|
||||
var result = GetTestCollection()
|
||||
.AsQueryable()
|
||||
.Where(spec.WhereExpressions.FirstOrDefault());
|
||||
var result = GetTestCollection()
|
||||
.AsQueryable()
|
||||
.Where(spec.WhereExpressions.FirstOrDefault());
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(2, result.ToList().Count);
|
||||
}
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(2, result.ToList().Count);
|
||||
}
|
||||
|
||||
private List<CatalogItem> GetTestCollection()
|
||||
{
|
||||
var catalogItemList = new List<CatalogItem>();
|
||||
private List<CatalogItem> GetTestCollection()
|
||||
{
|
||||
var catalogItemList = new List<CatalogItem>();
|
||||
|
||||
catalogItemList.Add(new CatalogItem(1, 1, "Item 1", "Item 1", 1.00m, "TestUri1"));
|
||||
catalogItemList.Add(new CatalogItem(1, 1, "Item 1.5", "Item 1.5", 1.50m, "TestUri1"));
|
||||
catalogItemList.Add(new CatalogItem(2, 2, "Item 2", "Item 2", 2.00m, "TestUri2"));
|
||||
catalogItemList.Add(new CatalogItem(3, 3, "Item 3", "Item 3", 3.00m, "TestUri3"));
|
||||
catalogItemList.Add(new CatalogItem(1, 1, "Item 1", "Item 1", 1.00m, "TestUri1"));
|
||||
catalogItemList.Add(new CatalogItem(1, 1, "Item 1.5", "Item 1.5", 1.50m, "TestUri1"));
|
||||
catalogItemList.Add(new CatalogItem(2, 2, "Item 2", "Item 2", 2.00m, "TestUri2"));
|
||||
catalogItemList.Add(new CatalogItem(3, 3, "Item 3", "Item 3", 3.00m, "TestUri3"));
|
||||
|
||||
return catalogItemList;
|
||||
}
|
||||
return catalogItemList;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,41 +1,40 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications;
|
||||
|
||||
public class CatalogFilterSpecification
|
||||
{
|
||||
public class CatalogFilterSpecification
|
||||
[Theory]
|
||||
[InlineData(null, null, 5)]
|
||||
[InlineData(1, null, 3)]
|
||||
[InlineData(2, null, 2)]
|
||||
[InlineData(null, 1, 2)]
|
||||
[InlineData(null, 3, 1)]
|
||||
[InlineData(1, 3, 1)]
|
||||
[InlineData(2, 3, 0)]
|
||||
public void MatchesExpectedNumberOfItems(int? brandId, int? typeId, int expectedCount)
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(null, null, 5)]
|
||||
[InlineData(1, null, 3)]
|
||||
[InlineData(2, null, 2)]
|
||||
[InlineData(null, 1, 2)]
|
||||
[InlineData(null, 3, 1)]
|
||||
[InlineData(1, 3, 1)]
|
||||
[InlineData(2, 3, 0)]
|
||||
public void MatchesExpectedNumberOfItems(int? brandId, int? typeId, int expectedCount)
|
||||
{
|
||||
var spec = new eShopWeb.ApplicationCore.Specifications.CatalogFilterSpecification(brandId, typeId);
|
||||
var spec = new eShopWeb.ApplicationCore.Specifications.CatalogFilterSpecification(brandId, typeId);
|
||||
|
||||
var result = GetTestItemCollection()
|
||||
.AsQueryable()
|
||||
.Where(spec.WhereExpressions.FirstOrDefault());
|
||||
var result = GetTestItemCollection()
|
||||
.AsQueryable()
|
||||
.Where(spec.WhereExpressions.FirstOrDefault());
|
||||
|
||||
Assert.Equal(expectedCount, result.Count());
|
||||
}
|
||||
Assert.Equal(expectedCount, result.Count());
|
||||
}
|
||||
|
||||
public List<CatalogItem> GetTestItemCollection()
|
||||
{
|
||||
return new List<CatalogItem>()
|
||||
public List<CatalogItem> GetTestItemCollection()
|
||||
{
|
||||
return new List<CatalogItem>()
|
||||
{
|
||||
new CatalogItem(1, 1, "Description", "Name", 0, "FakePath"),
|
||||
new CatalogItem(2, 1, "Description", "Name", 0, "FakePath"),
|
||||
new CatalogItem(3, 1, "Description", "Name", 0, "FakePath"),
|
||||
new CatalogItem(1, 2, "Description", "Name", 0, "FakePath"),
|
||||
new CatalogItem(2, 2, "Description", "Name", 0, "FakePath"),
|
||||
new CatalogItem(2, 2, "Description", "Name", 0, "FakePath"),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,55 +1,54 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
using Moq;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications;
|
||||
|
||||
public class CatalogItemsSpecification
|
||||
{
|
||||
public class CatalogItemsSpecification
|
||||
[Fact]
|
||||
public void MatchesSpecificCatalogItem()
|
||||
{
|
||||
[Fact]
|
||||
public void MatchesSpecificCatalogItem()
|
||||
{
|
||||
var catalogItemIds = new int[] { 1 };
|
||||
var spec = new eShopWeb.ApplicationCore.Specifications.CatalogItemsSpecification(catalogItemIds);
|
||||
var catalogItemIds = new int[] { 1 };
|
||||
var spec = new eShopWeb.ApplicationCore.Specifications.CatalogItemsSpecification(catalogItemIds);
|
||||
|
||||
var result = GetTestCollection()
|
||||
.AsQueryable()
|
||||
.Where(spec.WhereExpressions.FirstOrDefault());
|
||||
var result = GetTestCollection()
|
||||
.AsQueryable()
|
||||
.Where(spec.WhereExpressions.FirstOrDefault());
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Single(result.ToList());
|
||||
}
|
||||
Assert.NotNull(result);
|
||||
Assert.Single(result.ToList());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatchesAllCatalogItems()
|
||||
{
|
||||
var catalogItemIds = new int[] { 1, 3 };
|
||||
var spec = new eShopWeb.ApplicationCore.Specifications.CatalogItemsSpecification(catalogItemIds);
|
||||
[Fact]
|
||||
public void MatchesAllCatalogItems()
|
||||
{
|
||||
var catalogItemIds = new int[] { 1, 3 };
|
||||
var spec = new eShopWeb.ApplicationCore.Specifications.CatalogItemsSpecification(catalogItemIds);
|
||||
|
||||
var result = GetTestCollection()
|
||||
.AsQueryable()
|
||||
.Where(spec.WhereExpressions.FirstOrDefault());
|
||||
var result = GetTestCollection()
|
||||
.AsQueryable()
|
||||
.Where(spec.WhereExpressions.FirstOrDefault());
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(2, result.ToList().Count);
|
||||
}
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(2, result.ToList().Count);
|
||||
}
|
||||
|
||||
private List<CatalogItem> GetTestCollection()
|
||||
{
|
||||
var catalogItems = new List<CatalogItem>();
|
||||
private List<CatalogItem> GetTestCollection()
|
||||
{
|
||||
var catalogItems = new List<CatalogItem>();
|
||||
|
||||
var mockCatalogItem1 = new Mock<CatalogItem>(1, 1, "Item 1 description", "Item 1", 1.5m, "Item1Uri");
|
||||
mockCatalogItem1.SetupGet(x => x.Id).Returns(1);
|
||||
var mockCatalogItem1 = new Mock<CatalogItem>(1, 1, "Item 1 description", "Item 1", 1.5m, "Item1Uri");
|
||||
mockCatalogItem1.SetupGet(x => x.Id).Returns(1);
|
||||
|
||||
var mockCatalogItem3 = new Mock<CatalogItem>(3, 3, "Item 3 description", "Item 3", 3.5m, "Item3Uri");
|
||||
mockCatalogItem3.SetupGet(x => x.Id).Returns(3);
|
||||
var mockCatalogItem3 = new Mock<CatalogItem>(3, 3, "Item 3 description", "Item 3", 3.5m, "Item3Uri");
|
||||
mockCatalogItem3.SetupGet(x => x.Id).Returns(3);
|
||||
|
||||
catalogItems.Add(mockCatalogItem1.Object);
|
||||
catalogItems.Add(mockCatalogItem3.Object);
|
||||
catalogItems.Add(mockCatalogItem1.Object);
|
||||
catalogItems.Add(mockCatalogItem3.Object);
|
||||
|
||||
return catalogItems;
|
||||
}
|
||||
return catalogItems;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,66 +1,65 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications;
|
||||
|
||||
public class CustomerOrdersWithItemsSpecification
|
||||
{
|
||||
public class CustomerOrdersWithItemsSpecification
|
||||
private readonly string _buyerId = "TestBuyerId";
|
||||
private Address _shipToAddress = new Address("Street", "City", "OH", "US", "11111");
|
||||
|
||||
[Fact]
|
||||
public void ReturnsOrderWithOrderedItem()
|
||||
{
|
||||
private readonly string _buyerId = "TestBuyerId";
|
||||
private Address _shipToAddress = new Address("Street", "City", "OH", "US", "11111");
|
||||
var spec = new eShopWeb.ApplicationCore.Specifications.CustomerOrdersWithItemsSpecification(_buyerId);
|
||||
|
||||
[Fact]
|
||||
public void ReturnsOrderWithOrderedItem()
|
||||
{
|
||||
var spec = new eShopWeb.ApplicationCore.Specifications.CustomerOrdersWithItemsSpecification(_buyerId);
|
||||
var result = GetTestCollection()
|
||||
.AsQueryable()
|
||||
.FirstOrDefault(spec.WhereExpressions.FirstOrDefault());
|
||||
|
||||
var result = GetTestCollection()
|
||||
.AsQueryable()
|
||||
.FirstOrDefault(spec.WhereExpressions.FirstOrDefault());
|
||||
Assert.NotNull(result);
|
||||
Assert.NotNull(result.OrderItems);
|
||||
Assert.Equal(1, result.OrderItems.Count);
|
||||
Assert.NotNull(result.OrderItems.FirstOrDefault().ItemOrdered);
|
||||
}
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.NotNull(result.OrderItems);
|
||||
Assert.Equal(1, result.OrderItems.Count);
|
||||
Assert.NotNull(result.OrderItems.FirstOrDefault().ItemOrdered);
|
||||
}
|
||||
[Fact]
|
||||
public void ReturnsAllOrderWithAllOrderedItem()
|
||||
{
|
||||
var spec = new eShopWeb.ApplicationCore.Specifications.CustomerOrdersWithItemsSpecification(_buyerId);
|
||||
|
||||
[Fact]
|
||||
public void ReturnsAllOrderWithAllOrderedItem()
|
||||
{
|
||||
var spec = new eShopWeb.ApplicationCore.Specifications.CustomerOrdersWithItemsSpecification(_buyerId);
|
||||
var result = GetTestCollection()
|
||||
.AsQueryable()
|
||||
.Where(spec.WhereExpressions.FirstOrDefault())
|
||||
.ToList();
|
||||
|
||||
var result = GetTestCollection()
|
||||
.AsQueryable()
|
||||
.Where(spec.WhereExpressions.FirstOrDefault())
|
||||
.ToList();
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(2, result.Count);
|
||||
Assert.Equal(1, result[0].OrderItems.Count);
|
||||
Assert.NotNull(result[0].OrderItems.FirstOrDefault().ItemOrdered);
|
||||
Assert.Equal(2, result[1].OrderItems.Count);
|
||||
Assert.NotNull(result[1].OrderItems.ToList()[0].ItemOrdered);
|
||||
Assert.NotNull(result[1].OrderItems.ToList()[1].ItemOrdered);
|
||||
}
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(2, result.Count);
|
||||
Assert.Equal(1, result[0].OrderItems.Count);
|
||||
Assert.NotNull(result[0].OrderItems.FirstOrDefault().ItemOrdered);
|
||||
Assert.Equal(2, result[1].OrderItems.Count);
|
||||
Assert.NotNull(result[1].OrderItems.ToList()[0].ItemOrdered);
|
||||
Assert.NotNull(result[1].OrderItems.ToList()[1].ItemOrdered);
|
||||
}
|
||||
public List<Order> GetTestCollection()
|
||||
{
|
||||
var ordersList = new List<Order>();
|
||||
|
||||
public List<Order> GetTestCollection()
|
||||
{
|
||||
var ordersList = new List<Order>();
|
||||
|
||||
ordersList.Add(new Order(_buyerId, _shipToAddress,
|
||||
new List<OrderItem>
|
||||
{
|
||||
ordersList.Add(new Order(_buyerId, _shipToAddress,
|
||||
new List<OrderItem>
|
||||
{
|
||||
new OrderItem(new CatalogItemOrdered(1, "Product1", "testurl"), 10.50m, 1)
|
||||
}));
|
||||
ordersList.Add(new Order(_buyerId, _shipToAddress,
|
||||
new List<OrderItem>
|
||||
{
|
||||
}));
|
||||
ordersList.Add(new Order(_buyerId, _shipToAddress,
|
||||
new List<OrderItem>
|
||||
{
|
||||
new OrderItem(new CatalogItemOrdered(2, "Product2", "testurl"), 15.50m, 2),
|
||||
new OrderItem(new CatalogItemOrdered(2, "Product3", "testurl"), 20.50m, 1)
|
||||
}));
|
||||
}));
|
||||
|
||||
return ordersList;
|
||||
}
|
||||
return ordersList;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,28 +1,27 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.Builders
|
||||
{
|
||||
public class AddressBuilder
|
||||
{
|
||||
private Address _address;
|
||||
public string TestStreet => "123 Main St.";
|
||||
public string TestCity => "Kent";
|
||||
public string TestState => "OH";
|
||||
public string TestCountry => "USA";
|
||||
public string TestZipCode => "44240";
|
||||
namespace Microsoft.eShopWeb.UnitTests.Builders;
|
||||
|
||||
public AddressBuilder()
|
||||
{
|
||||
_address = WithDefaultValues();
|
||||
}
|
||||
public Address Build()
|
||||
{
|
||||
return _address;
|
||||
}
|
||||
public Address WithDefaultValues()
|
||||
{
|
||||
_address = new Address(TestStreet, TestCity, TestState, TestCountry, TestZipCode);
|
||||
return _address;
|
||||
}
|
||||
public class AddressBuilder
|
||||
{
|
||||
private Address _address;
|
||||
public string TestStreet => "123 Main St.";
|
||||
public string TestCity => "Kent";
|
||||
public string TestState => "OH";
|
||||
public string TestCountry => "USA";
|
||||
public string TestZipCode => "44240";
|
||||
|
||||
public AddressBuilder()
|
||||
{
|
||||
_address = WithDefaultValues();
|
||||
}
|
||||
public Address Build()
|
||||
{
|
||||
return _address;
|
||||
}
|
||||
public Address WithDefaultValues()
|
||||
{
|
||||
_address = new Address(TestStreet, TestCity, TestState, TestCountry, TestZipCode);
|
||||
return _address;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,40 +1,39 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
||||
using Moq;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.Builders
|
||||
namespace Microsoft.eShopWeb.UnitTests.Builders;
|
||||
|
||||
public class BasketBuilder
|
||||
{
|
||||
public class BasketBuilder
|
||||
private Basket _basket;
|
||||
public string BasketBuyerId => "testbuyerId@test.com";
|
||||
|
||||
public int BasketId => 1;
|
||||
|
||||
public BasketBuilder()
|
||||
{
|
||||
private Basket _basket;
|
||||
public string BasketBuyerId => "testbuyerId@test.com";
|
||||
_basket = WithNoItems();
|
||||
}
|
||||
|
||||
public int BasketId => 1;
|
||||
public Basket Build()
|
||||
{
|
||||
return _basket;
|
||||
}
|
||||
|
||||
public BasketBuilder()
|
||||
{
|
||||
_basket = WithNoItems();
|
||||
}
|
||||
public Basket WithNoItems()
|
||||
{
|
||||
var basketMock = new Mock<Basket>(BasketBuyerId);
|
||||
basketMock.SetupGet(s => s.Id).Returns(BasketId);
|
||||
|
||||
public Basket Build()
|
||||
{
|
||||
return _basket;
|
||||
}
|
||||
_basket = basketMock.Object;
|
||||
return _basket;
|
||||
}
|
||||
|
||||
public Basket WithNoItems()
|
||||
{
|
||||
var basketMock = new Mock<Basket>(BasketBuyerId);
|
||||
basketMock.SetupGet(s => s.Id).Returns(BasketId);
|
||||
|
||||
_basket = basketMock.Object;
|
||||
return _basket;
|
||||
}
|
||||
|
||||
public Basket WithOneBasketItem()
|
||||
{
|
||||
var basketMock = new Mock<Basket>(BasketBuyerId);
|
||||
_basket = basketMock.Object;
|
||||
_basket.AddItem(2, 3.40m, 4);
|
||||
return _basket;
|
||||
}
|
||||
public Basket WithOneBasketItem()
|
||||
{
|
||||
var basketMock = new Mock<Basket>(BasketBuyerId);
|
||||
_basket = basketMock.Object;
|
||||
_basket.AddItem(2, 3.40m, 4);
|
||||
return _basket;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,48 +1,47 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.Builders
|
||||
namespace Microsoft.eShopWeb.UnitTests.Builders;
|
||||
|
||||
public class OrderBuilder
|
||||
{
|
||||
public class OrderBuilder
|
||||
private Order _order;
|
||||
public string TestBuyerId => "12345";
|
||||
public int TestCatalogItemId => 234;
|
||||
public string TestProductName => "Test Product Name";
|
||||
public string TestPictureUri => "http://test.com/image.jpg";
|
||||
public decimal TestUnitPrice = 1.23m;
|
||||
public int TestUnits = 3;
|
||||
public CatalogItemOrdered TestCatalogItemOrdered { get; }
|
||||
|
||||
public OrderBuilder()
|
||||
{
|
||||
private Order _order;
|
||||
public string TestBuyerId => "12345";
|
||||
public int TestCatalogItemId => 234;
|
||||
public string TestProductName => "Test Product Name";
|
||||
public string TestPictureUri => "http://test.com/image.jpg";
|
||||
public decimal TestUnitPrice = 1.23m;
|
||||
public int TestUnits = 3;
|
||||
public CatalogItemOrdered TestCatalogItemOrdered { get; }
|
||||
TestCatalogItemOrdered = new CatalogItemOrdered(TestCatalogItemId, TestProductName, TestPictureUri);
|
||||
_order = WithDefaultValues();
|
||||
}
|
||||
|
||||
public OrderBuilder()
|
||||
{
|
||||
TestCatalogItemOrdered = new CatalogItemOrdered(TestCatalogItemId, TestProductName, TestPictureUri);
|
||||
_order = WithDefaultValues();
|
||||
}
|
||||
public Order Build()
|
||||
{
|
||||
return _order;
|
||||
}
|
||||
|
||||
public Order Build()
|
||||
{
|
||||
return _order;
|
||||
}
|
||||
public Order WithDefaultValues()
|
||||
{
|
||||
var orderItem = new OrderItem(TestCatalogItemOrdered, TestUnitPrice, TestUnits);
|
||||
var itemList = new List<OrderItem>() { orderItem };
|
||||
_order = new Order(TestBuyerId, new AddressBuilder().WithDefaultValues(), itemList);
|
||||
return _order;
|
||||
}
|
||||
|
||||
public Order WithDefaultValues()
|
||||
{
|
||||
var orderItem = new OrderItem(TestCatalogItemOrdered, TestUnitPrice, TestUnits);
|
||||
var itemList = new List<OrderItem>() { orderItem };
|
||||
_order = new Order(TestBuyerId, new AddressBuilder().WithDefaultValues(), itemList);
|
||||
return _order;
|
||||
}
|
||||
public Order WithNoItems()
|
||||
{
|
||||
_order = new Order(TestBuyerId, new AddressBuilder().WithDefaultValues(), new List<OrderItem>());
|
||||
return _order;
|
||||
}
|
||||
|
||||
public Order WithNoItems()
|
||||
{
|
||||
_order = new Order(TestBuyerId, new AddressBuilder().WithDefaultValues(), new List<OrderItem>());
|
||||
return _order;
|
||||
}
|
||||
|
||||
public Order WithItems(List<OrderItem> items)
|
||||
{
|
||||
_order = new Order(TestBuyerId, new AddressBuilder().WithDefaultValues(), items);
|
||||
return _order;
|
||||
}
|
||||
public Order WithItems(List<OrderItem> items)
|
||||
{
|
||||
_order = new Order(TestBuyerId, new AddressBuilder().WithDefaultValues(), items);
|
||||
return _order;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,39 +1,38 @@
|
||||
using Ardalis.Specification;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Ardalis.Specification;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
||||
using Microsoft.eShopWeb.Web.Features.MyOrders;
|
||||
using Moq;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.MediatorHandlers.OrdersTests
|
||||
namespace Microsoft.eShopWeb.UnitTests.MediatorHandlers.OrdersTests;
|
||||
|
||||
public class GetMyOrders
|
||||
{
|
||||
public class GetMyOrders
|
||||
private readonly Mock<IReadRepository<Order>> _mockOrderRepository;
|
||||
|
||||
public GetMyOrders()
|
||||
{
|
||||
private readonly Mock<IReadRepository<Order>> _mockOrderRepository;
|
||||
var item = new OrderItem(new CatalogItemOrdered(1, "ProductName", "URI"), 10.00m, 10);
|
||||
var address = new Address(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>());
|
||||
Order order = new Order("buyerId", address, new List<OrderItem> { item });
|
||||
|
||||
public GetMyOrders()
|
||||
{
|
||||
var item = new OrderItem(new CatalogItemOrdered(1, "ProductName", "URI"), 10.00m, 10);
|
||||
var address = new Address(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>());
|
||||
Order order = new Order("buyerId", address, new List<OrderItem> { item });
|
||||
_mockOrderRepository = new Mock<IReadRepository<Order>>();
|
||||
_mockOrderRepository.Setup(x => x.ListAsync(It.IsAny<ISpecification<Order>>(), default)).ReturnsAsync(new List<Order> { order });
|
||||
}
|
||||
|
||||
_mockOrderRepository = new Mock<IReadRepository<Order>>();
|
||||
_mockOrderRepository.Setup(x => x.ListAsync(It.IsAny<ISpecification<Order>>(),default)).ReturnsAsync(new List<Order> { order });
|
||||
}
|
||||
[Fact]
|
||||
public async Task NotReturnNullIfOrdersArePresIent()
|
||||
{
|
||||
var request = new eShopWeb.Web.Features.MyOrders.GetMyOrders("SomeUserName");
|
||||
|
||||
[Fact]
|
||||
public async Task NotReturnNullIfOrdersArePresIent()
|
||||
{
|
||||
var request = new eShopWeb.Web.Features.MyOrders.GetMyOrders("SomeUserName");
|
||||
var handler = new GetMyOrdersHandler(_mockOrderRepository.Object);
|
||||
|
||||
var handler = new GetMyOrdersHandler(_mockOrderRepository.Object);
|
||||
var result = await handler.Handle(request, CancellationToken.None);
|
||||
|
||||
var result = await handler.Handle(request, CancellationToken.None);
|
||||
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,41 +1,40 @@
|
||||
using Ardalis.Specification;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Ardalis.Specification;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Specifications;
|
||||
using Microsoft.eShopWeb.Web.Features.OrderDetails;
|
||||
using Moq;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.MediatorHandlers.OrdersTests
|
||||
namespace Microsoft.eShopWeb.UnitTests.MediatorHandlers.OrdersTests;
|
||||
|
||||
public class GetOrderDetails
|
||||
{
|
||||
public class GetOrderDetails
|
||||
private readonly Mock<IReadRepository<Order>> _mockOrderRepository;
|
||||
|
||||
public GetOrderDetails()
|
||||
{
|
||||
private readonly Mock<IReadRepository<Order>> _mockOrderRepository;
|
||||
var item = new OrderItem(new CatalogItemOrdered(1, "ProductName", "URI"), 10.00m, 10);
|
||||
var address = new Address(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>());
|
||||
Order order = new Order("buyerId", address, new List<OrderItem> { item });
|
||||
|
||||
public GetOrderDetails()
|
||||
{
|
||||
var item = new OrderItem(new CatalogItemOrdered(1, "ProductName", "URI"), 10.00m, 10);
|
||||
var address = new Address(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>());
|
||||
Order order = new Order("buyerId", address, new List<OrderItem> { item });
|
||||
_mockOrderRepository = new Mock<IReadRepository<Order>>();
|
||||
_mockOrderRepository.Setup(x => x.GetBySpecAsync(It.IsAny<OrderWithItemsByIdSpec>(), default))
|
||||
.ReturnsAsync(order);
|
||||
}
|
||||
|
||||
_mockOrderRepository = new Mock<IReadRepository<Order>>();
|
||||
_mockOrderRepository.Setup(x => x.GetBySpecAsync(It.IsAny<OrderWithItemsByIdSpec>(),default))
|
||||
.ReturnsAsync(order);
|
||||
}
|
||||
[Fact]
|
||||
public async Task NotBeNullIfOrderExists()
|
||||
{
|
||||
var request = new eShopWeb.Web.Features.OrderDetails.GetOrderDetails("SomeUserName", 0);
|
||||
|
||||
[Fact]
|
||||
public async Task NotBeNullIfOrderExists()
|
||||
{
|
||||
var request = new eShopWeb.Web.Features.OrderDetails.GetOrderDetails("SomeUserName", 0);
|
||||
var handler = new GetOrderDetailsHandler(_mockOrderRepository.Object);
|
||||
|
||||
var handler = new GetOrderDetailsHandler(_mockOrderRepository.Object);
|
||||
var result = await handler.Handle(request, CancellationToken.None);
|
||||
|
||||
var result = await handler.Handle(request, CancellationToken.None);
|
||||
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>Microsoft.eShopWeb.UnitTests</RootNamespace>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
using Microsoft.eShopWeb.Web.Extensions;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.Web.Extensions.CacheHelpersTests
|
||||
{
|
||||
public class GenerateBrandsCacheKey
|
||||
{
|
||||
[Fact]
|
||||
public void ReturnsBrandsCacheKey()
|
||||
{
|
||||
var result = CacheHelpers.GenerateBrandsCacheKey();
|
||||
namespace Microsoft.eShopWeb.UnitTests.Web.Extensions.CacheHelpersTests;
|
||||
|
||||
Assert.Equal("brands", result);
|
||||
}
|
||||
public class GenerateBrandsCacheKey
|
||||
{
|
||||
[Fact]
|
||||
public void ReturnsBrandsCacheKey()
|
||||
{
|
||||
var result = CacheHelpers.GenerateBrandsCacheKey();
|
||||
|
||||
Assert.Equal("brands", result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,20 +2,19 @@
|
||||
using Microsoft.eShopWeb.Web.Extensions;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.Web.Extensions.CacheHelpersTests
|
||||
namespace Microsoft.eShopWeb.UnitTests.Web.Extensions.CacheHelpersTests;
|
||||
|
||||
public class GenerateCatalogItemCacheKey
|
||||
{
|
||||
public class GenerateCatalogItemCacheKey
|
||||
[Fact]
|
||||
public void ReturnsCatalogItemCacheKey()
|
||||
{
|
||||
[Fact]
|
||||
public void ReturnsCatalogItemCacheKey()
|
||||
{
|
||||
var pageIndex = 0;
|
||||
int? brandId = null;
|
||||
int? typeId = null;
|
||||
var pageIndex = 0;
|
||||
int? brandId = null;
|
||||
int? typeId = null;
|
||||
|
||||
var result = CacheHelpers.GenerateCatalogItemCacheKey(pageIndex, Constants.ITEMS_PER_PAGE, brandId, typeId);
|
||||
var result = CacheHelpers.GenerateCatalogItemCacheKey(pageIndex, Constants.ITEMS_PER_PAGE, brandId, typeId);
|
||||
|
||||
Assert.Equal("items-0-10--", result);
|
||||
}
|
||||
Assert.Equal("items-0-10--", result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
using Microsoft.eShopWeb.Web.Extensions;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.Web.Extensions.CacheHelpersTests
|
||||
{
|
||||
public class GenerateTypesCacheKey
|
||||
{
|
||||
[Fact]
|
||||
public void ReturnsTypesCacheKey()
|
||||
{
|
||||
var result = CacheHelpers.GenerateTypesCacheKey();
|
||||
namespace Microsoft.eShopWeb.UnitTests.Web.Extensions.CacheHelpersTests;
|
||||
|
||||
Assert.Equal("types", result);
|
||||
}
|
||||
public class GenerateTypesCacheKey
|
||||
{
|
||||
[Fact]
|
||||
public void ReturnsTypesCacheKey()
|
||||
{
|
||||
var result = CacheHelpers.GenerateTypesCacheKey();
|
||||
|
||||
Assert.Equal("types", result);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user