Browse Source

Graphql tests

pull/492/head
Sebastian 6 years ago
parent
commit
daffe60c0d
  1. 2
      backend/src/Squidex.Domain.Apps.Entities/Contents/Queries/Steps/ConvertData.cs
  2. 11
      backend/src/Squidex.Infrastructure.MongoDb/EventSourcing/StreamPosition.cs
  3. 2
      backend/src/Squidex.Infrastructure.MongoDb/MongoDb/BsonHelper.cs
  4. 1
      backend/tools/TestSuite/TestSuite.ApiTests/AssetTests.cs
  5. 238
      backend/tools/TestSuite/TestSuite.ApiTests/GraphQLTests.cs
  6. 1
      backend/tools/TestSuite/TestSuite.ApiTests/LanguagesTests.cs
  7. 1
      backend/tools/TestSuite/TestSuite.Shared/Fixtures/ContentQueryFixture.cs
  8. 4
      backend/tools/TestSuite/TestSuite.Shared/Fixtures/CreatedAppFixture.cs

2
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<IEnumerable<Guid>> QueryContentIdsAsync(Context context, HashSet<Guid> ids) private async Task<IEnumerable<Guid>> QueryContentIdsAsync(Context context, HashSet<Guid> 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); return result.Select(x => x.Id);
} }

11
backend/src/Squidex.Infrastructure.MongoDb/EventSourcing/StreamPosition.cs

@ -38,11 +38,11 @@ namespace Squidex.Infrastructure.EventSourcing
var sb = new StringBuilder(); var sb = new StringBuilder();
sb.Append(position.Timestamp.Timestamp); sb.Append(position.Timestamp.Timestamp);
sb.Append(","); sb.Append("-");
sb.Append(position.Timestamp.Increment); sb.Append(position.Timestamp.Increment);
sb.Append(","); sb.Append("-");
sb.Append(position.CommitOffset); sb.Append(position.CommitOffset);
sb.Append(","); sb.Append("-");
sb.Append(position.CommitSize); sb.Append(position.CommitSize);
return sb.ToString(); return sb.ToString();
@ -54,7 +54,10 @@ namespace Squidex.Infrastructure.EventSourcing
{ {
var parts = position.Split('-'); 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); return new StreamPosition(EmptyTimestamp, -1, -1);

2
backend/src/Squidex.Infrastructure.MongoDb/MongoDb/BsonHelper.cs

@ -10,7 +10,7 @@ namespace Squidex.Infrastructure.MongoDb
public static class BsonHelper public static class BsonHelper
{ {
private const string TypeBson = "§type"; private const string TypeBson = "§type";
private const string TypeJson = "$json"; private const string TypeJson = "$type";
public static string UnescapeBson(this string value) public static string UnescapeBson(this string value)
{ {

1
backend/tools/TestSuite/TestSuite.ApiTests/AssetTests.cs

@ -8,7 +8,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using Squidex.ClientLibrary.Management; using Squidex.ClientLibrary.Management;

238
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<CreatedAppFixture>
{
public CreatedAppFixture _ { get; }
public GraphQLTests(CreatedAppFixture fixture)
{
_ = fixture;
}
public sealed class DynamicEntity : Content<object>
{
}
public sealed class Country
{
public CountryData Data { get; set; }
}
public sealed class CountryData
{
public string Name { get; set; }
public List<State> States { get; set; }
}
public sealed class State
{
public StateData Data { get; set; }
}
public sealed class StateData
{
public string Name { get; set; }
public List<City> 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<DynamicEntity, object>("countries");
var query = new
{
query = @"
{
queryCountriesContents {
data: flatData {
name,
states {
data: flatData {
name
cities {
data:flatData {
name
}
}
}
}
}
}
}"
};
var result1 = await countriesClient.GraphQlAsync<JToken>(query);
var typed = result1["queryCountriesContents"].ToObject<List<Country>>();
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<UpsertSchemaFieldDto>
{
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<UpsertSchemaFieldDto>
{
new UpsertSchemaFieldDto
{
Name = "name",
Properties = new StringFieldPropertiesDto()
},
new UpsertSchemaFieldDto
{
Name = "cities",
Properties = new ReferencesFieldPropertiesDto
{
SchemaIds = new List<Guid> { 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<UpsertSchemaFieldDto>
{
new UpsertSchemaFieldDto
{
Name = "name",
Properties = new StringFieldPropertiesDto()
},
new UpsertSchemaFieldDto
{
Name = "states",
Properties = new ReferencesFieldPropertiesDto
{
SchemaIds = new List<Guid> { 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<DynamicEntity, object>("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<DynamicEntity, object>("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<DynamicEntity, object>("countries");
await countriesClient.CreateAsync(countryData, true);
}
}
}

1
backend/tools/TestSuite/TestSuite.ApiTests/LanguagesTests.cs

@ -5,7 +5,6 @@
// All rights reserved. Licensed under the MIT license. // All rights reserved. Licensed under the MIT license.
// ========================================================================== // ==========================================================================
using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using TestSuite.Fixtures; using TestSuite.Fixtures;
using Xunit; using Xunit;

1
backend/tools/TestSuite/TestSuite.Shared/Fixtures/ContentQueryFixture.cs

@ -6,7 +6,6 @@
// ========================================================================== // ==========================================================================
using System.Threading.Tasks; using System.Threading.Tasks;
using Squidex.ClientLibrary;
using TestSuite.Model; using TestSuite.Model;
namespace TestSuite.Fixtures namespace TestSuite.Fixtures

4
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(); }).Wait();
} }
} }

Loading…
Cancel
Save