Adding unit test
This commit is contained in:
parent
f537d097ec
commit
fc1ced2d61
149
drinkMe.Server.Tests/ControllerTests/DrinksControllerTest.cs
Normal file
149
drinkMe.Server.Tests/ControllerTests/DrinksControllerTest.cs
Normal file
@ -0,0 +1,149 @@
|
||||
using collAnon.Pub.Shared;
|
||||
using drinkMe.Server.Controllers;
|
||||
using drinkMe.Server.Services.Interfaces;
|
||||
using drinkMe.Shared;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace drinkMe.Server.Tests
|
||||
{
|
||||
public class DrinksControllerTest
|
||||
{
|
||||
readonly IDataService DataService;
|
||||
readonly IPaymentService PaymentService;
|
||||
|
||||
public DrinksControllerTest(IDataService dataService, IPaymentService paymentService)
|
||||
{
|
||||
DataService = dataService;
|
||||
PaymentService = paymentService;
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("D1AMOND_HANDS")]
|
||||
public async Task IsValidDiscountCode_Test_DiscountFound(string code)
|
||||
{
|
||||
var controller = new DrinksController(DataService, PaymentService);
|
||||
Assert.NotNull(controller);
|
||||
var result = await controller.IsValidDiscountCode(code);
|
||||
Assert.NotNull(result);
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
var okResult = result as OkObjectResult;
|
||||
Assert.NotNull(okResult.Value);
|
||||
Assert.Equal(okResult.StatusCode, StatusCodes.Status200OK);
|
||||
Assert.IsType<DiscountCodeViewModel>(okResult.Value);
|
||||
var discountCodeData = okResult.Value as DiscountCodeViewModel;
|
||||
Assert.Equal(discountCodeData.Code, code);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("NOT_MY_D4Y")]
|
||||
public async Task IsValidDiscountCode_Test_DiscountNotFound(string code)
|
||||
{
|
||||
var controller = new DrinksController(DataService, PaymentService);
|
||||
Assert.NotNull(controller);
|
||||
var result = await controller.IsValidDiscountCode(code);
|
||||
Assert.NotNull(result);
|
||||
Assert.IsType<ObjectResult>(result);
|
||||
var objectResult = result as ObjectResult;
|
||||
Assert.NotNull(objectResult.Value);
|
||||
Assert.Equal(objectResult.StatusCode, StatusCodes.Status404NotFound);
|
||||
Assert.IsType<WebResult>(objectResult.Value);
|
||||
var webResult = objectResult.Value as WebResult;
|
||||
Assert.False(webResult.IsValid);
|
||||
Assert.Equal(webResult.StatusCode, StatusCodes.Status404NotFound);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetDrinks_Test_NotEmpty()
|
||||
{
|
||||
var controller = new DrinksController(DataService, PaymentService);
|
||||
Assert.NotNull(controller);
|
||||
var result = await controller.GetDrinks();
|
||||
Assert.NotNull(result);
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
var okResult = result as OkObjectResult;
|
||||
Assert.NotNull(okResult.Value);
|
||||
Assert.Equal(okResult.StatusCode, StatusCodes.Status200OK);
|
||||
Assert.IsType<DrinkViewModel[]>(okResult.Value);
|
||||
var drinks = okResult.Value as DrinkViewModel[];
|
||||
Assert.NotEmpty(drinks);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Pay_Test_WithSuccess()
|
||||
{
|
||||
var purchaseCart = new PurchaseCart
|
||||
{
|
||||
IsPayedWithCash = true,
|
||||
PurchasingItems = new List<CartItem>
|
||||
{
|
||||
new CartItem
|
||||
{
|
||||
Id = 1,
|
||||
Quantity = 1
|
||||
}
|
||||
}
|
||||
};
|
||||
var controller = new DrinksController(DataService, PaymentService);
|
||||
Assert.NotNull(controller);
|
||||
var result = await controller.Pay(purchaseCart);
|
||||
Assert.NotNull(result);
|
||||
Assert.IsType<OkResult>(result);
|
||||
var okResult = result as OkResult;
|
||||
Assert.Equal(okResult.StatusCode, StatusCodes.Status200OK);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Pay_Test_WithSuccess2()
|
||||
{
|
||||
var purchaseCart = new PurchaseCart
|
||||
{
|
||||
IsPayedWithCash = false,
|
||||
CreditCardNumber = "4000123412341234",
|
||||
CreditCardExpirationYear = "22",
|
||||
CreditCardCVVCode = "333",
|
||||
CreditCardExpirationMonth = "01",
|
||||
PurchasingItems = new List<CartItem>
|
||||
{
|
||||
new CartItem
|
||||
{
|
||||
Id = 1,
|
||||
Quantity = 1
|
||||
}
|
||||
}
|
||||
};
|
||||
var controller = new DrinksController(DataService, PaymentService);
|
||||
Assert.NotNull(controller);
|
||||
var result = await controller.Pay(purchaseCart);
|
||||
Assert.NotNull(result);
|
||||
Assert.IsType<OkResult>(result);
|
||||
var okResult = result as OkResult;
|
||||
Assert.Equal(okResult.StatusCode, StatusCodes.Status200OK);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Pay_Test_WithoutSuccess()
|
||||
{
|
||||
var purchaseCart = new PurchaseCart
|
||||
{
|
||||
IsPayedWithCash = false
|
||||
};
|
||||
var controller = new DrinksController(DataService, PaymentService);
|
||||
Assert.NotNull(controller);
|
||||
var result = await controller.Pay(purchaseCart);
|
||||
Assert.NotNull(result);
|
||||
Assert.IsType<BadRequestObjectResult>(result);
|
||||
var badResult = result as BadRequestObjectResult;
|
||||
Assert.NotNull(badResult.Value);
|
||||
Assert.IsType<WebResult>(badResult.Value);
|
||||
Assert.Equal(badResult.StatusCode, StatusCodes.Status400BadRequest);
|
||||
var webResult = badResult.Value as WebResult;
|
||||
Assert.False(webResult.IsValid);
|
||||
Assert.Equal(webResult.StatusCode, StatusCodes.Status400BadRequest);
|
||||
}
|
||||
}
|
||||
}
|
20
drinkMe.Server.Tests/Startup.cs
Normal file
20
drinkMe.Server.Tests/Startup.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using drinkMe.Server.Services;
|
||||
using drinkMe.Server.Services.Interfaces;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace drinkMe.Server.Tests
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddScoped<IDataService, DataService>()
|
||||
.AddScoped<IPaymentService, PaymentService>();
|
||||
}
|
||||
}
|
||||
}
|
28
drinkMe.Server.Tests/drinkMe.Server.Tests.csproj
Normal file
28
drinkMe.Server.Tests/drinkMe.Server.Tests.csproj
Normal file
@ -0,0 +1,28 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="Xunit.DependencyInjection" Version="7.2.0" />
|
||||
<PackageReference Include="xunit.extensibility.core" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="coverlet.collector" Version="3.0.3">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\drinkMe\Server\drinkMe.Server.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "drinkMe.Client.Models", "dr
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "drinkMe.Server.Models", "drinkMe\drinkMe.Server.Models\drinkMe.Server.Models.csproj", "{EC73DC9D-FD40-4505-989D-83B49018401D}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "drinkMe.Server.Tests", "drinkMe.Server.Tests\drinkMe.Server.Tests.csproj", "{A8EAD5E1-209A-4E0E-A31E-BD3C3235C8E5}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@ -39,6 +41,10 @@ Global
|
||||
{EC73DC9D-FD40-4505-989D-83B49018401D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{EC73DC9D-FD40-4505-989D-83B49018401D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{EC73DC9D-FD40-4505-989D-83B49018401D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{A8EAD5E1-209A-4E0E-A31E-BD3C3235C8E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{A8EAD5E1-209A-4E0E-A31E-BD3C3235C8E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{A8EAD5E1-209A-4E0E-A31E-BD3C3235C8E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{A8EAD5E1-209A-4E0E-A31E-BD3C3235C8E5}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
@ -11,6 +11,13 @@ namespace drinkMe.Server.Services
|
||||
{
|
||||
public async Task<bool> Pay(PurchaseCart purchaseCart)
|
||||
{
|
||||
if (!purchaseCart.IsPayedWithCash &&
|
||||
(purchaseCart.CreditCardNumber is { Length: 0 } || purchaseCart.CreditCardExpirationYear is { Length: 0 } ||
|
||||
purchaseCart.CreditCardExpirationMonth is { Length: 0 } || purchaseCart.CreditCardCVVCode is { Length: 0 }))
|
||||
return false;
|
||||
|
||||
if (purchaseCart.PurchasingItems.Count == 0)
|
||||
return false;
|
||||
//completing the purchase
|
||||
return true;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user