Skip to content

Commit

Permalink
Made some more unit tests for the logic
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRealDeLorian committed Dec 10, 2023
1 parent 1c70010 commit f6b0c3c
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 58 deletions.
147 changes: 145 additions & 2 deletions UnitTests/UnitTest1.cs → UnitTests/UnitTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@

using Bunit;
using Bunit.TestDoubles;
using FluentAssertions;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using MythicalToyMachine.Data;
using MythicalToyMachine.Logic;
using MythicalToyMachine.Pages;
using MythicalToyMachine.Services;
using System;
using System.Collections.Generic;
Expand All @@ -16,7 +19,132 @@
namespace UnitTests;


public class UnitTest1 : TestContext
public class LogicUnitTests : TestContext
{

[Fact]
public void AccessoryToString_ShopComponent_Test()
{
//Arrange

List<KitAccessory> accessories = new()
{
new KitAccessory()
{
Id = 1,
Acc = new Accessory()
{
Accessoryname = "TestAccessory1"
}
},
new KitAccessory()
{
Id = 2,
Acc = new Accessory()
{
Accessoryname = "TestAccessory2"
}
}
};

var kit = new Kit
{
Id = 1,
Kitname = "BogusKit1",
KitAccessories = accessories
};

//Act
ShopLogic shopLogic = new();
string testString = shopLogic.AccesoriesToString(kit);

//Assert
Assert.True(testString == "TestAccessory1, TestAccessory2");
}

[Fact]
public void AccessoryToString_ShopComponent_FailsWhenIncorrect_Test()
{
//Arrange

List<KitAccessory> accessories = new()
{
new KitAccessory()
{
Id = 1,
Acc = new Accessory()
{
Accessoryname = "TestAccessory1"
}
},
new KitAccessory()
{
Id = 2,
Acc = new Accessory()
{
Accessoryname = "TestAccessory2"
}
}
};

var kit = new Kit
{
Id = 1,
Kitname = "BogusKit1",
KitAccessories = accessories
};

//Act
ShopLogic shopLogic = new();
string testString = shopLogic.AccesoriesToString(kit);

//Assert
Assert.False(testString == "TestAccessory1, TestAccessory354375943759");
}


[Fact]
public void AccessoryToString_ShopComponent_ThrowsExceptionOnDataError_Test()
{
//Arrange

List<KitAccessory> accessories = new()
{
new KitAccessory()
{
Id = 1,
Acc = new Accessory()
{

}
},
new KitAccessory()
{
Id = 2,
Acc = new Accessory()
{
Accessoryname = "TestAccessory2"
}
}
};

var kit = new Kit
{
Id = 1,
Kitname = "BogusKit1",
KitAccessories = accessories
};

//Act
ShopLogic shopLogic = new();
string testString = shopLogic.AccesoriesToString(kit);

//Assert
testString.Should().Be("TestAccessory2");
}
}

