mirror of
https://github.com/rjNemo/ticket_manager
synced 2026-06-06 00:36:39 +00:00
Wrote Project COntrollers Test
This commit is contained in:
parent
89078bb24a
commit
faab56610c
9 changed files with 205 additions and 102 deletions
|
|
@ -37,7 +37,7 @@ namespace TicketManager.Controllers
|
|||
/// <response code="200">Returns a list of projects</response>
|
||||
[HttpGet]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
public async Task<IEnumerable<ProjectDTO>> GetProjects()
|
||||
public async Task<List<ProjectDTO>> GetProjects()
|
||||
{
|
||||
return await _context.Projects
|
||||
.Include(p => p.Assignments)
|
||||
|
|
|
|||
12
Startup.cs
12
Startup.cs
|
|
@ -1,7 +1,6 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Reflection;
|
||||
using System.IO;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.HttpsPolicy;
|
||||
|
|
@ -13,13 +12,10 @@ using Microsoft.Extensions.Logging;
|
|||
using Microsoft.AspNetCore.SpaServices.ReactDevelopmentServer;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using System.Reflection;
|
||||
using System.IO;
|
||||
using TicketManager.Data;
|
||||
using TicketManager.Models;
|
||||
using Microsoft.AspNetCore.Mvc.NewtonsoftJson;
|
||||
using Newtonsoft.Json;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Newtonsoft.Json;
|
||||
using TicketManager.Data;
|
||||
|
||||
[assembly: ApiController]
|
||||
namespace TicketManager
|
||||
|
|
|
|||
|
|
@ -0,0 +1,44 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using TicketManager.Data;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace TicketManager.Tests
|
||||
{
|
||||
public class ControllersTests
|
||||
{
|
||||
|
||||
public static void Wrapper(
|
||||
Func<DbContextOptions<AppDbContext>, Task> Test,
|
||||
Action<DbContextOptions<AppDbContext>> SeedDb)
|
||||
{
|
||||
// Create inMemory Test Database
|
||||
var connection = new SqliteConnection("DataSource=:memory:");
|
||||
connection.Open();
|
||||
|
||||
try
|
||||
{
|
||||
var options = new DbContextOptionsBuilder<AppDbContext>()
|
||||
.UseSqlite(connection)
|
||||
.Options;
|
||||
|
||||
// creates DB schema
|
||||
using (var context = new AppDbContext(options))
|
||||
{
|
||||
context.Database.EnsureCreated();
|
||||
}
|
||||
|
||||
// Seed DB usng one context instance
|
||||
SeedDb(options);
|
||||
|
||||
// use another context instance to run the test
|
||||
Test(options);
|
||||
}
|
||||
finally
|
||||
{
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,122 @@
|
|||
using System;
|
||||
using Xunit;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using TicketManager.Controllers;
|
||||
using TicketManager.Data;
|
||||
using TicketManager.Models;
|
||||
using TicketManager.DTO;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace TicketManager.Tests
|
||||
{
|
||||
public class ProjectsControllerTests
|
||||
{
|
||||
[Fact]
|
||||
public void Get_ReturnsListWith2Projects()
|
||||
{
|
||||
ControllersTests.Wrapper(Test_GetProjects, SeedDb.Projects);
|
||||
}
|
||||
|
||||
private static async Task Test_GetProjects(DbContextOptions<AppDbContext> options)
|
||||
{
|
||||
using (var context = new AppDbContext(options))
|
||||
{
|
||||
var controller = new ProjectsController(context);
|
||||
|
||||
var result = await controller.GetProjects();
|
||||
|
||||
Assert.IsAssignableFrom<IEnumerable<ProjectDTO>>(result);
|
||||
Assert.Equal(2, result.Count);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Get1_Returns1Project()
|
||||
{
|
||||
ControllersTests.Wrapper(Test_GetProject, SeedDb.Projects);
|
||||
}
|
||||
|
||||
private static async Task Test_GetProject(DbContextOptions<AppDbContext> options)
|
||||
{
|
||||
using (var context = new AppDbContext(options))
|
||||
{
|
||||
var controller = new ProjectsController(context);
|
||||
|
||||
// Should Return 1 Project
|
||||
var result = await controller.GetProject(1);
|
||||
Assert.IsAssignableFrom<ProjectDTO>(result);
|
||||
|
||||
// Should Return NotFound
|
||||
result = await controller.GetProject(3);
|
||||
Assert.IsType<NotFoundResult>(result);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Put1_Updates1Project()
|
||||
{
|
||||
ControllersTests.Wrapper(Test_PutProject, SeedDb.Projects);
|
||||
}
|
||||
|
||||
private static async Task Test_PutProject(DbContextOptions<AppDbContext> options)
|
||||
{
|
||||
using (var context = new AppDbContext(options))
|
||||
{
|
||||
var controller = new ProjectsController(context);
|
||||
|
||||
var result = await controller.PutProject(1,
|
||||
new Project()
|
||||
{
|
||||
Id = 1,
|
||||
Title = "Top Secret Project",
|
||||
Description = "Shht Don't Ask don't tell",
|
||||
PlannedEnding = new DateTime(2020, 7, 21)
|
||||
}
|
||||
);
|
||||
|
||||
// Should Update
|
||||
Assert.Equal("Top Secret Project", context.Projects.Find(1).Title);
|
||||
Assert.Equal(new DateTime(2020, 7, 21), context.Projects.Find(1).CreatedAt);
|
||||
Assert.IsType<NoContentResult>(result);
|
||||
|
||||
|
||||
result = await controller.PutProject(2,
|
||||
new Project()
|
||||
{
|
||||
Id = 1,
|
||||
Title = "Top Secret Project",
|
||||
Description = "Shht Don't Ask don't tell",
|
||||
PlannedEnding = new DateTime(2020, 7, 21)
|
||||
}
|
||||
);
|
||||
|
||||
// Should Return BadRequest
|
||||
Assert.NotEqual("Top Secret Project", context.Projects.Find(2).Title);
|
||||
Assert.NotEqual(new DateTime(2020, 7, 21), context.Projects.Find(2).CreatedAt);
|
||||
Assert.IsType<BadRequestResult>(result);
|
||||
|
||||
// Delete updated project
|
||||
context.Projects.RemoveRange(context.Projects.Find(1));
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
result = await controller.PutProject(1,
|
||||
new Project()
|
||||
{
|
||||
Id = 1,
|
||||
Title = "Top Secret Project",
|
||||
Description = "Shht Don't Ask don't tell",
|
||||
PlannedEnding = new DateTime(2020, 7, 21)
|
||||
}
|
||||
);
|
||||
|
||||
// Should Throw
|
||||
Assert.IsType<DbUpdateConcurrencyException>(result);
|
||||
Assert.Equal("Top Secret Project", context.Projects.Find(1).Title);
|
||||
Assert.Equal(new DateTime(2020, 7, 21), context.Projects.Find(1).CreatedAt);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
using System;
|
||||
using TicketManager.Data;
|
||||
using TicketManager.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace TicketManager.Tests
|
||||
{
|
||||
public class SeedDb
|
||||
{
|
||||
public static void Projects(DbContextOptions<AppDbContext> options)
|
||||
// Seed DB usng one context instance
|
||||
{
|
||||
using (var context = new AppDbContext(options))
|
||||
{
|
||||
context.Projects.AddRange(
|
||||
new Project()
|
||||
{
|
||||
Id = 1,
|
||||
Title = "Secret Project",
|
||||
Description = "Shht Don't Ask don't tell",
|
||||
PlannedEnding = new DateTime(2021, 7, 21)
|
||||
},
|
||||
new Project()
|
||||
{
|
||||
Id = 2,
|
||||
Title = "Public Project",
|
||||
Description = "It's quite obvious, isn't it?!",
|
||||
PlannedEnding = new DateTime(2036, 6, 16)
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,93 +0,0 @@
|
|||
using System;
|
||||
using Xunit;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Moq;
|
||||
using TicketManager.Controllers;
|
||||
using TicketManager.Data;
|
||||
using TicketManager.Models;
|
||||
using TicketManager.DTO;
|
||||
|
||||
namespace TicketManager.Tests
|
||||
{
|
||||
public class ProjectsControllerTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task Get_ReturnsAListofProjects()
|
||||
{
|
||||
// Arrange
|
||||
var mockRepo = new Mock<IProjectRepository>();
|
||||
mockRepo.Setup(r => r.List())
|
||||
.ReturnsAsync(GetTestProjects());
|
||||
var controller = new ProjectsController(mockRepo.Object);
|
||||
|
||||
// Act
|
||||
var result = await controller.GetProjects();
|
||||
|
||||
// Assert
|
||||
var viewResult = Assert.IsAssignableFrom<IEnumerable<Project>>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Get1_ReturnsProject1()
|
||||
{
|
||||
// Arrange
|
||||
var mockRepo = new Mock<IProjectRepository>();
|
||||
mockRepo.Setup(r => r.Get(1));
|
||||
// .ReturnsAsync(GetProjectDTO());
|
||||
var controller = new ProjectsController(mockRepo.Object);
|
||||
|
||||
// Act
|
||||
var result = await controller.GetProject(1);
|
||||
|
||||
// Assert
|
||||
var viewResult = Assert.IsAssignableFrom<Project>(result);
|
||||
}
|
||||
|
||||
private List<Project> GetTestProjects()
|
||||
{
|
||||
var projects = new List<Project>();
|
||||
projects.Add(new Project()
|
||||
{
|
||||
PlannedEnding = new DateTime(2016, 7, 2),
|
||||
Id = 1,
|
||||
Title = "Test One",
|
||||
});
|
||||
projects.Add(new Project()
|
||||
{
|
||||
PlannedEnding = new DateTime(2016, 7, 1),
|
||||
Id = 2,
|
||||
Title = "Test Two"
|
||||
});
|
||||
return projects;
|
||||
}
|
||||
|
||||
private ProjectDTO GetProjectDTO()
|
||||
{
|
||||
var project = new Project()
|
||||
{
|
||||
Id = 1,
|
||||
PlannedEnding = new DateTime(2016, 7, 2),
|
||||
Title = "Test One",
|
||||
Description = "Lorem Ipsum",
|
||||
Status = Status.InProgress
|
||||
};
|
||||
|
||||
return new ProjectDTO(project);
|
||||
}
|
||||
|
||||
|
||||
// [Fact]
|
||||
// public void Get_ReturnsProjectList()
|
||||
// {
|
||||
// // Arange
|
||||
// // var controller = new ProjectsController();
|
||||
|
||||
// // Act
|
||||
// // var result = controller.GetProjects();
|
||||
|
||||
// // Assert
|
||||
// // Assert.IsType<IEnumerable<Project>>(result);
|
||||
// }
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue