Added unit tests for .NET quickstart

This commit is contained in:
Jason Johnston 2023-07-05 14:41:47 -04:00
Родитель 7345ea5c7b
Коммит 4c5d1a857f
7 изменённых файлов: 209 добавлений и 3 удалений

4
.vscode/settings.json поставляемый
Просмотреть файл

@ -1,7 +1,9 @@
{
"cSpell.words": [
"fetchlibrary",
"kiotaposts",
"quickstart",
"regen"
"regen",
"Xunit"
]
}

Просмотреть файл

@ -1 +1 @@
kiota generate -l typescript -d ../getme.yml -c GetUserApiClient -o ./client
kiota generate -l TypeScript -c GetUserApiClient -d ../get-me.yml -o ./client --co

Просмотреть файл

@ -0,0 +1,28 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KiotaPosts", "src\KiotaPosts.csproj", "{730BDBA7-BE8D-4F2C-8F5D-28411B42C2F0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KiotaPostsTests", "test\KiotaPostsTests.csproj", "{8BCC4F8D-D788-4593-9A48-359EC36EDC3F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{730BDBA7-BE8D-4F2C-8F5D-28411B42C2F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{730BDBA7-BE8D-4F2C-8F5D-28411B42C2F0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{730BDBA7-BE8D-4F2C-8F5D-28411B42C2F0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{730BDBA7-BE8D-4F2C-8F5D-28411B42C2F0}.Release|Any CPU.Build.0 = Release|Any CPU
{8BCC4F8D-D788-4593-9A48-359EC36EDC3F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8BCC4F8D-D788-4593-9A48-359EC36EDC3F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8BCC4F8D-D788-4593-9A48-359EC36EDC3F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8BCC4F8D-D788-4593-9A48-359EC36EDC3F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

Просмотреть файл

@ -1 +1 @@
kiota generate -l CSharp -c PostsClient -n KiotaPosts.Client -d ../posts-api.yml -o ./src/Client
kiota generate -l CSharp -c PostsClient -n KiotaPosts.Client -d ../posts-api.yml -o ./src/Client --co

Просмотреть файл

@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Kiota.Abstractions" Version="1.2.1" />
<PackageReference Include="Microsoft.Kiota.Serialization.Json" Version="1.0.7" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="NSubstitute" Version="5.0.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\src\KiotaPosts.csproj" />
</ItemGroup>
</Project>

Просмотреть файл

@ -0,0 +1,143 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
// <UnitTestsSnippet>
using System.Text.Json;
using KiotaPosts.Client;
using KiotaPosts.Client.Models;
using Microsoft.Kiota.Abstractions;
using Microsoft.Kiota.Abstractions.Serialization;
using Microsoft.Kiota.Serialization.Json;
using NSubstitute;
namespace KiotaPostsTests;
public class PostsRequestsTests
{
private static readonly List<Post> postsMock = new()
{
new Post
{
Id = 1,
UserId = 1,
Title = "Post Title #1",
Body = "Posts Body #1",
},
new Post
{
Id = 2,
UserId = 2,
Title = "Post Title #2",
Body = "Post Body #2",
},
};
// Basic test for GET /posts
[Fact]
public async Task All_Posts()
{
// Arrange
var adapter = Substitute.For<IRequestAdapter>();
var postsClient = new PostsClient(adapter);
// Return the mocked list of posts
adapter.SendCollectionAsync(
Arg.Any<RequestInformation>(),
Arg.Any<ParsableFactory<Post>>(),
Arg.Any<Dictionary<string, ParsableFactory<IParsable>>>(),
Arg.Any<CancellationToken>())
.ReturnsForAnyArgs(postsMock);
// Act
var posts = await postsClient.Posts.GetAsync();
// Assert
Assert.NotNull(posts);
Assert.Equal(postsMock.Count, posts.Count);
}
// Basic test for GET /posts/{id}
[Fact]
public async Task Post_By_Id()
{
// Arrange
var adapter = Substitute.For<IRequestAdapter>();
var postsClient = new PostsClient(adapter);
// Return the mocked post #1 object if the path
// contains the ID of post #1
adapter.SendAsync(
Arg.Is<RequestInformation>(req => req.PathParameters.Values.Contains("1")),
Arg.Any<ParsableFactory<Post>>(),
Arg.Any<Dictionary<string, ParsableFactory<IParsable>>>(),
Arg.Any<CancellationToken>())
.Returns(postsMock[0]);
// Return the mocked post #2 object if the path
// contains the ID of post #2
adapter.SendAsync(
Arg.Is<RequestInformation>(req => req.PathParameters.Values.Contains("2")),
Arg.Any<ParsableFactory<Post>>(),
Arg.Any<Dictionary<string, ParsableFactory<IParsable>>>(),
Arg.Any<CancellationToken>())
.Returns(postsMock[1]);
// Act
var post1 = await postsClient.Posts["1"].GetAsync();
var post2 = await postsClient.Posts["2"].GetAsync();
// Assert
Assert.NotNull(post1);
Assert.Equal(postsMock[0].Title, post1.Title);
Assert.NotNull(post2);
Assert.Equal(postsMock[1].Title, post2.Title);
}
// Basic test for POST /posts
[Fact]
public async Task Post_Create_New()
{
// Arrange
var adapter = Substitute.For<IRequestAdapter>();
var postsClient = new PostsClient(adapter);
// Add JsonSerializationWriter to serialize Post objects
adapter.SerializationWriterFactory.GetSerializationWriter("application/json")
.Returns(sw => new JsonSerializationWriter());
// When the request is sent through the adapter
// save it so we can validate the content of the request
RequestInformation? postRequest = null;
await adapter.SendAsync(
Arg.Do<RequestInformation>(req => postRequest = req),
Arg.Any<ParsableFactory<Post>>(),
Arg.Any<Dictionary<string, ParsableFactory<IParsable>>?>(),
Arg.Any<CancellationToken>());
var newPost = new Post
{
UserId = 3,
Title = "Created new post",
Body = "For testing purposes",
};
// Act
await postsClient.Posts.PostAsync(newPost);
// Assert
Assert.NotNull(postRequest);
// Deserialize the request body
var postFromBody = JsonSerializer.Deserialize<Post>(
postRequest.Content,
new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
});
Assert.NotNull(postFromBody);
Assert.Equal(newPost.UserId, postFromBody.UserId);
Assert.Equal(newPost.Title, postFromBody.Title);
Assert.Equal(newPost.Body, postFromBody.Body);
}
}
// </UnitTestsSnippet>

Просмотреть файл

@ -0,0 +1 @@
global using Xunit;