Skip to content

Commit

Permalink
unit test fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
KaloyankerR committed Jan 8, 2024
1 parent 276f923 commit 14509bc
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 82 deletions.
39 changes: 28 additions & 11 deletions FitFusion/FitFusionTest/MockDAO/MockOrderDAO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public MockOrderDAO()
{
var users = new List<Customer>
{
new Customer { Id = 1, FirstName = "Alice", LastName = "Johnson", Address="Sofia", NutriPoints = 60 },
new Customer { Id = 2, FirstName = "Bob", LastName = "Smith", NutriPoints = 15 },
new Customer { Id = 3, FirstName = "John", LastName = "Doe", NutriPoints = 100 }
new Customer ( id: 1, firstName: "Alice", lastName: "Johnson", email: "[email protected]", passwordHash: "hash", passwordSalt: "salt", address: "Sofia", nutriPoints: 60),
new Customer ( id: 2, firstName: "Bob", lastName: "Smith", email: "[email protected]", passwordHash: "hash", passwordSalt: "salt", address: "USA", nutriPoints: 15),
new Customer ( id: 3, firstName: "John", lastName: "Doe", email: "[email protected]", passwordHash: "hash", passwordSalt: "salt", address: "England", nutriPoints: 100)
};

var products = new List<ProductModel>
Expand Down Expand Up @@ -71,13 +71,16 @@ public MockOrderDAO()
}


public bool CreateOrder(Order order)
{
orders.Add(order);
order.Customer.NutriPoints += order.Cart.NutriPointsReward;
order.Customer.NutriPoints -= order.Cart.NutriPointsNeeded;
return true;
}
//public bool CreateOrder(Order order)
//{
// orders.Add(order);
// order.Customer = new Customer();

// order.Customer.NutriPoints += order.Cart.NutriPointsReward;
// order.Customer.NutriPoints -= order.Cart.NutriPointsNeeded;

// return order;
//}

public ShoppingCart GetShoppingCart(int id)
{
Expand Down Expand Up @@ -118,6 +121,20 @@ public Dictionary<int, Dictionary<ProductModel, int>> GetRecommendations(int cus
{
throw new NotImplementedException();
}


public bool CreateOrder(Order order)
{
throw new NotImplementedException();
}

public ProductModel GetMerchantRecommendation(int customerId)
{
throw new NotImplementedException();
}

public bool CreateMerchantRecommendation(int customerId, ProductModel newProduct)
{
throw new NotImplementedException();
}
}
}
28 changes: 7 additions & 21 deletions FitFusion/FitFusionTest/MockDAO/MockUserDAO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,33 +94,19 @@ public bool UpdateUser(User user)
{
if (DoesEmailExists(user.Email))
{
User existingUser = users.FirstOrDefault(u => u.Email == user.Email)!;

existingUser.FirstName = user.FirstName;
existingUser.LastName = user.LastName;
existingUser.Email = user.Email;
existingUser.PasswordHash = user.PasswordHash;
existingUser.PasswordSalt = user.PasswordSalt;

if (user is Staff)
{
((Staff)existingUser).Phone = ((Staff)user).Phone;
}
else if (user is Owner)
{
((Owner)existingUser).Phone = ((Owner)user).Phone;
}
else if (user is Customer)
{
((Customer)existingUser).NutriPoints = ((Customer)user).NutriPoints;
}
User existingUser = users.FirstOrDefault(u => u.Id == user.Id)!;

users.Remove(existingUser);
users.Add(user);

return true;
}
else
{
throw new NullReferenceException("User doesn't exist.");
}

return true;
//TODO check
}

public bool DeleteUser(User user)
Expand Down
2 changes: 1 addition & 1 deletion FitFusion/FitFusionTest/ProductManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void DeleteProduct_ExistingProduct_ShouldRemoveProduct()
public void FilterByCategory_ShouldReturnFilteredProducts()
{
List<Product> products = _manager.GetProducts();
List<Product> firstSort = _manager.Sort(products, param: "titleAsc");
List<Product> firstSort = _manager.Sort(products, new TitleAscSortStrategy());
List<Product> products2 = _manager.GetProducts();
}

