lab1 refactor
This commit is contained in:
parent
825133bed3
commit
0061693eff
@ -1,3 +1,5 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using CandyHouseBase.Exceptions;
|
using CandyHouseBase.Exceptions;
|
||||||
using CandyHouseBase.Infrastructure;
|
using CandyHouseBase.Infrastructure;
|
||||||
using CandyHouseBase.Extensions;
|
using CandyHouseBase.Extensions;
|
||||||
@ -10,13 +12,17 @@ namespace CandyHouseBase.DataModels
|
|||||||
public string FIO { get; private set; }
|
public string FIO { get; private set; }
|
||||||
public string Position { get; private set; }
|
public string Position { get; private set; }
|
||||||
public decimal BonusCoefficient { get; private set; }
|
public decimal BonusCoefficient { get; private set; }
|
||||||
|
public List<ProductDataModel> ProductsItems { get; private set; }
|
||||||
|
|
||||||
public PekarDataModel(string id, string fio, string position, decimal bonusCoefficient)
|
|
||||||
|
public PekarDataModel(string id, string fio, string position, decimal bonusCoefficient,
|
||||||
|
List<ProductDataModel> productsItems)
|
||||||
{
|
{
|
||||||
Id = id;
|
Id = id;
|
||||||
FIO = fio;
|
FIO = fio;
|
||||||
Position = position;
|
Position = position;
|
||||||
BonusCoefficient = bonusCoefficient;
|
BonusCoefficient = bonusCoefficient;
|
||||||
|
ProductsItems = productsItems;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Validate()
|
public void Validate()
|
||||||
@ -24,6 +30,10 @@ namespace CandyHouseBase.DataModels
|
|||||||
if (Id.IsEmpty()) throw new ValidationException("Field Id is empty");
|
if (Id.IsEmpty()) throw new ValidationException("Field Id is empty");
|
||||||
if (!Id.IsGuid()) throw new ValidationException("Id must be a GUID");
|
if (!Id.IsGuid()) throw new ValidationException("Id must be a GUID");
|
||||||
if (FIO.IsEmpty()) throw new ValidationException("Field FIO is empty");
|
if (FIO.IsEmpty()) throw new ValidationException("Field FIO is empty");
|
||||||
|
var fioPattern = @"^[A-Za-zА-Яа-яЁё\s\-]+$";
|
||||||
|
if (!Regex.IsMatch(FIO, fioPattern))
|
||||||
|
throw new ValidationException("FIO contains invalid characters");
|
||||||
|
|
||||||
if (Position.IsEmpty()) throw new ValidationException("Field Position is empty");
|
if (Position.IsEmpty()) throw new ValidationException("Field Position is empty");
|
||||||
if (!Position.IsGuid()) throw new ValidationException("Field must be a GUID");
|
if (!Position.IsGuid()) throw new ValidationException("Field must be a GUID");
|
||||||
if (BonusCoefficient <= 0) throw new ValidationException("BonusCoefficient must be positive");
|
if (BonusCoefficient <= 0) throw new ValidationException("BonusCoefficient must be positive");
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using CandyHouseBase.Enums;
|
using CandyHouseBase.Enums;
|
||||||
using CandyHouseBase.Exceptions;
|
using CandyHouseBase.Exceptions;
|
||||||
using CandyHouseBase.Extensions;
|
using CandyHouseBase.Extensions;
|
||||||
@ -23,6 +24,10 @@ namespace CandyHouseBase.DataModels
|
|||||||
{
|
{
|
||||||
if (Id.IsEmpty()) throw new ValidationException("Field Id is empty");
|
if (Id.IsEmpty()) throw new ValidationException("Field Id is empty");
|
||||||
if (!Id.IsGuid()) throw new ValidationException("Id must be a GUID");
|
if (!Id.IsGuid()) throw new ValidationException("Id must be a GUID");
|
||||||
|
var titlePattern = @"^[A-Za-zА-Яа-яЁё\s\-]+$";
|
||||||
|
if (!Regex.IsMatch(Title, titlePattern))
|
||||||
|
throw new ValidationException("FIO contains invalid characters");
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(Title)) throw new ValidationException("Field Title is empty");
|
if (string.IsNullOrEmpty(Title)) throw new ValidationException("Field Title is empty");
|
||||||
if (!Enum.IsDefined(typeof(PositionType), Type)) throw new ValidationException("Invalid PositionType");
|
if (!Enum.IsDefined(typeof(PositionType), Type)) throw new ValidationException("Invalid PositionType");
|
||||||
}
|
}
|
||||||
|
@ -12,17 +12,15 @@ namespace CandyHouseBase.DataModels
|
|||||||
public DateTime Period { get; private set; }
|
public DateTime Period { get; private set; }
|
||||||
public decimal BaseRate { get; private set; }
|
public decimal BaseRate { get; private set; }
|
||||||
public decimal BonusRate { get; private set; }
|
public decimal BonusRate { get; private set; }
|
||||||
public int TotalQuantity { get; private set; }
|
|
||||||
public decimal TotalSalary { get; private set; }
|
public decimal TotalSalary { get; private set; }
|
||||||
|
|
||||||
public SalaryDataModel(string id, string pekarId, DateTime period, decimal baseRate, decimal bonusRate, int totalQuantity, decimal totalSalary)
|
public SalaryDataModel(string id, string pekarId, DateTime period, decimal baseRate, decimal bonusRate, decimal totalSalary)
|
||||||
{
|
{
|
||||||
Id = id;
|
Id = id;
|
||||||
PekarId = pekarId;
|
PekarId = pekarId;
|
||||||
Period = period;
|
Period = period;
|
||||||
BaseRate = baseRate;
|
BaseRate = baseRate;
|
||||||
BonusRate = bonusRate;
|
BonusRate = bonusRate;
|
||||||
TotalQuantity = totalQuantity;
|
|
||||||
TotalSalary = totalSalary;
|
TotalSalary = totalSalary;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,7 +32,6 @@ namespace CandyHouseBase.DataModels
|
|||||||
if (!PekarId.IsGuid()) throw new ValidationException("PekarId must be a GUID");
|
if (!PekarId.IsGuid()) throw new ValidationException("PekarId must be a GUID");
|
||||||
if (BaseRate < 0) throw new ValidationException("BaseRate cannot be negative");
|
if (BaseRate < 0) throw new ValidationException("BaseRate cannot be negative");
|
||||||
if (BonusRate < 0) throw new ValidationException("BonusRate cannot be negative");
|
if (BonusRate < 0) throw new ValidationException("BonusRate cannot be negative");
|
||||||
if (TotalQuantity < 0) throw new ValidationException("TotalQuantity cannot be negative");
|
|
||||||
if (TotalSalary < 0) throw new ValidationException("TotalSalary cannot be negative");
|
if (TotalSalary < 0) throw new ValidationException("TotalSalary cannot be negative");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
using CandyHouseBase.DataModels;
|
using CandyHouseBase.DataModels;
|
||||||
using CandyHouseBase.Exceptions;
|
using CandyHouseBase.Exceptions;
|
||||||
@ -15,7 +16,7 @@ namespace CandyHouseTests.DataModelsTests
|
|||||||
var name = "John Doe";
|
var name = "John Doe";
|
||||||
var experience = Guid.NewGuid().ToString();
|
var experience = Guid.NewGuid().ToString();
|
||||||
|
|
||||||
var pekar = new PekarDataModel(id, name, experience, 0);
|
var pekar = new PekarDataModel(id, name, experience, 0, new List<ProductDataModel>());
|
||||||
|
|
||||||
Assert.AreEqual(id, pekar.Id);
|
Assert.AreEqual(id, pekar.Id);
|
||||||
Assert.AreEqual(name, pekar.FIO);
|
Assert.AreEqual(name, pekar.FIO);
|
||||||
@ -25,7 +26,8 @@ namespace CandyHouseTests.DataModelsTests
|
|||||||
[Test]
|
[Test]
|
||||||
public void Validate_ValidData_ShouldNotThrowException()
|
public void Validate_ValidData_ShouldNotThrowException()
|
||||||
{
|
{
|
||||||
var pekar = new PekarDataModel(Guid.NewGuid().ToString(), "John Doe", Guid.NewGuid().ToString(), 6);
|
var pekar = new PekarDataModel(Guid.NewGuid().ToString(), "John Doe", Guid.NewGuid().ToString(), 6,
|
||||||
|
new List<ProductDataModel>());
|
||||||
|
|
||||||
Assert.DoesNotThrow(() => pekar.Validate());
|
Assert.DoesNotThrow(() => pekar.Validate());
|
||||||
}
|
}
|
||||||
@ -33,7 +35,7 @@ namespace CandyHouseTests.DataModelsTests
|
|||||||
[Test]
|
[Test]
|
||||||
public void Validate_InvalidId_ShouldThrowValidationException()
|
public void Validate_InvalidId_ShouldThrowValidationException()
|
||||||
{
|
{
|
||||||
var pekar = new PekarDataModel("", "John Doe", Guid.NewGuid().ToString(), 0);
|
var pekar = new PekarDataModel("", "John Doe", Guid.NewGuid().ToString(), 0, new List<ProductDataModel>());
|
||||||
|
|
||||||
Assert.Throws<ValidationException>(() => pekar.Validate());
|
Assert.Throws<ValidationException>(() => pekar.Validate());
|
||||||
}
|
}
|
||||||
@ -41,7 +43,8 @@ namespace CandyHouseTests.DataModelsTests
|
|||||||
[Test]
|
[Test]
|
||||||
public void Validate_InvalidName_ShouldThrowValidationException()
|
public void Validate_InvalidName_ShouldThrowValidationException()
|
||||||
{
|
{
|
||||||
var pekar = new PekarDataModel(Guid.NewGuid().ToString(), "", Guid.NewGuid().ToString(), 0);
|
var pekar = new PekarDataModel(Guid.NewGuid().ToString(), "", Guid.NewGuid().ToString(), 0,
|
||||||
|
new List<ProductDataModel>());
|
||||||
|
|
||||||
Assert.Throws<ValidationException>(() => pekar.Validate());
|
Assert.Throws<ValidationException>(() => pekar.Validate());
|
||||||
}
|
}
|
||||||
@ -49,7 +52,8 @@ namespace CandyHouseTests.DataModelsTests
|
|||||||
[Test]
|
[Test]
|
||||||
public void Validate_NegativeExperience_ShouldThrowValidationException()
|
public void Validate_NegativeExperience_ShouldThrowValidationException()
|
||||||
{
|
{
|
||||||
var pekar = new PekarDataModel(Guid.NewGuid().ToString(), "John Doe", "some-invalidate", 0);
|
var pekar = new PekarDataModel(Guid.NewGuid().ToString(), "John Doe", "some-invalidate", 0,
|
||||||
|
new List<ProductDataModel>());
|
||||||
|
|
||||||
Assert.Throws<ValidationException>(() => pekar.Validate());
|
Assert.Throws<ValidationException>(() => pekar.Validate());
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,7 @@ namespace CandyHouseTests.DataModelsTests
|
|||||||
var type = PositionType.Cool;
|
var type = PositionType.Cool;
|
||||||
var title = "Manager";
|
var title = "Manager";
|
||||||
var item = new PositionDataModel(invalidId, type, title);
|
var item = new PositionDataModel(invalidId, type, title);
|
||||||
|
|
||||||
Assert.Throws<ValidationException>(() => item.Validate());
|
Assert.Throws<ValidationException>(() => item.Validate());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,7 +41,18 @@ namespace CandyHouseTests.DataModelsTests
|
|||||||
var type = PositionType.Cool;
|
var type = PositionType.Cool;
|
||||||
var invalidTitle = "";
|
var invalidTitle = "";
|
||||||
var item = new PositionDataModel(id, type, invalidTitle);
|
var item = new PositionDataModel(id, type, invalidTitle);
|
||||||
|
|
||||||
|
Assert.Throws<ValidationException>(() => item.Validate());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CreatePositionDataModel_TitleWithInvalidCharacters_ShouldThrowValidationException()
|
||||||
|
{
|
||||||
|
var id = Guid.NewGuid().ToString();
|
||||||
|
var type = PositionType.Cool;
|
||||||
|
var invalidTitle = "Manager123";
|
||||||
|
var item = new PositionDataModel(id, type, invalidTitle);
|
||||||
|
|
||||||
Assert.Throws<ValidationException>(() => item.Validate());
|
Assert.Throws<ValidationException>(() => item.Validate());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -75,5 +75,16 @@ namespace CandyHouseTests.DataModelsTests
|
|||||||
|
|
||||||
Assert.Throws<ValidationException>(() => product.Validate());
|
Assert.Throws<ValidationException>(() => product.Validate());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CreateProductDataModel_InvalidIngredientsNull_ShouldThrowArgumentException()
|
||||||
|
{
|
||||||
|
var id = Guid.NewGuid().ToString();
|
||||||
|
var name = "Candy";
|
||||||
|
var invalidDescription = "";
|
||||||
|
var product = new ProductDataModel(id, name, invalidDescription, null);
|
||||||
|
|
||||||
|
Assert.Throws<ValidationException>(() => product.Validate());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -15,17 +15,15 @@ namespace CandyHouseTests.DataModelsTests
|
|||||||
var period = new DateTime(2023, 10, 1);
|
var period = new DateTime(2023, 10, 1);
|
||||||
var baseRate = 1000m;
|
var baseRate = 1000m;
|
||||||
var bonusRate = 200m;
|
var bonusRate = 200m;
|
||||||
var totalQuantity = 50;
|
|
||||||
var totalSalary = 1200m;
|
var totalSalary = 1200m;
|
||||||
|
|
||||||
var salaryData = new SalaryDataModel(id, pekarId, period, baseRate, bonusRate, totalQuantity, totalSalary);
|
var salaryData = new SalaryDataModel(id, pekarId, period, baseRate, bonusRate, totalSalary);
|
||||||
|
|
||||||
Assert.AreEqual(id, salaryData.Id);
|
Assert.AreEqual(id, salaryData.Id);
|
||||||
Assert.AreEqual(pekarId, salaryData.PekarId);
|
Assert.AreEqual(pekarId, salaryData.PekarId);
|
||||||
Assert.AreEqual(period, salaryData.Period);
|
Assert.AreEqual(period, salaryData.Period);
|
||||||
Assert.AreEqual(baseRate, salaryData.BaseRate);
|
Assert.AreEqual(baseRate, salaryData.BaseRate);
|
||||||
Assert.AreEqual(bonusRate, salaryData.BonusRate);
|
Assert.AreEqual(bonusRate, salaryData.BonusRate);
|
||||||
Assert.AreEqual(totalQuantity, salaryData.TotalQuantity);
|
|
||||||
Assert.AreEqual(totalSalary, salaryData.TotalSalary);
|
Assert.AreEqual(totalSalary, salaryData.TotalSalary);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,9 +35,8 @@ namespace CandyHouseTests.DataModelsTests
|
|||||||
var period = new DateTime(2023, 10, 1);
|
var period = new DateTime(2023, 10, 1);
|
||||||
var baseRate = 1000m;
|
var baseRate = 1000m;
|
||||||
var bonusRate = 200m;
|
var bonusRate = 200m;
|
||||||
var totalQuantity = 50;
|
|
||||||
var totalSalary = 1200m;
|
var totalSalary = 1200m;
|
||||||
var salary = new SalaryDataModel(invalidId, pekarId, period, baseRate, bonusRate, totalQuantity,
|
var salary = new SalaryDataModel(invalidId, pekarId, period, baseRate, bonusRate,
|
||||||
totalSalary);
|
totalSalary);
|
||||||
Assert.Throws<ValidationException>(() => salary.Validate());}
|
Assert.Throws<ValidationException>(() => salary.Validate());}
|
||||||
|
|
||||||
@ -51,9 +48,8 @@ namespace CandyHouseTests.DataModelsTests
|
|||||||
var period = new DateTime(2023, 10, 1);
|
var period = new DateTime(2023, 10, 1);
|
||||||
var invalidBaseRate = -1000m;
|
var invalidBaseRate = -1000m;
|
||||||
var bonusRate = 200m;
|
var bonusRate = 200m;
|
||||||
var totalQuantity = 50;
|
|
||||||
var totalSalary = 1200m;
|
var totalSalary = 1200m;
|
||||||
var salary = new SalaryDataModel(id, pekarId, period, invalidBaseRate, bonusRate, totalQuantity,
|
var salary = new SalaryDataModel(id, pekarId, period, invalidBaseRate, bonusRate,
|
||||||
totalSalary);
|
totalSalary);
|
||||||
Assert.Throws<ValidationException>(() => salary.Validate());
|
Assert.Throws<ValidationException>(() => salary.Validate());
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user