Browse Source

Tag grain tests.

pull/308/head
Sebastian 8 years ago
parent
commit
e45c404a46
  1. 3
      src/Squidex.Domain.Apps.Entities.MongoDb/Assets/Visitors/FindExtensions.cs
  2. 7
      src/Squidex.Domain.Apps.Entities/Tags/TagGrain.cs
  3. 19
      src/Squidex.Infrastructure/HashSet.cs
  4. 10
      tests/Squidex.Domain.Apps.Core.Tests/Operations/ExtractReferenceIds/ReferenceExtractionTests.cs
  5. 7
      tests/Squidex.Domain.Apps.Entities.Tests/Apps/Indexes/AppsByUserIndexGrainTests.cs
  6. 3
      tests/Squidex.Domain.Apps.Entities.Tests/Assets/OData/ODataQueryTests.cs
  7. 92
      tests/Squidex.Domain.Apps.Entities.Tests/Tags/TagGrainTests.cs

3
src/Squidex.Domain.Apps.Entities.MongoDb/Assets/Visitors/FindExtensions.cs

@ -94,8 +94,7 @@ namespace Squidex.Domain.Apps.Entities.MongoDb.Assets.Visitors
{
if (string.Equals(field, nameof(MongoAssetEntity.Tags), StringComparison.OrdinalIgnoreCase))
{
var tagIds = new HashSet<string>(new[] { value.ToString() });
var tagNames = Task.Run(() => tagService.GetTagIdsAsync(appId, TagGroups.Assets, tagIds)).Result;
var tagNames = Task.Run(() => tagService.GetTagIdsAsync(appId, TagGroups.Assets, HashSet.Of(value.ToString()))).Result;
return tagNames?.FirstOrDefault() ?? value;
}

7
src/Squidex.Domain.Apps.Entities/Tags/TagGrain.cs

@ -117,7 +117,12 @@ namespace Squidex.Domain.Apps.Entities.Tags
foreach (var name in names)
{
result.Add(state.Tags.FirstOrDefault(x => x.Value.Name == name).Key);
var id = state.Tags.FirstOrDefault(x => string.Equals(x.Value.Name, name, StringComparison.OrdinalIgnoreCase)).Key;
if (!string.IsNullOrWhiteSpace(id))
{
result.Add(id);
}
}
return Task.FromResult(result);

19
src/Squidex.Infrastructure/HashSet.cs

@ -0,0 +1,19 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System.Collections.Generic;
namespace Squidex.Infrastructure
{
public static class HashSet
{
public static HashSet<T> Of<T>(params T[] items)
{
return new HashSet<T>(items);
}
}
}

10
tests/Squidex.Domain.Apps.Core.Tests/Operations/ExtractReferenceIds/ReferenceExtractionTests.cs

@ -140,7 +140,7 @@ namespace Squidex.Domain.Apps.Core.Operations.ExtractReferenceIds
var sut = Fields.Assets(1, "my-asset", Partitioning.Invariant);
var result = sut.CleanReferences(CreateValue(id1, id2), new HashSet<Guid>(new[] { id2 }));
var result = sut.CleanReferences(CreateValue(id1, id2), HashSet.Of(id2));
Assert.Equal(CreateValue(id1), result);
}
@ -154,7 +154,7 @@ namespace Squidex.Domain.Apps.Core.Operations.ExtractReferenceIds
var sut = Fields.Assets(1, "my-asset", Partitioning.Invariant);
var token = CreateValue(id1, id2);
var result = sut.CleanReferences(token, new HashSet<Guid>(new[] { Guid.NewGuid() }));
var result = sut.CleanReferences(token, HashSet.Of(Guid.NewGuid()));
Assert.Same(token, result);
}
@ -214,7 +214,7 @@ namespace Squidex.Domain.Apps.Core.Operations.ExtractReferenceIds
var sut = Fields.References(1, "my-refs", Partitioning.Invariant,
new ReferencesFieldProperties { SchemaId = schemaId });
var result = sut.CleanReferences(CreateValue(id1, id2), new HashSet<Guid>(new[] { id2 }));
var result = sut.CleanReferences(CreateValue(id1, id2), HashSet.Of(id2));
Assert.Equal(CreateValue(id1), result);
}
@ -228,7 +228,7 @@ namespace Squidex.Domain.Apps.Core.Operations.ExtractReferenceIds
var sut = Fields.References(1, "my-refs", Partitioning.Invariant,
new ReferencesFieldProperties { SchemaId = schemaId });
var result = sut.CleanReferences(CreateValue(id1, id2), new HashSet<Guid>(new[] { schemaId }));
var result = sut.CleanReferences(CreateValue(id1, id2), HashSet.Of(schemaId));
Assert.Equal(CreateValue(), result);
}
@ -242,7 +242,7 @@ namespace Squidex.Domain.Apps.Core.Operations.ExtractReferenceIds
var sut = Fields.References(1, "my-refs", Partitioning.Invariant);
var token = CreateValue(id1, id2);
var result = sut.CleanReferences(token, new HashSet<Guid>(new[] { Guid.NewGuid() }));
var result = sut.CleanReferences(token, HashSet.Of(Guid.NewGuid()));
Assert.Same(token, result);
}