Expand Down
101 changes: 52 additions & 49 deletions FitFusion/FitFusionTest/UserManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Models.User.Enums;
using NUnit.Framework;
using Services;
using Services.Sort;
using System.Data;

namespace FitFusionTest
Expand All @@ -23,28 +24,30 @@ public void SetUp()
public void CreateUser_DuplicateEmail_ShouldThrowDuplicateNameException()
{
var existingUser = new Owner
{
FirstName = "Peter",
LastName = "Parker",
Email = "[email protected]",
PasswordHash = "[email protected]",
PasswordSalt = "salt",
Address = "USA",
Phone = "1234567890",
};
(
id: 24,
firstName: "Peter",
lastName: "Parker",
email: "[email protected]",
passwordHash: "[email protected]",
passwordSalt: "salt",
address: "USA",
phone: "1234567890"
);

_dao.CreateUser(existingUser);

var newUser = new Customer
{
FirstName = "Peter",
LastName = "Parker",
Email = "[email protected]",
PasswordHash = "[email protected]",
PasswordSalt = "salt",
Address = "USA",
NutriPoints = 0
};
(
id: 24,
firstName: "Peter",
lastName: "Parker",
email: "[email protected]",
passwordHash: "[email protected]",
passwordSalt: "salt",
address: "USA",
nutriPoints: 0
);

Assert.Throws<DuplicateNameException>(() => _manager.CreateUser(newUser));
}
Expand All @@ -53,30 +56,30 @@ public void CreateUser_DuplicateEmail_ShouldThrowDuplicateNameException()
public void UpdateUser_ExistingUser_ShouldUpdateUser()
{
var existingUser = new Customer
{
Id = 1,
FirstName = "Ivan",
LastName = "Ivanov",
Email = "[email protected]",
PasswordHash = "[email protected]",
PasswordSalt = "salt",
Address = "USA",
NutriPoints = 0
};
(
id: 23,
firstName: "Ivan",
lastName: "Ivanov",
email: "[email protected]",
passwordHash: "[email protected]",
passwordSalt: "salt",
address: "USA",
nutriPoints: 0
);

_dao.CreateUser(existingUser);

var updatedUser = new Customer
{
Id = 1,
FirstName = "Ivan",
LastName = "Parker",
Email = "[email protected]",
PasswordHash = "[email protected]",
PasswordSalt = "salt",
Address = "Bulgaria",
NutriPoints = 0
};
(
id: 23,
firstName: "Ivan",
lastName: "Parker",
email: "[email protected]",
passwordHash: "[email protected]",
passwordSalt: "salt",
address: "Bulgaria",
nutriPoints: 0
);

_manager.UpdateUser(updatedUser);

Expand All @@ -93,16 +96,16 @@ public void UpdateUser_ExistingUser_ShouldUpdateUser()
public void DeleteUser_ExistingUser_ShouldRemoveUser()
{
var existingUser = new Customer
{
Id = 1,
FirstName = "Doom",
LastName = "Doctor",
Email = "[email protected]",
PasswordHash = "[email protected]",
PasswordSalt = "salt",
Address = "USA",
NutriPoints = 0
};
(
id: 1,
firstName: "Doom",
lastName: "Doctor",
email: "[email protected]",
passwordHash: "[email protected]",
passwordSalt: "salt",
address: "USA",
nutriPoints: 0
);

_dao.CreateUser(existingUser);

Expand Down Expand Up @@ -151,7 +154,7 @@ public void Sort_ShouldSortUsersByLastNameInDescendingOrder()
List<User> users = _manager.GetAllUsers();

List<User> usersToCompare = users.OrderByDescending(x => x.LastName).ToList();
List<User> usersToCheck = _manager.Sort(users, SortParameter.LastNameDescending);
List<User> usersToCheck = _manager.Sort(users, new LastNameDescending());

Assert.That(usersToCheck, Is.EqualTo(usersToCompare));
}
Expand Down

0 comments on commit 14509bc

Please sign in to comment.