forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
333 lines
13 KiB
C#
333 lines
13 KiB
C#
using MagicCarpetContracts.DataModels;
|
|
using MagicCarpetContracts.Enums;
|
|
using MagicCarpetContracts.Exceptions;
|
|
using MagicCarpetContracts.Infrastructure.PostConfigurations;
|
|
using MagicCarpetDatabase.Implementations;
|
|
using MagicCarpetDatabase.Models;
|
|
using MagicCarpetTests.Infrastructure;
|
|
using MagicCarpetTests.StoragesContracts;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MagicCarpetTests.StoragesContracts;
|
|
[TestFixture]
|
|
internal class PostStorageContractTests : BaseStorageContractTest
|
|
{
|
|
private PostStorageContract _postStorageContract;
|
|
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
_postStorageContract = new PostStorageContract(MagicCarpetDbContext);
|
|
}
|
|
|
|
[TearDown]
|
|
public void TearDown()
|
|
{
|
|
MagicCarpetDbContext.RemovePostsFromDatabase();
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetList_WhenHaveRecords_Test()
|
|
{
|
|
var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postName: "name 1");
|
|
MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postName: "name 2");
|
|
MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postName: "name 3");
|
|
var list = _postStorageContract.GetList();
|
|
Assert.That(list, Is.Not.Null);
|
|
Assert.That(list, Has.Count.EqualTo(3));
|
|
AssertElement(list.First(x => x.Id == post.PostId), post);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetList_WhenNoRecords_Test()
|
|
{
|
|
var list = _postStorageContract.GetList();
|
|
Assert.That(list, Is.Not.Null);
|
|
Assert.That(list, Is.Empty);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetList_WhenDifferentConfigTypes_Test()
|
|
{
|
|
var postSimple = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postName: "name 1");
|
|
var postBuilder = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postName: "name 2", config: new TravelAgentPostConfiguration() { PersonalCount = 500 });
|
|
var postLoader = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postName: "name 3", config: new ChiefPostConfiguration() { PersonalCountTrendPremium = 20 });
|
|
var list = _postStorageContract.GetList();
|
|
Assert.That(list, Is.Not.Null);
|
|
AssertElement(list.First(x => x.Id == postSimple.PostId), postSimple);
|
|
AssertElement(list.First(x => x.Id == postBuilder.PostId), postBuilder);
|
|
AssertElement(list.First(x => x.Id == postLoader.PostId), postLoader);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetPostWithHistory_WhenHaveRecords_Test()
|
|
{
|
|
var postId = Guid.NewGuid().ToString();
|
|
MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postName: "name 1", isActual: true);
|
|
MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postId, "name 2", isActual: true);
|
|
MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postId, "name 2", isActual: false);
|
|
var list = _postStorageContract.GetPostWithHistory(postId);
|
|
Assert.That(list, Is.Not.Null);
|
|
Assert.That(list, Has.Count.EqualTo(2));
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetPostWithHistory_WhenNoRecords_Test()
|
|
{
|
|
var postId = Guid.NewGuid().ToString();
|
|
MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postName: "name 1", isActual: true);
|
|
MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postId, "name 2", isActual: true);
|
|
MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postId, "name 2", isActual: false);
|
|
var list = _postStorageContract.GetPostWithHistory(Guid.NewGuid().ToString());
|
|
Assert.That(list, Is.Not.Null);
|
|
Assert.That(list, Has.Count.EqualTo(0));
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetElementById_WhenHaveRecord_Test()
|
|
{
|
|
var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn();
|
|
AssertElement(_postStorageContract.GetElementById(post.PostId), post);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetElementById_WhenNoRecord_Test()
|
|
{
|
|
MagicCarpetDbContext.InsertPostToDatabaseAndReturn();
|
|
Assert.That(() => _postStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetElementById_WhenRecordWasDeleted_Test()
|
|
{
|
|
var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(isActual: false);
|
|
Assert.That(() => _postStorageContract.GetElementById(post.PostId), Is.Null);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetElementById_WhenTrySearchById_Test()
|
|
{
|
|
var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn();
|
|
Assert.That(() => _postStorageContract.GetElementById(post.Id), Is.Null);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetElementByName_WhenHaveRecord_Test()
|
|
{
|
|
var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn();
|
|
AssertElement(_postStorageContract.GetElementByName(post.PostName), post);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetElementByName_WhenNoRecord_Test()
|
|
{
|
|
MagicCarpetDbContext.InsertPostToDatabaseAndReturn();
|
|
Assert.That(() => _postStorageContract.GetElementByName("name"), Is.Null);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetElementByName_WhenRecordWasDeleted_Test()
|
|
{
|
|
var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(isActual: false);
|
|
Assert.That(() => _postStorageContract.GetElementById(post.PostName), Is.Null);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_AddElement_Test()
|
|
{
|
|
var post = CreateModel(Guid.NewGuid().ToString());
|
|
_postStorageContract.AddElement(post);
|
|
AssertElement(MagicCarpetDbContext.GetPostFromDatabaseByPostId(post.Id), post);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_AddElement_WhenHaveRecordWithSameName_Test()
|
|
{
|
|
var post = CreateModel(Guid.NewGuid().ToString(), "name unique");
|
|
MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postName: post.PostName, isActual: true);
|
|
Assert.That(() => _postStorageContract.AddElement(post), Throws.TypeOf<ElementExistsException>());
|
|
}
|
|
|
|
[Test]
|
|
public void Try_AddElement_WhenHaveRecordWithSamePostId_Test()
|
|
{
|
|
var post = CreateModel(Guid.NewGuid().ToString());
|
|
MagicCarpetDbContext.InsertPostToDatabaseAndReturn(post.Id, isActual: true);
|
|
Assert.That(() => _postStorageContract.AddElement(post), Throws.TypeOf<ElementExistsException>());
|
|
}
|
|
|
|
[Test]
|
|
public void Try_AddElement_WithTravelAgentPostConfiguration_Test()
|
|
{
|
|
var salePercent = 10;
|
|
var post = CreateModel(Guid.NewGuid().ToString(), config: new TravelAgentPostConfiguration() { PersonalCount = salePercent });
|
|
_postStorageContract.AddElement(post);
|
|
var element = MagicCarpetDbContext.GetPostFromDatabaseByPostId(post.Id);
|
|
Assert.That(element, Is.Not.Null);
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(element.Configuration.Type, Is.EqualTo(typeof(TravelAgentPostConfiguration).Name));
|
|
Assert.That((element.Configuration as TravelAgentPostConfiguration)!.PersonalCount, Is.EqualTo(salePercent));
|
|
});
|
|
}
|
|
|
|
[Test]
|
|
public void Try_AddElement_WithChiefPostConfiguration_Test()
|
|
{
|
|
var trendPremium = 20;
|
|
var post = CreateModel(Guid.NewGuid().ToString(), config: new ChiefPostConfiguration() { PersonalCountTrendPremium = trendPremium });
|
|
_postStorageContract.AddElement(post);
|
|
var element = MagicCarpetDbContext.GetPostFromDatabaseByPostId(post.Id);
|
|
Assert.That(element, Is.Not.Null);
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(element.Configuration.Type, Is.EqualTo(typeof(ChiefPostConfiguration).Name));
|
|
Assert.That((element.Configuration as ChiefPostConfiguration)!.PersonalCountTrendPremium, Is.EqualTo(trendPremium));
|
|
});
|
|
}
|
|
|
|
[Test]
|
|
public void Try_UpdElement_Test()
|
|
{
|
|
var post = CreateModel(Guid.NewGuid().ToString());
|
|
MagicCarpetDbContext.InsertPostToDatabaseAndReturn(post.Id, isActual: true);
|
|
_postStorageContract.UpdElement(post);
|
|
var posts = MagicCarpetDbContext.GetPostsFromDatabaseByPostId(post.Id);
|
|
Assert.That(posts, Is.Not.Null);
|
|
Assert.That(posts, Has.Length.EqualTo(2));
|
|
AssertElement(posts[0], CreateModel(post.Id));
|
|
AssertElement(posts[^1], CreateModel(post.Id));
|
|
}
|
|
|
|
[Test]
|
|
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
|
{
|
|
Assert.That(() => _postStorageContract.UpdElement(CreateModel(Guid.NewGuid().ToString())), Throws.TypeOf<ElementNotFoundException>());
|
|
}
|
|
|
|
[Test]
|
|
public void Try_UpdElement_WhenHaveRecordWithSameName_Test()
|
|
{
|
|
var post = CreateModel(Guid.NewGuid().ToString(), "New Name");
|
|
MagicCarpetDbContext.InsertPostToDatabaseAndReturn(post.Id, postName: "name");
|
|
MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postName: post.PostName);
|
|
Assert.That(() => _postStorageContract.UpdElement(post), Throws.TypeOf<ElementExistsException>());
|
|
}
|
|
|
|
[Test]
|
|
public void Try_UpdElement_WhenRecordWasDeleted_Test()
|
|
{
|
|
var post = CreateModel(Guid.NewGuid().ToString());
|
|
MagicCarpetDbContext.InsertPostToDatabaseAndReturn(post.Id, isActual: false);
|
|
Assert.That(() => _postStorageContract.UpdElement(post), Throws.TypeOf<ElementDeletedException>());
|
|
}
|
|
|
|
[Test]
|
|
public void Try_UpdElement_WithTravelAgentPostConfiguration_Test()
|
|
{
|
|
var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn();
|
|
var salePercent = 10;
|
|
_postStorageContract.UpdElement(CreateModel(post.PostId, config: new TravelAgentPostConfiguration() { PersonalCount = salePercent }));
|
|
var element = MagicCarpetDbContext.GetPostFromDatabaseByPostId(post.PostId);
|
|
Assert.That(element, Is.Not.Null);
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(element.Configuration.Type, Is.EqualTo(typeof(TravelAgentPostConfiguration).Name));
|
|
Assert.That((element.Configuration as TravelAgentPostConfiguration)!.PersonalCount, Is.EqualTo(salePercent));
|
|
});
|
|
}
|
|
|
|
[Test]
|
|
public void Try_UpdElement_WithChiefPostConfiguration_Test()
|
|
{
|
|
var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn();
|
|
var trendPremium = 20;
|
|
_postStorageContract.UpdElement(CreateModel(post.PostId, config: new ChiefPostConfiguration() { PersonalCountTrendPremium = trendPremium }));
|
|
var element = MagicCarpetDbContext.GetPostFromDatabaseByPostId(post.PostId);
|
|
Assert.That(element, Is.Not.Null);
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(element.Configuration.Type, Is.EqualTo(typeof(ChiefPostConfiguration).Name));
|
|
Assert.That((element.Configuration as ChiefPostConfiguration)!.PersonalCountTrendPremium, Is.EqualTo(trendPremium));
|
|
});
|
|
}
|
|
|
|
[Test]
|
|
public void Try_DelElement_Test()
|
|
{
|
|
var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(isActual: true);
|
|
_postStorageContract.DelElement(post.PostId);
|
|
Assert.That(MagicCarpetDbContext.GetPostFromDatabaseByPostId(post.PostId), Is.Null);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_DelElement_WhenNoRecordWithThisId_Test()
|
|
{
|
|
Assert.That(() => _postStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
|
}
|
|
|
|
[Test]
|
|
public void Try_DelElement_WhenRecordWasDeleted_Test()
|
|
{
|
|
var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(isActual: false);
|
|
Assert.That(() => _postStorageContract.DelElement(post.PostId), Throws.TypeOf<ElementDeletedException>());
|
|
}
|
|
|
|
[Test]
|
|
public void Try_ResElement_Test()
|
|
{
|
|
var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(isActual: false);
|
|
_postStorageContract.ResElement(post.PostId);
|
|
var element = MagicCarpetDbContext.GetPostFromDatabaseByPostId(post.PostId);
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(element, Is.Not.Null);
|
|
Assert.That(element!.IsActual);
|
|
});
|
|
}
|
|
|
|
[Test]
|
|
public void Try_ResElement_WhenNoRecordWithThisId_Test()
|
|
{
|
|
Assert.That(() => _postStorageContract.ResElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
|
}
|
|
|
|
[Test]
|
|
public void Try_ResElement_WhenRecordNotWasDeleted_Test()
|
|
{
|
|
var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(isActual: true);
|
|
Assert.That(() => _postStorageContract.ResElement(post.PostId), Throws.Nothing);
|
|
}
|
|
|
|
private static void AssertElement(PostDataModel? actual, Post expected)
|
|
{
|
|
Assert.That(actual, Is.Not.Null);
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(actual.Id, Is.EqualTo(expected.PostId));
|
|
Assert.That(actual.PostName, Is.EqualTo(expected.PostName));
|
|
Assert.That(actual.PostType, Is.EqualTo(expected.PostType));
|
|
Assert.That(actual.ConfigurationModel.Rate, Is.EqualTo(expected.Configuration.Rate));
|
|
});
|
|
}
|
|
|
|
private static PostDataModel CreateModel(string postId, string postName = "test", PostType postType = PostType.TravelAgent, PostConfiguration? config = null)
|
|
=> new(postId, postName, postType, config ?? new PostConfiguration() { Rate = 100 });
|
|
|
|
private static void AssertElement(Post? actual, PostDataModel expected)
|
|
{
|
|
Assert.That(actual, Is.Not.Null);
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(actual.PostId, Is.EqualTo(expected.Id));
|
|
Assert.That(actual.PostName, Is.EqualTo(expected.PostName));
|
|
Assert.That(actual.PostType, Is.EqualTo(expected.PostType));
|
|
Assert.That(actual.Configuration.Rate, Is.EqualTo(expected.ConfigurationModel.Rate));
|
|
});
|
|
}
|
|
}
|