Skip to content

Commit

Permalink
Added endpoint-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Braga Lund committed Nov 26, 2021
1 parent 7de87a2 commit 5a60b4a
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 66 deletions.
1 change: 0 additions & 1 deletion backend/.idea/.idea.bragapi/.idea/.name

This file was deleted.

4 changes: 0 additions & 4 deletions backend/.idea/.idea.bragapi/.idea/encodings.xml

This file was deleted.

10 changes: 0 additions & 10 deletions backend/.idea/.idea.bragapi/.idea/indexLayout.xml

This file was deleted.

6 changes: 0 additions & 6 deletions backend/.idea/.idea.bragapi/.idea/projectSettingsUpdater.xml

This file was deleted.

34 changes: 34 additions & 0 deletions backend/bragapi-tests/Controllers/SwaggerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.Hosting;
using Microsoft.VisualStudio.TestPlatform.TestHost;
using Xunit;

namespace bragapi_tests.Controllers
{
public class SwaggerTests
{
[Fact]
public async Task GetSwaggerUI_Returns_OK()
{
await using var application = new BragaApiApplication();

var client = application.CreateClient();
var response = await client.GetAsync("/swagger/index.html");

Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}

class BragaApiApplication : WebApplicationFactory<Program>
{
protected override IHost CreateHost(IHostBuilder builder)
{
// Add mock/test services to the builder here
builder.ConfigureServices(services => { });

return base.CreateHost(builder);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.Hosting;
using Microsoft.VisualStudio.TestPlatform.TestHost;
using Xunit;

namespace bragapi_tests.Controllers
{
public class WeatherForecastControllerTests
{

[Fact]
public async Task GetWeatherForecast_WhenRequested_ShouldReturnArray()
{
await using var application = new BragaApiApplication();

var client = application.CreateClient();
var response = await client.GetAsync("/WeatherForecast");

Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
class BragaApiApplication : WebApplicationFactory<Program>
{
protected override IHost CreateHost(IHostBuilder builder)
{
// Add mock/test services to the builder here
builder.ConfigureServices(services => { });

return base.CreateHost(builder);
}
}

}
}
12 changes: 7 additions & 5 deletions backend/bragapi-tests/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using Xunit;

namespace bragapi_tests;

public class UnitTest1
namespace bragapi_tests
{
[Fact]
public void Test1()

public class UnitTest1
{
[Fact]
public void Test1()
{

}
}
}
6 changes: 6 additions & 0 deletions backend/bragapi-tests/bragapi-tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
Expand All @@ -21,4 +22,9 @@
</PackageReference>
</ItemGroup>


<ItemGroup>
<ProjectReference Include="../bragapi/bragapi.csproj" />
</ItemGroup>

</Project>
51 changes: 28 additions & 23 deletions backend/bragapi/Controllers/WeatherForecastController.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace bragapi.Controllers;

[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
namespace bragapi.Controllers
{
private static readonly string[] Summaries = new[]
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private static readonly string[] Summaries =
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

private readonly ILogger<WeatherForecastController> _logger;
private readonly ILogger<WeatherForecastController> _logger;

public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}

[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}
}
6 changes: 5 additions & 1 deletion backend/bragapi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
Expand All @@ -22,4 +26,4 @@

app.MapControllers();

app.Run();
app.Run();
18 changes: 11 additions & 7 deletions backend/bragapi/WeatherForecast.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
namespace bragapi;
using System;

public class WeatherForecast
namespace bragapi
{
public DateTime Date { get; set; }

public int TemperatureC { get; set; }
public class WeatherForecast
{
public DateTime Date { get; set; }

public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public int TemperatureC { get; set; }

public string? Summary { get; set; }
}
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

public string? Summary { get; set; }
}
}
22 changes: 13 additions & 9 deletions backend/bragapi/bragapi.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3"/>
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="bragapi-tests" />
</ItemGroup>

</Project>

0 comments on commit 5a60b4a

Please sign in to comment.