diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Controllers/CategoryControllerTests.cs b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Controllers/CategoryControllerTests.cs new file mode 100644 index 0000000..397d5bc --- /dev/null +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Controllers/CategoryControllerTests.cs @@ -0,0 +1,37 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using LeedsBeerQuest.API.Controllers; +using LeedsBeerQuest.API.Data.Models; +using LeedsBeerQuest.API.Data.Services.Interfaces; +using LeedsBeerQuest.API.Tests.Data.Services; +using LeedsBeerQuest.API.Tests.TestHelpers; +using NSubstitute; +using NUnit.Framework; + +namespace LeedsBeerQuest.API.Tests.Controllers; + +[TestFixture] +public class CategoryControllerTests +{ + private ICategoryService _categoryService; + private CategoryController _categoryController; + + [SetUp] + public void SetUp() + { + _categoryService = Substitute.For(); + _categoryController = new CategoryController(_categoryService); + } + + [Test] + public async Task GetCategories_ReturnsAllCategories() + { + _categoryService.GetCategories().Returns(TestData.CategoryTestData); + + var result = await _categoryController.GetCategories(); + var resultObject = ControllerTestHelper.ActionResultToOkResultValue>(result); + + Assert.AreEqual(TestData.CategoryTestData.Length, resultObject.Count()); + } +} \ No newline at end of file diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Data/Services/CategoryServiceTests.cs b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Data/Services/CategoryServiceTests.cs new file mode 100644 index 0000000..c6ca394 --- /dev/null +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Data/Services/CategoryServiceTests.cs @@ -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() + .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 seedCategoryTestData() + { + var categoryTestData = TestData.CategoryTestData; + _context.Categories.AddRange(categoryTestData); + await _context.SaveChangesAsync(); + + return categoryTestData; + } +} \ No newline at end of file diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Data/Services/TestData.cs b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Data/Services/TestData.cs index 5b08319..b45086b 100644 --- a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Data/Services/TestData.cs +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Data/Services/TestData.cs @@ -6,7 +6,7 @@ namespace LeedsBeerQuest.API.Tests.Data.Services; public static class TestData { - public static readonly IEnumerable VenueTestData = new[] + public static IEnumerable VenueTestData => new[] { new Venue { @@ -34,4 +34,34 @@ public static class TestData Tags = new List() } }; + + 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" + }, + + }; } \ No newline at end of file diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Controllers/CategoryController.cs b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Controllers/CategoryController.cs index 5b27ac7..68c9c2b 100644 --- a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Controllers/CategoryController.cs +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Controllers/CategoryController.cs @@ -1,6 +1,23 @@ +using LeedsBeerQuest.API.Data.Services.Interfaces; +using Microsoft.AspNetCore.Mvc; + namespace LeedsBeerQuest.API.Controllers; -public class CategoryController +[ApiController] +[Route("[controller]")] +public class CategoryController : ControllerBase { - + private readonly ICategoryService _categoryService; + + public CategoryController(ICategoryService categoryService) + { + _categoryService = categoryService; + } + + [HttpGet] + public async Task GetCategories() + { + var categories = await _categoryService.GetCategories(); + return Ok(categories); + } } \ No newline at end of file diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Services/CategoryService.cs b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Services/CategoryService.cs index 792fddc..a432d56 100644 --- a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Services/CategoryService.cs +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Services/CategoryService.cs @@ -1,6 +1,18 @@ +using Microsoft.EntityFrameworkCore; +using LeedsBeerQuest.API.Data.Contexts; +using LeedsBeerQuest.API.Data.Models; +using LeedsBeerQuest.API.Data.Services.Interfaces; + namespace LeedsBeerQuest.API.Data.Services; -public class CategoryService +public class CategoryService : ICategoryService { - + private readonly LeedsBeerQuestDbContext _context; + + public CategoryService(LeedsBeerQuestDbContext context) + { + _context = context; + } + public async Task> GetCategories() => + await _context.Categories.ToListAsync(); } \ No newline at end of file diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Services/Interfaces/ICategoryService.cs b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Services/Interfaces/ICategoryService.cs index 735d103..af0dafa 100644 --- a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Services/Interfaces/ICategoryService.cs +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Services/Interfaces/ICategoryService.cs @@ -1,6 +1,8 @@ +using LeedsBeerQuest.API.Data.Models; + namespace LeedsBeerQuest.API.Data.Services.Interfaces; public interface ICategoryService { - + public Task> GetCategories(); } \ No newline at end of file diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/ServiceDependencies.cs b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/ServiceDependencies.cs index b12936f..27ea87e 100644 --- a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/ServiceDependencies.cs +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/ServiceDependencies.cs @@ -14,5 +14,6 @@ public static class ServiceDependencies serviceCollection.AddTransient(_ => new LeedsBeerQuestDbContext(dbOptions.Options)); serviceCollection.AddScoped(); + serviceCollection.AddScoped(); } } \ No newline at end of file