forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
216 lines
7.7 KiB
C#
216 lines
7.7 KiB
C#
using MagicCarpetContracts.Exceptions;
|
|
using MagicCarpetDatabase.Implementations;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using MagicCarpetDatabase.Models;
|
|
using MagicCarpetContracts.Enums;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using MagicCarpetContracts.DataModels;
|
|
|
|
namespace MagicCarpetTests.StoragesContractsTests;
|
|
|
|
[TestFixture]
|
|
internal class AgencyStorageContractTests : BaseStorageContractTest
|
|
{
|
|
private AgencyStorageContract _agencyStorageContract;
|
|
private Tour _tour;
|
|
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
_agencyStorageContract = new AgencyStorageContract(MagicCarpetDbContext);
|
|
_tour = InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString(), "name", TourType.Sightseeing);
|
|
}
|
|
|
|
[TearDown]
|
|
public void TearDown()
|
|
{
|
|
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Agencies\" CASCADE;");
|
|
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Tours\" CASCADE;");
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetList_WhenHaveRecords_Test()
|
|
{
|
|
var agency = InsertAgencyToDatabaseAndReturn(Guid.NewGuid().ToString(), TourType.Sightseeing, 5);
|
|
InsertAgencyToDatabaseAndReturn(Guid.NewGuid().ToString(), TourType.Sightseeing, 5);
|
|
InsertAgencyToDatabaseAndReturn(Guid.NewGuid().ToString(), TourType.Sightseeing, 5);
|
|
var list = _agencyStorageContract.GetList();
|
|
Assert.That(list, Is.Not.Null);
|
|
Assert.That(list, Has.Count.EqualTo(3));
|
|
AssertElement(list.First(), agency);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetList_WhenNoRecords_Test()
|
|
{
|
|
var list = _agencyStorageContract.GetList();
|
|
Assert.That(list, Is.Not.Null);
|
|
Assert.That(list, Is.Empty);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetElementById_WhenHaveRecord_Test()
|
|
{
|
|
var agency = InsertAgencyToDatabaseAndReturn(Guid.NewGuid().ToString(), TourType.Sightseeing, 5);
|
|
var result = _agencyStorageContract.GetElementById(agency.Id);
|
|
AssertElement(result, agency);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetElementById_WhenNoRecord_Test()
|
|
{
|
|
Assert.That(() => _agencyStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_AddElement_WhenValidData_Test()
|
|
{
|
|
var agency = CreateModel(Guid.NewGuid().ToString(), TourType.Sightseeing, 5, [_tour.Id]);
|
|
_agencyStorageContract.AddElement(agency);
|
|
var result = GetAgencyFromDatabaseById(agency.Id);
|
|
AssertElement(result, agency);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
|
|
{
|
|
var agency = CreateModel(Guid.NewGuid().ToString(), TourType.Sightseeing, 5, [_tour.Id]);
|
|
InsertAgencyToDatabaseAndReturn(agency.Id, TourType.Sightseeing, 5, [(_tour.Id, 3)]);
|
|
Assert.That(() => _agencyStorageContract.AddElement(agency), Throws.TypeOf<StorageException>());
|
|
}
|
|
|
|
[Test]
|
|
public void Try_AddElement_WhenNoHaveTour_Test()
|
|
{
|
|
var agency = CreateModel(Guid.NewGuid().ToString(), TourType.Sightseeing, 0, [_tour.Id]);
|
|
InsertAgencyToDatabaseAndReturn(agency.Id, TourType.Sightseeing, 5, [(_tour.Id, 3)]);
|
|
Assert.That(() => _agencyStorageContract.AddElement(agency), Throws.TypeOf<StorageException>());
|
|
}
|
|
|
|
[Test]
|
|
public void Try_UpdElement_WhenValidData_Test()
|
|
{
|
|
var agency = CreateModel(Guid.NewGuid().ToString(), TourType.Sightseeing, 5, [_tour.Id]);
|
|
InsertAgencyToDatabaseAndReturn(agency.Id, TourType.Ski, 10, null);
|
|
_agencyStorageContract.UpdElement(agency);
|
|
var result = GetAgencyFromDatabaseById(agency.Id);
|
|
AssertElement(result, agency);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
|
{
|
|
var agency = CreateModel(Guid.NewGuid().ToString(), TourType.Sightseeing, 5, [_tour.Id]);
|
|
Assert.That(() => _agencyStorageContract.UpdElement(agency), Throws.TypeOf<ElementNotFoundException>());
|
|
}
|
|
|
|
[Test]
|
|
public void Try_DelElement_WhenRecordExists_Test()
|
|
{
|
|
var agency = InsertAgencyToDatabaseAndReturn(Guid.NewGuid().ToString(), TourType.Sightseeing, 5);
|
|
_agencyStorageContract.DelElement(agency.Id);
|
|
var result = GetAgencyFromDatabaseById(agency.Id);
|
|
Assert.That(result, Is.Null);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_DelElement_WhenRecordDoesNotExist_ThrowsElementNotFoundException()
|
|
{
|
|
Assert.That(() => _agencyStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
|
}
|
|
|
|
private Tour InsertTourToDatabaseAndReturn(string id, string name, TourType type)
|
|
{
|
|
var tour = new Tour { Id = id, TourName = name, TourType = type };
|
|
MagicCarpetDbContext.Tours.Add(tour);
|
|
MagicCarpetDbContext.SaveChanges();
|
|
return tour;
|
|
}
|
|
|
|
private Agency InsertAgencyToDatabaseAndReturn(string id, TourType type, int count, List<(string, int)>? tours = null)
|
|
{
|
|
var agency = new Agency { Id = id, TourType = type, Count = count, Tours = [] };
|
|
if (tours is not null)
|
|
{
|
|
foreach (var elem in tours)
|
|
{
|
|
agency.Tours.Add(new TourAgency { AgencyId = agency.Id, TourId = elem.Item1, Count = elem.Item2 });
|
|
}
|
|
}
|
|
MagicCarpetDbContext.Agencies.Add(agency);
|
|
MagicCarpetDbContext.SaveChanges();
|
|
return agency;
|
|
}
|
|
|
|
private static void AssertElement(AgencyDataModel? actual, Agency expected)
|
|
{
|
|
Assert.That(actual, Is.Not.Null);
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
|
Assert.That(actual.TourType, Is.EqualTo(expected.TourType));
|
|
Assert.That(actual.Count, Is.EqualTo(expected.Count));
|
|
});
|
|
if (expected.Tours is not null)
|
|
{
|
|
Assert.That(actual.Tours, Is.Not.Null);
|
|
Assert.That(actual.Tours, Has.Count.EqualTo(expected.Tours.Count));
|
|
for (int i = 0; i < actual.Tours.Count; ++i)
|
|
{
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(actual.Tours[i].TourId, Is.EqualTo(expected.Tours[i].TourId));
|
|
Assert.That(actual.Tours[i].Count, Is.EqualTo(expected.Tours[i].Count));
|
|
});
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Assert.That(actual.Tours, Is.Null);
|
|
}
|
|
}
|
|
|
|
private static void AssertElement(Agency? actual, AgencyDataModel expected)
|
|
{
|
|
Assert.That(actual, Is.Not.Null);
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
|
Assert.That(actual.TourType, Is.EqualTo(expected.TourType));
|
|
Assert.That(actual.Count, Is.EqualTo(expected.Count));
|
|
});
|
|
if (expected.Tours is not null)
|
|
{
|
|
Assert.That(actual.Tours, Is.Not.Null);
|
|
Assert.That(actual.Tours, Has.Count.EqualTo(expected.Tours.Count));
|
|
for (int i = 0; i < actual.Tours.Count; ++i)
|
|
{
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(actual.Tours[i].TourId, Is.EqualTo(expected.Tours[i].TourId));
|
|
Assert.That(actual.Tours[i].Count, Is.EqualTo(expected.Tours[i].Count));
|
|
});
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Assert.That(actual.Tours, Is.Null);
|
|
}
|
|
}
|
|
|
|
private static AgencyDataModel CreateModel(string id, TourType type, int count, List<string> toursIds)
|
|
{
|
|
var tours = toursIds.Select(x => new TourAgencyDataModel(id, x, 1)).ToList();
|
|
return new(id, type, count, tours);
|
|
}
|
|
|
|
private Agency? GetAgencyFromDatabaseById(string id)
|
|
{
|
|
return MagicCarpetDbContext.Agencies.FirstOrDefault(x => x.Id == id);
|
|
}
|
|
}
|