Skip to content

Commit

Permalink
feat: Add additional unit tests for new extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
vipentti committed Dec 23, 2024
1 parent d623285 commit bb00147
Showing 1 changed file with 283 additions and 0 deletions.
283 changes: 283 additions & 0 deletions tests/Kiota.Builder.Tests/Extensions/OpenApiSchemaExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,289 @@ public void MergesIntersectionRecursively()
Assert.Equal("description", result.Description);
Assert.True(result.Deprecated);
}

public class MergeSingleInclusiveUnionInheritanceOrIntersectionSchemaEntries
{
[Fact]
public void DoesMergeWithInheritance()
{
var schema = new OpenApiSchema()
{
Type = "object",
AnyOf = [
new() {
Properties = new Dictionary<string, OpenApiSchema>() {
["one"] = new OpenApiSchema(),
},
AllOf = [
new() {
Reference = new() {
Id = "BaseClass"
},
},
new() {
Type = "object",
Properties = new Dictionary<string, OpenApiSchema>() {
["firstName"] = new OpenApiSchema(),
["lastName"] = new OpenApiSchema()
}
},
]
},
],
};

var result = schema.MergeSingleInclusiveUnionInheritanceOrIntersectionSchemaEntries();
Assert.True(schema.AnyOf[0].IsInherited());
Assert.NotNull(result);
Assert.True(result.IsInherited());
Assert.Contains("one", result.Properties.Keys);
Assert.Empty(result.AnyOf);
Assert.Equal(2, result.AllOf.Count);
}
[Fact]
public void DoesMergeWithIntersection()
{
var schema = new OpenApiSchema()
{
Type = "object",
AnyOf = [
new() {
Properties = new Dictionary<string, OpenApiSchema>() {
["one"] = new OpenApiSchema(),
},
AllOf = [
new() {
Type = "object",
Properties = new Dictionary<string, OpenApiSchema>() {
["first"] = new OpenApiSchema(),
}
},
new() {
Type = "object",
Properties = new Dictionary<string, OpenApiSchema>() {
["second"] = new OpenApiSchema(),
}
},
new() {
Type = "object",
Properties = new Dictionary<string, OpenApiSchema>() {
["third"] = new OpenApiSchema(),
}
},
]
},
],
};

var result = schema.MergeSingleInclusiveUnionInheritanceOrIntersectionSchemaEntries();
Assert.NotNull(result);
Assert.True(schema.AnyOf[0].IsIntersection());
Assert.True(result.IsIntersection());
Assert.Contains("one", result.Properties.Keys);
Assert.Empty(result.AnyOf);
Assert.Equal(3, result.AllOf.Count);
}
[Fact]
public void DoesNotMergeWithMoreThanOneInclusiveEntry()
{
var schema = new OpenApiSchema()
{
Type = "object",
AnyOf = [
new() {
Properties = new Dictionary<string, OpenApiSchema>() {
["one"] = new OpenApiSchema(),
},
AllOf = [
new() {
Reference = new() {
Id = "BaseClass"
},
},
new() {
Type = "object",
Properties = new Dictionary<string, OpenApiSchema>() {
["firstName"] = new OpenApiSchema(),
["lastName"] = new OpenApiSchema()
}
},
]
},
new() { Type = "object" },
],
};

var result = schema.MergeSingleInclusiveUnionInheritanceOrIntersectionSchemaEntries();
Assert.Null(result);
}
[Fact]
public void DoesNotMergeWithoutInheritanceOrIntersection()
{
var schema = new OpenApiSchema()
{
Type = "object",
AnyOf = [
new() {
AllOf = [
new() {
Type = "object",
Properties = new Dictionary<string, OpenApiSchema>() {
["firstName"] = new OpenApiSchema(),
["lastName"] = new OpenApiSchema()
}
},
]
},
],
};

var result = schema.MergeSingleInclusiveUnionInheritanceOrIntersectionSchemaEntries();
Assert.Null(result);
}
}

public class MergeSingleExclusiveUnionInheritanceOrIntersectionSchemaEntries
{
[Fact]
public void DoesMergeWithInheritance()
{
var schema = new OpenApiSchema()
{
Type = "object",
OneOf = [
new() {
Properties = new Dictionary<string, OpenApiSchema>() {
["one"] = new OpenApiSchema(),
},
AllOf = [
new() {
Reference = new() {
Id = "BaseClass"
},
},
new() {
Type = "object",
Properties = new Dictionary<string, OpenApiSchema>() {
["firstName"] = new OpenApiSchema(),
["lastName"] = new OpenApiSchema()
}
},
]
},
],
};

var result = schema.MergeSingleExclusiveUnionInheritanceOrIntersectionSchemaEntries();
Assert.True(schema.OneOf[0].IsInherited());
Assert.NotNull(result);
Assert.True(result.IsInherited());
Assert.Contains("one", result.Properties.Keys);
Assert.Empty(result.OneOf);
Assert.Equal(2, result.AllOf.Count);
}
[Fact]
public void DoesMergeWithIntersection()
{
var schema = new OpenApiSchema()
{
Type = "object",
OneOf = [
new() {
Properties = new Dictionary<string, OpenApiSchema>() {
["one"] = new OpenApiSchema(),
},
AllOf = [
new() {
Type = "object",
Properties = new Dictionary<string, OpenApiSchema>() {
["first"] = new OpenApiSchema(),
}
},
new() {
Type = "object",
Properties = new Dictionary<string, OpenApiSchema>() {
["second"] = new OpenApiSchema(),
}
},
new() {
Type = "object",
Properties = new Dictionary<string, OpenApiSchema>() {
["third"] = new OpenApiSchema(),
}
},
]
},
],
};

var result = schema.MergeSingleExclusiveUnionInheritanceOrIntersectionSchemaEntries();
Assert.NotNull(result);
Assert.True(schema.OneOf[0].IsIntersection());
Assert.True(result.IsIntersection());
Assert.Contains("one", result.Properties.Keys);
Assert.Empty(result.OneOf);
Assert.Equal(3, result.AllOf.Count);
}
[Fact]
public void DoesNotMergeWithMoreThanOneExclusiveEntry()
{
var schema = new OpenApiSchema()
{
Type = "object",
OneOf = [
new() {
Properties = new Dictionary<string, OpenApiSchema>() {
["one"] = new OpenApiSchema(),
},
AllOf = [
new() {
Reference = new() {
Id = "BaseClass"
},
},
new() {
Type = "object",
Properties = new Dictionary<string, OpenApiSchema>() {
["firstName"] = new OpenApiSchema(),
["lastName"] = new OpenApiSchema()
}
},
]
},
new() { Type = "object" },
],
};

var result = schema.MergeSingleExclusiveUnionInheritanceOrIntersectionSchemaEntries();
Assert.Null(result);
}
[Fact]
public void DoesNotMergeWithoutInheritanceOrIntersection()
{
var schema = new OpenApiSchema()
{
Type = "object",
OneOf = [
new() {
AllOf = [
new() {
Type = "object",
Properties = new Dictionary<string, OpenApiSchema>() {
["firstName"] = new OpenApiSchema(),
["lastName"] = new OpenApiSchema()
}
},
]
},
],
};

var result = schema.MergeSingleExclusiveUnionInheritanceOrIntersectionSchemaEntries();
Assert.Null(result);
}
}

[Fact]
public void IsArrayFalseOnEmptyItems()
{
Expand Down

0 comments on commit bb00147

Please sign in to comment.