Skip to content

Commit

Permalink
Refactor UpdateManagerTest and add new observer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasweimann committed Nov 8, 2023
1 parent 87da3f8 commit a68c9a3
Showing 1 changed file with 224 additions and 14 deletions.
238 changes: 224 additions & 14 deletions src/UnitTests/UpdateManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using RxTelegram.Bot.Api;
using RxTelegram.Bot.Interface.BaseTypes;
using RxTelegram.Bot.Interface.BaseTypes.Enums;
using RxTelegram.Bot.Interface.InlineMode;
using RxTelegram.Bot.Interface.Payments;
using RxTelegram.Bot.Interface.Setup;

namespace RxTelegram.Bot.UnitTests;
Expand All @@ -16,11 +18,10 @@ public class UpdateManagerTest
{
private ITelegramBot _telegramBotMock = Substitute.For<ITelegramBot>();

public static Array GetUpdateTypes() => Enum.GetValues(typeof(UpdateType));

[SetUp]
public void TestInitialize()
{
_telegramBotMock = Substitute.For<ITelegramBot>();
}
public void TestInitialize() => _telegramBotMock = Substitute.For<ITelegramBot>();

[Test]
public void TestGetUpdateTypes()
Expand Down Expand Up @@ -94,7 +95,7 @@ public async Task Remove_Should_Remove_Observer_From_Observers_List(UpdateType u
public void Given_TelegramBotException_On_RunUpdateSafe_Should_Handle_Exception()
{
// Arrange
_telegramBotMock.GetUpdate(default, default)
_telegramBotMock.GetUpdate(default)
.ThrowsForAnyArgs(new Exception());

// Assert
Expand All @@ -120,23 +121,19 @@ public void Given_ValidUpdate_On_DistributeUpdates_Should_PushUpdatesTo_Observer
{
// Arrange
var observerAll = Substitute.For<IObserver<Update>>();
var observerMessage = Substitute.For<IObserver<Message>>();
var updateManager = new UpdateManager(_telegramBotMock);
var disposableAll = updateManager.Update.Subscribe(observerAll);
var disposableMessage = updateManager.Message.Subscribe(observerMessage);
var updates = new[] { new Update { Message = new Message() } };

// Act
updateManager.DistributeUpdates(updates);

// Assert
observerAll.Received().OnNext(updates.Single());
observerMessage.Received().OnNext(updates.Single().Message);
observerAll.Received()
.OnNext(updates.Single());
disposableAll.Dispose();
disposableMessage.Dispose();
}


[Test]
[TestCaseSource(nameof(GetUpdateTypes))]
public void OnException_WhenCalled_DistributesExceptionToObservers(UpdateType updateType)
Expand All @@ -163,9 +160,222 @@ public void OnException_WhenCalled_DistributesExceptionToObservers(UpdateType up
}

// Assert
observer1Mock.Received().OnError(exception);
observer2Mock.Received().OnError(exception);
observer1Mock.Received()
.OnError(exception);
observer2Mock.Received()
.OnError(exception);
}

public static Array GetUpdateTypes() => Enum.GetValues(typeof(UpdateType));
#region UpdateTypes

[Test]
public void Given_ValidUpdate_On_DistributeUpdates_Should_PushUpdatesTo_MessageObservers()
{
// Arrange
var observerAll = Substitute.For<IObserver<Message>>();
var updateManager = new UpdateManager(_telegramBotMock);
var disposableAll = updateManager.Message.Subscribe(observerAll);
var updates = new[] { new Update { Message = new Message() } };

// Act
updateManager.DistributeUpdates(updates);

// Assert
observerAll.Received()
.OnNext(updates.Single()
.Message);
disposableAll.Dispose();
}

[Test]
public void Given_ValidUpdate_On_DistributeUpdates_Should_PushUpdatesTo_EditedMessageObservers()
{
// Arrange
var observerAll = Substitute.For<IObserver<Message>>();
var updateManager = new UpdateManager(_telegramBotMock);
var disposableAll = updateManager.EditedMessage.Subscribe(observerAll);
var updates = new[] { new Update { EditedMessage = new Message() } };

// Act
updateManager.DistributeUpdates(updates);

// Assert
observerAll.Received()
.OnNext(updates.Single()
.EditedMessage);
disposableAll.Dispose();
}

[Test]
public void Given_ValidUpdate_On_DistributeUpdates_Should_PushUpdatesTo_InlineQueryObservers()
{
// Arrange
var observerAll = Substitute.For<IObserver<InlineQuery>>();
var updateManager = new UpdateManager(_telegramBotMock);
var disposableAll = updateManager.InlineQuery.Subscribe(observerAll);
var updates = new[] { new Update { InlineQuery = new InlineQuery() } };

// Act
updateManager.DistributeUpdates(updates);

// Assert
observerAll.Received()
.OnNext(updates.Single()
.InlineQuery);
disposableAll.Dispose();
}

[Test]
public void Given_ValidUpdate_On_DistributeUpdates_Should_PushUpdatesTo_ChosenInlineResultObservers()
{
// Arrange
var observerAll = Substitute.For<IObserver<ChosenInlineResult>>();
var updateManager = new UpdateManager(_telegramBotMock);
var disposableAll = updateManager.ChosenInlineResult.Subscribe(observerAll);
var updates = new[] { new Update { ChosenInlineResult = new ChosenInlineResult() } };

// Act
updateManager.DistributeUpdates(updates);

// Assert
observerAll.Received()
.OnNext(updates.Single()
.ChosenInlineResult);
disposableAll.Dispose();
}

[Test]
public void Given_ValidUpdate_On_DistributeUpdates_Should_PushUpdatesTo_CallbackQueryObservers()
{
// Arrange
var observerAll = Substitute.For<IObserver<CallbackQuery>>();
var updateManager = new UpdateManager(_telegramBotMock);
var disposableAll = updateManager.CallbackQuery.Subscribe(observerAll);
var updates = new[] { new Update { CallbackQuery = new CallbackQuery() } };

// Act
updateManager.DistributeUpdates(updates);

// Assert
observerAll.Received()
.OnNext(updates.Single()
.CallbackQuery);
disposableAll.Dispose();
}

[Test]
public void Given_ValidUpdate_On_DistributeUpdates_Should_PushUpdatesTo_ChannelPostObservers()
{
// Arrange
var observerAll = Substitute.For<IObserver<Message>>();
var updateManager = new UpdateManager(_telegramBotMock);
var disposableAll = updateManager.ChannelPost.Subscribe(observerAll);
var updates = new[] { new Update { ChannelPost = new Message() } };

// Act
updateManager.DistributeUpdates(updates);

// Assert
observerAll.Received()
.OnNext(updates.Single()
.ChannelPost);
disposableAll.Dispose();
}

[Test]
public void Given_ValidUpdate_On_DistributeUpdates_Should_PushUpdatesTo_EditedChannelPostObservers()
{
// Arrange
var observerAll = Substitute.For<IObserver<Message>>();
var updateManager = new UpdateManager(_telegramBotMock);
var disposableAll = updateManager.EditedChannelPost.Subscribe(observerAll);
var updates = new[] { new Update { EditedChannelPost = new Message() } };

// Act
updateManager.DistributeUpdates(updates);

// Assert
observerAll.Received()
.OnNext(updates.Single()
.EditedChannelPost);
disposableAll.Dispose();
}

[Test]
public void Given_ValidUpdate_On_DistributeUpdates_Should_PushUpdatesTo_ShippingQueryObservers()
{
// Arrange
var observerAll = Substitute.For<IObserver<ShippingQuery>>();
var updateManager = new UpdateManager(_telegramBotMock);
var disposableAll = updateManager.ShippingQuery.Subscribe(observerAll);
var updates = new[] { new Update { ShippingQuery = new ShippingQuery() } };

// Act
updateManager.DistributeUpdates(updates);

// Assert
observerAll.Received()
.OnNext(updates.Single()
.ShippingQuery);
disposableAll.Dispose();
}

[Test]
public void Given_ValidUpdate_On_DistributeUpdates_Should_PushUpdatesTo_PreCheckoutQueryObservers()
{
// Arrange
var observerAll = Substitute.For<IObserver<PreCheckoutQuery>>();
var updateManager = new UpdateManager(_telegramBotMock);
var disposableAll = updateManager.PreCheckoutQuery.Subscribe(observerAll);
var updates = new[] { new Update { PreCheckoutQuery = new PreCheckoutQuery() } };

// Act
updateManager.DistributeUpdates(updates);

// Assert
observerAll.Received()
.OnNext(updates.Single()
.PreCheckoutQuery);
disposableAll.Dispose();
}

[Test]
public void Given_ValidUpdate_On_DistributeUpdates_Should_PushUpdatesTo_PollObservers()
{
// Arrange
var observerAll = Substitute.For<IObserver<Poll>>();
var updateManager = new UpdateManager(_telegramBotMock);
var disposableAll = updateManager.Poll.Subscribe(observerAll);
var updates = new[] { new Update { Poll = new Poll() } };

// Act
updateManager.DistributeUpdates(updates);

// Assert
observerAll.Received()
.OnNext(updates.Single()
.Poll);
disposableAll.Dispose();
}

[Test]
public void Given_ValidUpdate_On_DistributeUpdates_Should_PushUpdatesTo_PollAnswerObservers()
{
// Arrange
var observerAll = Substitute.For<IObserver<PollAnswer>>();
var updateManager = new UpdateManager(_telegramBotMock);
var disposableAll = updateManager.PollAnswer.Subscribe(observerAll);
var updates = new[] { new Update { PollAnswer = new PollAnswer() } };

// Act
updateManager.DistributeUpdates(updates);

// Assert
observerAll.Received()
.OnNext(updates.Single()
.PollAnswer);
disposableAll.Dispose();
}

#endregion
}

0 comments on commit a68c9a3

Please sign in to comment.