Add TagService
with TagController
This commit is contained in:
parent
431532e69f
commit
8cd07e2063
@ -25,7 +25,7 @@ public class CategoryControllerTests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public async Task GetCategories_ReturnsAllCategories()
|
public async Task GetCategories_ReturnsOk_WithAllCategories()
|
||||||
{
|
{
|
||||||
_categoryService.GetCategories().Returns(TestData.CategoryTestData);
|
_categoryService.GetCategories().Returns(TestData.CategoryTestData);
|
||||||
|
|
||||||
|
@ -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<ITagService>();
|
||||||
|
_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<IEnumerable<Tag>>(result);
|
||||||
|
|
||||||
|
Assert.AreEqual(TestData.TagTestData.Length, resultObject.Count());
|
||||||
|
}
|
||||||
|
}
|
@ -27,7 +27,7 @@ public class VenueControllerTests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public async Task GetAll_ReturnsAllVenues_WhenNoVenueIdIsPassed()
|
public async Task GetAll_ReturnsOk_WithAllVenues_WhenNoVenueIdIsPassed()
|
||||||
{
|
{
|
||||||
_venueService.GetAllVenues().Returns(TestData.VenueTestData);
|
_venueService.GetAllVenues().Returns(TestData.VenueTestData);
|
||||||
|
|
||||||
@ -39,7 +39,7 @@ public class VenueControllerTests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public async Task GetVenue_ReturnsVenue_WhenVenueIdIsPassedAndIsReturnedFromService()
|
public async Task GetVenue_ReturnsOk_WithVenue_WhenVenueIdIsPassedAndIsReturnedFromService()
|
||||||
{
|
{
|
||||||
var venueTestData = TestData.VenueTestData.ToArray();
|
var venueTestData = TestData.VenueTestData.ToArray();
|
||||||
_venueService.GetVenue(Arg.Any<int>()).Returns(venueTestData[0]);
|
_venueService.GetVenue(Arg.Any<int>()).Returns(venueTestData[0]);
|
||||||
|
@ -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<LeedsBeerQuestDbContext>()
|
||||||
|
.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<Tag[]> seedTagTestData()
|
||||||
|
{
|
||||||
|
var tagTestData = TestData.TagTestData;
|
||||||
|
_context.Tags.AddRange(tagTestData);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
return tagTestData;
|
||||||
|
}
|
||||||
|
}
|
@ -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",
|
||||||
|
},
|
||||||
|
|
||||||
|
};
|
||||||
}
|
}
|
@ -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<IActionResult> GetTags()
|
||||||
|
{
|
||||||
|
var tags = await _tagService.GetTags();
|
||||||
|
|
||||||
|
return Ok(tags);
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,8 @@
|
|||||||
|
using LeedsBeerQuest.API.Data.Models;
|
||||||
|
|
||||||
namespace LeedsBeerQuest.API.Data.Services.Interfaces;
|
namespace LeedsBeerQuest.API.Data.Services.Interfaces;
|
||||||
|
|
||||||
public interface ITagService
|
public interface ITagService
|
||||||
{
|
{
|
||||||
|
public Task<IEnumerable<Tag>> GetTags();
|
||||||
}
|
}
|
@ -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;
|
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<IEnumerable<Tag>> GetTags() =>
|
||||||
|
await _context.Tags.ToListAsync();
|
||||||
}
|
}
|
@ -15,5 +15,6 @@ public static class ServiceDependencies
|
|||||||
serviceCollection.AddTransient(_ => new LeedsBeerQuestDbContext(dbOptions.Options));
|
serviceCollection.AddTransient(_ => new LeedsBeerQuestDbContext(dbOptions.Options));
|
||||||
serviceCollection.AddScoped<IVenueService, VenueService>();
|
serviceCollection.AddScoped<IVenueService, VenueService>();
|
||||||
serviceCollection.AddScoped<ICategoryService, CategoryService>();
|
serviceCollection.AddScoped<ICategoryService, CategoryService>();
|
||||||
|
serviceCollection.AddScoped<ITagService, TagService>();
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user