Skip to content

Commit

Permalink
updates to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
madebygps committed Dec 1, 2023
1 parent 522afa5 commit d7cfce6
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 39 deletions.
55 changes: 55 additions & 0 deletions .devcontainer/tests/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/docker-existing-docker-compose
{
"name": ".NET API Tests",

// Update the 'dockerComposeFile' list if you have more compose files or use different names.
// The .devcontainer/docker-compose.yml file contains any overrides you need/want to make.
"dockerComposeFile": [
"../../docker-compose.yml"
],

// The 'service' property is the name of the service for the container that VS Code should
// use. Update this value and .devcontainer/docker-compose.yml to the real service name.
"service": "tests",

// The optional 'workspaceFolder' property is the path VS Code should open by default when
// connected. This is typically a file mount in .devcontainer/docker-compose.yml
"workspaceFolder": "/workspace/tests",

// Features to add to the dev container. More info: https://containers.dev/features.


// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Uncomment the next line if you want start specific services in your Docker Compose config.
// "runServices": [],

// Uncomment the next line if you want to keep your containers running after VS Code shuts down.
"shutdownAction": "none",
"customizations": {
"vscode": {
"extensions": [
"ms-dotnettools.csharp",
"rangav.vscode-thunder-client",
"GitHub.copilot",
"ms-dotnettools.csdevkit"
]
}
}

// Uncomment the next line to run commands after the container is created.
//"postCreateCommand": "chmod +x ./setup.sh; ./setup.sh"


// Configure tool-specific properties.
// "customizations": {},

// Uncomment to connect as an existing user other than the container default. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "devcontainer"
}




10 changes: 3 additions & 7 deletions api/Counter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@

namespace Api.Function
{
public class Counter
public class Counter(string id, int count)
{
[JsonPropertyName("id")]
public string Id { get; set; }
public string Id { get; set; } = id ?? throw new ArgumentNullException(nameof(id));

[JsonPropertyName("count")]
public int Count { get; set; }
public Counter(string id)
{
Id = id ?? throw new ArgumentNullException(nameof(id));
}
public int Count { get; set; } = count;
}
}
12 changes: 12 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ services:
command: sleep infinity
networks:
- mynet

tests:
image: mcr.microsoft.com/devcontainers/dotnet:8.0-bookworm
volumes:
# Mount the root folder that contains .git
- .:/workspace:cached
command: sleep infinity
links:
- api
networks:
- mynet
# ...

networks:
mynet:
Expand Down
2 changes: 1 addition & 1 deletion frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<!--- Basic Page Needs
================================================== -->
<meta charset="utf-8">
<title>Gwyn's Resume</title>
<title>GPS's Azure Resume</title>
<meta name="description" content="">
<meta name="author" content="">

Expand Down
1 change: 0 additions & 1 deletion tests/README.md

This file was deleted.

86 changes: 64 additions & 22 deletions tests/TestCounter.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,73 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Xunit;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Api.Function;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Moq;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace tests
public class GetVisitorCounterTests
{
public class TestCounter
private readonly ILogger<GetVisitorCounter> logger;
private readonly Mock<HttpRequestData> mockRequest;
private readonly Mock<FunctionContext> mockFunctionContext;

public GetVisitorCounterTests()
{
logger = Mock.Of<ILogger<GetVisitorCounter>>();
mockFunctionContext = new Mock<FunctionContext>();
mockRequest = new Mock<HttpRequestData>(MockBehavior.Strict, mockFunctionContext.Object);

mockRequest.Setup(req => req.CreateResponse()).Returns(() => CreateMockHttpResponseData(mockFunctionContext.Object));
}


[Fact]
public async Task Run_ShouldIncrementCounter()
{
private readonly ILogger logger = TestFactory.CreateLogger();

[Fact]
public async void Http_trigger_should_return_known_string()
{
var counter = new Company.Function.Counter();
counter.Id = "1";
counter.Count = 2;
var request = TestFactory.CreateHttpRequest();
var response = (HttpResponseMessage) Company.Function.GetResumeCounter.Run(request, counter, out counter, logger);
Assert.Equal(3, counter.Count);
}
// Arrange
var counter = new Counter(id : "index"){ Count = 1 };
var function = new GetVisitorCounter(logger);
var loggerFactoryMock = new Mock<ILoggerFactory>();
var loggerMock = new Mock<ILogger>();
loggerFactoryMock.Setup(x => x.CreateLogger(It.IsAny<string>())).Returns(loggerMock.Object);
var requestMock = new Mock<HttpRequestData>();
var responseMock = new Mock<HttpResponseData>();

// Simulate the function call
var response = function.Run(requestMock.Object, counter);

// Assert
Assert.NotNull(response);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);

// Verify that the response contains the expected content type
Assert.True(response.Headers.TryGetValues("Content-Type", out var contentTypes));
Assert.Contains("text/plain; charset=utf-8", contentTypes);

// Act
var result = await function.Run(mockRequest.Object, counter);

// Assert
Assert.NotNull(result);
Assert.Equal(2, result.NewCounter.Count); // Check if the counter is incremented
// Additional assertions as necessary
}
}
}

// Arrange











18 changes: 10 additions & 8 deletions tests/tests.csproj
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="Moq" Version="4.20.69" />
<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="1.3.0">
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand All @@ -24,4 +25,5 @@
<ProjectReference Include="..\api\api.csproj" />
</ItemGroup>


</Project>
31 changes: 31 additions & 0 deletions tests/tests.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.002.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "tests", "tests.csproj", "{C077F20F-7BF0-4112-84C9-AFE8DD6F2D66}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "testing1", "testing1\testing1.csproj", "{BAB3BC90-56E1-4FA6-BF08-01919496262C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C077F20F-7BF0-4112-84C9-AFE8DD6F2D66}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C077F20F-7BF0-4112-84C9-AFE8DD6F2D66}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C077F20F-7BF0-4112-84C9-AFE8DD6F2D66}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C077F20F-7BF0-4112-84C9-AFE8DD6F2D66}.Release|Any CPU.Build.0 = Release|Any CPU
{BAB3BC90-56E1-4FA6-BF08-01919496262C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BAB3BC90-56E1-4FA6-BF08-01919496262C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BAB3BC90-56E1-4FA6-BF08-01919496262C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BAB3BC90-56E1-4FA6-BF08-01919496262C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {09FE1130-5B6A-4B10-B953-AB072EAE676D}
EndGlobalSection
EndGlobal

0 comments on commit d7cfce6

Please sign in to comment.