-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from GuardianPlatform/UnitTesting
Unit testing Pagination
- Loading branch information
Showing
1 changed file
with
117 additions
and
0 deletions.
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
117
Guardian.Backend/Guardian.Test.Unit/Persistence/GameControllerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Guardian.Domain.Entities; | ||
using NUnit.Framework; | ||
using System.Threading.Tasks; | ||
using Guardian.Domain.Models; | ||
using System.Linq; | ||
using Guardian.Infrastructure.Database; | ||
using Guardian.Service.Features.Game.Queries; | ||
using System; | ||
|
||
namespace Guardian.Test.Unit.Persistence | ||
{ | ||
|
||
public class GameControllerTests | ||
{ | ||
|
||
[Test] | ||
public async Task PaginationTest_FirstPageThreeItems() | ||
{ | ||
DbContextOptions<ApplicationDbContext> dbContextOptions = new DbContextOptionsBuilder<ApplicationDbContext>() | ||
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()) | ||
.Options; | ||
var context = new ApplicationDbContext(dbContextOptions); | ||
|
||
context.Add(new Game { Name = "gra1", Description = "dsc1", Author = "aa1" }); | ||
context.Add(new Game { Name = "gra2", Description = "dsc2", Author = "aa2" }); | ||
context.Add(new Game { Name = "gra3", Description = "dsc3", Author = "aa3" }); | ||
context.Add(new Game { Name = "gra4", Description = "dsc4", Author = "aa4" }); | ||
context.SaveChanges(); | ||
const int MaxGamescount = 3; | ||
var request = new GetAllGamesQuery() | ||
{ | ||
Pagination = new PagiantionModel(0, MaxGamescount) | ||
}; | ||
|
||
var service = new GetAllGamesQuery.GetAllGamesQueryHandler(context); | ||
|
||
var result = await service.Handle(request, default); | ||
|
||
Assert.AreEqual(MaxGamescount, result.Count()); | ||
} | ||
|
||
[Test] | ||
public async Task PaginationTest_SecondPageOneItem() | ||
{ | ||
DbContextOptions<ApplicationDbContext> dbContextOptions = new DbContextOptionsBuilder<ApplicationDbContext>() | ||
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()) | ||
.Options; | ||
var context = new ApplicationDbContext(dbContextOptions); | ||
|
||
context.Add(new Game { Name = "gra1", Description = "dsc1", Author = "aa1" }); | ||
context.Add(new Game { Name = "gra2", Description = "dsc2", Author = "aa2" }); | ||
context.Add(new Game { Name = "gra3", Description = "dsc3", Author = "aa3" }); | ||
context.Add(new Game { Name = "gra4", Description = "dsc4", Author = "aa4" }); | ||
context.SaveChanges(); | ||
const int MaxGamescount = 3; | ||
var request = new GetAllGamesQuery() | ||
{ | ||
Pagination = new PagiantionModel(1, MaxGamescount) | ||
}; | ||
|
||
var service = new GetAllGamesQuery.GetAllGamesQueryHandler(context); | ||
|
||
var result = await service.Handle(request, default); | ||
|
||
Assert.AreEqual(1, result.Count()); | ||
} | ||
|
||
[Test] | ||
public async Task PaginationTest_OneItemFirstPage() | ||
{ | ||
DbContextOptions<ApplicationDbContext> dbContextOptions = new DbContextOptionsBuilder<ApplicationDbContext>() | ||
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()) | ||
.Options; | ||
var context = new ApplicationDbContext(dbContextOptions); | ||
|
||
context.Add(new Game { Name = "gra1", Description = "dsc1", Author = "aa1" }); | ||
|
||
context.SaveChanges(); | ||
const int MaxGamescount = 3; | ||
var request = new GetAllGamesQuery() | ||
{ | ||
Pagination = new PagiantionModel(0, MaxGamescount) | ||
}; | ||
|
||
var service = new GetAllGamesQuery.GetAllGamesQueryHandler(context); | ||
|
||
var result = await service.Handle(request, default); | ||
|
||
Assert.AreEqual(1, result.Count()); | ||
} | ||
|
||
[Test] | ||
public async Task PaginationTest_OneItemSecondPage() | ||
{ | ||
DbContextOptions<ApplicationDbContext> dbContextOptions = new DbContextOptionsBuilder<ApplicationDbContext>() | ||
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()) | ||
.Options; | ||
var context = new ApplicationDbContext(dbContextOptions); | ||
|
||
context.Add(new Game { Name = "gra1", Description = "dsc1", Author = "aa1" }); | ||
context.SaveChanges(); | ||
const int MaxGamescount = 3; | ||
var request = new GetAllGamesQuery() | ||
{ | ||
Pagination = new PagiantionModel(1, MaxGamescount) | ||
}; | ||
|
||
var service = new GetAllGamesQuery.GetAllGamesQueryHandler(context); | ||
|
||
var result = await service.Handle(request, default); | ||
|
||
Assert.AreEqual(0, result.Count()); | ||
} | ||
} | ||
|
||
} |