Skip to content

Commit

Permalink
- fixes unit tests for navigation code constant
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Biret <[email protected]>
  • Loading branch information
baywet committed Dec 20, 2023
1 parent 6fe6a3e commit 214a0b4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/Kiota.Builder/CodeDOM/CodeConstant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public string? UriTemplate
ArgumentNullException.ThrowIfNull(codeClass);
if (codeClass.Kind != CodeClassKind.RequestBuilder) return default;
if (!(codeClass.Properties.Any(static x => x.Kind is CodePropertyKind.RequestBuilder) ||
codeClass.Methods.Any(x => x.Kind is CodeMethodKind.IndexerBackwardCompatibility or CodeMethodKind.RequestBuilderWithParameters)))
codeClass.Methods.Any(static x => x.Kind is CodeMethodKind.IndexerBackwardCompatibility or CodeMethodKind.RequestBuilderWithParameters)))
return default;
return new CodeConstant
{
Expand All @@ -69,7 +69,7 @@ public string? UriTemplate
{
ArgumentNullException.ThrowIfNull(codeClass);
if (codeClass.Kind != CodeClassKind.RequestBuilder) return default;
if (!codeClass.Methods.Any(x => x.Kind is CodeMethodKind.RequestExecutor or CodeMethodKind.RequestGenerator))
if (!codeClass.Methods.Any(static x => x.Kind is CodeMethodKind.RequestExecutor or CodeMethodKind.RequestGenerator))
return default;
return new CodeConstant
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ public void WritesIndexer()
{
AddRequestProperties();
method.Kind = CodeMethodKind.IndexerBackwardCompatibility;
parentClass.Kind = CodeClassKind.RequestBuilder;
method.OriginalIndexer = new()
{
Name = "indx",
Expand All @@ -310,37 +311,53 @@ public void WritesIndexer()
},
}
};
var constant = CodeConstant.FromRequestBuilderToRequestsMetadata(parentClass);
parentClass.GetImmediateParentOfType<CodeNamespace>().AddConstant(constant);
writer.Write(constant);
var result = tw.ToString();
Assert.Contains("this.requestAdapter", result);
Assert.Contains("this.pathParameters", result);
Assert.Contains("id", result);
Assert.Contains("return new", result);
}
[Fact]
public void WritesPathParameterRequestBuilder()
{
AddRequestProperties();
method.Kind = CodeMethodKind.RequestBuilderWithParameters;
var parentInterface = new CodeInterface
{
Name = "parentClass",
Kind = CodeInterfaceKind.RequestBuilder
};
method.AddParameter(new CodeParameter
{
Name = "pathParam",
Kind = CodeParameterKind.Path,
Name = "id",
Type = new CodeType
{
Name = "string"
}
Name = "string",
IsNullable = true,
},
SerializationName = "foo-id",
Kind = CodeParameterKind.Path
});
var constant = CodeConstant.FromRequestBuilderToRequestsMetadata(parentClass);
parentClass.GetImmediateParentOfType<CodeNamespace>().AddConstant(constant);
var parentNS = parentClass.GetImmediateParentOfType<CodeNamespace>();
Assert.NotNull(parentNS);
var childNS = parentNS.AddNamespace($"{parentNS.Name}.childNS");
childNS.TryAddCodeFile("foo",
new CodeConstant
{
Name = "SomecustomtypeUriTemplate",
Kind = CodeConstantKind.UriTemplate,
},
new CodeConstant
{
Name = "SomecustomtypeNavigationMetadata",
Kind = CodeConstantKind.NavigationMetadata,
},
new CodeConstant
{
Name = "SomecustomtypeRequestsMetadata",
Kind = CodeConstantKind.RequestsMetadata,
});
parentInterface.AddMethod(method);
var constant = CodeConstant.FromRequestBuilderToNavigationMetadata(parentClass);
Assert.NotNull(constant);
parentNS.TryAddCodeFile("foo", constant, parentInterface);
writer.Write(constant);
var result = tw.ToString();
Assert.Contains("this.requestAdapter", result);
Assert.Contains("this.pathParameters", result);
Assert.Contains("pathParam", result);
Assert.Contains("return new", result);
Assert.Contains("export const ParentClassNavigationMetadata: Record<Exclude<keyof ParentClass, KeysToExcludeForNavigationMetadata>, NavigationMetadata> = {", result);
Assert.Contains("methodName", result);
Assert.Contains("uriTemplate: SomecustomtypeUriTemplate", result);
Assert.Contains("requestsMetadata: SomecustomtypeRequestsMetadata", result);
Assert.Contains("navigationMetadata: SomecustomtypeNavigationMetadata", result);
Assert.Contains("pathParametersMappings: [\"foo-id\"]", result);
}
private void AddRequestProperties()
{
Expand Down

0 comments on commit 214a0b4

Please sign in to comment.