2022-10-30 02:57:37 +00:00
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using LeedsBeerQuest.API.Data.Contexts;
|
2022-10-30 03:40:17 +00:00
|
|
|
using LeedsBeerQuest.API.Data.Models;
|
2022-10-30 02:57:37 +00:00
|
|
|
using LeedsBeerQuest.API.Data.Services;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
|
|
namespace LeedsBeerQuest.API.Tests.Data.Services;
|
|
|
|
|
|
|
|
[TestFixture]
|
|
|
|
public class VenueServiceTests
|
|
|
|
{
|
|
|
|
private LeedsBeerQuestDbContext _context;
|
|
|
|
private VenueService _venueService;
|
|
|
|
|
|
|
|
[SetUp]
|
2022-10-30 15:13:18 +00:00
|
|
|
public void SetUp()
|
2022-10-30 02:57:37 +00:00
|
|
|
{
|
|
|
|
var builder = new DbContextOptionsBuilder<LeedsBeerQuestDbContext>()
|
|
|
|
.UseInMemoryDatabase($"VenueServiceTests.{Guid.NewGuid().ToString()}");
|
|
|
|
_context = new LeedsBeerQuestDbContext(builder.Options);
|
|
|
|
|
|
|
|
_venueService = new VenueService(_context);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public async Task GetAllVenues_ReturnsAllVenues_WhenAllVenuesArePresentInDatabase()
|
|
|
|
{
|
2022-10-30 03:40:17 +00:00
|
|
|
var venueTestData = await seedVenueTestData();
|
2022-10-30 02:57:37 +00:00
|
|
|
|
|
|
|
var result = await _venueService.GetAllVenues();
|
|
|
|
var resultArray = result.ToArray();
|
|
|
|
|
2022-10-30 17:48:47 +00:00
|
|
|
Assert.AreEqual(venueTestData.Length, resultArray.Length);
|
2022-10-30 02:57:37 +00:00
|
|
|
|
|
|
|
Assert.AreEqual(venueTestData[0].Id, resultArray[0].Id);
|
|
|
|
Assert.AreEqual(venueTestData[0].Name, resultArray[0].Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public async Task GetAllVenues_ReturnsNoVenues_WhenNoVenuesArePresentInDatabase()
|
|
|
|
{
|
|
|
|
var result = await _venueService.GetAllVenues();
|
|
|
|
|
|
|
|
Assert.AreEqual(0, result.Count());
|
|
|
|
}
|
2022-10-30 03:40:17 +00:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public async Task GetVenue_ReturnsVenue_WhenRequestedVenueExists()
|
|
|
|
{
|
|
|
|
var venueTestData = await seedVenueTestData();
|
|
|
|
var expectedVenueId = venueTestData[0].Id;
|
|
|
|
var expectedVenueName = venueTestData[0].Name;
|
|
|
|
|
|
|
|
var result = await _venueService.GetVenue(expectedVenueId);;
|
|
|
|
|
|
|
|
Assert.NotNull(result);
|
|
|
|
Assert.AreEqual(expectedVenueId, result.Id);
|
|
|
|
Assert.AreEqual(expectedVenueName, result.Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public async Task GetVenue_ReturnsNull_WhenRequestedVenueDoesNotExist()
|
|
|
|
{
|
|
|
|
var result = await _venueService.GetVenue(1);
|
|
|
|
|
|
|
|
Assert.Null(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<Venue[]> seedVenueTestData()
|
|
|
|
{
|
2022-10-30 17:48:47 +00:00
|
|
|
var venueTestData = TestData.VenueTestData;
|
2022-10-30 03:40:17 +00:00
|
|
|
_context.Venues.AddRange(venueTestData);
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
|
|
|
return venueTestData;
|
|
|
|
}
|
2022-10-30 02:57:37 +00:00
|
|
|
}
|