Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Robbert/opdracht1 #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
################################################################################
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
################################################################################

/SuperMarketApp/.vs/SuperMarketApp/v16/TestStore/0/000.testlog
/SuperMarketApp/.vs/SuperMarketApp/v16/TestStore/0/testlog.manifest
/SuperMarketApp/.vs/SuperMarketApp/DesignTimeBuild/.dtbcache.v2
/SuperMarketApp/.vs/SuperMarketApp/v16/TestStore/0/001.testlog
*.testlog
133 changes: 133 additions & 0 deletions SuperMarketApp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.

# User-specific files
*.suo
*.user
*.sln.docstates

# Build results

[Dd]ebug/
[Rr]elease/
x64/
[Bb]in/
[Oo]bj/

# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*

*_i.c
*_p.c
*_i.h
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.log
*.svclog
*.scc

# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opensdf
*.sdf
*.cachefile

# Visual Studio profiler
*.psess
*.vsp
*.vspx

# Guidance Automation Toolkit
*.gpState

# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user

# Click-Once directory
publish/

# Publish Web Output
*.Publish.xml
*.pubxml
*.azurePubxml

# NuGet Packages Directory
## TODO: If you have NuGet Package Restore enabled, uncomment the next line
packages/
## TODO: If the tool you use requires repositories.config, also uncomment the next line
!packages/repositories.config

# Windows Azure Build Output
csx/
*.build.csdef

# Windows Store app package directory
AppPackages/

# Others
sql/
*.Cache
ClientBin/
[Ss]tyle[Cc]op.*
![Ss]tyle[Cc]op.targets
~$*
*~
*.dbmdl
*.[Pp]ublish.xml

*.publishsettings

# RIA/Silverlight projects
Generated_Code/

# Backup & report files from converting an old project file to a newer
# Visual Studio version. Backup files are not needed, because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm

