Skip to content

Commit

Permalink
datamodeling tests fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkoSekulic committed Jan 16, 2025
1 parent 8e969ba commit 1be475f
Show file tree
Hide file tree
Showing 38 changed files with 278 additions and 217 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,25 @@ public class AltinnJsonSchemaValidationTests : SchemaConversionTestsBase<AltinnJ
public void ValidJsonSchema_ShouldNotHave_ValidationIssues(string jsonSchemaPath)
{
When.JsonSchemaLoaded(jsonSchemaPath)
.And.LoadedJsonSchemaValidated()
.Then.ValidationResult.IsValid.Should().BeTrue();
.And.LoadedJsonSchemaValidated();

Assert.True(ValidationResult.IsValid);
}

[Theory]
[MemberData(nameof(AltinnJsonSchemaValidationTestData.InvalidSchemas), MemberType = typeof(AltinnJsonSchemaValidationTestData))]
public void InvalidJsonSchema_ShouldHave_ValidationIssues(string jsonSchemaPath, params Tuple<string, string>[] expectedValidationIssues)
{
When.JsonSchemaLoaded(jsonSchemaPath)
.And.LoadedJsonSchemaValidated()
.Then.ValidationResult.IsValid.Should().BeFalse();
.And.LoadedJsonSchemaValidated();

Assert.False(ValidationResult.IsValid);

And.ValidationResult.ValidationIssues.Should().HaveCount(expectedValidationIssues.Length);
Assert.Equal(ValidationResult.ValidationIssues.Count, expectedValidationIssues.Length);

foreach ((string expectedPointer, string expectedCode) in expectedValidationIssues)
{
ValidationResult.ValidationIssues.Should().Contain(x => x.ErrorCode == expectedCode && JsonPointer.Parse(x.IssuePointer) == JsonPointer.Parse(expectedPointer));
Assert.Contains(ValidationResult.ValidationIssues, x => x.ErrorCode == expectedCode && JsonPointer.Parse(x.IssuePointer) == JsonPointer.Parse(expectedPointer));
}
}

