1 Commits

Author SHA1 Message Date
f91a3cfca8 первый этап 2025-06-07 16:26:39 +04:00
11 changed files with 417 additions and 0 deletions

31
ClassLibrary1.sln Normal file
View File

@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.36202.13
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClassLibrary1", "ClassLibrary1\ClassLibrary1.csproj", "{A8265ED8-D9BB-4164-B013-31B1C2A0C0EE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test.csproj", "{8FC911A3-5BFF-4FFC-A8EE-B38E44B572FB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A8265ED8-D9BB-4164-B013-31B1C2A0C0EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A8265ED8-D9BB-4164-B013-31B1C2A0C0EE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A8265ED8-D9BB-4164-B013-31B1C2A0C0EE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A8265ED8-D9BB-4164-B013-31B1C2A0C0EE}.Release|Any CPU.Build.0 = Release|Any CPU
{8FC911A3-5BFF-4FFC-A8EE-B38E44B572FB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8FC911A3-5BFF-4FFC-A8EE-B38E44B572FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8FC911A3-5BFF-4FFC-A8EE-B38E44B572FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8FC911A3-5BFF-4FFC-A8EE-B38E44B572FB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DA63DE87-95F7-4C1E-B2BE-835170C9A079}
EndGlobalSection
EndGlobal

31
ClassLibrary1/Article.cs Normal file
View File

@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary1
{
public class Article(string id, string title, string tema, DateTime dateCreat) : IValidation
{
public string Id { get; set; } = id;
public string Title { get; set; } = title;
public string Tema { get; set; } = tema;
public DateTime DateCreat { get; set; } = dateCreat;
public void Validate()
{
if (Id.IsEmpty())
throw new ValidationException("Field Id is empty");
if (!Id.IsGuid())
throw new ValidationException("Id is Guid");
if (Title.IsEmpty())
throw new ValidationException("Field Id is empty");
if (!Title.IsGuid())
throw new ValidationException("Id is Guid");
if (Tema.IsEmpty())
throw new ValidationException("Field Id is empty");
if (!Tema.IsGuid())
throw new ValidationException("Id is Guid");
}
}
}

45
ClassLibrary1/Author.cs Normal file
View File

@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace ClassLibrary1
{
public class Author(string id, string fio, string email, DateTime birthDate, string work, string articleId) : IValidation
{
public string Id { get; set; } = id;
public string FIO { get; set; } = fio;
public string Email { get; set; } = email;
public DateTime BirthDate { get; private set; } = birthDate;
public string Work { get; set; } = work;
public string ArticleId { get; set; } = articleId;
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 (!FIO.IsGuid())
throw new ValidationException("Field FIO is guid");
if (ArticleId.IsEmpty())
throw new ValidationException("Field ArticleId is empty");
if (!ArticleId.IsGuid())
throw new ValidationException("The value in the field ArticleId is not a unique identifier");
if (Work.IsEmpty())
throw new ValidationException("Field ArticleId is empty");
if (!Work.IsGuid())
throw new ValidationException("The value in the field ArticleId is not a unique identifier");
if (BirthDate.Date > DateTime.Now.AddYears(-16).Date)
throw new ValidationException($"Minors cannot be hired (BirthDate = {BirthDate.ToShortDateString()})");
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 valid email address");
}
}
}

7
ClassLibrary1/Class1.cs Normal file
View File

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

View File

@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

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

View File

