-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a018228
commit ae5239d
Showing
1 changed file
with
85 additions
and
0 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
coffeecard/CoffeeCard.Tests.Unit/Services/v2/ProductServiceTest.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,85 @@ | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using CoffeeCard.Common.Configuration; | ||
using CoffeeCard.Library.Persistence; | ||
using CoffeeCard.Library.Services.v2; | ||
using CoffeeCard.Models.DataTransferObjects.v2.Product; | ||
using CoffeeCard.Models.Entities; | ||
using Microsoft.EntityFrameworkCore; | ||
using Xunit; | ||
|
||
namespace CoffeeCard.Tests.Unit.Services.v2 | ||
{ | ||
public class ProductServiceTest | ||
{ | ||
[Fact(DisplayName = "UpdateProduct removes ommitted user groups and only adds selected user groups")] | ||
public async Task UpdateProduct_Removes_Omitted_UserGroups() | ||
{ | ||
var builder = new DbContextOptionsBuilder<CoffeeCardContext>() | ||
.UseInMemoryDatabase(nameof(UpdateProduct_Removes_Omitted_UserGroups)); | ||
|
||
var databaseSettings = new DatabaseSettings | ||
{ | ||
SchemaName = "test" | ||
}; | ||
var environmentSettings = new EnvironmentSettings() | ||
{ | ||
EnvironmentType = EnvironmentType.Test | ||
}; | ||
|
||
await using var context = new CoffeeCardContext(builder.Options, databaseSettings, environmentSettings); | ||
var p = new Product | ||
{ | ||
Id = 1, | ||
Name = "Coffee", | ||
Description = "Coffee Clip card", | ||
NumberOfTickets = 10, | ||
Price = 10, | ||
ExperienceWorth = 10, | ||
Visible = true | ||
}; | ||
await context.AddAsync(p); | ||
await context.SaveChangesAsync(); | ||
|
||
await context.AddAsync(new ProductUserGroup | ||
{ | ||
Product = p, | ||
UserGroup = UserGroup.Barista | ||
}); | ||
|
||
await context.AddAsync(new ProductUserGroup | ||
{ | ||
Product = p, | ||
UserGroup = UserGroup.Manager | ||
}); | ||
|
||
await context.SaveChangesAsync(); | ||
|
||
using var productService = new ProductService(context); | ||
|
||
await productService.UpdateProduct(new UpdateProductRequest() | ||
{ | ||
Id = 1, | ||
Visible = true, | ||
Price = 10, | ||
NumberOfTickets = 10, | ||
Name = "Coffee", | ||
Description = "Coffee Clip card", | ||
AllowedUserGroups = new List<UserGroup>() { UserGroup.Customer, UserGroup.Board } | ||
}); | ||
|
||
var expected = new List<UserGroup> | ||
{ | ||
UserGroup.Customer, UserGroup.Board | ||
}; | ||
|
||
var result = await productService.GetProductAsync(1); | ||
|
||
Assert.Collection<UserGroup>(expected, | ||
e => e.Equals(UserGroup.Customer), | ||
e => e.Equals(UserGroup.Board)); | ||
} | ||
} | ||
} |