mirror of
https://github.com/rjNemo/ticket_manager
synced 2026-06-06 08:46:39 +00:00
Wrote Project COntrollers Test
This commit is contained in:
parent
faab56610c
commit
ea6395f863
5 changed files with 214 additions and 159 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -9,6 +9,8 @@ Data/UnitOfWork.cs
|
|||
Data/*Repository.cs
|
||||
Migrations/
|
||||
Properties/
|
||||
Tests/TicketManager.Tests/UnitTests/ControllersTests/ControllerTests.cs
|
||||
Tests/TicketManager.Tests/UnitTests/ControllersTests/SeedDb.cs
|
||||
|
||||
# client
|
||||
client/src/pages/TestPage.tsx
|
||||
|
|
|
|||
|
|
@ -1,44 +0,0 @@
|
|||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2,121 +2,252 @@ using System;
|
|||
using Xunit;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Data.Sqlite;
|
||||
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()
|
||||
public async Task Get_ReturnsListWith2Projects()
|
||||
{
|
||||
ControllersTests.Wrapper(Test_GetProjects, SeedDb.Projects);
|
||||
}
|
||||
var connection = new SqliteConnection("DataSource=:memory:");
|
||||
connection.Open();
|
||||
|
||||
private static async Task Test_GetProjects(DbContextOptions<AppDbContext> options)
|
||||
{
|
||||
using (var context = new AppDbContext(options))
|
||||
try
|
||||
{
|
||||
var controller = new ProjectsController(context);
|
||||
var options = new DbContextOptionsBuilder<AppDbContext>()
|
||||
.UseSqlite(connection)
|
||||
.Options;
|
||||
|
||||
var result = await controller.GetProjects();
|
||||
// creates DB schema
|
||||
using (var context = new AppDbContext(options))
|
||||
{
|
||||
context.Database.EnsureCreated();
|
||||
}
|
||||
|
||||
Assert.IsAssignableFrom<IEnumerable<ProjectDTO>>(result);
|
||||
Assert.Equal(2, result.Count);
|
||||
// Seed DB usng one context instance
|
||||
SeedDb(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);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Get1_Returns1Project()
|
||||
public async Task Get1_Returns1Project()
|
||||
{
|
||||
ControllersTests.Wrapper(Test_GetProject, SeedDb.Projects);
|
||||
}
|
||||
var connection = new SqliteConnection("DataSource=:memory:");
|
||||
connection.Open();
|
||||
|
||||
private static async Task Test_GetProject(DbContextOptions<AppDbContext> options)
|
||||
{
|
||||
using (var context = new AppDbContext(options))
|
||||
try
|
||||
{
|
||||
var controller = new ProjectsController(context);
|
||||
var options = new DbContextOptionsBuilder<AppDbContext>()
|
||||
.UseSqlite(connection)
|
||||
.Options;
|
||||
|
||||
// Should Return 1 Project
|
||||
var result = await controller.GetProject(1);
|
||||
Assert.IsAssignableFrom<ProjectDTO>(result);
|
||||
// creates DB schema
|
||||
using (var context = new AppDbContext(options))
|
||||
{
|
||||
context.Database.EnsureCreated();
|
||||
}
|
||||
|
||||
// Should Return NotFound
|
||||
result = await controller.GetProject(3);
|
||||
Assert.IsType<NotFoundResult>(result);
|
||||
// Seed DB usng one context instance
|
||||
SeedDb(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);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Put1_Updates1Project()
|
||||
public async Task Put1_Updates1Project()
|
||||
{
|
||||
ControllersTests.Wrapper(Test_PutProject, SeedDb.Projects);
|
||||
}
|
||||
// ControllersTests.Wrapper(Test_PutProject, SeedDb);
|
||||
// }
|
||||
|
||||
private static async Task Test_PutProject(DbContextOptions<AppDbContext> options)
|
||||
{
|
||||
using (var context = new AppDbContext(options))
|
||||
// private static async Task Test_PutProject()
|
||||
// {
|
||||
var connection = new SqliteConnection("DataSource=:memory:");
|
||||
connection.Open();
|
||||
|
||||
try
|
||||
{
|
||||
var controller = new ProjectsController(context);
|
||||
var options = new DbContextOptionsBuilder<AppDbContext>()
|
||||
.UseSqlite(connection)
|
||||
.Options;
|
||||
|
||||
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)
|
||||
}
|
||||
);
|
||||
// creates DB schema
|
||||
using (var context = new AppDbContext(options))
|
||||
{
|
||||
context.Database.EnsureCreated();
|
||||
}
|
||||
|
||||
// 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);
|
||||
// Seed DB usng one context instance
|
||||
SeedDb(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).PlannedEnding);
|
||||
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)
|
||||
}
|
||||
);
|
||||
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);
|
||||
// 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();
|
||||
// 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)
|
||||
}
|
||||
);
|
||||
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);
|
||||
// Should Throw
|
||||
Assert.IsType<NotFoundResult>(result);
|
||||
Assert.Equal("Top Secret Project", context.Projects.Find(1).Title);
|
||||
Assert.Equal(new DateTime(2020, 7, 21), context.Projects.Find(1).PlannedEnding);
|
||||
}
|
||||
}
|
||||
|
||||
finally
|
||||
{
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Post_CreatesProject()
|
||||
{
|
||||
// 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
|
||||
using (var context = new AppDbContext(options))
|
||||
{
|
||||
var proj = new Project()
|
||||
{
|
||||
Title = "The Third",
|
||||
Description = "Thrice in a row",
|
||||
PlannedEnding = DateTime.Now
|
||||
};
|
||||
|
||||
var controller = new ProjectsController(context);
|
||||
|
||||
var result = await controller.PostProject(proj);
|
||||
|
||||
Assert.IsAssignableFrom<ProjectDTO>(result);
|
||||
Assert.Equal(3, await context.Projects.CountAsync());
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
|
||||
private static void SeedDb(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,34 +0,0 @@
|
|||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -50,6 +50,13 @@ namespace TicketManager.Tests
|
|||
Ticket t5 = new Ticket();
|
||||
Ticket t6 = new Ticket();
|
||||
|
||||
p1.Tickets.Add(t1);
|
||||
p2.Tickets.Add(t2);
|
||||
p2.Tickets.Add(t3);
|
||||
p3.Tickets.Add(t4);
|
||||
p3.Tickets.Add(t5);
|
||||
p3.Tickets.Add(t6);
|
||||
|
||||
Assignment a1 = new Assignment()
|
||||
{
|
||||
User = user,
|
||||
|
|
@ -69,13 +76,6 @@ namespace TicketManager.Tests
|
|||
};
|
||||
user.Assignments.Add(a3);
|
||||
|
||||
p1.Tickets.Add(t1);
|
||||
p2.Tickets.Add(t2);
|
||||
p2.Tickets.Add(t3);
|
||||
p3.Tickets.Add(t4);
|
||||
p3.Tickets.Add(t5);
|
||||
p3.Tickets.Add(t6);
|
||||
|
||||
var res = user.GetTickets().Count;
|
||||
Assert.Equal(6, res);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue