-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Test examples and code complexity
- Loading branch information
Showing
9 changed files
with
337 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using Moq; | ||
using Samples.BlanketsHoldingNoHeat; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace SampleTests | ||
{ | ||
public class OrderLogicTests | ||
{ | ||
[Fact] | ||
public async Task WhenValidationErrorsExist_ReturnsErrorsAndDoesNotSave() | ||
{ | ||
//arrange | ||
Mock<IOrderValidator> mockValidator = new Mock<IOrderValidator>(); | ||
Mock<IOrderProvider> mockProvider = new Mock<IOrderProvider>(); | ||
Mock<ILogger> mockLogger = new Mock<ILogger>(); | ||
|
||
mockValidator.Setup(v => v.ValidateOrder(It.IsAny<Order>())) | ||
.ReturnsAsync(new OrderValidationError[] { | ||
new OrderValidationError() | ||
{ | ||
Message = "Live long and prosper" | ||
} | ||
}); | ||
var logicUnderTest = new OrderLogic( | ||
mockValidator.Object, | ||
mockProvider.Object, | ||
mockLogger.Object); | ||
|
||
//act | ||
var result = await logicUnderTest.PlaceOrder(new Order()); | ||
|
||
//assert | ||
mockLogger.Verify(l => l.Log(It.IsAny<string>())); | ||
Assert.Single(result); | ||
var error = result.First(); | ||
Assert.Equal("Live long and prosper", error.Message); | ||
|
||
mockProvider.Verify(p => p.SaveOrder(It.IsAny<Order>()), Times.Never); | ||
} | ||
|
||
[Fact] | ||
public async Task WhenNoValidationErrors_SavesOrder() | ||
{ | ||
//arrange | ||
Mock<IOrderValidator> mockValidator = new Mock<IOrderValidator>(); | ||
Mock<IOrderProvider> mockProvider = new Mock<IOrderProvider>(); | ||
Mock<ILogger> mockLogger = new Mock<ILogger>(); | ||
|
||
mockValidator.Setup(v => v.ValidateOrder(It.IsAny<Order>())) | ||
.ReturnsAsync(Enumerable.Empty<OrderValidationError>()); | ||
|
||
var fakeOrder = new Order(); | ||
|
||
var logicUnderTest = new OrderLogic( | ||
mockValidator.Object, | ||
mockProvider.Object, | ||
mockLogger.Object); | ||
|
||
//act | ||
var result = await logicUnderTest.PlaceOrder(fakeOrder); | ||
|
||
//assert | ||
Assert.Empty(result); | ||
mockProvider.Verify(p => p.SaveOrder(fakeOrder), Times.Once); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" /> | ||
<PackageReference Include="xunit" Version="2.4.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Samples\Samples.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Samples.BlanketsHoldingNoHeat | ||
{ | ||
public interface IOrderLogic | ||
{ | ||
Task<IEnumerable<OrderValidationError>> PlaceOrder(Order order); | ||
} | ||
|
||
public interface IOrderValidator | ||
{ | ||
Task<IEnumerable<OrderValidationError>> ValidateOrder(Order order); | ||
} | ||
|
||
public class OrderValidationError | ||
{ | ||
public string Message { get; set; } | ||
} | ||
|
||
public interface ILogger | ||
{ | ||
void Log(string message); | ||
} | ||
|
||
public interface IOrderProvider | ||
{ | ||
Task SaveOrder(Order order); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Samples.BlanketsHoldingNoHeat | ||
{ | ||
public class Order | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Samples.BlanketsHoldingNoHeat | ||
{ | ||
public class OrderLogic : IOrderLogic | ||
{ | ||
readonly IOrderValidator _validator; | ||
readonly IOrderProvider _orderProvider; | ||
readonly ILogger _logger; | ||
|
||
public OrderLogic(IOrderValidator validator, IOrderProvider orderProvider, ILogger logger) | ||
{ | ||
_validator = validator; | ||
_orderProvider = orderProvider; | ||
_logger = logger; | ||
} | ||
|
||
public async Task<IEnumerable<OrderValidationError>> PlaceOrder(Order order) | ||
{ | ||
var errors = await _validator.ValidateOrder(order); | ||
|
||
if (errors.Any()) | ||
{ | ||
_logger.Log("Cannot process order"); | ||
return errors; | ||
} | ||
|
||
await _orderProvider.SaveOrder(order); | ||
|
||
return errors; | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
Samples/BlanketsHoldingNoHeat/OrderLogic/PlaceOrderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//using Moq; | ||
//using System; | ||
//using System.Collections.Generic; | ||
//using System.Linq; | ||
//using System.Text; | ||
//using System.Threading.Tasks; | ||
//using Xunit; | ||
|
||
//namespace Samples.BlanketsHoldingNoHeat.OrderLogicTests | ||
//{ | ||
|
||
// public class PlaceOrderTests | ||
// { | ||
// [Fact] | ||
// public async Task WhenValidationErrorsExist_ReturnsErrorsAndDoesNotSave() | ||
// { | ||
// //arrange | ||
// Mock<IOrderValidator> mockValidator = new Mock<IOrderValidator>(); | ||
// Mock<IOrderProvider> mockProvider = new Mock<IOrderProvider>(); | ||
// Mock<ILogger> mockLogger = new Mock<ILogger>(); | ||
|
||
// mockValidator.Setup(v => v.ValidateOrder(It.IsAny<Order>())) | ||
// .ReturnsAsync(new OrderValidationError[] { | ||
// new OrderValidationError() | ||
// { | ||
// Message = "Live long and prosper" | ||
// } | ||
// }); | ||
// var logicUnderTest = new OrderLogic( | ||
// mockValidator.Object, | ||
// mockProvider.Object, | ||
// mockLogger.Object); | ||
|
||
// //act | ||
// var result = await logicUnderTest.PlaceOrder(new Order()); | ||
|
||
// //assert | ||
// mockLogger.Verify(l => l.Log(It.IsAny<string>())); | ||
// Assert.Single(result); | ||
// var error = result.First(); | ||
// Assert.Equal("Live long and prosper", error.Message); | ||
|
||
// mockProvider.Verify(p => p.SaveOrder(It.IsAny<Order>()), Times.Never); | ||
// } | ||
// } | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Samples.OofOne | ||
{ | ||
class Examples | ||
{ | ||
readonly ICategoryProvider _categoryProvider; | ||
readonly IBookProvider _bookProvider; | ||
|
||
IEnumerable<Category> GetCategoriesFromBooks( | ||
IEnumerable<Book> books, | ||
IEnumerable<Category> categories) | ||
{ | ||
List<Category> usedCategories = new List<Category>(); | ||
|
||
foreach (var book in books) | ||
{ | ||
foreach (var catId in book.CategoryIds) | ||
{ | ||
var category = categories.FirstOrDefault(c => c.ID == catId); | ||
if (category != null && | ||
usedCategories.Any(uc => uc.ID == category.ID)) | ||
{ | ||
usedCategories.Add(category); | ||
} | ||
} | ||
} | ||
return usedCategories; | ||
} | ||
|
||
IEnumerable<Category> GetCategoriesFromBooks2( | ||
IEnumerable<Book> books, | ||
IEnumerable<Category> categories) | ||
{ | ||
var usedCategoryIds = books.SelectMany(b => b.CategoryIds); | ||
var categoryHash = new HashSet<int>(usedCategoryIds); | ||
|
||
foreach (var category in categories) | ||
{ | ||
if (categoryHash.Contains(category.ID)) | ||
{ | ||
yield return category; | ||
} | ||
} | ||
} | ||
|
||
public IEnumerable<Book> GetRelatedBooks(Book book) | ||
{ | ||
Dictionary<Book, int> bookCounts = new Dictionary<Book, int>(); | ||
foreach (var cat in _categoryProvider.GetCategories(book)) | ||
{ | ||
var categories = _bookProvider.GetBooksInCategory(cat.ID); | ||
UpdateBookCounts(bookCounts, categories); | ||
} | ||
|
||
return bookCounts.OrderBy(kvp => kvp.Value).Select(kvp => kvp.Key); | ||
} | ||
|
||
private static void UpdateBookCounts( | ||
Dictionary<Book, int> bookCounts, IEnumerable<Book> books) | ||
{ | ||
foreach (var relatedBook in books) | ||
{ | ||
if (bookCounts.ContainsKey(relatedBook)) | ||
{ | ||
bookCounts[relatedBook] = bookCounts[relatedBook] + 1; | ||
} | ||
else | ||
{ | ||
bookCounts[relatedBook] = 1; | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
public interface ICategoryProvider | ||
{ | ||
IEnumerable<Category> GetCategories(Book book); | ||
} | ||
|
||
public interface IBookProvider | ||
{ | ||
Book GetById(int id); | ||
IEnumerable<Book> GetBooksInCategory(int catId); | ||
} | ||
|
||
public class Book | ||
{ | ||
public int ID { get; set; } | ||
public string Title { get; set; } | ||
public IEnumerable<int> CategoryIds { get; set; } | ||
} | ||
|
||
public class Category | ||
{ | ||
public int ID { get; set; } | ||
public string Name { get; set; } | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters