Skip to content

Commit

Permalink
fix(tests): change invalid names of content types
Browse files Browse the repository at this point in the history
  • Loading branch information
bkapustik committed Apr 9, 2024
1 parent fe8b8d8 commit 7bad601
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 47 deletions.
30 changes: 15 additions & 15 deletions tests/Kentico.Xperience.Algolia.Tests/Data/MockDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
};


Expand All @@ -31,7 +30,8 @@ internal static class MockDataProvider
IndexName = DefaultIndex,
ChannelName = DefaultChannel,
LanguageNames = new List<string>() { EnglishLanguageName, CzechLanguageName },
Paths = new List<AlgoliaIndexIncludedPath>() { Path }
Paths = new List<AlgoliaIndexIncludedPath>() { Path },
StrategyName = "strategy"
},
[]
);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -54,7 +56,7 @@ public void IsIndexedByIndex_Will_Return_False_When_The_Matching_Index_Has_No_Ma
{
var log = Substitute.For<EventLogService>();

IEnumerable<AlgoliaIndexIncludedPath> paths = [new("/path") { ContentTypes = ["contentType"], Identifier = "1" }];
IEnumerable<AlgoliaIndexIncludedPath> paths = [new("/path") { ContentTypes = [new("contentType", "contentType")], Identifier = "1" }];

var index = new AlgoliaIndex(new AlgoliaConfigurationModel
{
Expand All @@ -80,7 +82,7 @@ public void IsIndexedByIndex_Will_Return_False_When_The_Matching_Index_Has_No_Ma
var log = Substitute.For<EventLogService>();
List<string> contentTypes = ["contentType"];

IEnumerable<AlgoliaIndexIncludedPath> exactPaths = [new("/path") { ContentTypes = contentTypes, Identifier = "1" }];
IEnumerable<AlgoliaIndexIncludedPath> exactPaths = [new("/path") { ContentTypes = [new("contentType", "contentType")], Identifier = "1" }];

var index1 = new AlgoliaIndex(new AlgoliaConfigurationModel
{
Expand All @@ -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<AlgoliaIndexIncludedPath> wildcardPaths = [new("/home/%") { ContentTypes = contentTypes, Identifier = "1" }];
IEnumerable<AlgoliaIndexIncludedPath> wildcardPaths = [new("/home/%") { ContentTypes = [new("contentType", "contentType")], Identifier = "1" }];
var index2 = new AlgoliaIndex(new AlgoliaConfigurationModel
{
ChannelName = "channel",
Expand All @@ -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<EventLogService>();
List<string> contentTypes = ["contentType"];
List<AlgoliaIndexContentType> contentTypes = [new("contentType", "contentType")];

IEnumerable<AlgoliaIndexIncludedPath> exactPaths = [new("/path/abc/def") { ContentTypes = contentTypes, Identifier = "1" }];

Expand All @@ -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<AlgoliaIndexIncludedPath> wildcardPaths = [new("/path/%") { ContentTypes = ["contentType"], Identifier = "1" }];
IEnumerable<AlgoliaIndexIncludedPath> wildcardPaths = [new("/path/%") { ContentTypes = [new("contentType", "contentType")], Identifier = "1" }];

var index2 = new AlgoliaIndex(new AlgoliaConfigurationModel
{
Expand All @@ -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();
Expand Down
10 changes: 8 additions & 2 deletions tests/Kentico.Xperience.Algolia.Tests/Tests/IndexStoreTests.cs
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -24,6 +26,7 @@ public void AddAndGetIndex()
[Test]
public void AddIndex_AlreadyExists()
{
var fixture = new Fixture();

Check warning on line 29 in tests/Kentico.Xperience.Algolia.Tests/Tests/IndexStoreTests.cs

View workflow job for this annotation

GitHub Actions / build

Remove the unused local variable 'fixture'. (https://rules.sonarsource.com/csharp/RSPEC-1481)

Check warning on line 29 in tests/Kentico.Xperience.Algolia.Tests/Tests/IndexStoreTests.cs

View workflow job for this annotation

GitHub Actions / build

Remove the unused local variable 'fixture'. (https://rules.sonarsource.com/csharp/RSPEC-1481)
AlgoliaIndexStore.Instance.SetIndicies(new List<AlgoliaConfigurationModel>());
AlgoliaIndexStore.Instance.AddIndex(MockDataProvider.Index);

Expand Down Expand Up @@ -55,4 +58,7 @@ public void SetIndicies()
Assert.That(AlgoliaIndexStore.Instance.GetIndex(simpleIndex.IndexName) is not null);
});
}

[TearDown]
public void TearDown() => AlgoliaIndexStore.Instance.SetIndicies([]);
}
Original file line number Diff line number Diff line change
@@ -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<IEventLogService>();

AlgoliaIndexStore.Instance.SetIndicies(new List<AlgoliaConfigurationModel>());
AlgoliaIndexStore.Instance.AddIndex(MockDataProvider.Index);

Assert.That(MockDataProvider.WebModel.IsIndexedByIndex(log, MockDataProvider.DefaultIndex, MockDataProvider.EventName));
var fixture = new Fixture();
var item = fixture.Create<IndexEventWebPageItemModel>();

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<IEventLogService>();
var fixture = new Fixture();
var item = fixture.Create<IndexEventWebPageItemModel>();

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<AlgoliaIndexIncludedPath>() { path };

AlgoliaIndexStore.Instance.SetIndicies(new List<AlgoliaConfigurationModel>());
AlgoliaIndexStore.Instance.AddIndex(index);

Assert.That(model.IsIndexedByIndex(log, MockDataProvider.DefaultIndex, MockDataProvider.EventName));
Expand All @@ -48,11 +50,17 @@ public void WildCard()
[Test]
public void WrongWildCard()
{
var model = MockDataProvider.WebModel;
Service.InitializeContainer();
var log = Substitute.For<IEventLogService>();

var fixture = new Fixture();
var item = fixture.Create<IndexEventWebPageItemModel>();

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<AlgoliaIndexIncludedPath>() { path };

Expand All @@ -65,11 +73,17 @@ public void WrongWildCard()
[Test]
public void WrongPath()
{
var model = MockDataProvider.WebModel;
Service.InitializeContainer();
var log = Substitute.For<IEventLogService>();

var fixture = new Fixture();
var item = fixture.Create<IndexEventWebPageItemModel>();

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<AlgoliaIndexIncludedPath>() { path };

Expand All @@ -82,7 +96,13 @@ public void WrongPath()
[Test]
public void WrongContentType()
{
var model = MockDataProvider.WebModel;
Service.InitializeContainer();
var log = Substitute.For<IEventLogService>();

var fixture = new Fixture();
var item = fixture.Create<IndexEventWebPageItemModel>();

var model = MockDataProvider.WebModel(item);
model.ContentTypeName = "DancingGoat.HomePage";

AlgoliaIndexStore.Instance.SetIndicies(new List<AlgoliaConfigurationModel>());
Expand All @@ -94,16 +114,29 @@ public void WrongContentType()
[Test]
public void WrongIndex()
{
Service.InitializeContainer();
var log = Substitute.For<IEventLogService>();

AlgoliaIndexStore.Instance.SetIndicies(new List<AlgoliaConfigurationModel>());
AlgoliaIndexStore.Instance.AddIndex(MockDataProvider.Index);

Assert.That(!MockDataProvider.WebModel.IsIndexedByIndex(log, "NewIndex", MockDataProvider.EventName));
var fixture = new Fixture();
var item = fixture.Create<IndexEventWebPageItemModel>();

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<IEventLogService>();

var fixture = new Fixture();
var item = fixture.Create<IndexEventWebPageItemModel>();

var model = MockDataProvider.WebModel(item);
model.LanguageName = "sk";

AlgoliaIndexStore.Instance.SetIndicies(new List<AlgoliaConfigurationModel>());
Expand Down

0 comments on commit 7bad601

Please sign in to comment.