Browse Source

Tests for scripting.

pull/539/head
Sebastian 6 years ago
parent
commit
d5302806f1
  1. 62
      backend/tools/TestSuite/TestSuite.ApiTests/ContentScriptingTests.cs
  2. 8
      backend/tools/TestSuite/TestSuite.Shared/Model/TestEntity.cs

62
backend/tools/TestSuite/TestSuite.ApiTests/ContentScriptingTests.cs

@ -0,0 +1,62 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System;
using System.Threading.Tasks;
using Squidex.ClientLibrary.Management;
using TestSuite.Fixtures;
using TestSuite.Model;
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
namespace TestSuite.ApiTests
{
public class ContentScriptingTests : IClassFixture<CreatedAppFixture>
{
public CreatedAppFixture _ { get; }
public ContentScriptingTests(CreatedAppFixture fixture)
{
_ = fixture;
}
[Fact]
public async Task Should_use_creating_and_query_tests()
{
var schemaName = $"schema-{DateTime.UtcNow.Ticks}";
// STEP 1: Create a schema.
await TestEntity.CreateSchemaAsync(_.Schemas, _.AppName, schemaName);
// STEP 2: Set scripts
await _.Schemas.PutScriptsAsync(_.AppName, schemaName, new SchemaScriptsDto
{
Query = @$"
ctx.data.{TestEntityData.StringField}.iv = ctx.data.{TestEntityData.StringField}.iv + '_Updated'
replace()",
Create = @$"
ctx.data.{TestEntityData.NumberField}.iv *= 2;
replace()"
});
// STEP 3: Create content
var contents = _.ClientManager.CreateContentsClient<TestEntity, TestEntityData>(schemaName);
var data = new TestEntityData { Number = 13, String = "Hello" };
var content = await contents.CreateAsync(data);
Assert.Equal(26, content.Data.Number);
Assert.Equal("Hello_Updated", content.Data.String);
}
}
}

8
backend/tools/TestSuite/TestSuite.Shared/Model/TestEntity.cs

@ -24,7 +24,7 @@ namespace TestSuite.Model
{
new UpsertSchemaFieldDto
{
Name = nameof(TestEntityData.Number).ToLowerInvariant(),
Name = TestEntityData.NumberField,
Properties = new NumberFieldPropertiesDto
{
IsRequired = true
@ -32,7 +32,7 @@ namespace TestSuite.Model
},
new UpsertSchemaFieldDto
{
Name = nameof(TestEntityData.String).ToLowerInvariant(),
Name = TestEntityData.StringField,
Properties = new StringFieldPropertiesDto
{
IsRequired = false
@ -48,6 +48,10 @@ namespace TestSuite.Model
public sealed class TestEntityData
{
public static readonly string StringField = nameof(String).ToLowerInvariant();
public static readonly string NumberField = nameof(Number).ToLowerInvariant();
[JsonConverter(typeof(InvariantConverter))]
public int Number { get; set; }

Loading…
Cancel
Save