201 lines
7.4 KiB
C#
201 lines
7.4 KiB
C#
using CandyHouseBase.DataModels;
|
|
using CandyHouseBase.Exceptions;
|
|
using CandyHouseDataBase.Implementations;
|
|
using CandyHouseDataBase.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using NUnit.Framework;
|
|
using System;
|
|
using System.Linq;
|
|
|
|
namespace CandyHouseTests.StoragesContracts
|
|
{
|
|
[TestFixture]
|
|
internal class IngredientStorageContractTests : BaseStorageContractTest
|
|
{
|
|
private IngredientStorageContract _ingredientStorageContract;
|
|
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
_ingredientStorageContract = new IngredientStorageContract(CandyHouseDbContext);
|
|
}
|
|
|
|
[TearDown]
|
|
public void TearDown()
|
|
{
|
|
CandyHouseDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Ingredients\" CASCADE;");
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetList_WhenHaveRecords_Test()
|
|
{
|
|
var ingredient = InsertIngredientToDatabaseAndReturn(Guid.NewGuid().ToString(), "Sugar");
|
|
InsertIngredientToDatabaseAndReturn(Guid.NewGuid().ToString(), "Flour");
|
|
InsertIngredientToDatabaseAndReturn(Guid.NewGuid().ToString(), "Salt");
|
|
|
|
var list = _ingredientStorageContract.GetList();
|
|
|
|
Assert.That(list, Is.Not.Null);
|
|
Assert.That(list, Has.Count.EqualTo(3));
|
|
AssertElement(list.First(x => x.Id == ingredient.Id), ingredient);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetList_WhenNoRecords_Test()
|
|
{
|
|
var list = _ingredientStorageContract.GetList();
|
|
|
|
Assert.That(list, Is.Not.Null);
|
|
Assert.That(list, Is.Empty);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetElementById_WhenHaveRecord_Test()
|
|
{
|
|
var ingredient = InsertIngredientToDatabaseAndReturn(Guid.NewGuid().ToString(), "Sugar");
|
|
|
|
AssertElement(_ingredientStorageContract.GetElementById(ingredient.Id), ingredient);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetElementById_WhenNoRecord_Test()
|
|
{
|
|
InsertIngredientToDatabaseAndReturn(Guid.NewGuid().ToString(), "Sugar");
|
|
|
|
Assert.That(() => _ingredientStorageContract.GetElementById(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetElementByName_WhenHaveRecord_Test()
|
|
{
|
|
var ingredient = InsertIngredientToDatabaseAndReturn(Guid.NewGuid().ToString(), "Sugar");
|
|
|
|
AssertElement(_ingredientStorageContract.GetElementByName(ingredient.Name), ingredient);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetElementByName_WhenNoRecord_Test()
|
|
{
|
|
InsertIngredientToDatabaseAndReturn(Guid.NewGuid().ToString(), "Sugar");
|
|
|
|
Assert.That(() => _ingredientStorageContract.GetElementByName("Salt"), Throws.TypeOf<ElementNotFoundException>());
|
|
}
|
|
|
|
[Test]
|
|
public void Try_AddElement_Test()
|
|
{
|
|
var ingredient = CreateModel(Guid.NewGuid().ToString(), "Sugar");
|
|
|
|
_ingredientStorageContract.AddElement(ingredient);
|
|
AssertElement(GetIngredientFromDatabase(ingredient.Id), ingredient);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
|
|
{
|
|
var ingredientId = Guid.NewGuid().ToString();
|
|
var ingredient = CreateModel(ingredientId, "Sugar");
|
|
InsertIngredientToDatabaseAndReturn(ingredientId, "Flour");
|
|
|
|
Assert.That(() => _ingredientStorageContract.AddElement(ingredient), Throws.TypeOf<StorageException>());
|
|
}
|
|
|
|
[Test]
|
|
public void Try_AddElement_WhenHaveRecordWithSameName_Test()
|
|
{
|
|
var ingredient = CreateModel(Guid.NewGuid().ToString(), "Sugar");
|
|
InsertIngredientToDatabaseAndReturn(Guid.NewGuid().ToString(), ingredient.Name);
|
|
|
|
Assert.That(() => _ingredientStorageContract.AddElement(ingredient), Throws.TypeOf<ElementExistsException>());
|
|
}
|
|
|
|
[Test]
|
|
public void Try_UpdateElement_Test()
|
|
{
|
|
var ingredientId = Guid.NewGuid().ToString();
|
|
var ingredient = CreateModel(ingredientId, "Sugar");
|
|
InsertIngredientToDatabaseAndReturn(ingredientId, "Flour");
|
|
|
|
_ingredientStorageContract.UpdateElement(ingredient);
|
|
AssertElement(GetIngredientFromDatabase(ingredient.Id), ingredient);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_UpdateElement_WhenNoRecordWithThisId_Test()
|
|
{
|
|
var ingredient = CreateModel(Guid.NewGuid().ToString(), "Sugar");
|
|
|
|
Assert.That(() => _ingredientStorageContract.UpdateElement(ingredient), Throws.TypeOf<ElementNotFoundException>());
|
|
}
|
|
|
|
[Test]
|
|
public void Try_UpdateElement_WhenHaveRecordWithSameName_Test()
|
|
{
|
|
var ingredientId = Guid.NewGuid().ToString();
|
|
var ingredient = CreateModel(ingredientId, "Sugar");
|
|
InsertIngredientToDatabaseAndReturn(ingredientId, "Flour");
|
|
InsertIngredientToDatabaseAndReturn(Guid.NewGuid().ToString(), "Sugar");
|
|
|
|
Assert.That(() => _ingredientStorageContract.UpdateElement(ingredient), Throws.TypeOf<ElementExistsException>());
|
|
}
|
|
|
|
[Test]
|
|
public void Try_DeleteElement_Test()
|
|
{
|
|
var ingredient = InsertIngredientToDatabaseAndReturn(Guid.NewGuid().ToString(), "Sugar");
|
|
|
|
_ingredientStorageContract.DeleteElement(ingredient.Id);
|
|
var element = GetIngredientFromDatabase(ingredient.Id);
|
|
|
|
Assert.That(element, Is.Null);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_DeleteElement_WhenNoRecordWithThisId_Test()
|
|
{
|
|
Assert.That(() => _ingredientStorageContract.DeleteElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
|
}
|
|
|
|
private Ingredient InsertIngredientToDatabaseAndReturn(string id, string name, string unit = "kg", decimal cost = 1.5m)
|
|
{
|
|
var ingredient = new Ingredient { Id = id, Name = name, Unit = unit, Cost = cost };
|
|
CandyHouseDbContext.Ingredients.Add(ingredient);
|
|
CandyHouseDbContext.SaveChanges();
|
|
return ingredient;
|
|
}
|
|
|
|
private static void AssertElement(IngredientDataModel? actual, Ingredient expected)
|
|
{
|
|
Assert.That(actual, Is.Not.Null);
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
|
Assert.That(actual.Name, Is.EqualTo(expected.Name));
|
|
Assert.That(actual.Unit, Is.EqualTo(expected.Unit));
|
|
Assert.That(actual.Cost, Is.EqualTo(expected.Cost));
|
|
});
|
|
}
|
|
|
|
private static IngredientDataModel CreateModel(string id, string name, string unit = "kg", decimal cost = 1.5m)
|
|
{
|
|
return new IngredientDataModel(id, name, unit, cost);
|
|
}
|
|
|
|
private Ingredient? GetIngredientFromDatabase(string id)
|
|
{
|
|
return CandyHouseDbContext.Ingredients.FirstOrDefault(x => x.Id == id);
|
|
}
|
|
|
|
private static void AssertElement(Ingredient? actual, IngredientDataModel expected)
|
|
{
|
|
Assert.That(actual, Is.Not.Null);
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
|
Assert.That(actual.Name, Is.EqualTo(expected.Name));
|
|
Assert.That(actual.Unit, Is.EqualTo(expected.Unit));
|
|
Assert.That(actual.Cost, Is.EqualTo(expected.Cost));
|
|
});
|
|
}
|
|
}
|
|
} |