Skip to content

Commit

Permalink
Merge pull request #18 from RxTelegram/fix_update_manager
Browse files Browse the repository at this point in the history
Fix type issue on update manager
  • Loading branch information
niklasweimann authored Nov 8, 2023
2 parents 36e9727 + ce286d0 commit c4ec05c
Show file tree
Hide file tree
Showing 2 changed files with 250 additions and 33 deletions.
46 changes: 24 additions & 22 deletions src/RxTelegram.Bot/Api/UpdateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using RxTelegram.Bot.Interface.InlineMode;
using RxTelegram.Bot.Interface.Payments;
using RxTelegram.Bot.Interface.Setup;

#if NETSTANDARD2_1
using RxTelegram.Bot.Utils;
#endif
Expand Down Expand Up @@ -159,38 +158,39 @@ internal void DistributeUpdates(Update[] updates)
return;
}

var updateStrategies = new Dictionary<Func<Update, object>, UpdateType>
var updateStrategies = new List<Action<Update>>
{
{ update => update.PreCheckoutQuery, UpdateType.PreCheckoutQuery },
{ update => update.ShippingQuery, UpdateType.ShippingQuery },
{ update => update.EditedChannelPost, UpdateType.EditedChannelPost },
{ update => update.ChannelPost, UpdateType.ChannelPost },
{ update => update.CallbackQuery, UpdateType.CallbackQuery },
{ update => update.Poll, UpdateType.Poll },
{ update => update.PollAnswer, UpdateType.PollAnswer },
{ update => update.ChosenInlineResult, UpdateType.ChosenInlineResult },
{ update => update.InlineQuery, UpdateType.InlineQuery },
{ update => update.EditedMessage, UpdateType.EditedMessage },
{ update => update.Message, UpdateType.Message },
update => OnNext(UpdateType.PreCheckoutQuery, update.PreCheckoutQuery),
update => OnNext(UpdateType.ShippingQuery, update.ShippingQuery),
update => OnNext(UpdateType.EditedChannelPost, update.EditedChannelPost),
update => OnNext(UpdateType.ChannelPost, update.ChannelPost),
update => OnNext(UpdateType.CallbackQuery, update.CallbackQuery),
update => OnNext(UpdateType.Poll, update.Poll),
update => OnNext(UpdateType.PollAnswer, update.PollAnswer),
update => OnNext(UpdateType.ChosenInlineResult, update.ChosenInlineResult),
update => OnNext(UpdateType.InlineQuery, update.InlineQuery),
update => OnNext(UpdateType.EditedMessage, update.EditedMessage),
update => OnNext(UpdateType.Message, update.Message),
};

foreach (var update in updates)
{
OnNext(null, update);

foreach (var pair in updateStrategies)
foreach (var func in updateStrategies)
{
var updateObject = pair.Key(update);
if (updateObject != null)
{
OnNext(pair.Value, updateObject);
}
func(update);
}
}
}

internal void OnNext<T>(UpdateType? updateType, T updateMessage)
{
if (updateMessage == null)
{
return;
}

var observers = GetObservers(updateType);
if (!observers.Any())
{
Expand All @@ -199,7 +199,7 @@ internal void OnNext<T>(UpdateType? updateType, T updateMessage)

foreach (var observerObject in observers)
{
if (!(observerObject is IObserver<T> observer))
if (observerObject is not IObserver<T> observer)
{
continue;
}
Expand All @@ -222,8 +222,10 @@ internal void OnException(Exception exception)
return;
}

foreach (var observer in _observerDictionary.Values.SelectMany(x => x)
.ToList())
var observers = _observerDictionary.Values.SelectMany(x => x).ToList();
observers.AddRange(_updateObservers);

foreach (var observer in observers)
{
var observerType = observer.GetType();
if (!observerType.GetInterfaces()
Expand Down
237 changes: 226 additions & 11 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 @@ -121,15 +122,16 @@ public void Given_ValidUpdate_On_DistributeUpdates_Should_PushUpdatesTo_Observer
// Arrange
var observer = Substitute.For<IObserver<Update>>();
var updateManager = new UpdateManager(_telegramBotMock);
var disposable = updateManager.Update.Subscribe(observer);
var disposableAll = updateManager.Update.Subscribe(observer);
var updates = new[] { new Update { Message = new Message() } };

// Act
updateManager.DistributeUpdates(updates);

// Assert
observer.Received().OnNext(updates.Single());
disposable.Dispose();
observer.Received()
.OnNext(updates.Single());
disposableAll.Dispose();
}

[Test]
Expand Down Expand Up @@ -158,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 observer = Substitute.For<IObserver<Message>>();
var updateManager = new UpdateManager(_telegramBotMock);
var disposableAll = updateManager.Message.Subscribe(observer);
var updates = new[] { new Update { Message = new Message() } };

// Act
updateManager.DistributeUpdates(updates);

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

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

// Act
updateManager.DistributeUpdates(updates);

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

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

// Act
updateManager.DistributeUpdates(updates);

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

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

// Act
updateManager.DistributeUpdates(updates);

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

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

// Act
updateManager.DistributeUpdates(updates);

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

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

// Act
updateManager.DistributeUpdates(updates);

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

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

// Act
updateManager.DistributeUpdates(updates);

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

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

// Act
updateManager.DistributeUpdates(updates);

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

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

// Act
updateManager.DistributeUpdates(updates);

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

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

// Act
updateManager.DistributeUpdates(updates);

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

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

// Act
updateManager.DistributeUpdates(updates);

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

#endregion
}

0 comments on commit c4ec05c

Please sign in to comment.