Skip to content

Commit

Permalink
Add some additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebazzz committed Mar 29, 2020
1 parent 68e97d4 commit fe86370
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
// Project : PokerTime.Application
// ******************************************************************************

namespace PokerTime.Application.Estimations.Queries {
namespace PokerTime.Application.Common.Models {
using System;
using System.Collections.Generic;
using System.Linq;
using Common.Mapping;
using Common.Models;
using Domain.Entities;
using Mapping;

public sealed class UserStoryEstimation : IMapFrom<UserStory> {
public int Id { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ namespace PokerTime.Application.Estimations.Queries {
using System.Threading;
using System.Threading.Tasks;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using Common;
using Common.Abstractions;
using Common.Models;
using Domain.Entities;
using MediatR;
using Microsoft.EntityFrameworkCore;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace PokerTime.Application.Estimations.Queries {
using System.Collections.Generic;
using System.Linq;
using Common.Models;

public sealed class GetEstimationsOverviewQueryResponse {
public ICollection<UserStoryEstimation> UserStoryEstimations { get; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// ******************************************************************************
// © 2020 Sebastiaan Dammann | damsteen.nl
//
// File: : GetEstimationsOverviewQueryHandlerTests.cs
// Project : PokerTime.Application.Tests.Unit
// ******************************************************************************

namespace PokerTime.Application.Tests.Unit.Estimations.Queries {
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Application.Common;
using Application.Estimations.Queries;
using Domain.Entities;
using NUnit.Framework;
using Support;

[TestFixture]
public sealed class GetEstimationsOverviewQueryHandlerTests : QueryTestBase {
[Test]
public void GetEstimationsOverviewQueryHandlerTests_ThrowsNotFoundException_WhenNotFound() {
// Given
const string sessionId = "surely-not-found";
var query = new GetEstimationsOverviewQuery(sessionId);
var handler = new GetEstimationsOverviewQueryHandler(this.Context, this.Mapper);

// When
TestDelegate action = () => handler.Handle(query, CancellationToken.None).GetAwaiter().GetResult();

// Then
Assert.That(action, Throws.InstanceOf<NotFoundException>());
}

[Test]
public async Task GetEstimationsOverviewQueryHandlerTests_ReturnsUserStoryEstimations() {
// Given
var session = new Session {
Title = "Yet another test",
Participants =
{
new Participant { Name = "John", Color = Color.BlueViolet },
new Participant { Name = "Jane", Color = Color.Aqua },
},
HashedPassphrase = "abef",
CurrentStage = SessionStage.Discussion
};
string sessionId = session.UrlId.StringId;
this.Context.Sessions.Add(session);
await this.Context.SaveChangesAsync(CancellationToken.None);

this.Context.UserStories.Add(new UserStory {
Title = "First",
Estimations =
{
new Estimation {Participant = session.Participants.First(), Symbol = this.Context.Symbols.First()},
new Estimation {Participant = session.Participants.Last(), Symbol = this.Context.Symbols.Skip(1).First()}
},
Session = session
});

this.Context.UserStories.Add(new UserStory {
Title = "First",
Estimations =
{
new Estimation
{Participant = session.Participants.First(), Symbol = this.Context.Symbols.Skip(1).First()},
new Estimation {Participant = session.Participants.Last(), Symbol = this.Context.Symbols.First()}
},
Session = session
});
await this.Context.SaveChangesAsync(CancellationToken.None);

var query = new GetEstimationsOverviewQuery(sessionId);
var handler = new GetEstimationsOverviewQueryHandler(this.Context, this.Mapper);

// When
GetEstimationsOverviewQueryResponse result = await handler.Handle(query, CancellationToken.None);

// Then
Assert.That(result, Is.Not.Null);

Assert.That(result.UserStoryEstimations, Has.Count.EqualTo(2));
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// ******************************************************************************
// © 2020 Sebastiaan Dammann | damsteen.nl
//
// File: : GetEstimationsQueryHandlerTests.cs
// Project : PokerTime.Application.Tests.Unit
// ******************************************************************************

namespace PokerTime.Application.Tests.Unit.Estimations.Queries {
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Application.Common;
using Application.Estimations.Queries;
using Domain.Entities;
using NUnit.Framework;
using Support;

[TestFixture]
public sealed class GetEstimationsQueryHandlerTests : QueryTestBase {
[Test]
public void GetEstimationsQueryHandlerTests_ThrowsNotFoundException_WhenSessionNotFound() {
// Given
const string sessionId = "surely-not-found";
var query = new GetEstimationsQuery(sessionId, 3);
var handler = new GetEstimationsQueryHandler(this.Context, this.Mapper);

// When
TestDelegate action = () => handler.Handle(query, CancellationToken.None).GetAwaiter().GetResult();

// Then
Assert.That(action, Throws.InstanceOf<NotFoundException>());
}

[Test]
public async Task GetEstimationsQueryHandlerTests_ThrowsNotFoundException_WhenUserStoryNotFound() {
// Given
var session = new Session {
Title = "Yet another test",
Participants =
{
new Participant { Name = "John", Color = Color.BlueViolet },
new Participant { Name = "Jane", Color = Color.Aqua },
},
HashedPassphrase = "abef",
CurrentStage = SessionStage.Discussion
};
string sessionId = session.UrlId.StringId;
this.Context.Sessions.Add(session);
await this.Context.SaveChangesAsync(CancellationToken.None);

var query = new GetEstimationsQuery(sessionId, -1);
var handler = new GetEstimationsQueryHandler(this.Context, this.Mapper);

// When
TestDelegate action = () => handler.Handle(query, CancellationToken.None).GetAwaiter().GetResult();

// Then
Assert.That(action, Throws.InstanceOf<NotFoundException>());
}

[Test]
public async Task GetEstimationsQueryHandlerTests_ReturnsUserStoryEstimations() {
// Given
var session = new Session {
Title = "Yet another test",
Participants =
{
new Participant { Name = "John", Color = Color.BlueViolet },
new Participant { Name = "Jane", Color = Color.Aqua },
},
HashedPassphrase = "abef",
CurrentStage = SessionStage.Discussion
};
string sessionId = session.UrlId.StringId;
this.Context.Sessions.Add(session);
await this.Context.SaveChangesAsync(CancellationToken.None);

this.Context.UserStories.Add(new UserStory {
Title = "First",
Estimations =
{
new Estimation {Participant = session.Participants.First(), Symbol = this.Context.Symbols.First()},
new Estimation {Participant = session.Participants.Last(), Symbol = this.Context.Symbols.Skip(1).First()}
},
Session = session
});

UserStory lastUserStory = this.Context.UserStories.Add(new UserStory {
Title = "First",
Estimations =
{
new Estimation
{Participant = session.Participants.First(), Symbol = this.Context.Symbols.Skip(1).First()},
new Estimation {Participant = session.Participants.Last(), Symbol = this.Context.Symbols.First()}
},
Session = session
}).Entity;
await this.Context.SaveChangesAsync(CancellationToken.None);

var query = new GetEstimationsQuery(sessionId, lastUserStory.Id);
var handler = new GetEstimationsQueryHandler(this.Context, this.Mapper);

// When
GetEstimationsQueryResponse result = await handler.Handle(query, CancellationToken.None);

// Then
Assert.That(result, Is.Not.Null);

Assert.That(result.Estimations, Has.Count.EqualTo(2));
}

}
}
1 change: 1 addition & 0 deletions tests/PokerTime.Application.Tests.Unit/MappingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace PokerTime.Application.Tests.Unit {
using System.Drawing;
using System.Linq;
using Application.Common.Models;
using Application.Estimations.Queries;
using Application.PredefinedParticipantColors.Queries.GetAvailablePredefinedParticipantColors;
using Application.Sessions.Queries.GetParticipantsInfo;
using Domain.Entities;
Expand Down

0 comments on commit fe86370

Please sign in to comment.