Compare commits

...

7 Commits

20 changed files with 890 additions and 8 deletions

View File

@ -1,7 +0,0 @@
namespace SeniorPomidorContracts
{
public class Class1
{
}
}

View File

@ -0,0 +1,62 @@
using SeniorPomidorContracts.Enums;
using SeniorPomidorContracts.Extensions;
using SeniorPomidorContracts.Infrastructure;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace SeniorPomidorContracts.DataModels;
public class CropDataModel(string id, string harvestId, string cropName, double price,
double? prevPrice, double? prevPrevPrice, CropType cropType, bool isActual,
DateTime replenishmentDate, bool isDeleted) : IValidation
{
public string Id { get; private set; } = id;
public string HarvestId { get; private set; } = harvestId;
public string CropName { get; private set; } = cropName;
public double Price { get; private set; } = price;
public double? PrevPrice { get; private set; } = prevPrice;
public double? PrevPrevPrice { get; private set; } = prevPrevPrice;
public CropType CropType { get; private set; } = cropType;
public bool IsActual { get; private set; } = isActual;
public DateTime ReplenishmentDate { get; private set; } = replenishmentDate;
public bool IsDeleted { get; private set; } = isDeleted;
public void Validate()
{
if (Id.IsEmpty())
throw new ValidationException("Field Id is empty");
if (!Id.IsGuid())
throw new ValidationException("The value in the field Id is not a unique identifier");
if (HarvestId.IsEmpty())
throw new ValidationException("Field HarvestId is empty");
if (!HarvestId.IsGuid())
throw new ValidationException("The value in the field HarvestId is not a unique identifier");
if (CropName.IsEmpty())
throw new ValidationException("Field CropName is empty");
if (CropType == CropType.None)
throw new ValidationException("Field CropType is empty");
if (Price <= 0)
throw new ValidationException("Field Price is less than or equal to 0");
}
}

View File

@ -0,0 +1,42 @@
using SeniorPomidorContracts.Extensions;
using SeniorPomidorContracts.Infrastructure;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SeniorPomidorContracts.DataModels;
public class HarvestDataModel(string id, string cropId, string harvestName, int harvestAmount) : IValidation
{
public string Id { get; private set; } = id;
public string CropId { get; private set; } = cropId;
public string HarvestName { get; private set; } = harvestName;
public int HarvestAmount { get; private set; } = harvestAmount;
public void Validate()
{
if (Id.IsEmpty())
throw new ValidationException("Field Id is empty");
if (!Id.IsGuid())
throw new ValidationException("The value in the field Id is not a unique identifier");
if (CropId.IsEmpty())
throw new ValidationException("Field CropId is empty");
if (!CropId.IsGuid())
throw new ValidationException("The value in the field CropId is not a unique identifier");
if (HarvestName.IsEmpty())
throw new ValidationException("Field HarvestName is empty");
if (HarvestAmount < 0)
throw new ValidationException("Field HarvestAmount is less than 0");
}
}

View File