public class UnitTests : TestContext
{
[Fact]
public void Test1()
Expand Down Expand Up @@ -71,7 +199,7 @@ public void Test1()



public class AuthenticationUnitTests
public class AuthenticationUnitTests : TestContext
{
[Fact]
public void UserNotAuthenticated_BuildAToyComponent_Test()
Expand Down Expand Up @@ -113,6 +241,21 @@ public void UserAuthenticated_BuildAToyComponent_Test()
}


public class UnitTestShoppingCartServiceProvider : IShoppingCartService
{
public List<Kit> AllKitsThatAreInTheCart => throw new NotImplementedException();

public void AddKitToCart(Kit kit)
{
throw new NotImplementedException();
}

public void RemoveKitFromCart(Kit kit)
{
throw new NotImplementedException();
}
}

public class UnitTestAuthenticationProvider : IUserRoleService
{
public bool IsAuthenticated { get; set; }
Expand Down
29 changes: 29 additions & 0 deletions Website/Logic/ShopLogic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using MythicalToyMachine.Data;

namespace MythicalToyMachine.Logic
{
public class ShopLogic
{
public string AccesoriesToString(Kit kit)
{
List<KitAccessory> kitList = kit.KitAccessories.ToList();
string accesoryList = "";
for (int i = 0; i < kitList.Count; i++)
{
if (i == kitList.Count - 1)
{
accesoryList += kitList[i].Acc.Accessoryname;
}
else
{
if (kitList[i].Acc.Accessoryname is not null)
{
accesoryList += $"{kitList[i].Acc.Accessoryname}, ";
}
}
}

return accesoryList;
}
}
}
2 changes: 1 addition & 1 deletion Website/Pages/BuildAToy.razor
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

@if (userRoleService.IsAuthenticated)
{
<body style=" height: 100vh; margin: 0; overflow: hidden;">
<body>

<div id="flex-container">
<div id="accessory-column" Class="flex-column">
Expand Down
4 changes: 0 additions & 4 deletions Website/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@
</footer>

@code{

private string selectedSlide = "2";

const string ContainerBackgroundColor = "rgba(165, 181, 167,.15)";
Expand All @@ -160,9 +159,6 @@
string flexItemStyle = $"background-color: {ItemBackgroundColor}; border: 1px solid {ItemBorderColor};";

string buttonWidth = "width: 100%;";



}


Expand Down
1 change: 0 additions & 1 deletion Website/Pages/Manager.razor
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@
{
try
{

userRoleService.ResetUser();
AccesoryCounts = new List<(Accessory, int)>();
User = _httpContextAccessor.HttpContext.User;
Expand Down
31 changes: 7 additions & 24 deletions Website/Pages/Shop.razor
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
@using MythicalToyMachine.Services;
@using MythicalToyMachine.Data;

Check warning on line 10 in Website/Pages/Shop.razor

View workflow job for this annotation

GitHub Actions / build

The using directive for 'MythicalToyMachine.Data' appeared previously in this namespace

Check warning on line 10 in Website/Pages/Shop.razor

View workflow job for this annotation

GitHub Actions / build

The using directive for 'MythicalToyMachine.Data' appeared previously in this namespace
@using Microsoft.EntityFrameworkCore;

Check warning on line 11 in Website/Pages/Shop.razor

View workflow job for this annotation

GitHub Actions / build

The using directive for 'Microsoft.EntityFrameworkCore' appeared previously in this namespace

Check warning on line 11 in Website/Pages/Shop.razor

View workflow job for this annotation

GitHub Actions / build

The using directive for 'Microsoft.EntityFrameworkCore' appeared previously in this namespace
@using MythicalToyMachine.Logic;
@inject IHttpContextAccessor _httpContextAccessor
@inject HttpClient Http
@inject IDbContextFactory<PostgresContext> dbcontext
Expand Down Expand Up @@ -41,7 +42,7 @@
<div class="product-info">
<div class="product-title">@k.Kitname</div>
<div class="product-price">$@CalculateTotalPrice(KitQties[k.Id], k.Id)</div>
<div class="accessories">Includes accessories: @AccesoriesToString(k)</div>
<div class="accessories">Includes accessories: @shopLogic.AccesoriesToString(k)</div>

<div style="display: flex; align-items: center; margin-top: 10px;">
<label for="quantity" style="margin-right: 10px;">Quantity:</label>
Expand Down Expand Up @@ -95,6 +96,7 @@
Dictionary<int, int> KitQties = new();
CartItem[] CartItemsForId { get; set; }
int userId;
ShopLogic shopLogic = new();

protected override async Task OnInitializedAsync()
{
Expand All @@ -113,6 +115,8 @@
KitQties[kit.Id] = 1;
}



}

private decimal CalculateTotalPrice(int qty, int itemid) => qty * CalulatePrice(itemid);
Expand Down Expand Up @@ -168,6 +172,7 @@
}
return price;
}

private async Task AddToCart(int userid, int kitid, int Qty)
{
//TODO: if not logged in we need to let the user know they need to log in or heck just redirect them to the login page
Expand Down Expand Up @@ -218,27 +223,5 @@
await context.SaveChangesAsync();
}

public string AccesoriesToString(Kit kit)
{
List<KitAccessory> kitList = kit.KitAccessories.ToList();
string accesoryList = "";
for (int i = 0; i < kitList.Count; i++)
{
if (i == kitList.Count - 1)
{
accesoryList += kitList[i].Acc.Accessoryname;
}
else
{
accesoryList += $"{kitList[i].Acc.Accessoryname}, ";
}
}

return accesoryList;
}


//add to card button adds to card


}
25 changes: 0 additions & 25 deletions Website/Pages/TestPage.razor

This file was deleted.

12 changes: 12 additions & 0 deletions Website/Services/IShoppingCartService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using MythicalToyMachine.Data;

namespace MythicalToyMachine.Services
{
public interface IShoppingCartService
{
List<Kit> AllKitsThatAreInTheCart { get; }

void AddKitToCart(Kit kit);
void RemoveKitFromCart(Kit kit);
}
}
2 changes: 1 addition & 1 deletion Website/Services/ShoppingCartService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace MythicalToyMachine.Services;

public class ShoppingCartService
public class ShoppingCartService : IShoppingCartService
{
public List<Kit> AllKitsThatAreInTheCart { get; } = new();

Expand Down

0 comments on commit f6b0c3c

Please sign in to comment.