Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AB#120 Feature/120 integration test for taskitemscontroller #8

Merged
Merged
6 changes: 3 additions & 3 deletions FunCoding.WhatToDo.WebApi/Controllers/TaskItemsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public async Task<IActionResult> CreateTask(TaskItem newTaskItem)
return CreatedAtAction(nameof(CreateTask), new { id = taskItem.Id }, taskItem);
}

[HttpPut]
public async Task<IActionResult> UpdateTask(TaskItem updateTaskItem)
[HttpPut("{id:guid}")]
public async Task<IActionResult> UpdateTask(Guid id,TaskItem updateTaskItem)
{
var taskItem = await _context.TaskItems.FindAsync(updateTaskItem.Id);
var taskItem = await _context.TaskItems.FindAsync(id);
if (taskItem == null)
{
return NotFound();
Expand Down
32 changes: 32 additions & 0 deletions FunCoding.WhatToDo.WebApi/Controllers/WeatherForecastController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Microsoft.AspNetCore.Mvc;

namespace FunCoding.WhatToDo.WebApi.Controllers;

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

private readonly ILogger<WeatherForecastController> _logger;

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

[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
4 changes: 4 additions & 0 deletions FunCoding.WhatToDo.WebApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@
app.MapControllers();

app.Run();

public partial class Program
{
}
12 changes: 12 additions & 0 deletions FunCoding.WhatToDo.WebApi/WeatherForecast.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace FunCoding.WhatToDo.WebApi;

public class WeatherForecast
{
public DateOnly Date { get; set; }

public int TemperatureC { get; set; }

public int TemperatureF => 32 + (int) (TemperatureC / 0.5556);

public string? Summary { get; set; }
}
6 changes: 6 additions & 0 deletions FunCoding.WhatToDo.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FunCoding.WhatToDo.WebApi", "FunCoding.WhatToDo.WebApi\FunCoding.WhatToDo.WebApi.csproj", "{2A353F9A-16F6-4134-B9E8-FA235EDE215F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FunCoding.WhatTodo.IntegrationTests", "FunCoding.WhatTodo.IntegrationTests\FunCoding.WhatTodo.IntegrationTests.csproj", "{3AB0DDA7-9EBE-44E6-97CD-0A55E612E197}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -18,5 +20,9 @@ Global
{2A353F9A-16F6-4134-B9E8-FA235EDE215F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2A353F9A-16F6-4134-B9E8-FA235EDE215F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2A353F9A-16F6-4134-B9E8-FA235EDE215F}.Release|Any CPU.Build.0 = Release|Any CPU
{3AB0DDA7-9EBE-44E6-97CD-0A55E612E197}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3AB0DDA7-9EBE-44E6-97CD-0A55E612E197}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3AB0DDA7-9EBE-44E6-97CD-0A55E612E197}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3AB0DDA7-9EBE-44E6-97CD-0A55E612E197}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using FunCoding.WhatTodo.IntegrationTests.Helpers;
using FunCoding.WhatToDo.WebApi.Data;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

namespace FunCoding.WhatTodo.IntegrationTests;

public class CustomIntegrationTestsFixture : WebApplicationFactory<Program>
{
private const string ConnectionString =
"Server=localhost,1433;Database=WhatToDoServerTest;User Id=SA;Password=Burn@123;TrustServerCertificate=True;";

protected override void ConfigureWebHost(IWebHostBuilder builder)
{
// Set up test database
builder.ConfigureServices(services =>
{
var descriptor =
services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<ApplicationDbContext>));
services.Remove(descriptor);
services.AddDbContext<ApplicationDbContext>(options =>
{
options.UseSqlServer(ConnectionString);
});
using var scope = services.BuildServiceProvider().CreateScope();
var scopeServices = scope.ServiceProvider;
var dbContext = scopeServices.GetRequiredService<ApplicationDbContext>();
Utilities.InitializeDatabase(dbContext);


});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="8.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.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="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FunCoding.WhatToDo.WebApi\FunCoding.WhatToDo.WebApi.csproj" />
</ItemGroup>


</Project>
1 change: 1 addition & 0 deletions FunCoding.WhatTodo.IntegrationTests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
45 changes: 45 additions & 0 deletions FunCoding.WhatTodo.IntegrationTests/Helpers/Utilities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using FunCoding.WhatToDo.WebApi.Data;
using FunCoding.WhatToDo.WebApi.Models;
using Microsoft.EntityFrameworkCore;

namespace FunCoding.WhatTodo.IntegrationTests.Helpers;

public static class Utilities
{
public static void InitializeDatabase(ApplicationDbContext context)
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
SeedDatabase(context);
}

public static void Cleanup(ApplicationDbContext context)
{
context.TaskItems.ExecuteDelete();
context.SaveChanges();
SeedDatabase(context);
}

private static void SeedDatabase(ApplicationDbContext context)
{
// Create a few Contacts
var taskItems = new List<TaskItem>
{
new()
{
Id = Guid.Parse("8a9de219-2dde-4f2a-9ebd-b1f8df9fef03"),
Title = "Read a book",
Description = "Read a book everyday and write some notes about it.",
},
new()
{
Id = Guid.Parse("798a4706-4c51-48c9-8310-531d7364c926"),
Title = "Have a walk",
Description = "Have a walk everyday for at least 20 mins.",
}
};
context.TaskItems.AddRange(taskItems);
context.SaveChanges();
}

}
Loading