From 8cd07e206338cc78a49df26178d13da780acf8f2 Mon Sep 17 00:00:00 2001 From: Stedoss <29103029+Stedoss@users.noreply.github.com> Date: Sun, 30 Oct 2022 20:23:48 +0000 Subject: [PATCH] Add `TagService` with `TagController` --- .../Controllers/CategoryControllerTests.cs | 2 +- .../Controllers/TagControllerTests.cs | 37 +++++++++++++++ .../Controllers/VenueControllerTests.cs | 4 +- .../Data/Services/TagServiceTests.cs | 47 +++++++++++++++++++ .../Data/Services/TestData.cs | 30 ++++++++++++ .../Controllers/TagController.cs | 24 ++++++++++ .../Data/Services/Interfaces/ITagService.cs | 4 +- .../Data/Services/TagService.cs | 17 ++++++- .../LeedsBeerQuest.API/ServiceDependencies.cs | 1 + 9 files changed, 160 insertions(+), 6 deletions(-) create mode 100644 backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Controllers/TagControllerTests.cs create mode 100644 backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Data/Services/TagServiceTests.cs create mode 100644 backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Controllers/TagController.cs diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Controllers/CategoryControllerTests.cs b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Controllers/CategoryControllerTests.cs index 397d5bc..802d403 100644 --- a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Controllers/CategoryControllerTests.cs +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Controllers/CategoryControllerTests.cs @@ -25,7 +25,7 @@ public class CategoryControllerTests } [Test] - public async Task GetCategories_ReturnsAllCategories() + public async Task GetCategories_ReturnsOk_WithAllCategories() { _categoryService.GetCategories().Returns(TestData.CategoryTestData); diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Controllers/TagControllerTests.cs b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Controllers/TagControllerTests.cs new file mode 100644 index 0000000..987742b --- /dev/null +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Controllers/TagControllerTests.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 TagControllerTests +{ + private ITagService _tagService; + private TagController _tagController; + + [SetUp] + public void SetUp() + { + _tagService = Substitute.For(); + _tagController = new TagController(_tagService); + } + + [Test] + public async Task GetTags_ReturnsOk_WithAllTags() + { + _tagService.GetTags().Returns(TestData.TagTestData); + + var result = await _tagController.GetTags(); + var resultObject = ControllerTestHelper.ActionResultToOkResultValue>(result); + + Assert.AreEqual(TestData.TagTestData.Length, resultObject.Count()); + } +} \ No newline at end of file diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Controllers/VenueControllerTests.cs b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Controllers/VenueControllerTests.cs index 31039b2..77f2665 100644 --- a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Controllers/VenueControllerTests.cs +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Controllers/VenueControllerTests.cs @@ -27,7 +27,7 @@ public class VenueControllerTests } [Test] - public async Task GetAll_ReturnsAllVenues_WhenNoVenueIdIsPassed() + public async Task GetAll_ReturnsOk_WithAllVenues_WhenNoVenueIdIsPassed() { _venueService.GetAllVenues().Returns(TestData.VenueTestData); @@ -39,7 +39,7 @@ public class VenueControllerTests } [Test] - public async Task GetVenue_ReturnsVenue_WhenVenueIdIsPassedAndIsReturnedFromService() + public async Task GetVenue_ReturnsOk_WithVenue_WhenVenueIdIsPassedAndIsReturnedFromService() { var venueTestData = TestData.VenueTestData.ToArray(); _venueService.GetVenue(Arg.Any()).Returns(venueTestData[0]); diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Data/Services/TagServiceTests.cs b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Data/Services/TagServiceTests.cs new file mode 100644 index 0000000..31625c2 --- /dev/null +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Data/Services/TagServiceTests.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 TagServiceTests +{ + private LeedsBeerQuestDbContext _context; + private ITagService _tagService; + + [SetUp] + public void SetUp() + { + var builder = new DbContextOptionsBuilder() + .UseInMemoryDatabase($"CategoryServiceTests.{Guid.NewGuid().ToString()}"); + _context = new LeedsBeerQuestDbContext(builder.Options); + + _tagService = new TagService(_context); + } + + [Test] + public async Task GetTags_ReturnsAllTags_WhenAllTagsArePresentInDatabase() + { + var tagTestData = await seedTagTestData(); + + var result = await _tagService.GetTags(); + + Assert.AreEqual(tagTestData.Length, result.Count()); + } + + private async Task seedTagTestData() + { + var tagTestData = TestData.TagTestData; + _context.Tags.AddRange(tagTestData); + await _context.SaveChangesAsync(); + + return tagTestData; + } +} \ 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 59fbbd5..1f17654 100644 --- a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Data/Services/TestData.cs +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Data/Services/TestData.cs @@ -64,4 +64,34 @@ public static class TestData }, }; + + public static Tag[] TagTestData => new[] + { + new Tag + { + Id = 1, + Name = "Tag 1", + }, + new Tag + { + Id = 2, + Name = "Tag 2", + }, + new Tag + { + Id = 3, + Name = "Tag 3", + }, + new Tag + { + Id = 4, + Name = "Tag 4", + }, + new Tag + { + Id = 5, + Name = "Tag 5", + }, + + }; } \ No newline at end of file diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Controllers/TagController.cs b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Controllers/TagController.cs new file mode 100644 index 0000000..de1b6e6 --- /dev/null +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Controllers/TagController.cs @@ -0,0 +1,24 @@ +using LeedsBeerQuest.API.Data.Services.Interfaces; +using Microsoft.AspNetCore.Mvc; + +namespace LeedsBeerQuest.API.Controllers; + +[ApiController] +[Route("[controller]")] +public class TagController : ControllerBase +{ + private ITagService _tagService; + + public TagController(ITagService tagService) + { + _tagService = tagService; + } + + [HttpGet] + public async Task GetTags() + { + var tags = await _tagService.GetTags(); + + return Ok(tags); + } +} \ No newline at end of file diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Services/Interfaces/ITagService.cs b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Services/Interfaces/ITagService.cs index 1359485..8ea1c5b 100644 --- a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Services/Interfaces/ITagService.cs +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Services/Interfaces/ITagService.cs @@ -1,6 +1,8 @@ +using LeedsBeerQuest.API.Data.Models; + namespace LeedsBeerQuest.API.Data.Services.Interfaces; public interface ITagService { - + public Task> GetTags(); } \ No newline at end of file diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Services/TagService.cs b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Services/TagService.cs index 0187b46..dfe7e66 100644 --- a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Services/TagService.cs +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/Data/Services/TagService.cs @@ -1,6 +1,19 @@ +using LeedsBeerQuest.API.Data.Contexts; +using LeedsBeerQuest.API.Data.Models; +using LeedsBeerQuest.API.Data.Services.Interfaces; +using Microsoft.EntityFrameworkCore; + namespace LeedsBeerQuest.API.Data.Services; -public class TagService +public class TagService : ITagService { - + private readonly LeedsBeerQuestDbContext _context; + + public TagService(LeedsBeerQuestDbContext context) + { + _context = context; + } + + public async Task> GetTags() => + await _context.Tags.ToListAsync(); } \ 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 27ea87e..07eaa6f 100644 --- a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/ServiceDependencies.cs +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API/ServiceDependencies.cs @@ -15,5 +15,6 @@ public static class ServiceDependencies serviceCollection.AddTransient(_ => new LeedsBeerQuestDbContext(dbOptions.Options)); serviceCollection.AddScoped(); serviceCollection.AddScoped(); + serviceCollection.AddScoped(); } } \ No newline at end of file