Create Category controller and service
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using LeedsBeerQuest.API.Data.Contexts;
|
||||
using LeedsBeerQuest.API.Data.Models;
|
||||
using LeedsBeerQuest.API.Data.Services;
|
||||
using LeedsBeerQuest.API.Data.Services.Interfaces;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace LeedsBeerQuest.API.Tests.Data.Services;
|
||||
|
||||
[TestFixture]
|
||||
public class CategoryServiceTests
|
||||
{
|
||||
private LeedsBeerQuestDbContext _context;
|
||||
private ICategoryService _categoryService;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
var builder = new DbContextOptionsBuilder<LeedsBeerQuestDbContext>()
|
||||
.UseInMemoryDatabase($"CategoryServiceTests.{Guid.NewGuid().ToString()}");
|
||||
_context = new LeedsBeerQuestDbContext(builder.Options);
|
||||
|
||||
_categoryService = new CategoryService(_context);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetCategories_ReturnsAllCategories_WhenAllCategoriesArePresentInDatabase()
|
||||
{
|
||||
var categoryTestData = await seedCategoryTestData();
|
||||
|
||||
var result = await _categoryService.GetCategories();
|
||||
|
||||
Assert.AreEqual(categoryTestData.Length, result.Count());
|
||||
}
|
||||
|
||||
private async Task<Category[]> seedCategoryTestData()
|
||||
{
|
||||
var categoryTestData = TestData.CategoryTestData;
|
||||
_context.Categories.AddRange(categoryTestData);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return categoryTestData;
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ namespace LeedsBeerQuest.API.Tests.Data.Services;
|
||||
|
||||
public static class TestData
|
||||
{
|
||||
public static readonly IEnumerable<Venue> VenueTestData = new[]
|
||||
public static IEnumerable<Venue> VenueTestData => new[]
|
||||
{
|
||||
new Venue
|
||||
{
|
||||
@@ -34,4 +34,34 @@ public static class TestData
|
||||
Tags = new List<Tag>()
|
||||
}
|
||||
};
|
||||
|
||||
public static Category[] CategoryTestData => new[]
|
||||
{
|
||||
new Category
|
||||
{
|
||||
Id = 1,
|
||||
Name = "Category 1"
|
||||
},
|
||||
new Category
|
||||
{
|
||||
Id = 2,
|
||||
Name = "Category 2"
|
||||
},
|
||||
new Category
|
||||
{
|
||||
Id = 3,
|
||||
Name = "Category 3"
|
||||
},
|
||||
new Category
|
||||
{
|
||||
Id = 4,
|
||||
Name = "Category 4"
|
||||
},
|
||||
new Category
|
||||
{
|
||||
Id = 5,
|
||||
Name = "Category 5"
|
||||
},
|
||||
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user