Expand Down
40 changes: 20 additions & 20 deletions backend/tests/DataModeling.Tests/Assertions/TypeAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ public static void IsEquivalentTo(Type expected, Type actual)
{
if (expected.IsPrimitive || expected == typeof(string) || expected == typeof(DateTime) || expected == typeof(decimal))
{
expected.Should().Be(actual);
Assert.Equal(expected, actual);
return;
}

expected.Name.Should().Be(actual.Name);
expected.Namespace.Should().Be(actual.Namespace);
expected.IsArray.Should().Be(actual.IsArray);
expected.IsClass.Should().Be(actual.IsClass);
expected.IsGenericType.Should().Be(actual.IsGenericType);
Assert.Equal(expected.Name, actual.Name);
Assert.Equal(expected.Namespace, actual.Namespace);
Assert.Equal(expected.IsArray, actual.IsArray);
Assert.Equal(expected.IsClass, actual.IsClass);
Assert.Equal(expected.IsGenericType, actual.IsGenericType);
if (expected.IsGenericType)
{
foreach (var expectedArg in expected.GenericTypeArguments)
Expand All @@ -46,7 +46,7 @@ public static void IsEquivalentTo(Type expected, Type actual)

private static void IsEquivalentTo(IReadOnlyCollection<FieldInfo> expected, IReadOnlyCollection<FieldInfo> actual)
{
expected.Count.Should().Be(actual.Count);
Assert.Equal(expected.Count, actual.Count);
foreach (var expectedItem in expected)
{
var actualItem = actual.Single(x => x.Name == expectedItem.Name);
Expand All @@ -56,16 +56,16 @@ private static void IsEquivalentTo(IReadOnlyCollection<FieldInfo> expected, IRea

private static void IsEquivalentTo(FieldInfo expected, FieldInfo actual)
{
expected.Name.Should().Be(actual.Name);
Assert.Equal(expected.Name, actual.Name);
IsEquivalentTo(expected.FieldType, actual.FieldType);
IsEquivalentTo(expected.CustomAttributes, actual.CustomAttributes);
expected.IsPrivate.Should().Be(actual.IsPrivate);
expected.IsPublic.Should().Be(actual.IsPublic);
Assert.Equal(expected.IsPrivate, actual.IsPrivate);
Assert.Equal(expected.IsPublic, actual.IsPublic);
}

private static void IsEquivalentTo(IReadOnlyCollection<PropertyInfo> expected, IReadOnlyCollection<PropertyInfo> actual)
{
expected.Count.Should().Be(actual.Count);
Assert.Equal(expected.Count, actual.Count);
foreach (var expectedItem in expected)
{
var actualItem = actual.Single(x => x.Name == expectedItem.Name);
Expand All @@ -75,7 +75,7 @@ private static void IsEquivalentTo(IReadOnlyCollection<PropertyInfo> expected, I

private static void IsEquivalentTo(PropertyInfo expected, PropertyInfo actual)
{
expected.Name.Should().Be(actual.Name);
Assert.Equal(expected.Name, actual.Name);
IsEquivalentTo(expected.PropertyType, actual.PropertyType);
IsEquivalentTo(expected.CustomAttributes, actual.CustomAttributes);
}
Expand All @@ -84,17 +84,17 @@ private static void IsEquivalentTo(IEnumerable<CustomAttributeData> expected, IE
{
var expectedStrings = expected.Select(e => e.ToString());
var actualStrings = actual.Select(a => a.ToString());
expectedStrings.Should().BeEquivalentTo(actualStrings);
Assert.True(expectedStrings.SequenceEqual(actualStrings));
}

private static void IsEquivalentTo(TypeAttributes expected, TypeAttributes actual)
{
expected.Should().Be(actual);
Assert.Equal(expected, actual);
}

private static void IsEquivalentTo(IReadOnlyCollection<MethodInfo> expected, IReadOnlyCollection<MethodInfo> actual)
{
expected.Count.Should().Be(actual.Count);
Assert.Equal(expected.Count, actual.Count);
foreach (var expectedItem in expected)
{
var actualItem = actual.Single(x => x.Name == expectedItem.Name);
Expand All @@ -104,17 +104,17 @@ private static void IsEquivalentTo(IReadOnlyCollection<MethodInfo> expected, IRe

private static void IsEquivalentTo(MethodInfo expected, MethodInfo actual)
{
expected.Name.Should().Be(actual.Name);
Assert.Equal(expected.Name, actual.Name);
IsEquivalentTo(expected.ReturnType, actual.ReturnType);
actual.IsPublic.Should().Be(expected.IsPublic);
Assert.Equal(expected.IsPublic, actual.IsPublic);
}

public static void PropertyShouldContainCustomAnnotationAndHaveTypeType(Type type, string propertyName, string propertyType, string expectedAnnotationString)
{
var property = type.Properties().Single(x => x.Name == propertyName);
var property = type.GetProperties().Single(x => x.Name == propertyName);
var simpleCompiledAssembly = Compiler.CompileToAssembly(DynamicAnnotationClassString(propertyName, propertyType, expectedAnnotationString));
var expectedProperty = simpleCompiledAssembly.Types()
.First(x => x.Name == "DynamicAnnotationClass").Properties().Single();
var expectedProperty = simpleCompiledAssembly.GetTypes()
.First(x => x.Name == "DynamicAnnotationClass").GetProperties().Single();
IsEquivalentTo(expectedProperty.PropertyType, property.PropertyType);
var expectedAnnotation = expectedProperty.CustomAttributes.Single();
Assert.Single(property.CustomAttributes, x => x.ToString() == expectedAnnotation.ToString());
Expand Down
38 changes: 22 additions & 16 deletions backend/tests/DataModeling.Tests/CsharpEnd2EndGenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public void Convert_FromXsd_Should_EqualExpected(string xsdSchemaPath, string ex
.When.LoadedXsdSchemaConvertedToJsonSchema()
.And.ConvertedJsonSchemaConvertedToModelMetadata()
.And.ModelMetadataConvertedToCsharpClass()
.And.CSharpClassesCompiledToAssembly()
.Then
.CompiledAssembly.Should().NotBeNull();
.And.CSharpClassesCompiledToAssembly();

Assert.NotNull(CompiledAssembly);

And.GeneratedClassesShouldBeEquivalentToExpected(expectedCsharpClassPath);
}
Expand All @@ -43,8 +43,9 @@ public void Convert_CSharpClass_ShouldContainRestriction(string xsdSchemaPath, s
.When.LoadedXsdSchemaConvertedToJsonSchema()
.And.ConvertedJsonSchemaConvertedToModelMetadata()
.And.ModelMetadataConvertedToCsharpClass()
.And.CSharpClassesCompiledToAssembly()
.Then.CompiledAssembly.Should().NotBeNull();
.And.CSharpClassesCompiledToAssembly();

Assert.NotNull(CompiledAssembly);

And.PropertyShouldHaveDefinedTypeAndContainAnnotation("Root", propertyName, expectedPropertyType, restrictionString);
}
Expand All @@ -56,12 +57,14 @@ public void JsonSchemaShouldConvertToXsdAndCSharp(string jsonSchemaPath, params
Given.That.JsonSchemaLoaded(jsonSchemaPath)
.When.LoadedJsonSchemaConvertedToModelMetadata()
.And.ModelMetadataConvertedToCsharpClass()
.And.CSharpClassesCompiledToAssembly()
.Then.CompiledAssembly.Should().NotBeNull();
.And.CSharpClassesCompiledToAssembly();

Assert.NotNull(CompiledAssembly);

And.ClassesShouldBeGenerated(typesCreated)
.And.When.LoadedJsonSchemaConvertedToXsdSchema()
.Then.ConvertedXsdSchema.Should().NotBeNull();
.And.When.LoadedJsonSchemaConvertedToXsdSchema();

Assert.NotNull(ConvertedXsdSchema);
}

[Theory]
Expand All @@ -71,8 +74,9 @@ public void JsonSchemaWithStringFieldInUriFormatShouldConvertToCSharp(string jso
Given.That.JsonSchemaLoaded(jsonSchemaPath)
.When.LoadedJsonSchemaConvertedToModelMetadata()
.And.ModelMetadataConvertedToCsharpClass()
.And.CSharpClassesCompiledToAssembly()
.Then.CompiledAssembly.Should().NotBeNull();
.And.CSharpClassesCompiledToAssembly();

Assert.NotNull(CompiledAssembly);
}

private void GeneratedClassesShouldBeEquivalentToExpected(string expectedCsharpClassPath, bool overwriteExpected = false)
Expand All @@ -93,15 +97,16 @@ private void GeneratedClassesShouldBeEquivalentToExpected(string expectedCsharpC
var expectedAssembly = Compiler.CompileToAssembly(expectedClasses);

// Compare root types.
var newType = CompiledAssembly.Types().Single(type => type.CustomAttributes.Any(att => att.AttributeType == typeof(XmlRootAttribute)));
var newType = CompiledAssembly.GetTypes().Single(type => type.CustomAttributes.Any(att => att.AttributeType == typeof(XmlRootAttribute)));
var oldType = expectedAssembly.GetType(newType.FullName);
oldType.Should().NotBeNull();
Assert.NotNull(oldType);

TypeAssertions.IsEquivalentTo(oldType, newType);
}

private void PropertyShouldHaveDefinedTypeAndContainAnnotation(string className, string propertyName, string propertyType, string annotationString)
{
var type = CompiledAssembly.Types().Single(type => type.Name == className);
var type = CompiledAssembly.GetTypes().Single(type => type.Name == className);
TypeAssertions.PropertyShouldContainCustomAnnotationAndHaveTypeType(type, propertyName, propertyType, annotationString);
}

Expand All @@ -110,8 +115,9 @@ private CsharpEnd2EndGenerationTests ClassesShouldBeGenerated(string[] className
{
foreach (string className in classNames)
{
var type = CompiledAssembly.Types().Single(type => type.Name == className);
type.Should().NotBeNull();
var type = CompiledAssembly.GetTypes().Single(type => type.Name == className);

Assert.NotNull(type);
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ public void Data_ShouldValidateAgainstSchemas(string xsdSchemaPath)
.When.LoadedXsdSchemaConvertedToJsonSchema()
.And.ConvertedJsonSchemaConvertedToModelMetadata()
.And.ModelMetadataConvertedToCsharpClass()
.And.CSharpClassesCompiledToAssembly()
.Then.CompiledAssembly.Should().NotBeNull();
.And.CSharpClassesCompiledToAssembly();

Assert.NotNull(CompiledAssembly);


When.RepresentingTypeFromLoadedFromAssembly()
.And.RandomRepresentingObjectGenerated()
Expand All @@ -55,7 +57,7 @@ public void Data_ShouldValidateAgainstSchemas(string xsdSchemaPath)

private DataValidationWithModelPopulatingTests RepresentingTypeFromLoadedFromAssembly()
{
RepresentingType = CompiledAssembly.Types().Single(type => type.CustomAttributes.Any(att => att.AttributeType == typeof(XmlRootAttribute)));
RepresentingType = CompiledAssembly.GetTypes().Single(type => type.CustomAttributes.Any(att => att.AttributeType == typeof(XmlRootAttribute)));
return this;
}

Expand All @@ -68,7 +70,8 @@ private DataValidationWithModelPopulatingTests RandomRepresentingObjectGenerated
private DataValidationWithModelPopulatingTests RepresentingObject_ShouldBeValid()
{
var isValid = Validator.TryValidateObject(RandomRepresentingObject, new ValidationContext(RandomRepresentingObject), null, true);
isValid.Should().BeTrue();

Assert.True(isValid);
return this;
}

Expand Down Expand Up @@ -100,7 +103,8 @@ void ValidationEventHandler(object sender, ValidationEventArgs e)
document.Schemas.Add(LoadedXsdSchema);
ValidationEventHandler eventHandler = ValidationEventHandler;
document.Validate(eventHandler);
isValid.Should().BeTrue();

Assert.True(isValid);

return this;
}
Expand All @@ -113,6 +117,7 @@ private void RepresentingObject_ShouldValidateAgainstJsonSchema()
});
var jsonNode = JsonNode.Parse(json);
var validationResults = ConvertedJsonSchema.Evaluate(jsonNode);
validationResults.IsValid.Should().BeTrue();

Assert.True(validationResults.IsValid);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void TestDateFormat(string property, bool expected)
objects.Add(node[property]);

var result = checkDateMethod.Invoke(null, objects.ToArray());
expected.Should().Be((bool)result);

Assert.Equal(expected, (bool)result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Task Normalize_NoNormalization_ShouldEqualSourceSchema(string jsonSchemaT
var normalizedJsonSchema = jsonSchemaNormalizer.Normalize(jsonSchema);
var normalizedJsonSchemaText = JsonSerializer.Serialize(normalizedJsonSchema, new JsonSerializerOptions() { Encoder = JavaScriptEncoder.Create(UnicodeRanges.BasicLatin, UnicodeRanges.Latin1Supplement) });

normalizedJsonSchemaText.Should().BeEquivalentTo(jsonSchemaText);
Assert.Equal(jsonSchemaText, normalizedJsonSchemaText);
return Task.CompletedTask;
}

Expand Down Expand Up @@ -77,7 +77,7 @@ public Task Normalize_WithNormalization_ShouldRemoveSingleAllOfs(string jsonSche

var json = JsonSerializer.Serialize(normalizedJsonSchema, new JsonSerializerOptions { WriteIndented = true });

normalizedJsonSchemaText.Should().BeEquivalentTo(expectedNormalizedJsonSchemaText);
Assert.Equal(expectedNormalizedJsonSchemaText, normalizedJsonSchemaText);
return Task.CompletedTask;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void IsValidComplexType_ComplexType_ShouldReturnTrue(string path, string

var results = analyzer.AnalyzeSchema(schema);

results.GetCompatibleTypes(JsonPointer.Parse(jsonPointer)).Should().Contain(CompatibleXsdType.ComplexType);
Assert.Contains(CompatibleXsdType.ComplexType, results.GetCompatibleTypes(JsonPointer.Parse(jsonPointer)));
}

[Theory]
Expand All @@ -57,10 +57,10 @@ public void IsValidComplexContentExtension_ComplexContentExtention_ShouldReturnT

var results = analyzer.AnalyzeSchema(schema);

results.GetCompatibleTypes(JsonPointer.Parse(jsonPointer)).Should().Contain(CompatibleXsdType.ComplexContent);
results.GetCompatibleTypes(JsonPointer.Parse(jsonPointer)).Should().Contain(CompatibleXsdType.ComplexContentExtension);
results.GetCompatibleTypes(JsonPointer.Parse(jsonPointer)).Should().NotContain(CompatibleXsdType.SimpleContentExtension);
results.GetCompatibleTypes(JsonPointer.Parse(jsonPointer)).Should().NotContain(CompatibleXsdType.SimpleContentRestriction);
Assert.Contains(CompatibleXsdType.ComplexContent, results.GetCompatibleTypes(JsonPointer.Parse(jsonPointer)));
Assert.Contains(CompatibleXsdType.ComplexContentExtension, results.GetCompatibleTypes(JsonPointer.Parse(jsonPointer)));
Assert.DoesNotContain(CompatibleXsdType.SimpleContentExtension, results.GetCompatibleTypes(JsonPointer.Parse(jsonPointer)));
Assert.DoesNotContain(CompatibleXsdType.SimpleContentRestriction, results.GetCompatibleTypes(JsonPointer.Parse(jsonPointer)));
}

[Theory]
Expand All @@ -74,8 +74,8 @@ public void IsValidComplexContentExtension_NotComplexContentExtention_ShouldRetu

var results = analyzer.AnalyzeSchema(schema);

results.GetCompatibleTypes(JsonPointer.Parse(jsonPointer)).Should().NotContain(CompatibleXsdType.ComplexContent);
results.GetCompatibleTypes(JsonPointer.Parse(jsonPointer)).Should().NotContain(CompatibleXsdType.ComplexContentExtension);
Assert.DoesNotContain(CompatibleXsdType.ComplexContent, results.GetCompatibleTypes(JsonPointer.Parse(jsonPointer)));
Assert.DoesNotContain(CompatibleXsdType.ComplexContentExtension, results.GetCompatibleTypes(JsonPointer.Parse(jsonPointer)));
}

[Theory]
Expand All @@ -92,8 +92,8 @@ public void IsValidAttribute_Attribute_ShouldReturnTrue(string path, string json

var results = analyzer.AnalyzeSchema(schema);

results.GetCompatibleTypes(JsonPointer.Parse(jsonPointer)).Should().Contain(CompatibleXsdType.SimpleType);
results.GetCompatibleTypes(JsonPointer.Parse(jsonPointer)).Should().Contain(CompatibleXsdType.Attribute);
Assert.Contains(CompatibleXsdType.SimpleType, results.GetCompatibleTypes(JsonPointer.Parse(jsonPointer)));
Assert.Contains(CompatibleXsdType.Attribute, results.GetCompatibleTypes(JsonPointer.Parse(jsonPointer)));
}

[Theory]
Expand All @@ -108,7 +108,7 @@ public void IsValidNillableAttribute_NillableAttribute_ShouldReturnTrue(string p

var results = analyzer.AnalyzeSchema(schema);

results.GetCompatibleTypes(JsonPointer.Parse(jsonPointer)).Should().Contain(CompatibleXsdType.Nillable);
Assert.Contains(CompatibleXsdType.Nillable, results.GetCompatibleTypes(JsonPointer.Parse(jsonPointer)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Altinn.Studio.DataModeling.Utils;
using Json.Schema;
using SharedResources.Tests;
using Xunit;

namespace DataModeling.Tests.Json.Keywords.BaseClasses;

Expand Down Expand Up @@ -50,7 +51,20 @@ protected TTestType KeywordReadFromSchema()

protected TTestType SerializedKeywordShouldBe(string json)
{
KeywordNodeJson.Should().Be(json);
Assert.Equal(KeywordNodeJson, json);
return this as TTestType;
}

protected TTestType KeywordShouldNotBeNull()
{
Assert.NotNull(Keyword);
return this as TTestType;
}

// protected TTestType KeywordShouldBeNull()
// {
// Assert.Null(Keyword);
// return this as TTestType;
// }

}
Loading

0 comments on commit 1be475f

Please sign in to comment.