Browse Source

Add a test from support request.

pull/572/head
Sebastian 5 years ago
parent
commit
d9739048f1
  1. 24
      backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/GraphQLExecutionContext.cs
  2. 2
      backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/GraphQLModel.cs
  3. 10
      backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Extensions.cs
  4. 23
      backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/HandleRules/RuleEventFormatterTests.cs

24
backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/GraphQLExecutionContext.cs

@ -15,6 +15,7 @@ using Squidex.Domain.Apps.Core;
using Squidex.Domain.Apps.Entities.Assets;
using Squidex.Domain.Apps.Entities.Contents.GraphQL.Types;
using Squidex.Domain.Apps.Entities.Contents.Queries;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Json.Objects;
using Squidex.Infrastructure.Log;
@ -70,37 +71,37 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL
return await dataLoader.LoadAsync(id);
}
public async Task<IReadOnlyList<IEnrichedAssetEntity>> GetReferencedAssetsAsync(IJsonValue value)
public Task<IReadOnlyList<IEnrichedAssetEntity>> GetReferencedAssetsAsync(IJsonValue value)
{
var ids = ParseIds(value);
if (ids == null)
{
return EmptyAssets;
return Task.FromResult<IReadOnlyList<IEnrichedAssetEntity>>(EmptyAssets);
}
var dataLoader = GetAssetsLoader();
return await dataLoader.LoadManyAsync(ids);
return LoadManyAsync(dataLoader, ids);
}
public async Task<IReadOnlyList<IContentEntity>> GetReferencedContentsAsync(IJsonValue value)
public Task<IReadOnlyList<IContentEntity>> GetReferencedContentsAsync(IJsonValue value)
{
var ids = ParseIds(value);
if (ids == null)
{
return EmptyContents;
return Task.FromResult<IReadOnlyList<IContentEntity>>(EmptyContents);
}
var dataLoader = GetContentsLoader();
return await dataLoader.LoadManyAsync(ids);
return LoadManyAsync(dataLoader, ids);
}
private IDataLoader<Guid, IEnrichedAssetEntity> GetAssetsLoader()
{
return dataLoaderContextAccessor.Context.GetOrAddBatchLoader<Guid, IEnrichedAssetEntity>("Assets",
return dataLoaderContextAccessor.Context.GetOrAddBatchLoader<Guid, IEnrichedAssetEntity>(nameof(GetAssetsLoader),
async batch =>
{
var result = await GetReferencedAssetsAsync(new List<Guid>(batch));
@ -111,7 +112,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL
private IDataLoader<Guid, IContentEntity> GetContentsLoader()
{
return dataLoaderContextAccessor.Context.GetOrAddBatchLoader<Guid, IContentEntity>("References",
return dataLoaderContextAccessor.Context.GetOrAddBatchLoader<Guid, IContentEntity>(nameof(GetContentsLoader),
async batch =>
{
var result = await GetReferencedContentsAsync(new List<Guid>(batch));
@ -120,6 +121,13 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL
});
}
private static async Task<IReadOnlyList<T>> LoadManyAsync<TKey, T>(IDataLoader<TKey, T> dataLoader, ICollection<TKey> keys) where T : class
{
var contents = await Task.WhenAll(keys.Select(dataLoader.LoadAsync));
return contents.NotNull().ToList();
}
private static ICollection<Guid>? ParseIds(IJsonValue value)
{
try

2
backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/GraphQLModel.cs

@ -153,7 +153,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL
public IObjectGraphType GetAssetType()
{
return assetType as IObjectGraphType;
return assetType;
}
public IObjectGraphType GetContentType(Guid schemaId)

10
backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Extensions.cs

@ -7,10 +7,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using GraphQL.DataLoader;
using Squidex.Domain.Apps.Core.Schemas;
using Squidex.Infrastructure;
using Squidex.Text;
namespace Squidex.Domain.Apps.Entities.Contents.GraphQL.Types
@ -47,12 +44,5 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL.Types
return value;
}
public static async Task<IReadOnlyList<T>> LoadManyAsync<TKey, T>(this IDataLoader<TKey, T> dataLoader, ICollection<TKey> keys) where T : class
{
var contents = await Task.WhenAll(keys.Select(dataLoader.LoadAsync));
return contents.NotNull().ToList();
}
}
}

23
backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/HandleRules/RuleEventFormatterTests.cs

@ -279,5 +279,28 @@ namespace Squidex.Domain.Apps.Core.Operations.HandleRules
Assert.Equal("Created", result);
}
[Fact]
public async Task Should_return_json_string_when_array()
{
var @event = new EnrichedContentEvent
{
Data =
new NamedContentData()
.AddField("categories",
new ContentFieldData()
.AddJsonValue(JsonValue.Array("ref1", "ref2", "ref3")))
};
var script = @"
Script(JSON.stringify(
{
'categories': event.data.categories.iv
}))";
var result = await sut.FormatAsync(script, @event);
Assert.Equal("{'categories':['ref1','ref2','ref3']}", result?.Replace(" ", string.Empty).Replace("\"", "'"));
}
}
}

Loading…
Cancel
Save