# SQL Server files
App_Data/*.mdf
App_Data/*.ldf

# =========================
# Windows detritus
# =========================

# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Mac desktop service store files
.DS_Store

_NCrunch*
28 changes: 28 additions & 0 deletions SuperMarketApp/IntegrationTests/Init.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using Service.Interfaces;
using Service.Services;

namespace Service.IntegrationTests
{
public class Init
{
protected ICalculateProductPrice CalculateProductPrice;
protected ICalculateCartPrice CalculateCartPrice;
protected IRegisterService RegisterService;

[SetUp]
public void SetUp()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddScoped<ICalculateProductPrice, CalculateProductPrice>();
serviceCollection.AddScoped<ICalculateCartPrice, CalculateCartPrice>();
serviceCollection.AddScoped<IRegisterService, RegisterService>();
var serviceProvider = serviceCollection.BuildServiceProvider();

CalculateProductPrice = serviceProvider.GetService<ICalculateProductPrice>();
CalculateCartPrice = serviceProvider.GetService<ICalculateCartPrice>();
RegisterService = serviceProvider.GetService<IRegisterService>();
}
}
}
20 changes: 20 additions & 0 deletions SuperMarketApp/IntegrationTests/Service.IntegrationTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.9" />
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Service\Service.csproj" />
</ItemGroup>

</Project>
67 changes: 67 additions & 0 deletions SuperMarketApp/IntegrationTests/ServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using NUnit.Framework;
using Service.Enum;
using Service.Models;

namespace Service.IntegrationTests
{
public class ServiceTests : Init
{
private Cart _cart;

[Test]
public void CalculateCart_NoDiscount_ShouldBeCorrectPrice()
{
// Assemble
_cart = new Cart();
_cart.AddToCart(new Product("Kaas", 156734, 4.99));
_cart.AddToCart(new Product("Ham", 579843, 1.49));
_cart.AddToCart(new Product("Melk", 378941, 0.99));
_cart.AddToCart(new Product("Pizza", 739214, 4.59));
_cart.AddToCart(new Product("WC papier", 798234, 1.12));

// Act
var price = CalculateCartPrice.Calculate(_cart);

// Assert
Assert.AreEqual(13.18, price);
}

[Test]
public void CalculateCart_WithBonusAndExpiryDiscount_ShouldBeCorrectPrice()
{
_cart = new Cart();
_cart.AddToCart(new Product("Kaas", 156734, 4.99, Discount.Bonus));
_cart.AddToCart(new Product("Ham", 579843, 1.49));
_cart.AddToCart(new Product("Melk", 378941, 0.99, Discount.Expiry));
_cart.AddToCart(new Product("Pizza", 739214, 4.59));
_cart.AddToCart(new Product("WC papier", 798234, 1.12, Discount.Bonus));

// Act
var price = CalculateCartPrice.Calculate(_cart);

// Assert
Assert.AreEqual(11.61, price);
}

[Test]
public void CartWithProduct_CheckOut_ShouldPrintCorrectReceipt()
{
// Assemble
_cart = new Cart();
_cart.AddToCart(new Product("Kaas", 156734, 4.99));
_cart.AddToCart(new Product("Ham", 579843, 1.49));
_cart.AddToCart(new Product("Melk", 378941, 0.99));
_cart.AddToCart(new Product("Pizza", 739214, 4.59));
_cart.AddToCart(new Product("WC papier", 798234, 1.12));

// Assign
var expectedPrice = 13.18;

// Act
var receipt = RegisterService.CheckOut(_cart);

// Assert
Assert.That(receipt.Contains(expectedPrice.ToString()));
}
}
}
84 changes: 84 additions & 0 deletions SuperMarketApp/Service.Tests/CalculateCartPriceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using Moq;
using NUnit.Framework;
using Service.Interfaces;
using Service.Models;
using Service.Services;

namespace Service.Tests
{
public class CalculateCartPriceTests
{
private Mock<ICalculateProductPrice> _calculateProductPriceMock;

private Cart _cart;

[SetUp]
public void Setup()
{
_calculateProductPriceMock = new Mock<ICalculateProductPrice>();

_cart = new Cart();
_cart.AddToCart(new Product("Kaas", 156734, 4.99));
_cart.AddToCart(new Product("Ham", 579843, 1.49));
_cart.AddToCart(new Product("Melk", 378941, 0.99));
_cart.AddToCart(new Product("Pizza", 739214, 4.59));
_cart.AddToCart(new Product("WC papier", 798234, 1.12));
}

[Test]
public void CalculatePrice_ShouldReturnCorrectDouble_WhenGivenCartNoDiscount()
{
// Assemble
_calculateProductPriceMock.Setup(mock => mock.Calculate(_cart.Products[0])).Returns(_cart.Products[0].Price);
_calculateProductPriceMock.Setup(mock => mock.Calculate(_cart.Products[1])).Returns(_cart.Products[1].Price);
_calculateProductPriceMock.Setup(mock => mock.Calculate(_cart.Products[2])).Returns(_cart.Products[2].Price);
_calculateProductPriceMock.Setup(mock => mock.Calculate(_cart.Products[3])).Returns(_cart.Products[3].Price);
_calculateProductPriceMock.Setup(mock => mock.Calculate(_cart.Products[4])).Returns(_cart.Products[4].Price);

var calculatePriceService = new CalculateCartPrice(_calculateProductPriceMock.Object);

// Assign
var expectedPrice = 13.18;

// Act
var price = calculatePriceService.Calculate(_cart);

// Assert
Assert.AreEqual(expectedPrice, price);
}

[Test]
public void CalculatePrice_ShouldRoundUp_WhenGivenThreeDecimals()
{
// Assemble
_calculateProductPriceMock.Setup(mock => mock.Calculate(_cart.Products[0])).Returns(5.495);
var calculatePriceService = new CalculateCartPrice(_calculateProductPriceMock.Object);

// Assign
var expectedPrice = 5.50;

// Act
var price = calculatePriceService.Calculate(_cart);

// Assert
Assert.AreEqual(expectedPrice, price);
}

[Test]
public void CalculatePrice_ShouldRoundDown_WhenGivenThreeDecimals()
{
// Assemble
_calculateProductPriceMock.Setup(mock => mock.Calculate(_cart.Products[0])).Returns(5.494);
var calculatePriceService = new CalculateCartPrice(_calculateProductPriceMock.Object);

// Assign
var expectedPrice = 5.49;

// Act
var price = calculatePriceService.Calculate(_cart);

// Assert
Assert.AreEqual(expectedPrice, price);
}
}
}
Loading