-
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.
Merge pull request #223 from AnalogIO/fix-set-product-user-groups
Allows setting product user groups
- Loading branch information
Showing
6 changed files
with
184 additions
and
20 deletions.
There are no files selected for viewing
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
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
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
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
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
128 changes: 128 additions & 0 deletions
128
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,128 @@ | ||
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)); | ||
} | ||
|
||
[Fact(DisplayName = "AddProduct adds only selected user groups")] | ||
public async Task AddProduct_Sets_Correct_UserGroups() | ||
{ | ||
var builder = new DbContextOptionsBuilder<CoffeeCardContext>() | ||
.UseInMemoryDatabase(nameof(AddProduct_Sets_Correct_UserGroups)); | ||
|
||
var databaseSettings = new DatabaseSettings | ||
{ | ||
SchemaName = "test" | ||
}; | ||
var environmentSettings = new EnvironmentSettings() | ||
{ | ||
EnvironmentType = EnvironmentType.Test | ||
}; | ||
|
||
await using var context = new CoffeeCardContext(builder.Options, databaseSettings, environmentSettings); | ||
|
||
using var productService = new ProductService(context); | ||
|
||
var p = new AddProductRequest | ||
{ | ||
Name = "Coffee", | ||
Description = "Coffee Clip card", | ||
NumberOfTickets = 10, | ||
Price = 10, | ||
Visible = true, | ||
AllowedUserGroups = new List<UserGroup> { UserGroup.Manager, UserGroup.Board } | ||
}; | ||
|
||
await productService.AddProduct(p); | ||
|
||
var expected = new List<UserGroup> | ||
{ | ||
UserGroup.Manager, UserGroup.Board | ||
}; | ||
|
||
var result = await productService.GetProductAsync(1); | ||
|
||
Assert.Collection<UserGroup>(expected, | ||
e => e.Equals(UserGroup.Customer), | ||
e => e.Equals(UserGroup.Board)); | ||
} | ||
} | ||
} |