7
tests/Squidex.Domain.Apps.Entities.Tests/Apps/Indexes/AppsByUserIndexGrainTests.cs

@ -9,6 +9,7 @@ using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using FakeItEasy;
using Squidex.Infrastructure;
using Squidex.Infrastructure.States;
using Xunit;
@ -63,11 +64,7 @@ namespace Squidex.Domain.Apps.Entities.Apps.Indexes
[Fact]
public async Task Should_replace_app_ids_on_rebuild()
{
var state = new HashSet<Guid>
{
appId1,
appId2
};
var state = HashSet.Of(appId1, appId2);
await sut.RebuildAsync(state);

3
tests/Squidex.Domain.Apps.Entities.Tests/Assets/OData/ODataQueryTests.cs

@ -15,6 +15,7 @@ using Squidex.Domain.Apps.Entities.Assets.Edm;
using Squidex.Domain.Apps.Entities.MongoDb.Assets;
using Squidex.Domain.Apps.Entities.MongoDb.Assets.Visitors;
using Squidex.Domain.Apps.Entities.Tags;
using Squidex.Infrastructure;
using Squidex.Infrastructure.MongoDb;
using Squidex.Infrastructure.MongoDb.OData;
using Xunit;
@ -38,7 +39,7 @@ namespace Squidex.Domain.Apps.Entities.Assets.OData
public ODataQueryTests()
{
A.CallTo(() => tagService.GetTagIdsAsync(appId, TagGroups.Assets, A<HashSet<string>>.That.Contains("tag1")))
.Returns(new HashSet<string>(new[] { "normalized1" }));
.Returns(HashSet.Of("normalized1"));
valueConverter = FindExtensions.CreateValueConverter(appId, tagService);
}

92
tests/Squidex.Domain.Apps.Entities.Tests/Tags/TagGrainTests.cs

@ -0,0 +1,92 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FakeItEasy;
using Squidex.Infrastructure;
using Squidex.Infrastructure.States;
using Xunit;
namespace Squidex.Domain.Apps.Entities.Tags
{
public class TagGrainTests
{
private readonly IStore<string> store = A.Fake<IStore<string>>();
private readonly IPersistence<TagGrain.State> persistence = A.Fake<IPersistence<TagGrain.State>>();
private readonly TagGrain sut;
public TagGrainTests()
{
A.CallTo(() => store.WithSnapshots(A<Type>.Ignored, A<string>.Ignored, A<Func<TagGrain.State, Task>>.Ignored))
.Returns(persistence);
sut = new TagGrain(store);
sut.OnActivateAsync(string.Empty).Wait();
}
[Fact]
public async Task Should_add_tags_to_grain()
{
var result1 = await sut.NormalizeTagsAsync(HashSet.Of("tag1", "tag2"), null);
var result2 = await sut.NormalizeTagsAsync(HashSet.Of("tag2", "tag3"), null);
var allTags = await sut.GetTagsAsync();
Assert.Equal(new Dictionary<string, int>
{
["tag1"] = 1,
["tag2"] = 2,
["tag3"] = 1
}, allTags);
}
[Fact]
public async Task Should_not_add_tags_if_already_added()
{
var result1 = await sut.NormalizeTagsAsync(HashSet.Of("tag1", "tag2"), null);
var result2 = await sut.NormalizeTagsAsync(HashSet.Of("tag1", "tag2", "tag3"), result1);
var allTags = await sut.GetTagsAsync();
Assert.Equal(new Dictionary<string, int>
{
["tag1"] = 1,
["tag2"] = 1,
["tag3"] = 1
}, allTags);
}
[Fact]
public async Task Should_remove_tags_from_grain()
{
var result1 = await sut.NormalizeTagsAsync(HashSet.Of("tag1", "tag2"), null);
var result2 = await sut.NormalizeTagsAsync(HashSet.Of("tag2", "tag3"), null);
await sut.NormalizeTagsAsync(null, result1);
var allTags = await sut.GetTagsAsync();
Assert.Equal(new Dictionary<string, int>
{
["tag2"] = 1,
["tag3"] = 1
}, allTags);
}
[Fact]
public async Task Should_resolve_tag_names()
{
var tagIds = await sut.NormalizeTagsAsync(HashSet.Of("tag1", "tag2"), null);
var tagNames = await sut.GetTagIdsAsync(HashSet.Of("tag1", "tag2", "invalid1"));
Assert.Equal(tagIds, tagNames);
}
}
}
Loading…
Cancel
Save