forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
192 lines
6.8 KiB
C#
192 lines
6.8 KiB
C#
using MagicCarpetContracts.DataModels;
|
|
using MagicCarpetContracts.Enums;
|
|
using MagicCarpetContracts.Exceptions;
|
|
using MagicCarpetDatabase.Implementations;
|
|
using MagicCarpetDatabase.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MagicCarpetTests.StoragesContractsTests;
|
|
|
|
[TestFixture]
|
|
internal class SuppliesStorageContractTests : BaseStorageContractTest
|
|
{
|
|
private SuppliesStorageContract _suppliesStorageContract;
|
|
private Tour _tour;
|
|
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
_suppliesStorageContract = new SuppliesStorageContract(MagicCarpetDbContext);
|
|
_tour = InsertTourToDatabaseAndReturn();
|
|
}
|
|
|
|
[TearDown]
|
|
public void TearDown()
|
|
{
|
|
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Supplieses\" CASCADE;");
|
|
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Tours\" CASCADE;");
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetList_WhenHaveRecords_Test()
|
|
{
|
|
var supply = InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString(), TourType.Ski, 5);
|
|
InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString(), TourType.Ski, 5);
|
|
InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString(), TourType.Ski, 5);
|
|
var list = _suppliesStorageContract.GetList();
|
|
Assert.That(list, Is.Not.Null);
|
|
Assert.That(list, Has.Count.EqualTo(3));
|
|
AssertElement(list.First(), supply);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetList_WhenNoRecords_Test()
|
|
{
|
|
var list = _suppliesStorageContract.GetList();
|
|
Assert.That(list, Is.Not.Null);
|
|
Assert.That(list, Is.Empty);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetElementById_WhenHaveRecord_Test()
|
|
{
|
|
var supply = InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString(), TourType.Ski, 5);
|
|
AssertElement(_suppliesStorageContract.GetElementById(supply.Id), supply);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetElementById_WhenNoRecord_Test()
|
|
{
|
|
Assert.That(() => _suppliesStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_AddElement_Test()
|
|
{
|
|
var supply = CreateModel(Guid.NewGuid().ToString(), TourType.Ski, 5, [_tour.Id]);
|
|
_suppliesStorageContract.AddElement(supply);
|
|
AssertElement(GetSupplyFromDatabaseById(supply.Id), supply);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
|
|
{
|
|
var supply = CreateModel(Guid.NewGuid().ToString(), TourType.Ski, 5, [_tour.Id]);
|
|
InsertSupplyToDatabaseAndReturn(supply.Id, TourType.Ski, 5, [(_tour.Id, 3)]);
|
|
Assert.That(() => _suppliesStorageContract.AddElement(supply), Throws.TypeOf<StorageException>());
|
|
}
|
|
|
|
[Test]
|
|
public void Try_UpdElement_Test()
|
|
{
|
|
var supply = CreateModel(Guid.NewGuid().ToString(), TourType.Ski, 5, [_tour.Id]);
|
|
InsertSupplyToDatabaseAndReturn(supply.Id, TourType.Ski, 5, null);
|
|
_suppliesStorageContract.UpdElement(supply);
|
|
AssertElement(GetSupplyFromDatabaseById(supply.Id), supply);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
|
{
|
|
Assert.That(() => _suppliesStorageContract.UpdElement(CreateModel(Guid.NewGuid().ToString(), TourType.Ski, 5, [_tour.Id])), Throws.TypeOf<ElementNotFoundException>());
|
|
}
|
|
|
|
private Tour InsertTourToDatabaseAndReturn()
|
|
{
|
|
var tour = new Tour { Id = Guid.NewGuid().ToString(), TourName = "Test Tour", TourType = TourType.Ski };
|
|
MagicCarpetDbContext.Tours.Add(tour);
|
|
MagicCarpetDbContext.SaveChanges();
|
|
return tour;
|
|
}
|
|
|
|
private Supplies InsertSupplyToDatabaseAndReturn(string id, TourType type, int count, List<(string, int)>? tours = null)
|
|
{
|
|
var supply = new Supplies { Id = id, Type = type, ProductuionDate = DateTime.UtcNow, Count = count, Tours = [] };
|
|
if (tours is not null)
|
|
{
|
|
foreach (var elem in tours)
|
|
{
|
|
supply.Tours.Add(new TourSupplies { SuppliesId = supply.Id, TourId = elem.Item1, Count = elem.Item2 });
|
|
}
|
|
}
|
|
MagicCarpetDbContext.Supplieses.Add(supply);
|
|
MagicCarpetDbContext.SaveChanges();
|
|
return supply;
|
|
}
|
|
|
|
private static void AssertElement(SuppliesDataModel? actual, Supplies expected)
|
|
{
|
|
Assert.That(actual, Is.Not.Null);
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
|
Assert.That(actual.TourType, Is.EqualTo(expected.Type));
|
|
Assert.That(actual.ProductuionDate, Is.EqualTo(expected.ProductuionDate));
|
|
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(Supplies? actual, SuppliesDataModel expected)
|
|
{
|
|
Assert.That(actual, Is.Not.Null);
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
|
Assert.That(actual.Type, Is.EqualTo(expected.TourType));
|
|
Assert.That(actual.ProductuionDate, Is.EqualTo(expected.ProductuionDate));
|
|
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 SuppliesDataModel CreateModel(string id, TourType type, int count, List<string> toursIds)
|
|
{
|
|
var tours = toursIds.Select(x => new TourSuppliesDataModel(id, x, 1)).ToList();
|
|
return new(id, type, DateTime.UtcNow, count, tours);
|
|
}
|
|
|
|
|
|
private Supplies? GetSupplyFromDatabaseById(string id)
|
|
{
|
|
return MagicCarpetDbContext.Supplieses.FirstOrDefault(x => x.Id == id);
|
|
}
|
|
}
|