From daffe60c0d379f11da92bb269bb6d87aaeb2b57f Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 24 Feb 2020 17:56:26 +0100 Subject: [PATCH] Graphql tests --- .../Contents/Queries/Steps/ConvertData.cs | 2 +- .../EventSourcing/StreamPosition.cs | 11 +- .../MongoDb/BsonHelper.cs | 2 +- .../TestSuite.ApiTests/AssetTests.cs | 1 - .../TestSuite.ApiTests/ContentQueryTests.cs | 10 +- .../TestSuite.ApiTests/GraphQLTests.cs | 238 ++++++++++++++++++ .../TestSuite.ApiTests/LanguagesTests.cs | 1 - .../Fixtures/ContentQueryFixture.cs | 1 - .../Fixtures/CreatedAppFixture.cs | 4 +- 9 files changed, 255 insertions(+), 15 deletions(-) create mode 100644 backend/tools/TestSuite/TestSuite.ApiTests/GraphQLTests.cs diff --git a/backend/src/Squidex.Domain.Apps.Entities/Contents/Queries/Steps/ConvertData.cs b/backend/src/Squidex.Domain.Apps.Entities/Contents/Queries/Steps/ConvertData.cs index 8352afb08..5a4984dd6 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Contents/Queries/Steps/ConvertData.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Contents/Queries/Steps/ConvertData.cs @@ -87,7 +87,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.Queries.Steps private async Task> QueryContentIdsAsync(Context context, HashSet ids) { - var result = await contentRepository.QueryIdsAsync(context.App.Id, ids, context.ShouldProvideUnpublished() ? SearchScope.All : SearchScope.Published); + var result = await contentRepository.QueryIdsAsync(context.App.Id, ids, context.Scope()); return result.Select(x => x.Id); } diff --git a/backend/src/Squidex.Infrastructure.MongoDb/EventSourcing/StreamPosition.cs b/backend/src/Squidex.Infrastructure.MongoDb/EventSourcing/StreamPosition.cs index 81e9e8fd8..fdd4be6d4 100644 --- a/backend/src/Squidex.Infrastructure.MongoDb/EventSourcing/StreamPosition.cs +++ b/backend/src/Squidex.Infrastructure.MongoDb/EventSourcing/StreamPosition.cs @@ -38,11 +38,11 @@ namespace Squidex.Infrastructure.EventSourcing var sb = new StringBuilder(); sb.Append(position.Timestamp.Timestamp); - sb.Append(","); + sb.Append("-"); sb.Append(position.Timestamp.Increment); - sb.Append(","); + sb.Append("-"); sb.Append(position.CommitOffset); - sb.Append(","); + sb.Append("-"); sb.Append(position.CommitSize); return sb.ToString(); @@ -54,7 +54,10 @@ namespace Squidex.Infrastructure.EventSourcing { var parts = position.Split('-'); - return new StreamPosition(new BsonTimestamp(int.Parse(parts[0]), int.Parse(parts[1])), long.Parse(parts[2]), long.Parse(parts[3])); + return new StreamPosition( + new BsonTimestamp(int.Parse(parts[0]), int.Parse(parts[1])), + long.Parse(parts[2]), + long.Parse(parts[3])); } return new StreamPosition(EmptyTimestamp, -1, -1); diff --git a/backend/src/Squidex.Infrastructure.MongoDb/MongoDb/BsonHelper.cs b/backend/src/Squidex.Infrastructure.MongoDb/MongoDb/BsonHelper.cs index dc6c45fb4..9e5370191 100644 --- a/backend/src/Squidex.Infrastructure.MongoDb/MongoDb/BsonHelper.cs +++ b/backend/src/Squidex.Infrastructure.MongoDb/MongoDb/BsonHelper.cs @@ -10,7 +10,7 @@ namespace Squidex.Infrastructure.MongoDb public static class BsonHelper { private const string TypeBson = "§type"; - private const string TypeJson = "$json"; + private const string TypeJson = "$type"; public static string UnescapeBson(this string value) { diff --git a/backend/tools/TestSuite/TestSuite.ApiTests/AssetTests.cs b/backend/tools/TestSuite/TestSuite.ApiTests/AssetTests.cs index c30e9be78..3ec7d5a5a 100644 --- a/backend/tools/TestSuite/TestSuite.ApiTests/AssetTests.cs +++ b/backend/tools/TestSuite/TestSuite.ApiTests/AssetTests.cs @@ -8,7 +8,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; using System.Net.Http; using System.Threading.Tasks; using Squidex.ClientLibrary.Management; diff --git a/backend/tools/TestSuite/TestSuite.ApiTests/ContentQueryTests.cs b/backend/tools/TestSuite/TestSuite.ApiTests/ContentQueryTests.cs index d9b685e2d..80dc23b08 100644 --- a/backend/tools/TestSuite/TestSuite.ApiTests/ContentQueryTests.cs +++ b/backend/tools/TestSuite/TestSuite.ApiTests/ContentQueryTests.cs @@ -188,12 +188,12 @@ namespace TestSuite.ApiTests query = @" { queryMyReadsContents(filter: ""data/number/iv gt 3 and data/number/iv lt 7"", orderby: ""data/number/iv asc"") { - id, - data { - number { - iv + id, + data { + number { + iv + } } - } } }" }; diff --git a/backend/tools/TestSuite/TestSuite.ApiTests/GraphQLTests.cs b/backend/tools/TestSuite/TestSuite.ApiTests/GraphQLTests.cs new file mode 100644 index 000000000..d7973331b --- /dev/null +++ b/backend/tools/TestSuite/TestSuite.ApiTests/GraphQLTests.cs @@ -0,0 +1,238 @@ +// ========================================================================== +// Squidex Headless CMS +// ========================================================================== +// Copyright (c) Squidex UG (haftungsbeschraenkt) +// All rights reserved. Licensed under the MIT license. +// ========================================================================== + +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Newtonsoft.Json.Linq; +using Squidex.ClientLibrary; +using Squidex.ClientLibrary.Management; +using TestSuite.Fixtures; +using Xunit; + +#pragma warning disable SA1300 // Element should begin with upper-case letter +#pragma warning disable SA1507 // Code should not contain multiple blank lines in a row +#pragma warning disable RECS0022 // A catch clause that catches System.Exception and has an empty body + +namespace TestSuite.ApiTests +{ + public sealed class GraphQLTests : IClassFixture + { + public CreatedAppFixture _ { get; } + + public GraphQLTests(CreatedAppFixture fixture) + { + _ = fixture; + } + + public sealed class DynamicEntity : Content + { + } + + public sealed class Country + { + public CountryData Data { get; set; } + } + + public sealed class CountryData + { + public string Name { get; set; } + + public List States { get; set; } + } + + public sealed class State + { + public StateData Data { get; set; } + } + + public sealed class StateData + { + public string Name { get; set; } + + public List Cities { get; set; } + } + + public sealed class City + { + public CityData Data { get; set; } + } + + public sealed class CityData + { + public string Name { get; set; } + } + + [Fact] + public async Task Should_create_and_query_with_graphql() + { + try + { + await CreateSchemasAsync(); + await CreateContentsAsync(); + } + catch + { + // Do nothing + } + + var countriesClient = _.ClientManager.CreateContentsClient("countries"); + + var query = new + { + query = @" + { + queryCountriesContents { + data: flatData { + name, + states { + data: flatData { + name + cities { + data:flatData { + name + } + } + } + } + } + } + }" + }; + + var result1 = await countriesClient.GraphQlAsync(query); + + var typed = result1["queryCountriesContents"].ToObject>(); + + Assert.Equal("Leipzig", typed[0].Data.States[0].Data.Cities[0].Data.Name); + } + + private async Task CreateSchemasAsync() + { + // STEP 1: Create cities schema. + var createCitiesRequest = new CreateSchemaDto + { + Name = "cities", + Fields = new List + { + new UpsertSchemaFieldDto + { + Name = "name", + Properties = new StringFieldPropertiesDto() + } + }, + IsPublished = true + }; + + var cities = await _.Schemas.PostSchemaAsync(_.AppName, createCitiesRequest); + + + // STEP 2: Create states schema. + var createStatesRequest = new CreateSchemaDto + { + Name = "states", + Fields = new List + { + new UpsertSchemaFieldDto + { + Name = "name", + Properties = new StringFieldPropertiesDto() + }, + new UpsertSchemaFieldDto + { + Name = "cities", + Properties = new ReferencesFieldPropertiesDto + { + SchemaIds = new List { cities.Id } + } + } + }, + IsPublished = true + }; + + var states = await _.Schemas.PostSchemaAsync(_.AppName, createStatesRequest); + + + // STEP 3: Create countries schema. + var createCountriesRequest = new CreateSchemaDto + { + Name = "countries", + Fields = new List + { + new UpsertSchemaFieldDto + { + Name = "name", + Properties = new StringFieldPropertiesDto() + }, + new UpsertSchemaFieldDto + { + Name = "states", + Properties = new ReferencesFieldPropertiesDto + { + SchemaIds = new List { states.Id } + } + } + }, + IsPublished = true + }; + + await _.Schemas.PostSchemaAsync(_.AppName, createCountriesRequest); + } + + private async Task CreateContentsAsync() + { + // STEP 1: Create city + var cityData = new + { + name = new + { + iv = "Leipzig" + } + }; + + var citiesClient = _.ClientManager.CreateContentsClient("cities"); + + var city = await citiesClient.CreateAsync(cityData, true); + + + // STEP 2: Create city + var stateData = new + { + name = new + { + iv = "Saxony" + }, + cities = new + { + iv = new[] { city.Id } + } + }; + + var statesClient = _.ClientManager.CreateContentsClient("states"); + + var state = await statesClient.CreateAsync(stateData, true); + + + // STEP 3: Create country + var countryData = new + { + name = new + { + iv = "Germany" + }, + states = new + { + iv = new[] { state.Id } + } + }; + + var countriesClient = _.ClientManager.CreateContentsClient("countries"); + + await countriesClient.CreateAsync(countryData, true); + } + } +} diff --git a/backend/tools/TestSuite/TestSuite.ApiTests/LanguagesTests.cs b/backend/tools/TestSuite/TestSuite.ApiTests/LanguagesTests.cs index d583d5276..5aaf19cf3 100644 --- a/backend/tools/TestSuite/TestSuite.ApiTests/LanguagesTests.cs +++ b/backend/tools/TestSuite/TestSuite.ApiTests/LanguagesTests.cs @@ -5,7 +5,6 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== -using System; using System.Threading.Tasks; using TestSuite.Fixtures; using Xunit; diff --git a/backend/tools/TestSuite/TestSuite.Shared/Fixtures/ContentQueryFixture.cs b/backend/tools/TestSuite/TestSuite.Shared/Fixtures/ContentQueryFixture.cs index b2387f46a..13e834f58 100644 --- a/backend/tools/TestSuite/TestSuite.Shared/Fixtures/ContentQueryFixture.cs +++ b/backend/tools/TestSuite/TestSuite.Shared/Fixtures/ContentQueryFixture.cs @@ -6,7 +6,6 @@ // ========================================================================== using System.Threading.Tasks; -using Squidex.ClientLibrary; using TestSuite.Model; namespace TestSuite.Fixtures diff --git a/backend/tools/TestSuite/TestSuite.Shared/Fixtures/CreatedAppFixture.cs b/backend/tools/TestSuite/TestSuite.Shared/Fixtures/CreatedAppFixture.cs index 322d04b7c..3302f869b 100644 --- a/backend/tools/TestSuite/TestSuite.Shared/Fixtures/CreatedAppFixture.cs +++ b/backend/tools/TestSuite/TestSuite.Shared/Fixtures/CreatedAppFixture.cs @@ -28,7 +28,9 @@ namespace TestSuite.Fixtures } } - await Apps.PostContributorAsync(AppName, new AssignContributorDto { ContributorId = "sebastian@squidex.io", Invite = true, Role = "Owner" }); + var invite = new AssignContributorDto { ContributorId = "sebastian@squidex.io", Invite = true, Role = "Owner" }; + + await Apps.PostContributorAsync(AppName, invite); }).Wait(); } }