Add TagService with TagController

This commit is contained in:
Stedoss
2022-10-30 20:23:48 +00:00
parent 431532e69f
commit 8cd07e2063
9 changed files with 160 additions and 6 deletions

View File

@@ -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;
}
}

View File

@@ -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",
},
};
}