From dce59dadb7b91a745914ee6471eacb69b1bc5b83 Mon Sep 17 00:00:00 2001 From: enisn Date: Mon, 11 Jan 2021 12:57:25 +0300 Subject: [PATCH] CmsKit - Update Tests according to EntityTag domain logic --- .../Tags/TagAdminAppService_Tests.cs | 4 +-- .../Tags/EntityTagManager_Tests.cs | 27 +++++++++++++++ .../Tags/TagManager_Tests.cs | 20 ++++++++--- .../CmsKitDataSeedContributor.cs | 34 +++++++++++-------- 4 files changed, 64 insertions(+), 21 deletions(-) create mode 100644 modules/cms-kit/test/Volo.CmsKit.Domain.Tests/Tags/EntityTagManager_Tests.cs diff --git a/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Tags/TagAdminAppService_Tests.cs b/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Tags/TagAdminAppService_Tests.cs index 4ad0d43468..b2128bb385 100644 --- a/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Tags/TagAdminAppService_Tests.cs +++ b/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Tags/TagAdminAppService_Tests.cs @@ -32,8 +32,8 @@ namespace Volo.CmsKit.Tags { var list = await _tagAdminAppService.CreateAsync(new TagCreateDto { - EntityType = "any_new_type", - Name = "1", + EntityType = _cmsKitTestData.EntityType1, + Name = "My First Tag", }); list.Id.ShouldNotBe(Guid.Empty); diff --git a/modules/cms-kit/test/Volo.CmsKit.Domain.Tests/Tags/EntityTagManager_Tests.cs b/modules/cms-kit/test/Volo.CmsKit.Domain.Tests/Tags/EntityTagManager_Tests.cs new file mode 100644 index 0000000000..e259472d96 --- /dev/null +++ b/modules/cms-kit/test/Volo.CmsKit.Domain.Tests/Tags/EntityTagManager_Tests.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Volo.CmsKit.Tags +{ + public class EntityTagManager_Tests : CmsKitDomainTestBase + { + private readonly CmsKitTestData _cmsKitTestData; + private readonly IEntityTagManager _entityTagManager; + private readonly IEntityTagManager _entityTagManager; + + public EntityTagManager_Tests() + { + _cmsKitTestData = GetRequiredService(); + _entityTagManager = GetRequiredService(); + } + + public async Task AddTagToEntityAsync_ShouldAdd_WhenEverythingCorrect() + { + var tag = await + await _entityTagManager.AddTagToEntityAsync(_cmsKitTestData.tag) + } + } +} diff --git a/modules/cms-kit/test/Volo.CmsKit.Domain.Tests/Tags/TagManager_Tests.cs b/modules/cms-kit/test/Volo.CmsKit.Domain.Tests/Tags/TagManager_Tests.cs index 0fe8d5ddb4..7d7416c8ce 100644 --- a/modules/cms-kit/test/Volo.CmsKit.Domain.Tests/Tags/TagManager_Tests.cs +++ b/modules/cms-kit/test/Volo.CmsKit.Domain.Tests/Tags/TagManager_Tests.cs @@ -22,7 +22,7 @@ namespace Volo.CmsKit.Tags [Fact] public async Task ShouldAddWhenGettingAsync() { - var newTagEntityType = "testEntity"; + var newTagEntityType = _cmsKitTestData.EntityType1; var newTagName = "test_tag_2123"; var doesExist = await _tagRepository.AnyAsync(newTagEntityType, newTagName); @@ -53,16 +53,28 @@ namespace Volo.CmsKit.Tags } [Fact] - public async Task ShouldInsert() + public async Task ShouldInsertAsync() { - var tag = await _tagManager.InsertAsync(Guid.NewGuid(), "test", "test"); + var tagName = "Freshly Created New Tag"; + var tag = await _tagManager.InsertAsync(Guid.NewGuid(), _cmsKitTestData.EntityType1, tagName); tag.ShouldNotBeNull(); - var doesExist = await _tagRepository.AnyAsync("test", "test"); + var doesExist = await _tagRepository.AnyAsync(_cmsKitTestData.EntityType1, tagName); doesExist.ShouldBeTrue(); } + [Fact] + public async Task ShouldntInsertWithUnconfiguredEntityTypeAsync() + { + var notConfiguredEntityType = "My.Namespace.SomeEntity"; + + var exception = await Should.ThrowAsync(async () => + await _tagManager.InsertAsync(Guid.NewGuid(), notConfiguredEntityType, "test")); + + exception.ShouldNotBeNull(); + exception.Data[nameof(Tag.EntityType)].ShouldBe(notConfiguredEntityType); + } [Fact] public async Task ShouldNotInsert() diff --git a/modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitDataSeedContributor.cs b/modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitDataSeedContributor.cs index d9f671229d..589249a65f 100644 --- a/modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitDataSeedContributor.cs +++ b/modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitDataSeedContributor.cs @@ -27,7 +27,7 @@ namespace Volo.CmsKit private readonly IRatingRepository _ratingRepository; private readonly ICurrentTenant _currentTenant; private readonly IContentRepository _contentRepository; - private readonly IEntityTagRepository _entityTagRepository; + private readonly IEntityTagManager _entityTagManager; private readonly ITagManager _tagManager; private readonly IPageRepository _pageRepository; private readonly IOptions _options; @@ -39,10 +39,10 @@ namespace Volo.CmsKit ICommentRepository commentRepository, ReactionManager reactionManager, IRatingRepository ratingRepository, - ICurrentTenant currentTenant, + ICurrentTenant currentTenant, IContentRepository contentRepository, - ITagManager tagManager, - IEntityTagRepository entityTagRepository, + ITagManager tagManager, + IEntityTagManager entityTagManager, IPageRepository pageRepository, IOptions options) { @@ -55,7 +55,7 @@ namespace Volo.CmsKit _currentTenant = currentTenant; _contentRepository = contentRepository; _tagManager = tagManager; - _entityTagRepository = entityTagRepository; + _entityTagManager = entityTagManager; _pageRepository = pageRepository; _options = options; } @@ -64,6 +64,8 @@ namespace Volo.CmsKit { using (_currentTenant.Change(context?.TenantId)) { + await ConfigureCmsKitOptionsAsync(); + await SeedUsersAsync(); await SeedCommentsAsync(); @@ -77,13 +79,15 @@ namespace Volo.CmsKit await SeedTagsAsync(); await SeedPagesAsync(); - - await ConfigureCmsKitOptionsAsync(); } } private Task ConfigureCmsKitOptionsAsync() { + _options.Value.Tags.AddOrReplace(_cmsKitTestData.EntityType1); + _options.Value.Tags.AddOrReplace(_cmsKitTestData.EntityType2); + _options.Value.Tags.AddOrReplace(_cmsKitTestData.Content_1_EntityType); + _options.Value.Tags.AddOrReplace(_cmsKitTestData.Content_2_EntityType); _options.Value.Tags.AddOrReplace(_cmsKitTestData.TagDefinition_1_EntityType); return Task.CompletedTask; @@ -222,14 +226,14 @@ namespace Volo.CmsKit _cmsKitTestData.Content_1_EntityId, _cmsKitTestData.Content_1 ); - + var content2 = new Content( _cmsKitTestData.Content_2_Id, _cmsKitTestData.Content_2_EntityType, _cmsKitTestData.Content_2_EntityId, _cmsKitTestData.Content_2 ); - + var content3 = new Content( Guid.NewGuid(), "deleted_entity_type", @@ -251,14 +255,14 @@ namespace Volo.CmsKit { var tagEntity = await _tagManager.InsertAsync(_guidGenerator.Create(), _cmsKitTestData.Content_1_EntityType, tag); - await _entityTagRepository.InsertAsync(new EntityTag(tagEntity.Id, _cmsKitTestData.Content_1_EntityId)); + await _entityTagManager.AddTagToEntityAsync(tagEntity.Id, _cmsKitTestData.Content_1_EntityType, _cmsKitTestData.Content_1_EntityId); } - + foreach (var tag in _cmsKitTestData.Content_2_Tags) { var tagEntity = await _tagManager.InsertAsync(_guidGenerator.Create(), _cmsKitTestData.Content_2_EntityType, tag); - - await _entityTagRepository.InsertAsync(new EntityTag(tagEntity.Id, _cmsKitTestData.Content_2_EntityId)); + + await _entityTagManager.AddTagToEntityAsync(tagEntity.Id, _cmsKitTestData.Content_2_EntityType, _cmsKitTestData.Content_2_EntityId); } } @@ -266,10 +270,10 @@ namespace Volo.CmsKit { var page1 = new Page(_cmsKitTestData.Page_1_Id, _cmsKitTestData.Page_1_Title, _cmsKitTestData.Page_1_Url, _cmsKitTestData.Page_1_Description); var page1Content = new Content(_guidGenerator.Create(), nameof(Page), page1.Id.ToString(), _cmsKitTestData.Page_1_Content); - + await _pageRepository.InsertAsync(page1); await _contentRepository.InsertAsync(page1Content); - + var page2 = new Page(_cmsKitTestData.Page_2_Id, _cmsKitTestData.Page_2_Title, _cmsKitTestData.Page_2_Url, _cmsKitTestData.Page_2_Description); var page2Content = new Content(_guidGenerator.Create(), nameof(Page), page2.Id.ToString(), _cmsKitTestData.Page_2_Content);