diff --git a/tests/Kentico.Xperience.Algolia.Tests/Data/MockDataProvider.cs b/tests/Kentico.Xperience.Algolia.Tests/Data/MockDataProvider.cs index 1954052..0fd0081 100644 --- a/tests/Kentico.Xperience.Algolia.Tests/Data/MockDataProvider.cs +++ b/tests/Kentico.Xperience.Algolia.Tests/Data/MockDataProvider.cs @@ -5,23 +5,22 @@ namespace Kentico.Xperience.Algolia.Tests.Base; internal static class MockDataProvider { - public static IndexEventWebPageItemModel WebModel => new( - itemID: 0, - itemGuid: Guid.NewGuid(), - languageName: CzechLanguageName, - contentTypeName: ArticlePage.CONTENT_TYPE_NAME, - name: "Name", - isSecured: false, - contentTypeID: 1, - contentLanguageID: 1, - websiteChannelName: DefaultChannel, - webPageItemTreePath: "/", - order: 0 - ); + public static IndexEventWebPageItemModel WebModel(IndexEventWebPageItemModel item) + { + item.LanguageName = CzechLanguageName; + item.ContentTypeName = ArticlePage.CONTENT_TYPE_NAME; + item.Name = "Name"; + item.ContentTypeID = 1; + item.ContentLanguageID = 1; + item.WebsiteChannelName = DefaultChannel; + item.WebPageItemTreePath = "/%"; + + return item; + } public static AlgoliaIndexIncludedPath Path => new("/%") { - ContentTypes = [ArticlePage.CONTENT_TYPE_NAME] + ContentTypes = [new AlgoliaIndexContentType(ArticlePage.CONTENT_TYPE_NAME, nameof(ArticlePage))] }; @@ -31,7 +30,8 @@ internal static class MockDataProvider IndexName = DefaultIndex, ChannelName = DefaultChannel, LanguageNames = new List() { EnglishLanguageName, CzechLanguageName }, - Paths = new List() { Path } + Paths = new List() { Path }, + StrategyName = "strategy" }, [] ); diff --git a/tests/Kentico.Xperience.Algolia.Tests/Indexing/IndexedItemModelExtensionsTests.cs b/tests/Kentico.Xperience.Algolia.Tests/Indexing/IndexedItemModelExtensionsTests.cs index 9ca7a6c..b8249bb 100644 --- a/tests/Kentico.Xperience.Algolia.Tests/Indexing/IndexedItemModelExtensionsTests.cs +++ b/tests/Kentico.Xperience.Algolia.Tests/Indexing/IndexedItemModelExtensionsTests.cs @@ -1,7 +1,9 @@ using CMS.Core; using CMS.EventLog; using CMS.Tests; + using FluentAssertions; + using Kentico.Xperience.Algolia.Admin; using Kentico.Xperience.Algolia.Indexing; @@ -54,7 +56,7 @@ public void IsIndexedByIndex_Will_Return_False_When_The_Matching_Index_Has_No_Ma { var log = Substitute.For(); - IEnumerable paths = [new("/path") { ContentTypes = ["contentType"], Identifier = "1" }]; + IEnumerable paths = [new("/path") { ContentTypes = [new("contentType", "contentType")], Identifier = "1" }]; var index = new AlgoliaIndex(new AlgoliaConfigurationModel { @@ -80,7 +82,7 @@ public void IsIndexedByIndex_Will_Return_False_When_The_Matching_Index_Has_No_Ma var log = Substitute.For(); List contentTypes = ["contentType"]; - IEnumerable exactPaths = [new("/path") { ContentTypes = contentTypes, Identifier = "1" }]; + IEnumerable exactPaths = [new("/path") { ContentTypes = [new("contentType", "contentType")], Identifier = "1" }]; var index1 = new AlgoliaIndex(new AlgoliaConfigurationModel { @@ -94,7 +96,7 @@ public void IsIndexedByIndex_Will_Return_False_When_The_Matching_Index_Has_No_Ma }, new() { { "strategy", typeof(DefaultAlgoliaIndexingStrategy) } }); AlgoliaIndexStore.Instance.AddIndex(index1); - IEnumerable wildcardPaths = [new("/home/%") { ContentTypes = contentTypes, Identifier = "1" }]; + IEnumerable wildcardPaths = [new("/home/%") { ContentTypes = [new("contentType", "contentType")], Identifier = "1" }]; var index2 = new AlgoliaIndex(new AlgoliaConfigurationModel { ChannelName = "channel", @@ -119,7 +121,7 @@ public void IsIndexedByIndex_Will_Return_False_When_The_Matching_Index_Has_No_Ma public void IsIndexedByIndex_Will_Return_True_When_The_Matching_Index_Has_An_Exact_Path_Match() { var log = Substitute.For(); - List contentTypes = ["contentType"]; + List contentTypes = [new("contentType", "contentType")]; IEnumerable exactPaths = [new("/path/abc/def") { ContentTypes = contentTypes, Identifier = "1" }]; @@ -135,7 +137,7 @@ public void IsIndexedByIndex_Will_Return_True_When_The_Matching_Index_Has_An_Exa }, new() { { "strategy", typeof(DefaultAlgoliaIndexingStrategy) } }); AlgoliaIndexStore.Instance.AddIndex(index1); - IEnumerable wildcardPaths = [new("/path/%") { ContentTypes = ["contentType"], Identifier = "1" }]; + IEnumerable wildcardPaths = [new("/path/%") { ContentTypes = [new("contentType", "contentType")], Identifier = "1" }]; var index2 = new AlgoliaIndex(new AlgoliaConfigurationModel { @@ -150,7 +152,7 @@ public void IsIndexedByIndex_Will_Return_True_When_The_Matching_Index_Has_An_Exa AlgoliaIndexStore.Instance.AddIndex(index2); var sut = GetDefaultIndexEventWebPageItemModel(); - sut.ContentTypeName = contentTypes[0]; + sut.ContentTypeName = contentTypes[0].ContentTypeName; sut.WebPageItemTreePath = exactPaths.First().AliasPath; sut.IsIndexedByIndex(log, index1.IndexName, "event").Should().BeTrue(); diff --git a/tests/Kentico.Xperience.Algolia.Tests/Tests/IndexStoreTests.cs b/tests/Kentico.Xperience.Algolia.Tests/Tests/IndexStoreTests.cs index a98e012..c3755de 100644 --- a/tests/Kentico.Xperience.Algolia.Tests/Tests/IndexStoreTests.cs +++ b/tests/Kentico.Xperience.Algolia.Tests/Tests/IndexStoreTests.cs @@ -1,9 +1,11 @@ -using Kentico.Xperience.Algolia.Admin; +using CMS.Tests; + +using Kentico.Xperience.Algolia.Admin; using Kentico.Xperience.Algolia.Indexing; using Kentico.Xperience.Algolia.Tests.Base; namespace Kentico.Xperience.Algolia.Tests.Tests; -internal class IndexStoreTests +internal class IndexStoreTests : UnitTests { [Test] @@ -24,6 +26,7 @@ public void AddAndGetIndex() [Test] public void AddIndex_AlreadyExists() { + var fixture = new Fixture(); AlgoliaIndexStore.Instance.SetIndicies(new List()); AlgoliaIndexStore.Instance.AddIndex(MockDataProvider.Index); @@ -55,4 +58,7 @@ public void SetIndicies() Assert.That(AlgoliaIndexStore.Instance.GetIndex(simpleIndex.IndexName) is not null); }); } + + [TearDown] + public void TearDown() => AlgoliaIndexStore.Instance.SetIndicies([]); } diff --git a/tests/Kentico.Xperience.Algolia.Tests/Tests/IndexedItemModelExtensionsTests.cs b/tests/Kentico.Xperience.Algolia.Tests/Tests/IndexedItemModelExtensionsTests.cs index c3752d9..d2f45f2 100644 --- a/tests/Kentico.Xperience.Algolia.Tests/Tests/IndexedItemModelExtensionsTests.cs +++ b/tests/Kentico.Xperience.Algolia.Tests/Tests/IndexedItemModelExtensionsTests.cs @@ -1,45 +1,47 @@ using CMS.Core; +using CMS.Tests; + using DancingGoat.Models; + using Kentico.Xperience.Algolia.Admin; using Kentico.Xperience.Algolia.Indexing; using Kentico.Xperience.Algolia.Tests.Base; namespace Kentico.Xperience.Algolia.Tests.Tests; -internal class MockEventLogService : IEventLogService -{ - public void LogEvent(EventLogData eventLogData) - { - // Method intentionally left empty. - } -} - -internal class IndexedItemModelExtensionsTests +internal class IndexedItemModelExtensionsTests : UnitTests { - private readonly IEventLogService log; - - public IndexedItemModelExtensionsTests() => log = new MockEventLogService(); - [Test] public void IsIndexedByIndex() { + Service.InitializeContainer(); + var log = Substitute.For(); + AlgoliaIndexStore.Instance.SetIndicies(new List()); AlgoliaIndexStore.Instance.AddIndex(MockDataProvider.Index); - Assert.That(MockDataProvider.WebModel.IsIndexedByIndex(log, MockDataProvider.DefaultIndex, MockDataProvider.EventName)); + var fixture = new Fixture(); + var item = fixture.Create(); + + var model = MockDataProvider.WebModel(item); + Assert.That(model.IsIndexedByIndex(log, MockDataProvider.DefaultIndex, MockDataProvider.EventName)); } [Test] public void WildCard() { - var model = MockDataProvider.WebModel; + Service.InitializeContainer(); + var log = Substitute.For(); + var fixture = new Fixture(); + var item = fixture.Create(); + + var model = MockDataProvider.WebModel(item); model.WebPageItemTreePath = "/Home"; var index = MockDataProvider.Index; - var path = new AlgoliaIndexIncludedPath("/%") { ContentTypes = [ArticlePage.CONTENT_TYPE_NAME] }; + var path = new AlgoliaIndexIncludedPath("/%") { ContentTypes = [new(ArticlePage.CONTENT_TYPE_NAME, nameof(ArticlePage))] }; index.IncludedPaths = new List() { path }; - AlgoliaIndexStore.Instance.SetIndicies(new List()); AlgoliaIndexStore.Instance.AddIndex(index); Assert.That(model.IsIndexedByIndex(log, MockDataProvider.DefaultIndex, MockDataProvider.EventName)); @@ -48,11 +50,17 @@ public void WildCard() [Test] public void WrongWildCard() { - var model = MockDataProvider.WebModel; + Service.InitializeContainer(); + var log = Substitute.For(); + + var fixture = new Fixture(); + var item = fixture.Create(); + + var model = MockDataProvider.WebModel(item); model.WebPageItemTreePath = "/Home"; var index = MockDataProvider.Index; - var path = new AlgoliaIndexIncludedPath("/Index/%") { ContentTypes = [ArticlePage.CONTENT_TYPE_NAME] }; + var path = new AlgoliaIndexIncludedPath("/Index/%") { ContentTypes = [new("contentType", "contentType")] }; index.IncludedPaths = new List() { path }; @@ -65,11 +73,17 @@ public void WrongWildCard() [Test] public void WrongPath() { - var model = MockDataProvider.WebModel; + Service.InitializeContainer(); + var log = Substitute.For(); + + var fixture = new Fixture(); + var item = fixture.Create(); + + var model = MockDataProvider.WebModel(item); model.WebPageItemTreePath = "/Home"; var index = MockDataProvider.Index; - var path = new AlgoliaIndexIncludedPath("/Index") { ContentTypes = [ArticlePage.CONTENT_TYPE_NAME] }; + var path = new AlgoliaIndexIncludedPath("/Index") { ContentTypes = [new("contentType", "contentType")] }; index.IncludedPaths = new List() { path }; @@ -82,7 +96,13 @@ public void WrongPath() [Test] public void WrongContentType() { - var model = MockDataProvider.WebModel; + Service.InitializeContainer(); + var log = Substitute.For(); + + var fixture = new Fixture(); + var item = fixture.Create(); + + var model = MockDataProvider.WebModel(item); model.ContentTypeName = "DancingGoat.HomePage"; AlgoliaIndexStore.Instance.SetIndicies(new List()); @@ -94,16 +114,29 @@ public void WrongContentType() [Test] public void WrongIndex() { + Service.InitializeContainer(); + var log = Substitute.For(); + AlgoliaIndexStore.Instance.SetIndicies(new List()); AlgoliaIndexStore.Instance.AddIndex(MockDataProvider.Index); - Assert.That(!MockDataProvider.WebModel.IsIndexedByIndex(log, "NewIndex", MockDataProvider.EventName)); + var fixture = new Fixture(); + var item = fixture.Create(); + + var model = MockDataProvider.WebModel(item); + Assert.That(!model.IsIndexedByIndex(log, "NewIndex", MockDataProvider.EventName)); } [Test] public void WrongLanguage() { - var model = MockDataProvider.WebModel; + Service.InitializeContainer(); + var log = Substitute.For(); + + var fixture = new Fixture(); + var item = fixture.Create(); + + var model = MockDataProvider.WebModel(item); model.LanguageName = "sk"; AlgoliaIndexStore.Instance.SetIndicies(new List());