final lab1
This commit is contained in:
parent
9be273db7b
commit
36af9a9fa1
30
Romashki/RomashkiContract/DataModels/DiscountDataModel.cs
Normal file
30
Romashki/RomashkiContract/DataModels/DiscountDataModel.cs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using RomashkiContracts.Exceptions;
|
||||||
|
using RomashkiContracts.Extensions;
|
||||||
|
using RomashkiContracts.Infrastructure;
|
||||||
|
|
||||||
|
namespace RomashkiContract.DataModels;
|
||||||
|
|
||||||
|
public class DiscountDataModel(string buyerId, DateTime discountDate, double discountSize) : IValidation
|
||||||
|
{
|
||||||
|
public string BuyerId{ get; private set; } = buyerId;
|
||||||
|
public DateTime DiscountDate { get; private set; } = discountDate;
|
||||||
|
public double DiscountSize { get; private set; } = discountSize;
|
||||||
|
public void Validate()
|
||||||
|
{
|
||||||
|
if (BuyerId.IsEmpty())
|
||||||
|
throw new ValidationException("Field BuyerId is empty");
|
||||||
|
if (!BuyerId.IsGuid())
|
||||||
|
throw new ValidationException("The value in the field BuyerId is not a unique identifier");
|
||||||
|
if (discountSize < 0)
|
||||||
|
throw new ValidationException("Field DiscountSize is less than or equal to 0");
|
||||||
|
if (discountSize > 100)
|
||||||
|
throw new ValidationException("Field DiscountSize is more than 100");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -9,8 +9,8 @@ namespace RomashkiContracts.Enums;
|
|||||||
public enum ProductType
|
public enum ProductType
|
||||||
{
|
{
|
||||||
None = 0,
|
None = 0,
|
||||||
HomemadeFlowers = 1,
|
HomeFlower = 1,
|
||||||
BeautifulFlowers = 2,
|
Flower = 2,
|
||||||
Accessory = 3
|
Accessory = 3
|
||||||
}
|
}
|
||||||
|
|
||||||
|
53
Romashki/RomashkiTests/DiscountDataModelTests.cs
Normal file
53
Romashki/RomashkiTests/DiscountDataModelTests.cs
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using RomashkiContract.DataModels;
|
||||||
|
using RomashkiContracts.Exceptions;
|
||||||
|
|
||||||
|
namespace RomashkiTests;
|
||||||
|
|
||||||
|
internal class DiscountDataModelTests
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
public void BuyerIdIsEmptyTest()
|
||||||
|
{
|
||||||
|
var discount = CreateDataModel(null, DateTime.Now, 10);
|
||||||
|
Assert.That(() => discount.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
discount = CreateDataModel(string.Empty, DateTime.Now, 10);
|
||||||
|
Assert.That(() => discount.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void BuyerIdIsNotGuidTest()
|
||||||
|
{
|
||||||
|
var discount = CreateDataModel("buyerId", DateTime.Now, 10);
|
||||||
|
Assert.That(() => discount.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void DiscountIsLessOrMoreTest()
|
||||||
|
{
|
||||||
|
var discount = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, -1);
|
||||||
|
Assert.That(() => discount.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
discount = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, 101);
|
||||||
|
Assert.That(() => discount.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void AllFieldsIsCorrectTest()
|
||||||
|
{
|
||||||
|
var buyerId = Guid.NewGuid().ToString();
|
||||||
|
var discountDate = DateTime.Now.AddDays(-3).AddMinutes(-5);
|
||||||
|
var discountSize = 10;
|
||||||
|
var discount = CreateDataModel(buyerId, discountDate, discountSize);
|
||||||
|
Assert.That(() => discount.Validate(), Throws.Nothing);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(discount.BuyerId, Is.EqualTo(buyerId));
|
||||||
|
Assert.That(discount.DiscountDate, Is.EqualTo(discountDate));
|
||||||
|
Assert.That(discount.DiscountSize, Is.EqualTo(discountSize));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
private static DiscountDataModel CreateDataModel(string? buyerId, DateTime discountDate, double discountSize) =>
|
||||||
|
new(buyerId, discountDate, discountSize);
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user