@ -0,0 +1,46 @@
using SeniorPomidorContracts.Extensions;
using SeniorPomidorContracts.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;
using System.Xml;
using System.ComponentModel.DataAnnotations;
namespace SeniorPomidorContracts.DataModels;
public class SupplierDataModel(string id, string fio, string email, double discountSize) : IValidation
{
public string Id { get; private set; } = id;
public string FIO { get; private set; } = fio;
public string Email { get; private set; } = email;
public double DiscountSize { get; private set; } = discountSize;
public void Validate()
{
if (Id.IsEmpty())
throw new ValidationException("Field Id is empty");
if (!Id.IsGuid())
throw new ValidationException("The value in the field Id is not a unique identifier");
if (FIO.IsEmpty())
throw new ValidationException("Field FIO is empty");
if (Email.IsEmpty())
throw new ValidationException("Field Email is empty");
if (!Regex.IsMatch(Email, @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"))
throw new ValidationException("Field Email is not a email");
if (DiscountSize < 0)
throw new ValidationException("Field DiscountSize is less than zero");
}
}

View File

@ -0,0 +1,32 @@
using SeniorPomidorContracts.Extensions;
using SeniorPomidorContracts.Infrastructure;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SeniorPomidorContracts.DataModels;
public class SupplierHistoryDataModel(string supplierId, string oldName) : IValidation
{
public string SupplierId { get; private set; } = supplierId;
public string OldName { get; private set; } = oldName;
public DateTime ChangeTime { get; private set; } = DateTime.UtcNow;
public void Validate()
{
if (SupplierId.IsEmpty())
throw new ValidationException("Field SupplierId is empty");
if (!SupplierId.IsGuid())
throw new ValidationException("The value in the field SupplierId is not a unique identifier");
if (OldName.IsEmpty())
throw new ValidationException("Field OldName is empty");
}
}

View File

@ -0,0 +1,37 @@
using SeniorPomidorContracts.Extensions;
using SeniorPomidorContracts.Infrastructure;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SeniorPomidorContracts.DataModels;
public class SupplyCropDataModel(string supplyId, string cropId, int amount) : IValidation
{
public string SupplyId { get; private set; } = supplyId;
public string CropId { get; private set; } = cropId;
public int Amount { get; private set; } = amount;
public void Validate()
{
if (SupplyId.IsEmpty())
throw new ValidationException("Field SupplyId is empty");
if (!SupplyId.IsGuid())
throw new ValidationException("The value in the field SupplyId is not a unique identifier");
if (CropId.IsEmpty())
throw new ValidationException("Field CropId is empty");
if (!CropId.IsGuid())
throw new ValidationException("The value in the field CropId is not a unique identifier");
if (Amount <= 0)
throw new ValidationException("Field Amount is less than or equal to 0");
}
}

View File

@ -0,0 +1,49 @@
using SeniorPomidorContracts.Enums;
using SeniorPomidorContracts.Extensions;
using SeniorPomidorContracts.Infrastructure;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SeniorPomidorContracts.DataModels;
public class SupplyDataModel(string id, string supplierId, int sum, DateTime supplyDate,
bool isCancel, List<SupplyCropDataModel> crops, DiscountType discountType, double discount) : IValidation
{
public string Id { get; private set; } = id;
public string? SupplierId { get; private set; } = supplierId;
public int Sum { get; private set; } = sum;
public DateTime SupplyDate { get; private set; } = supplyDate;
public List<SupplyCropDataModel> Crops { get; private set; } = crops;
public DiscountType DiscountType { get; private set; } = discountType;
public double Discount { get; private set; } = discount;
public bool IsCancel { get; private set; } = isCancel;
public void Validate()
{
if (Id.IsEmpty())
throw new ValidationException("Field Id is empty");
if (!Id.IsGuid())
throw new ValidationException("The value in the field Id is not a unique identifier");
if (!SupplierId?.IsGuid() ?? !SupplierId?.IsEmpty() ?? false)
throw new ValidationException("The value in the field SupplierId is not a unique identifier");
if (Sum <= 0)
throw new ValidationException("Field Sum is less than or equal to 0");
if ((Crops?.Count ?? 0) == 0)
throw new ValidationException("The sale must include crops");
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SeniorPomidorContracts.Enums;
public enum CropType
{
None = 0,
Fruit = 1,
Vegetable = 2,
Berry = 3
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SeniorPomidorContracts.Enums;
[Flags]
public enum DiscountType
{
None = 0,
OnSale = 1,
RegularSupplier = 2,
Certificate = 4
}

View File

@ -0,0 +1,9 @@
namespace SeniorPomidorContracts.Exceptions;
public class ValidationException : Exception
{
public ValidationException() : base()
{
}
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SeniorPomidorContracts.Extensions;
public static class StringExtension
{
public static bool IsEmpty(this string str) => string.IsNullOrEmpty(str);
public static bool IsGuid(this string str) => Guid.TryParse(str, out _);
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SeniorPomidorContracts.Infrastructure;
public interface IValidation
{
void Validate();
}

View File

@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.10.35013.160
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SeniorPomidorContracts", "SeniorPomidorContracts.csproj", "{926128BD-C8C0-48CA-92A8-A9EE4269D0BE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SeniorPomidorContracts", "SeniorPomidorContracts.csproj", "{926128BD-C8C0-48CA-92A8-A9EE4269D0BE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SeniorPomidorTests", "..\SeniorPomidorTests\SeniorPomidorTests.csproj", "{449570AB-03BD-4337-BE60-02117C731B12}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -15,6 +17,10 @@ Global
{926128BD-C8C0-48CA-92A8-A9EE4269D0BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{926128BD-C8C0-48CA-92A8-A9EE4269D0BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{926128BD-C8C0-48CA-92A8-A9EE4269D0BE}.Release|Any CPU.Build.0 = Release|Any CPU
{449570AB-03BD-4337-BE60-02117C731B12}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{449570AB-03BD-4337-BE60-02117C731B12}.Debug|Any CPU.Build.0 = Debug|Any CPU
{449570AB-03BD-4337-BE60-02117C731B12}.Release|Any CPU.ActiveCfg = Release|Any CPU
{449570AB-03BD-4337-BE60-02117C731B12}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,118 @@
using SeniorPomidorContracts.DataModels;
using SeniorPomidorContracts.Enums;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SeniorPomidorTests.DataModelsTests;
[TestFixture]
internal class CropDataModelsTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var crop = CreateDataModel(null, Guid.NewGuid().ToString(), "name",
10, CropType.Berry, true, DateTime.UtcNow, false);
Assert.That(() => crop.Validate(), Throws.TypeOf<ValidationException>());
crop = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), "name",
10, CropType.Berry, true, DateTime.UtcNow, false);
Assert.That(() => crop.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuid()
{
var crop = CreateDataModel("Id", Guid.NewGuid().ToString(), "name",
10, CropType.Berry, true, DateTime.UtcNow, false);
Assert.That(() => crop.Validate(), Throws.TypeOf<ValidationException>());
}
public void HarvestIdIsNullOrEmptyTest()
{
var crop = CreateDataModel(Guid.NewGuid().ToString(), null, "name",
10, CropType.Berry, true, DateTime.UtcNow, false);
Assert.That(() => crop.Validate(), Throws.TypeOf<ValidationException>());
crop = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, "name",
10, CropType.Berry, true, DateTime.UtcNow, false);
Assert.That(() => crop.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void HarvestIdIsNotGuid()
{
var crop = CreateDataModel(Guid.NewGuid().ToString(), "Id", "name",
10, CropType.Berry, true, DateTime.UtcNow, false);
Assert.That(() => crop.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void CropNameIsEmptyTest()
{
var crop = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null,
10, CropType.Berry, true, DateTime.UtcNow, false);
Assert.That(() => crop.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PriceIsLessOrZeroTest()
{
var crop = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name",
0, CropType.Berry, true, DateTime.UtcNow, false);
Assert.That(() => crop.Validate(), Throws.TypeOf<ValidationException>());
crop = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name",
-10, CropType.Berry, true, DateTime.UtcNow, false);
Assert.That(() => crop.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void CropTypeIsNoneTest()
{
var crop = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name",
10, CropType.None, true, DateTime.UtcNow, false);
Assert.That(() => crop.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsAreCorrectTest()
{
var cropId = Guid.NewGuid().ToString();
var harvestId = Guid.NewGuid().ToString();
var cropName = "name";
var price = 10;
var prevPrice = 9;
var prevPrevPrice = 8;
var cropType = CropType.Berry;
var isActual = true;
var replenishmentDate = DateTime.UtcNow.AddDays(-1);
var isDeleted = false;
var crop = CreateDataModel(cropId, harvestId, cropName, price, cropType, isActual,
replenishmentDate, isDeleted, prevPrice, prevPrevPrice);
Assert.That(() => crop.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(crop.Id, Is.EqualTo(cropId));
Assert.That(crop.HarvestId, Is.EqualTo(harvestId));
Assert.That(crop.CropName, Is.EqualTo(cropName));
Assert.That(crop.CropType, Is.EqualTo(cropType));
Assert.That(crop.Price, Is.EqualTo(price));
Assert.That(crop.PrevPrice, Is.EqualTo(prevPrice));
Assert.That(crop.PrevPrevPrice, Is.EqualTo(prevPrevPrice));
Assert.That(crop.IsActual, Is.EqualTo(isActual));
Assert.That(crop.ReplenishmentDate, Is.EqualTo(replenishmentDate));
Assert.That(crop.IsDeleted, Is.EqualTo(isDeleted));
});
}
private static CropDataModel CreateDataModel(string? id, string? harvestId, string? cropName,
double price, CropType cropType, bool isActual, DateTime replenishmentDate, bool isDeleted,
double? prevPrice = null, double? prevPrevPrice = null) => new (id, harvestId, cropName,
price, prevPrice, prevPrevPrice, cropType, isActual, replenishmentDate, isDeleted);
}

View File

@ -0,0 +1,85 @@
using SeniorPomidorContracts.DataModels;
using SeniorPomidorContracts.Enums;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SeniorPomidorTests.DataModelsTests;
[TestFixture]
internal class HarvestDataModelsTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var harvest = CreateDataModel(null, Guid.NewGuid().ToString(), "name", 10);
Assert.That(() => harvest.Validate(), Throws.TypeOf<ValidationException>());
harvest = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), "name", 10);
Assert.That(() => harvest.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuid()
{
var harvest = CreateDataModel("Id", Guid.NewGuid().ToString(), "name", 10);
Assert.That(() => harvest.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void CropIdIsNullOrEmptyTest()
{
var harvest = CreateDataModel(Guid.NewGuid().ToString(), null, "name", 10);
Assert.That(() => harvest.Validate(), Throws.TypeOf<ValidationException>());
harvest = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), "name", 10);
Assert.That(() => harvest.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void CropIdIsNotGuid()
{
var harvest = CreateDataModel(Guid.NewGuid().ToString(), "CropId", "name", 10);
Assert.That(() => harvest.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void HarvestNameIsEmptyTest()
{
var harvest = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10);
Assert.That(() => harvest.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AmountIsLessZero()
{
var harvest = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name", -10);
Assert.That(() => harvest.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsAreCorrect()
{
var id = Guid.NewGuid().ToString();
var cropId = Guid.NewGuid().ToString();
var harvestName = "name";
var harvestAmount = 10;
var harvest = CreateDataModel(id, cropId, harvestName, harvestAmount);
Assert.That(() => harvest.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(harvest.Id, Is.EqualTo(id));
Assert.That(harvest.CropId, Is.EqualTo(cropId));
Assert.That(harvest.HarvestName, Is.EqualTo(harvestName));
Assert.That(harvest.HarvestAmount, Is.EqualTo(harvestAmount));
});
}
private static HarvestDataModel CreateDataModel(string? id, string? cropId,
string? harvestName, int harvestAmount) => new(id, cropId, harvestName, harvestAmount);
}

View File

@ -0,0 +1,85 @@
using SeniorPomidorContracts.DataModels;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SeniorPomidorTests.DataModelsTests;
[TestFixture]
internal class SupplierDataModelsTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var supplier = CreateDataModel(null, "Барышев Дмитрий Алексеевич", "email", 10);
Assert.That(() => supplier.Validate(), Throws.TypeOf<ValidationException>());
supplier = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), "a@yandex.ru", 10);
Assert.That(() => supplier.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuid()
{
var supplier = CreateDataModel("Id", "Барышев Дмитрий Алексеевич", "a@yandex.ru", 10);
Assert.That(() => supplier.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void FIOIsNullOrEmptyTest()
{
var supplier = CreateDataModel(Guid.NewGuid().ToString(), null, "a@yandex.ru", 10);
Assert.That(() => supplier.Validate(), Throws.TypeOf<ValidationException>());
supplier = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, "a@yandex.ru", 10);
Assert.That(() => supplier.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void EmailIsNullOrEmptyTest()
{
var supplier = CreateDataModel(Guid.NewGuid().ToString(), "Барышев Дмитрий Алексеевич", null, 10);
Assert.That(() => supplier.Validate(), Throws.TypeOf<ValidationException>());
supplier = CreateDataModel(Guid.NewGuid().ToString(), "Барышев Дмитрий Алексеевич", string.Empty, 10);
Assert.That(() => supplier.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void EmailIsIncorrectTest()
{
var supplier = CreateDataModel(Guid.NewGuid().ToString(), "Барышев Дмитрий Алексеевич", "i.ru", 10);
Assert.That(() => supplier.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void DiscountIsLessZero()
{
var supplier = CreateDataModel(Guid.NewGuid().ToString(), "Барышев Дмитрий Алексеевич", "a@yandex.ru", -10);
Assert.That(() => supplier.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsAreCorrect()
{
var id = Guid.NewGuid().ToString();
var fio = "Барышев Дмитрий Алексеевич";
var email = "senior_pomidor@yandex.ru";
var discountSize = 10;
var supplier = CreateDataModel(id, fio, email, discountSize);
Assert.That(() => supplier.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(supplier.Id, Is.EqualTo(id));
Assert.That(supplier.FIO, Is.EqualTo(fio));
Assert.That(supplier.Email, Is.EqualTo(email));
Assert.That(supplier.DiscountSize, Is.EqualTo(discountSize));
});
}
private static SupplierDataModel CreateDataModel(string? id, string? fio,
string? phoneNumber, int discountSize) => new(id, fio, phoneNumber, discountSize);
}

View File

@ -0,0 +1,58 @@
using SeniorPomidorContracts.DataModels;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SeniorPomidorTests.DataModelsTests;
[TestFixture]
internal class SupplierHistoryDataModelsTests
{
[Test]
public void SuppplierIdIsNullOrEmptyTest()
{
var supplierHistory = CreateDataModel(null, "name");
Assert.That(() => supplierHistory.Validate(), Throws.TypeOf<ValidationException>());
supplierHistory = CreateDataModel(string.Empty, "name");
Assert.That(() => supplierHistory.Validate(),Throws.TypeOf<ValidationException>());
}
[Test]
public void SuppplierIdIsNotGuid()
{
var supplierHistory = CreateDataModel("Id", "name");
Assert.That(() => supplierHistory.Validate(), Throws.TypeOf<ValidationException>());
}
public void OldNameNullOrEmptyTest()
{
var supplierHistory = CreateDataModel(Guid.NewGuid().ToString(), null);
Assert.That(() => supplierHistory.Validate(), Throws.TypeOf<ValidationException>());
supplierHistory = CreateDataModel(Guid.NewGuid().ToString(), string.Empty);
Assert.That(() => supplierHistory.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var supplierId = Guid.NewGuid().ToString();
var oldName = "Name";
var supplyHistory = CreateDataModel(supplierId, oldName);
Assert.That(() => supplyHistory.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(supplyHistory.SupplierId, Is.EqualTo(supplierId));
Assert.That(supplyHistory.OldName, Is.EqualTo(oldName));
Assert.That(supplyHistory.ChangeTime, Is.LessThan(DateTime.UtcNow));
Assert.That(supplyHistory.ChangeTime, Is.GreaterThan(DateTime.UtcNow.AddMinutes(-1)));
});
}
private static SupplierHistoryDataModel CreateDataModel(string? supplierId,
string? oldName) => new(supplierId, oldName);
}

View File

@ -0,0 +1,75 @@
using SeniorPomidorContracts.DataModels;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SeniorPomidorTests.DataModelsTests;
[TestFixture]
internal class SupplyCropDataModelsTests
{
[Test]
public void SupplyIdIsNullOrEmptyTest()
{
var supplyCrop = CreateDataModel(null, Guid.NewGuid().ToString(), 10);
Assert.That(() => supplyCrop.Validate(), Throws.TypeOf<ValidationException>());
supplyCrop = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 10);
Assert.That(() => supplyCrop.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void SupplyIdIsNotGuid()
{
var supplyCrop = CreateDataModel("supplyId", Guid.NewGuid().ToString(), 10);
Assert.That(() => supplyCrop.Validate(), Throws.TypeOf<ValidationException>());
}
public void CropIdIsNullOrEmptyTest()
{
var supplyCrop = CreateDataModel(Guid.NewGuid().ToString(), null, 10);
Assert.That(() => supplyCrop.Validate(), Throws.TypeOf<ValidationException>());
supplyCrop = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, 10);
Assert.That(() => supplyCrop.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void CropIdIsNotGuid()
{
var supplyCrop = CreateDataModel(Guid.NewGuid().ToString(), "CropId", 10);
Assert.That(() => supplyCrop.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AmountIsLessOrZeroTest()
{
var supplyCrop = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0);
Assert.That(() => supplyCrop.Validate(), Throws.TypeOf<ValidationException>());
supplyCrop = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -10);
Assert.That(() => supplyCrop.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsAreCorrect()
{
var supplyId = Guid.NewGuid().ToString();
var cropId = Guid.NewGuid().ToString();
var amount = 10;
var supplyCrop = CreateDataModel(supplyId, cropId, amount);
Assert.That(() => supplyCrop.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(supplyCrop.SupplyId, Is.EqualTo(supplyId));
Assert.That(supplyCrop.CropId, Is.EqualTo(cropId));
Assert.That(supplyCrop.Amount, Is.EqualTo(amount));
});
}
public static SupplyCropDataModel CreateDataModel(string? supplyId, string? cropId, int amount)
=> new(supplyId, cropId, amount);
}

View File

@ -0,0 +1,101 @@
using SeniorPomidorContracts.DataModels;
using SeniorPomidorContracts.Enums;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SeniorPomidorTests.DataModelsTests;
[TestFixture]
internal class SupplyDataModelsTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var supply = CreateDataModel(null, Guid.NewGuid().ToString(), 10, DateTime.UtcNow, false,
CreateSubDataModel(), DiscountType.OnSale, 10);
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
supply = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 10, DateTime.UtcNow, false,
CreateSubDataModel(), DiscountType.OnSale, 10);
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var supply = CreateDataModel("Id", Guid.NewGuid().ToString(), 10, DateTime.UtcNow, false,
CreateSubDataModel(), DiscountType.OnSale, 10);
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void SupplierIdIsNotGuidTest()
{
var supply = CreateDataModel(Guid.NewGuid().ToString(),"SupplierId", 10, DateTime.UtcNow, false,
CreateSubDataModel(), DiscountType.OnSale, 10);
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void SumIsLessOrZeroTest()
{
var supply = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0,
DateTime.UtcNow, false, CreateSubDataModel(), DiscountType.OnSale, 10);
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
supply = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -10,
DateTime.UtcNow, false, CreateSubDataModel(), DiscountType.OnSale, 10);
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void CropsIsNullOrEmptyTest()
{
var supply = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10,
DateTime.UtcNow, false, null, DiscountType.OnSale, 10);
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
supply = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10,
DateTime.UtcNow, false, [], DiscountType.OnSale, 10);
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsAreCorrect()
{
var id = Guid.NewGuid().ToString();
var supplierId = Guid.NewGuid().ToString();
var sum = 10;
var supplyDate = DateTime.UtcNow.AddDays(-1);
var isCancel = false;
var crops = CreateSubDataModel();
var discountType = DiscountType.OnSale;
var discount = 10;
var supply = CreateDataModel(id, supplierId, sum, supplyDate,
isCancel, crops, discountType, discount);
Assert.That(() => supply.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(supply.Id, Is.EqualTo(id));
Assert.That(supply.SupplierId, Is.EqualTo(supplierId));
Assert.That(supply.Sum, Is.EqualTo(sum));
Assert.That(supply.SupplyDate, Is.EqualTo(supplyDate));
Assert.That(supply.IsCancel, Is.EqualTo(isCancel));
Assert.That(supply.Crops, Is.EquivalentTo(crops));
Assert.That(supply.DiscountType, Is.EqualTo(discountType));
Assert.That(supply.Discount, Is.EqualTo(discount));
});
}
private static SupplyDataModel CreateDataModel(string? id, string? supplierId, int sum,
DateTime supplyDate, bool isCancel, List<SupplyCropDataModel>? crops, DiscountType discountType,
double discount) => new(id, supplierId, sum, supplyDate, isCancel, crops, discountType, discount);
private static List<SupplyCropDataModel> CreateSubDataModel() =>
[new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10)];
}

View File

@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SeniorPomidorContracts\SeniorPomidorContracts.csproj" />
</ItemGroup>
<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>
</Project>