|
| 1 | +// ****************************************************************************** |
| 2 | +// © 2020 Sebastiaan Dammann | damsteen.nl |
| 3 | +// |
| 4 | +// File: : GetEstimationsQueryHandlerTests.cs |
| 5 | +// Project : PokerTime.Application.Tests.Unit |
| 6 | +// ****************************************************************************** |
| 7 | + |
| 8 | +namespace PokerTime.Application.Tests.Unit.Estimations.Queries { |
| 9 | + using System.Drawing; |
| 10 | + using System.Linq; |
| 11 | + using System.Threading; |
| 12 | + using System.Threading.Tasks; |
| 13 | + using Application.Common; |
| 14 | + using Application.Estimations.Queries; |
| 15 | + using Domain.Entities; |
| 16 | + using NUnit.Framework; |
| 17 | + using Support; |
| 18 | + |
| 19 | + [TestFixture] |
| 20 | + public sealed class GetEstimationsQueryHandlerTests : QueryTestBase { |
| 21 | + [Test] |
| 22 | + public void GetEstimationsQueryHandlerTests_ThrowsNotFoundException_WhenSessionNotFound() { |
| 23 | + // Given |
| 24 | + const string sessionId = "surely-not-found"; |
| 25 | + var query = new GetEstimationsQuery(sessionId, 3); |
| 26 | + var handler = new GetEstimationsQueryHandler(this.Context, this.Mapper); |
| 27 | + |
| 28 | + // When |
| 29 | + TestDelegate action = () => handler.Handle(query, CancellationToken.None).GetAwaiter().GetResult(); |
| 30 | + |
| 31 | + // Then |
| 32 | + Assert.That(action, Throws.InstanceOf<NotFoundException>()); |
| 33 | + } |
| 34 | + |
| 35 | + [Test] |
| 36 | + public async Task GetEstimationsQueryHandlerTests_ThrowsNotFoundException_WhenUserStoryNotFound() { |
| 37 | + // Given |
| 38 | + var session = new Session { |
| 39 | + Title = "Yet another test", |
| 40 | + Participants = |
| 41 | + { |
| 42 | + new Participant { Name = "John", Color = Color.BlueViolet }, |
| 43 | + new Participant { Name = "Jane", Color = Color.Aqua }, |
| 44 | + }, |
| 45 | + HashedPassphrase = "abef", |
| 46 | + CurrentStage = SessionStage.Discussion |
| 47 | + }; |
| 48 | + string sessionId = session.UrlId.StringId; |
| 49 | + this.Context.Sessions.Add(session); |
| 50 | + await this.Context.SaveChangesAsync(CancellationToken.None); |
| 51 | + |
| 52 | + var query = new GetEstimationsQuery(sessionId, -1); |
| 53 | + var handler = new GetEstimationsQueryHandler(this.Context, this.Mapper); |
| 54 | + |
| 55 | + // When |
| 56 | + TestDelegate action = () => handler.Handle(query, CancellationToken.None).GetAwaiter().GetResult(); |
| 57 | + |
| 58 | + // Then |
| 59 | + Assert.That(action, Throws.InstanceOf<NotFoundException>()); |
| 60 | + } |
| 61 | + |
| 62 | + [Test] |
| 63 | + public async Task GetEstimationsQueryHandlerTests_ReturnsUserStoryEstimations() { |
| 64 | + // Given |
| 65 | + var session = new Session { |
| 66 | + Title = "Yet another test", |
| 67 | + Participants = |
| 68 | + { |
| 69 | + new Participant { Name = "John", Color = Color.BlueViolet }, |
| 70 | + new Participant { Name = "Jane", Color = Color.Aqua }, |
| 71 | + }, |
| 72 | + HashedPassphrase = "abef", |
| 73 | + CurrentStage = SessionStage.Discussion |
| 74 | + }; |
| 75 | + string sessionId = session.UrlId.StringId; |
| 76 | + this.Context.Sessions.Add(session); |
| 77 | + await this.Context.SaveChangesAsync(CancellationToken.None); |
| 78 | + |
| 79 | + this.Context.UserStories.Add(new UserStory { |
| 80 | + Title = "First", |
| 81 | + Estimations = |
| 82 | + { |
| 83 | + new Estimation {Participant = session.Participants.First(), Symbol = this.Context.Symbols.First()}, |
| 84 | + new Estimation {Participant = session.Participants.Last(), Symbol = this.Context.Symbols.Skip(1).First()} |
| 85 | + }, |
| 86 | + Session = session |
| 87 | + }); |
| 88 | + |
| 89 | + UserStory lastUserStory = this.Context.UserStories.Add(new UserStory { |
| 90 | + Title = "First", |
| 91 | + Estimations = |
| 92 | + { |
| 93 | + new Estimation |
| 94 | + {Participant = session.Participants.First(), Symbol = this.Context.Symbols.Skip(1).First()}, |
| 95 | + new Estimation {Participant = session.Participants.Last(), Symbol = this.Context.Symbols.First()} |
| 96 | + }, |
| 97 | + Session = session |
| 98 | + }).Entity; |
| 99 | + await this.Context.SaveChangesAsync(CancellationToken.None); |
| 100 | + |
| 101 | + var query = new GetEstimationsQuery(sessionId, lastUserStory.Id); |
| 102 | + var handler = new GetEstimationsQueryHandler(this.Context, this.Mapper); |
| 103 | + |
| 104 | + // When |
| 105 | + GetEstimationsQueryResponse result = await handler.Handle(query, CancellationToken.None); |
| 106 | + |
| 107 | + // Then |
| 108 | + Assert.That(result, Is.Not.Null); |
| 109 | + |
| 110 | + Assert.That(result.Estimations, Has.Count.EqualTo(2)); |
| 111 | + } |
| 112 | + |
| 113 | + } |
| 114 | +} |
0 commit comments