From d5302806f1eb1934243b3dc029ee6b2fbf1a38f5 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 22 Jun 2020 18:15:33 +0200 Subject: [PATCH] Tests for scripting. --- .../ContentScriptingTests.cs | 62 +++++++++++++++++++ .../TestSuite.Shared/Model/TestEntity.cs | 8 ++- 2 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 backend/tools/TestSuite/TestSuite.ApiTests/ContentScriptingTests.cs diff --git a/backend/tools/TestSuite/TestSuite.ApiTests/ContentScriptingTests.cs b/backend/tools/TestSuite/TestSuite.ApiTests/ContentScriptingTests.cs new file mode 100644 index 000000000..4fc29cb94 --- /dev/null +++ b/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 + { + 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(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); + } + } +} diff --git a/backend/tools/TestSuite/TestSuite.Shared/Model/TestEntity.cs b/backend/tools/TestSuite/TestSuite.Shared/Model/TestEntity.cs index 0c8d243e9..2ae39378c 100644 --- a/backend/tools/TestSuite/TestSuite.Shared/Model/TestEntity.cs +++ b/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; }