@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary1
{
public static class StringExtensions
{
public static bool IsEmpty(this string str)
{
return string.IsNullOrWhiteSpace(str);
}
public static bool IsGuid(this string str)
{
return Guid.TryParse(str, out _);
}
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary1
{
public class ValidationException(string message) : Exception(message)
{
}
}

88
Test/ArticleTests.cs Normal file
View File

@@ -0,0 +1,88 @@
using ClassLibrary1;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test;
[TestFixture]
internal class ArticleTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var article = CreateArticle(null, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(-1));
Assert.That(() => article.Validate(), Throws.TypeOf<ValidationException>());
article = CreateArticle(string.Empty, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(-1));
Assert.That(() => article.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var article = CreateArticle("not-a-guid", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(-1));
Assert.That(() => article.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void TitleIsNullOrEmptyTest()
{
var article = CreateArticle(Guid.NewGuid().ToString(), null, Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(-1));
Assert.That(() => article.Validate(), Throws.TypeOf<ValidationException>());
article = CreateArticle(Guid.NewGuid().ToString(), string.Empty, Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(-1));
Assert.That(() => article.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void TitleIsNotGuidTest()
{
var article = CreateArticle(Guid.NewGuid().ToString(), "not-a-guid", Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(-1));
Assert.That(() => article.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void TemaIsNullOrEmptyTest()
{
var article = CreateArticle(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, DateTime.UtcNow.AddDays(-1));
Assert.That(() => article.Validate(), Throws.TypeOf<ValidationException>());
article = CreateArticle(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), string.Empty, DateTime.UtcNow.AddDays(-1));
Assert.That(() => article.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void TemaIsNotGuidTest()
{
var article = CreateArticle(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "not-a-guid", DateTime.UtcNow.AddDays(-1));
Assert.That(() => article.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsAreCorrectTest()
{
var id = Guid.NewGuid().ToString();
var title = Guid.NewGuid().ToString(); // Title должен быть GUID согласно текущей валидации
var tema = Guid.NewGuid().ToString(); // Tema должен быть GUID согласно текущей валидации
var dateCreat = DateTime.UtcNow.AddDays(-1);
var article = CreateArticle(id, title, tema, dateCreat);
Assert.That(() => article.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(article.Id, Is.EqualTo(id));
Assert.That(article.Title, Is.EqualTo(title));
Assert.That(article.Tema, Is.EqualTo(tema));
Assert.That(article.DateCreat, Is.EqualTo(dateCreat));
});
}
private static Article CreateArticle(string? id, string? title, string? tema, DateTime dateCreat) =>
new(id, title, tema, dateCreat);
}

133
Test/AuthorTests.cs Normal file
View File

@@ -0,0 +1,133 @@
using ClassLibrary1;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test;
[TestFixture]
internal class AuthorTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var author = CreateAuthor(null, Guid.NewGuid().ToString(), "test@example.com", DateTime.UtcNow.AddYears(-20), Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
Assert.That(() => author.Validate(), Throws.TypeOf<ValidationException>());
author = CreateAuthor(string.Empty, Guid.NewGuid().ToString(), "test@example.com", DateTime.UtcNow.AddYears(-20), Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
Assert.That(() => author.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var author = CreateAuthor("not-a-guid", Guid.NewGuid().ToString(), "test@example.com", DateTime.UtcNow.AddYears(-20), Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
Assert.That(() => author.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void FIOIsNullOrEmptyTest()
{
var author = CreateAuthor(Guid.NewGuid().ToString(), null, "test@example.com", DateTime.UtcNow.AddYears(-20), Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
Assert.That(() => author.Validate(), Throws.TypeOf<ValidationException>());
author = CreateAuthor(Guid.NewGuid().ToString(), string.Empty, "test@example.com", DateTime.UtcNow.AddYears(-20), Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
Assert.That(() => author.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void FIOIsNotGuidTest()
{
var author = CreateAuthor(Guid.NewGuid().ToString(), "not-a-guid", "test@example.com", DateTime.UtcNow.AddYears(-20), Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
Assert.That(() => author.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void ArticleIdIsNullOrEmptyTest()
{
var author = CreateAuthor(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "test@example.com", DateTime.UtcNow.AddYears(-20), Guid.NewGuid().ToString(), null);
Assert.That(() => author.Validate(), Throws.TypeOf<ValidationException>());
author = CreateAuthor(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "test@example.com", DateTime.UtcNow.AddYears(-20), Guid.NewGuid().ToString(), string.Empty);
Assert.That(() => author.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void ArticleIdIsNotGuidTest()
{
var author = CreateAuthor(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "test@example.com", DateTime.UtcNow.AddYears(-20), Guid.NewGuid().ToString(), "not-a-guid");
Assert.That(() => author.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void WorkIsNullOrEmptyTest()
{
var author = CreateAuthor(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "test@example.com", DateTime.UtcNow.AddYears(-20), null, Guid.NewGuid().ToString());
Assert.That(() => author.Validate(), Throws.TypeOf<ValidationException>());
author = CreateAuthor(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "test@example.com", DateTime.UtcNow.AddYears(-20), string.Empty, Guid.NewGuid().ToString());
Assert.That(() => author.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void WorkIsNotGuidTest()
{
var author = CreateAuthor(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "test@example.com", DateTime.UtcNow.AddYears(-20), "not-a-guid", Guid.NewGuid().ToString());
Assert.That(() => author.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void EmailIsNullOrEmptyTest()
{
var author = CreateAuthor(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, DateTime.UtcNow.AddYears(-20), Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
Assert.That(() => author.Validate(), Throws.TypeOf<ValidationException>());
author = CreateAuthor(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), string.Empty, DateTime.UtcNow.AddYears(-20), Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
Assert.That(() => author.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void EmailIsNotValidTest()
{
var author = CreateAuthor(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "invalid.email", DateTime.UtcNow.AddYears(-20), Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
Assert.That(() => author.Validate(), Throws.TypeOf<ValidationException>());
author = CreateAuthor(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "no@domain", DateTime.UtcNow.AddYears(-20), Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
Assert.That(() => author.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void BirthDateIsTooRecentTest()
{
var author = CreateAuthor(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "test@example.com", DateTime.UtcNow.AddYears(-15), Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
Assert.That(() => author.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsAreCorrectTest()
{
var id = Guid.NewGuid().ToString();
var fio = Guid.NewGuid().ToString();
var email = "test@example.com";
var birthDate = DateTime.UtcNow.AddYears(-20);
var work = Guid.NewGuid().ToString();
var articleId = Guid.NewGuid().ToString();
var author = CreateAuthor(id, fio, email, birthDate, work, articleId);
Assert.That(() => author.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(author.Id, Is.EqualTo(id));
Assert.That(author.FIO, Is.EqualTo(fio));
Assert.That(author.Email, Is.EqualTo(email));
Assert.That(author.BirthDate, Is.EqualTo(birthDate));
Assert.That(author.Work, Is.EqualTo(work));
Assert.That(author.ArticleId, Is.EqualTo(articleId));
});
}
private static Author CreateAuthor(string? id, string? fio, string? email, DateTime birthDate, string? work, string? articleId) =>
new(id, fio, email, birthDate, work, articleId);
}

27
Test/Test.csproj Normal file
View File

@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit.Analyzers" Version="4.4.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj" />
</ItemGroup>
<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>
</Project>