From fe960ba402578aa250b3b631e1593b53ccda2fbc Mon Sep 17 00:00:00 2001 From: Florian Rohrer Date: Fri, 19 May 2023 08:28:01 +0200 Subject: [PATCH 1/9] start working on SchemaValidatorTests --- src/Backend/Confix.sln | 7 +++++ .../src/Authoring.Core/InternalsVisibleTo.cs | 1 + .../Schema/Services/SchemaValidator.cs | 2 +- .../Authoring.Core.Tests.csproj | 13 ++++++++ .../Authoring.Core.Tests/SchemaValidator.cs | 30 +++++++++++++++++++ 5 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 src/Backend/test/Authoring.Core.Tests/Authoring.Core.Tests.csproj create mode 100644 src/Backend/test/Authoring.Core.Tests/SchemaValidator.cs diff --git a/src/Backend/Confix.sln b/src/Backend/Confix.sln index b2103052..d80e0322 100644 --- a/src/Backend/Confix.sln +++ b/src/Backend/Confix.sln @@ -75,6 +75,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Authoring.Authentication.Te EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Authoring.Integration.Tests", "test\Authoring.Integration.Tests\Authoring.Integration.Tests.csproj", "{A6467E98-B5F2-488A-984D-D80C158B49C2}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Authoring.Core.Tests", "test\Authoring.Core.Tests\Authoring.Core.Tests.csproj", "{99F7E325-A61C-46EF-9F66-641C6BA23F14}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -220,6 +222,10 @@ Global {A6467E98-B5F2-488A-984D-D80C158B49C2}.Debug|Any CPU.Build.0 = Debug|Any CPU {A6467E98-B5F2-488A-984D-D80C158B49C2}.Release|Any CPU.ActiveCfg = Release|Any CPU {A6467E98-B5F2-488A-984D-D80C158B49C2}.Release|Any CPU.Build.0 = Release|Any CPU + {99F7E325-A61C-46EF-9F66-641C6BA23F14}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {99F7E325-A61C-46EF-9F66-641C6BA23F14}.Debug|Any CPU.Build.0 = Debug|Any CPU + {99F7E325-A61C-46EF-9F66-641C6BA23F14}.Release|Any CPU.ActiveCfg = Release|Any CPU + {99F7E325-A61C-46EF-9F66-641C6BA23F14}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {A506ADEE-0136-4A7B-B3C8-0D23C176F4DE} = {8C4B0F32-7C73-4C83-80AF-CDD992121D3D} @@ -256,5 +262,6 @@ Global {3957135B-E458-44D1-9C9B-9D7AD6220638} = {8C4B0F32-7C73-4C83-80AF-CDD992121D3D} {D7B34C75-E53E-4DAD-8ADD-C92D7576C714} = {D40302DA-D28B-4601-BD53-3A7D4405826B} {A6467E98-B5F2-488A-984D-D80C158B49C2} = {D40302DA-D28B-4601-BD53-3A7D4405826B} + {99F7E325-A61C-46EF-9F66-641C6BA23F14} = {D40302DA-D28B-4601-BD53-3A7D4405826B} EndGlobalSection EndGlobal diff --git a/src/Backend/src/Authoring.Core/InternalsVisibleTo.cs b/src/Backend/src/Authoring.Core/InternalsVisibleTo.cs index a32f9c37..eb882b70 100644 --- a/src/Backend/src/Authoring.Core/InternalsVisibleTo.cs +++ b/src/Backend/src/Authoring.Core/InternalsVisibleTo.cs @@ -2,3 +2,4 @@ using Confix.Authentication.ApiKey; [assembly: InternalsVisibleTo("Confix.Authoring.GraphQL")] +[assembly: InternalsVisibleTo("Confix.Authoring.Core.Tests")] diff --git a/src/Backend/src/Authoring.Core/Schema/Services/SchemaValidator.cs b/src/Backend/src/Authoring.Core/Schema/Services/SchemaValidator.cs index 46ff6b50..92393369 100644 --- a/src/Backend/src/Authoring.Core/Schema/Services/SchemaValidator.cs +++ b/src/Backend/src/Authoring.Core/Schema/Services/SchemaValidator.cs @@ -63,5 +63,5 @@ private ISchema CreateSchema(string schemaSdl) })!; private static string CacheKey(string input) - => Encoding.UTF8.GetString(SHA256.HashData(Encoding.UTF8.GetBytes(input))); + => "schema." + Encoding.UTF8.GetString(SHA256.HashData(Encoding.UTF8.GetBytes(input))); } diff --git a/src/Backend/test/Authoring.Core.Tests/Authoring.Core.Tests.csproj b/src/Backend/test/Authoring.Core.Tests/Authoring.Core.Tests.csproj new file mode 100644 index 00000000..dc6ba729 --- /dev/null +++ b/src/Backend/test/Authoring.Core.Tests/Authoring.Core.Tests.csproj @@ -0,0 +1,13 @@ + + + + Confix.Authoring.Core.Tests + Confix.Authoring.Core.Tests + + + + + + + + diff --git a/src/Backend/test/Authoring.Core.Tests/SchemaValidator.cs b/src/Backend/test/Authoring.Core.Tests/SchemaValidator.cs new file mode 100644 index 00000000..cb8238e6 --- /dev/null +++ b/src/Backend/test/Authoring.Core.Tests/SchemaValidator.cs @@ -0,0 +1,30 @@ +using Squadron; +using Xunit; +using Microsoft.Extensions.Caching.Memory; +using Confix.Authoring.Internal; + +namespace Confix.Authoring.Core.Tests; + +public class SchemaValidatorTests +{ + [Theory] + [InlineData( + """ + type Component { + someText: String! + someNumber: Int! + someDouble: Double! + someBoolean: Boolean! + } + """)] + public void ValidateSchema_ValidSchemas_NoException(string schema) + { + // Arrange + SchemaValidator validator = new(Mock.Of()); + + // Act && Assert + validator.ValidateSchema(schema); + } + + +} From 23434b609e609b257c4d39fb05677e8204c438d9 Mon Sep 17 00:00:00 2001 From: Florian Rohrer Date: Fri, 19 May 2023 09:04:25 +0200 Subject: [PATCH 2/9] Validate Schema Tests --- .../Authoring.Core.Tests/SchemaValidator.cs | 113 +++++++++++++++--- 1 file changed, 95 insertions(+), 18 deletions(-) diff --git a/src/Backend/test/Authoring.Core.Tests/SchemaValidator.cs b/src/Backend/test/Authoring.Core.Tests/SchemaValidator.cs index cb8238e6..c903490b 100644 --- a/src/Backend/test/Authoring.Core.Tests/SchemaValidator.cs +++ b/src/Backend/test/Authoring.Core.Tests/SchemaValidator.cs @@ -1,30 +1,107 @@ -using Squadron; -using Xunit; using Microsoft.Extensions.Caching.Memory; -using Confix.Authoring.Internal; +using Confix.Authoring.Internal; +using Microsoft.Extensions.DependencyInjection; namespace Confix.Authoring.Core.Tests; public class SchemaValidatorTests { - [Theory] - [InlineData( - """ - type Component { + [Fact] + public void ValidateSchema_Primitives_Valid() + => AssertValidSchema(""" + type Configuration { + someText: String! + someNumber: Int! + someFloat: Float! + someLong: Long! + someBoolean: Boolean! + } + """); + + [Fact] + public void ValidateSchema_Enums_Valid() + => AssertValidSchema(""" + type Configuration { + someText: String! + someEnum: ConfigurationEnum! + } + + enum ConfigurationEnum { + OPTION_1 + OPTION_2 + OPTION_3 + } + """); + + [Fact] + public void ValidateSchema_Arrays_Valid() + => AssertValidSchema(""" + type Configuration { someText: String! - someNumber: Int! - someDouble: Double! - someBoolean: Boolean! + someNumbers: [Int!]! + someStrings: [String]! + someCustom: [Custom!] + someCustom2: [Custom]! + someCustom3: [Custom!]! } - """)] - public void ValidateSchema_ValidSchemas_NoException(string schema) - { - // Arrange - SchemaValidator validator = new(Mock.Of()); - // Act && Assert - validator.ValidateSchema(schema); - } + type Custom { + unicornCount: Int! + } + """); + + [Fact] + public void ValidateSchema_Unions_Valid() + => AssertValidSchema(""" + type Configuration { + someField: UnionOfPrimitivesOrCustomTypes! + } + + union UnionOfPrimitivesOrCustomTypes = PrimitiveUnion | CustomTypeUnion + union PrimitiveUnion = String | Int | Boolean + union CustomTypeUnion = CustomType1 | CustomType2 + + type CustomType1 { + field1: String! + } + type CustomType2 { + field2: Int! + } + """); + [Fact] + public void ValidateSchema_Nested_Valid() + => AssertValidSchema(""" + type Configuration { + someText: String! + level1: Level1! + } + + type Level1 { + field1: String! + level2: Level2! + } + + type Level2 { + field2: String! + level3: Level3! + } + + type Level3 { + field3: String! + } + """); + + private void AssertValidSchema(string schema) + => new SchemaValidator(GetMemoryCache()).ValidateSchema(schema); + + private IMemoryCache GetMemoryCache() + { + var services = new ServiceCollection(); + services.AddMemoryCache(); + var serviceProvider = services.BuildServiceProvider(); + + return serviceProvider.GetRequiredService(); + } } From 489b4736ce287f66dbc712b22f13677f462c9048 Mon Sep 17 00:00:00 2001 From: Florian Rohrer Date: Fri, 19 May 2023 09:37:10 +0200 Subject: [PATCH 3/9] fix validate unions test --- .../Schema/Exceptions/InvalidSchemaException.cs | 2 +- src/Backend/test/Authoring.Core.Tests/SchemaValidator.cs | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Backend/src/Authoring.Abstractions/Schema/Exceptions/InvalidSchemaException.cs b/src/Backend/src/Authoring.Abstractions/Schema/Exceptions/InvalidSchemaException.cs index 3b98e854..d236e665 100644 --- a/src/Backend/src/Authoring.Abstractions/Schema/Exceptions/InvalidSchemaException.cs +++ b/src/Backend/src/Authoring.Abstractions/Schema/Exceptions/InvalidSchemaException.cs @@ -8,4 +8,4 @@ public InvalidSchemaException(GraphQLSchemaError[] errors) : base("The Provided public GraphQLSchemaError[] Errors; } -public record GraphQLSchemaError(string message); +public record GraphQLSchemaError(string Message); diff --git a/src/Backend/test/Authoring.Core.Tests/SchemaValidator.cs b/src/Backend/test/Authoring.Core.Tests/SchemaValidator.cs index c903490b..cc818958 100644 --- a/src/Backend/test/Authoring.Core.Tests/SchemaValidator.cs +++ b/src/Backend/test/Authoring.Core.Tests/SchemaValidator.cs @@ -54,11 +54,9 @@ public void ValidateSchema_Arrays_Valid() public void ValidateSchema_Unions_Valid() => AssertValidSchema(""" type Configuration { - someField: UnionOfPrimitivesOrCustomTypes! + someField: CustomTypeUnion! } - union UnionOfPrimitivesOrCustomTypes = PrimitiveUnion | CustomTypeUnion - union PrimitiveUnion = String | Int | Boolean union CustomTypeUnion = CustomType1 | CustomType2 type CustomType1 { From c9a74592a33cbd2eceee2954170f7cd55bda7f84 Mon Sep 17 00:00:00 2001 From: Florian Rohrer Date: Fri, 19 May 2023 09:39:05 +0200 Subject: [PATCH 4/9] ValidateSchema_WithComments_Valid --- .../{SchemaValidator.cs => SchemaValidatorTests.cs} | 9 +++++++++ 1 file changed, 9 insertions(+) rename src/Backend/test/Authoring.Core.Tests/{SchemaValidator.cs => SchemaValidatorTests.cs} (91%) diff --git a/src/Backend/test/Authoring.Core.Tests/SchemaValidator.cs b/src/Backend/test/Authoring.Core.Tests/SchemaValidatorTests.cs similarity index 91% rename from src/Backend/test/Authoring.Core.Tests/SchemaValidator.cs rename to src/Backend/test/Authoring.Core.Tests/SchemaValidatorTests.cs index cc818958..067f3214 100644 --- a/src/Backend/test/Authoring.Core.Tests/SchemaValidator.cs +++ b/src/Backend/test/Authoring.Core.Tests/SchemaValidatorTests.cs @@ -91,6 +91,15 @@ public void ValidateSchema_Nested_Valid() } """); + [Fact] + public void ValidateSchema_WithComments_Valid() + => AssertValidSchema(""" + type Configuration { + # some comment to make it clear + someText: String! + } + """); + private void AssertValidSchema(string schema) => new SchemaValidator(GetMemoryCache()).ValidateSchema(schema); From eb01ae820a3e308b81770f84afc6dd37302e844d Mon Sep 17 00:00:00 2001 From: Florian Rohrer Date: Fri, 19 May 2023 09:57:18 +0200 Subject: [PATCH 5/9] Invalid schema tests --- .../Schema/Services/SchemaValidator.cs | 9 ++++- .../SchemaValidatorTests.cs | 34 +++++++++++++++++++ ...ValidateSchema_MissingBrace_Exception.snap | 5 +++ ...s.ValidateSchema_NoRootType_Exception.snap | 5 +++ ....ValidateSchema_UnknownType_Exception.snap | 5 +++ 5 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/Backend/test/Authoring.Core.Tests/__snapshots__/SchemaValidatorTests.ValidateSchema_MissingBrace_Exception.snap create mode 100644 src/Backend/test/Authoring.Core.Tests/__snapshots__/SchemaValidatorTests.ValidateSchema_NoRootType_Exception.snap create mode 100644 src/Backend/test/Authoring.Core.Tests/__snapshots__/SchemaValidatorTests.ValidateSchema_UnknownType_Exception.snap diff --git a/src/Backend/src/Authoring.Core/Schema/Services/SchemaValidator.cs b/src/Backend/src/Authoring.Core/Schema/Services/SchemaValidator.cs index 92393369..c8a4e62a 100644 --- a/src/Backend/src/Authoring.Core/Schema/Services/SchemaValidator.cs +++ b/src/Backend/src/Authoring.Core/Schema/Services/SchemaValidator.cs @@ -27,6 +27,13 @@ public void ValidateSchema(string schemaSdl) { throw new InvalidSchemaException(ex.Errors.Select(e => new GraphQLSchemaError(e.Message)).ToArray()); } + catch (SyntaxException) + { + throw new InvalidSchemaException( + new GraphQLSchemaError[] { + new("Could not parse Schema") + }); + } } public void ValidateValues(JsonElement values, string schemaSdl) @@ -57,7 +64,7 @@ private ISchema CreateSchema(string schemaSdl) .ModifyOptions(c => { c.QueryTypeName = "Configuration"; - c.StrictValidation = false; + c.StrictValidation = true; }) .Create(); })!; diff --git a/src/Backend/test/Authoring.Core.Tests/SchemaValidatorTests.cs b/src/Backend/test/Authoring.Core.Tests/SchemaValidatorTests.cs index 067f3214..333242b9 100644 --- a/src/Backend/test/Authoring.Core.Tests/SchemaValidatorTests.cs +++ b/src/Backend/test/Authoring.Core.Tests/SchemaValidatorTests.cs @@ -100,9 +100,43 @@ public void ValidateSchema_WithComments_Valid() } """); + [Fact] + public void ValidateSchema_NoRootType_Exception() + => AssertInvalidSchema(""" + type Unicorn { + someText: String! + } + """); + + [Fact] + public void ValidateSchema_MissingBrace_Exception() + => AssertInvalidSchema(""" + type Configuration { + someText: String! + """); + + [Fact] + public void ValidateSchema_UnknownType_Exception() + => AssertInvalidSchema(""" + type Configuration { + someText: Unicorn! + } + """); + private void AssertValidSchema(string schema) => new SchemaValidator(GetMemoryCache()).ValidateSchema(schema); + private void AssertInvalidSchema(string schema) + { + // arrange + SchemaValidator validator = new(GetMemoryCache()); + + // act && assert + var exception = Assert.Throws(() => validator.ValidateSchema(schema)); + exception.Errors.MatchSnapshot(); + } + + private IMemoryCache GetMemoryCache() { var services = new ServiceCollection(); diff --git a/src/Backend/test/Authoring.Core.Tests/__snapshots__/SchemaValidatorTests.ValidateSchema_MissingBrace_Exception.snap b/src/Backend/test/Authoring.Core.Tests/__snapshots__/SchemaValidatorTests.ValidateSchema_MissingBrace_Exception.snap new file mode 100644 index 00000000..460c670a --- /dev/null +++ b/src/Backend/test/Authoring.Core.Tests/__snapshots__/SchemaValidatorTests.ValidateSchema_MissingBrace_Exception.snap @@ -0,0 +1,5 @@ +[ + { + "Message": "Could not parse Schema" + } +] diff --git a/src/Backend/test/Authoring.Core.Tests/__snapshots__/SchemaValidatorTests.ValidateSchema_NoRootType_Exception.snap b/src/Backend/test/Authoring.Core.Tests/__snapshots__/SchemaValidatorTests.ValidateSchema_NoRootType_Exception.snap new file mode 100644 index 00000000..5de9d509 --- /dev/null +++ b/src/Backend/test/Authoring.Core.Tests/__snapshots__/SchemaValidatorTests.ValidateSchema_NoRootType_Exception.snap @@ -0,0 +1,5 @@ +[ + { + "Message": "The schema builder was unable to identify the query type of the schema. Either specify which type is the query type or set the schema builder to non-strict validation mode." + } +] diff --git a/src/Backend/test/Authoring.Core.Tests/__snapshots__/SchemaValidatorTests.ValidateSchema_UnknownType_Exception.snap b/src/Backend/test/Authoring.Core.Tests/__snapshots__/SchemaValidatorTests.ValidateSchema_UnknownType_Exception.snap new file mode 100644 index 00000000..42d14971 --- /dev/null +++ b/src/Backend/test/Authoring.Core.Tests/__snapshots__/SchemaValidatorTests.ValidateSchema_UnknownType_Exception.snap @@ -0,0 +1,5 @@ +[ + { + "Message": "Unable to resolve type reference `Unicorn!`." + } +] From 9933bc4fa20d5a938774bfd6ad63b2f78aba88a8 Mon Sep 17 00:00:00 2001 From: Florian Rohrer Date: Fri, 19 May 2023 09:57:58 +0200 Subject: [PATCH 6/9] make region --- src/Backend/test/Authoring.Core.Tests/SchemaValidatorTests.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Backend/test/Authoring.Core.Tests/SchemaValidatorTests.cs b/src/Backend/test/Authoring.Core.Tests/SchemaValidatorTests.cs index 333242b9..70cec67a 100644 --- a/src/Backend/test/Authoring.Core.Tests/SchemaValidatorTests.cs +++ b/src/Backend/test/Authoring.Core.Tests/SchemaValidatorTests.cs @@ -6,6 +6,7 @@ namespace Confix.Authoring.Core.Tests; public class SchemaValidatorTests { + #region ValidateSchema [Fact] public void ValidateSchema_Primitives_Valid() => AssertValidSchema(""" @@ -136,6 +137,7 @@ private void AssertInvalidSchema(string schema) exception.Errors.MatchSnapshot(); } + #endregion private IMemoryCache GetMemoryCache() { From d264d9398d567f68c5dffc8ff022023d5600197e Mon Sep 17 00:00:00 2001 From: Florian Rohrer Date: Fri, 19 May 2023 17:59:16 +0200 Subject: [PATCH 7/9] AssertValidValues (unions broken) --- .../Schema/Services/ValueHelper.cs | 1 + .../SchemaValidatorTests.cs | 201 +++++++++++++++++- 2 files changed, 194 insertions(+), 8 deletions(-) diff --git a/src/Backend/src/Authoring.Core/Schema/Services/ValueHelper.cs b/src/Backend/src/Authoring.Core/Schema/Services/ValueHelper.cs index ad53283a..34ca977f 100644 --- a/src/Backend/src/Authoring.Core/Schema/Services/ValueHelper.cs +++ b/src/Backend/src/Authoring.Core/Schema/Services/ValueHelper.cs @@ -189,6 +189,7 @@ internal static class ValueHelper public static Dictionary DeserializeDictionary(JsonElement element, IType type) { var dictionary = new Dictionary(); + // TODO: fix this for unionTypes var objectType = (ObjectType)type.NamedType(); foreach (var property in element.EnumerateObject()) diff --git a/src/Backend/test/Authoring.Core.Tests/SchemaValidatorTests.cs b/src/Backend/test/Authoring.Core.Tests/SchemaValidatorTests.cs index 70cec67a..6fa712fe 100644 --- a/src/Backend/test/Authoring.Core.Tests/SchemaValidatorTests.cs +++ b/src/Backend/test/Authoring.Core.Tests/SchemaValidatorTests.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.Caching.Memory; using Confix.Authoring.Internal; using Microsoft.Extensions.DependencyInjection; +using System.Text.Json; namespace Confix.Authoring.Core.Tests; @@ -14,7 +15,6 @@ public void ValidateSchema_Primitives_Valid() someText: String! someNumber: Int! someFloat: Float! - someLong: Long! someBoolean: Boolean! } """); @@ -125,26 +125,211 @@ public void ValidateSchema_UnknownType_Exception() """); private void AssertValidSchema(string schema) - => new SchemaValidator(GetMemoryCache()).ValidateSchema(schema); + => GetValidator().ValidateSchema(schema); private void AssertInvalidSchema(string schema) { - // arrange - SchemaValidator validator = new(GetMemoryCache()); - // act && assert - var exception = Assert.Throws(() => validator.ValidateSchema(schema)); + var exception = Assert.Throws(() => GetValidator().ValidateSchema(schema)); exception.Errors.MatchSnapshot(); } #endregion - private IMemoryCache GetMemoryCache() + #region ValidateValues + [Fact] + public void ValidateValues_Primitives_Valid() + => AssertValidValues( + """ + { + "someText": "Sample text", + "someNumber": 42, + "someFloat": 3.14, + "someBoolean": true + } + """, + """ + type Configuration { + someText: String! + someNumber: Int! + someFloat: Float! + someBoolean: Boolean! + } + """); + + [Fact] + public void ValidateValues_Enums_Valid() + => AssertValidValues( + """ + { + "someText": "Sample text", + "someEnum": "OPTION_2" + } + """, + """ + type Configuration { + someText: String! + someEnum: ConfigurationEnum! + } + + enum ConfigurationEnum { + OPTION_1 + OPTION_2 + OPTION_3 + } + """); + + [Fact] + public void ValidateValues_Arrays_Valid() + => AssertValidValues( + """ + { + "someText": "Sample text", + "someNumbers": [1, 2, 3], + "someStrings": ["String 1", "String 2"], + "someCustom": null, + "someCustom2": [ + null, + {"unicornCount": 8}, + {"unicornCount": 12} + ], + "someCustom3": [ + {"unicornCount": 15}, + {"unicornCount": 20} + ] + } + + """, + """ + type Configuration { + someText: String! + someNumbers: [Int!]! + someStrings: [String]! + someCustom: [Custom!] + someCustom2: [Custom]! + someCustom3: [Custom!]! + } + + type Custom { + unicornCount: Int! + } + """); + + [Fact] + public void ValidateValues_UnionsInRoot_Valid() + => AssertValidValues( + """ + { + "someField": { + "field1": "Sample string" + }, + "someField1": { + "field2": 42 + }, + "someField1": null + } + """, + """ + type Configuration { + someField: CustomTypeUnion! + someField1: CustomTypeUnion! + someField2: CustomTypeUnion + } + + union CustomTypeUnion = CustomType1 | CustomType2 + + type CustomType1 { + field1: String! + } + + type CustomType2 { + field2: Int! + } + """); + [Fact] + public void ValidateValues_UnionsInArray_Valid() + => AssertValidValues( + """ + { + "someField": [ + { + "field1": "Sample string" + }, + { + "field2": 42 + } + ] + } + """, + """ + type Configuration { + someField: [CustomTypeUnion!] + } + + union CustomTypeUnion = CustomType1 | CustomType2 + + type CustomType1 { + field1: String! + } + + type CustomType2 { + field2: Int! + } + """); + + [Fact] + public void ValidateValues_Nested_Valid() + => AssertValidValues( + """ + { + "someText": "Hello, world!", + "level1": { + "field1": "Value 1", + "level2": { + "field2": "Value 2", + "level3": { + "field3": "Value 3" + } + } + } + } + """, + """ + type Configuration { + someText: String! + level1: Level1! + } + + type Level1 { + field1: String! + level2: Level2! + } + + type Level2 { + field2: String! + level3: Level3! + } + + type Level3 { + field3: String! + } + """); + + private void AssertValidValues(string jsonValue, string schema) + => GetValidator().ValidateValues( + JsonDocument.Parse(jsonValue).RootElement, + schema); + + #endregion + + private ISchemaValidator GetValidator() { var services = new ServiceCollection(); services.AddMemoryCache(); + services.AddSingleton(); var serviceProvider = services.BuildServiceProvider(); - return serviceProvider.GetRequiredService(); + return serviceProvider.GetRequiredService(); } + } From 8262280b61bf63e3c6d3a23120ad67e6e56e858b Mon Sep 17 00:00:00 2001 From: Florian Rohrer Date: Fri, 19 May 2023 19:31:12 +0200 Subject: [PATCH 8/9] work --- .../SchemaValidatorTests.cs | 32 +++++++++++++++++++ ...alues_PrimitivesNullViolation_Invalid.snap | 26 +++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/Backend/test/Authoring.Core.Tests/__snapshots__/SchemaValidatorTests.ValidateValues_PrimitivesNullViolation_Invalid.snap diff --git a/src/Backend/test/Authoring.Core.Tests/SchemaValidatorTests.cs b/src/Backend/test/Authoring.Core.Tests/SchemaValidatorTests.cs index 6fa712fe..ad07e3a7 100644 --- a/src/Backend/test/Authoring.Core.Tests/SchemaValidatorTests.cs +++ b/src/Backend/test/Authoring.Core.Tests/SchemaValidatorTests.cs @@ -315,11 +315,43 @@ public void ValidateValues_Nested_Valid() } """); + + [Fact] + // TODO: null violations should somehow have a better error + public void ValidateValues_PrimitivesNullViolation_Invalid() + => AssertInvalidValues( + """ + { + "someText": null, + "someNumber": null, + "someFloat": null, + "someBoolean": null + } + """, + """ + type Configuration { + someText: String! + someNumber: Int! + someFloat: Float! + someBoolean: Boolean! + } + """); + private void AssertValidValues(string jsonValue, string schema) => GetValidator().ValidateValues( JsonDocument.Parse(jsonValue).RootElement, schema); + + private void AssertInvalidValues(string jsonValue, string schema) + { + // act && assert + var exception = Assert.Throws(() => + GetValidator().ValidateValues( + JsonDocument.Parse(jsonValue).RootElement, + schema)); + exception.Violations.MatchSnapshot(); + } #endregion private ISchemaValidator GetValidator() diff --git a/src/Backend/test/Authoring.Core.Tests/__snapshots__/SchemaValidatorTests.ValidateValues_PrimitivesNullViolation_Invalid.snap b/src/Backend/test/Authoring.Core.Tests/__snapshots__/SchemaValidatorTests.ValidateValues_PrimitivesNullViolation_Invalid.snap new file mode 100644 index 00000000..411e285e --- /dev/null +++ b/src/Backend/test/Authoring.Core.Tests/__snapshots__/SchemaValidatorTests.ValidateValues_PrimitivesNullViolation_Invalid.snap @@ -0,0 +1,26 @@ +[ + { + "Path": [ + "someText" + ], + "Code": "INVALID_TYPE" + }, + { + "Path": [ + "someNumber" + ], + "Code": "INVALID_TYPE" + }, + { + "Path": [ + "someFloat" + ], + "Code": "INVALID_TYPE" + }, + { + "Path": [ + "someBoolean" + ], + "Code": "INVALID_TYPE" + } +] From 87393e237c42c6d89e78688ab13c0c72b74512ca Mon Sep 17 00:00:00 2001 From: Florian Rohrer Date: Mon, 22 May 2023 07:58:59 +0200 Subject: [PATCH 9/9] ong --- .../SchemaValidatorTests.cs | 11 +++++++++- ...alues_PrimitivesNullViolation_Invalid.snap | 20 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/Backend/test/Authoring.Core.Tests/SchemaValidatorTests.cs b/src/Backend/test/Authoring.Core.Tests/SchemaValidatorTests.cs index ad07e3a7..69f89b2b 100644 --- a/src/Backend/test/Authoring.Core.Tests/SchemaValidatorTests.cs +++ b/src/Backend/test/Authoring.Core.Tests/SchemaValidatorTests.cs @@ -325,7 +325,10 @@ public void ValidateValues_PrimitivesNullViolation_Invalid() "someText": null, "someNumber": null, "someFloat": null, - "someBoolean": null + "someBoolean": null, + "someUnicorn": null, + "someOtherUnicorn": {"count": null}, + "someOtherCoolUnicorn": {} } """, """ @@ -334,6 +337,12 @@ public void ValidateValues_PrimitivesNullViolation_Invalid() someNumber: Int! someFloat: Float! someBoolean: Boolean! + someUnicorn: Unicorn! + someOtherUnicorn: Unicorn! + someOtherCoolUnicorn: Unicorn! + } + type Unicorn{ + count: Int! } """); diff --git a/src/Backend/test/Authoring.Core.Tests/__snapshots__/SchemaValidatorTests.ValidateValues_PrimitivesNullViolation_Invalid.snap b/src/Backend/test/Authoring.Core.Tests/__snapshots__/SchemaValidatorTests.ValidateValues_PrimitivesNullViolation_Invalid.snap index 411e285e..f403e7ef 100644 --- a/src/Backend/test/Authoring.Core.Tests/__snapshots__/SchemaValidatorTests.ValidateValues_PrimitivesNullViolation_Invalid.snap +++ b/src/Backend/test/Authoring.Core.Tests/__snapshots__/SchemaValidatorTests.ValidateValues_PrimitivesNullViolation_Invalid.snap @@ -22,5 +22,25 @@ "someBoolean" ], "Code": "INVALID_TYPE" + }, + { + "Path": [ + "someUnicorn" + ], + "Code": "INVALID_TYPE" + }, + { + "Path": [ + "someOtherUnicorn", + "count" + ], + "Code": "INVALID_TYPE" + }, + { + "Path": [ + "someOtherCoolUnicorn", + "count" + ], + "Code": "NON_NULL" } ]