Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/v7.0.0' into v7.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasweimann committed Dec 31, 2023
2 parents fed9f09 + f2a7c16 commit f5c4173
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 32 deletions.
1 change: 1 addition & 0 deletions src/RxTelegram.Bot/Api/BaseTelegramBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public static JsonSerializerSettings JsonSerializerSettings
new UnixDateTimeConverter(),
new InputFileConverter(),
new ChatIdConverter(),
new ChatBoostSourceConverter(),
new StringEnumConverter(new SnakeCaseNamingStrategy())
};
return _jsonSerializerSettings = new JsonSerializerSettings
Expand Down
32 changes: 32 additions & 0 deletions src/RxTelegram.Bot/Utils/ChatBoostSourceConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RxTelegram.Bot.Interface.BaseTypes;
using RxTelegram.Bot.Interface.BaseTypes.Enums;

namespace RxTelegram.Bot.Utils;

public class ChatBoostSourceConverter : JsonConverter
{
public override bool CanWrite => false;

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) => throw new NotSupportedException();

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var jsonObject = JObject.Load(reader);
var sourceType = jsonObject.GetValue("source")
?.ToObject<ChatBoostSourceType>(serializer);

return sourceType switch
{
ChatBoostSourceType.GiftCode => jsonObject.ToObject<ChatBoostSourceGiftCode>(serializer),
ChatBoostSourceType.Premium => jsonObject.ToObject<ChatBoostSourcePremium>(serializer),
ChatBoostSourceType.Giveaway => jsonObject.ToObject<ChatBoostSourceGiveaway>(serializer),
_ => throw new NotSupportedException($"The specified type {sourceType} is not supported by this converter.")
};
}

public override bool CanConvert(Type objectType) => objectType == typeof(ChatBoostSource);

}
32 changes: 0 additions & 32 deletions src/UnitTests/ChatBoostTest.cs

This file was deleted.

67 changes: 67 additions & 0 deletions src/UnitTests/JsonConverters/ChatBoostSourceConverterTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using NUnit.Framework;
using RxTelegram.Bot.Interface.BaseTypes;
using RxTelegram.Bot.Interface.BaseTypes.Enums;
using RxTelegram.Bot.Utils;

namespace RxTelegram.Bot.UnitTests.JsonConverters;

[TestFixture]
public class ChatBoostSourceConverterTest : BaseConverterTest
{
private const string User = "{\"id\":123,\"is_bot\":false,\"first_name\":\"Test\"}";

[Test]
public async Task ShouldDeserializeHttpResponse()
{
const string boost = "{\"boost_id\":\"abc\",\"add_date\":123,\"expiration_date\":321, \"source\": {\"source\":\"premium\", \"user\": " + User + "}}";
const string content = "{\"ok\":true,\"result\":{\"boosts\":[" + boost + "]}}";
var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(content) };

var userChatBoosts = await Task.FromResult(response)
.ParseResponse<UserChatBoosts>();
Assert.That(userChatBoosts, Is.Not.Null);
Assert.That(userChatBoosts.Boosts, Has.Count.EqualTo(1));
Assert.That(userChatBoosts.Boosts[0].BoostId, Is.EqualTo("abc"));
Assert.That(userChatBoosts.Boosts[0].AddDate, Is.EqualTo(123));
Assert.That(userChatBoosts.Boosts[0].ExpirationDate, Is.EqualTo(321));
Assert.That(userChatBoosts.Boosts[0].Source.Source, Is.EqualTo(ChatBoostSourceType.Premium));
Assert.That(userChatBoosts.Boosts[0].Source, Is.TypeOf<ChatBoostSourcePremium>());
}

[Test]
public void ShouldDeserializePremium()
{
const string json = "{\"boost_id\":\"abc\",\"add_date\":123,\"expiration_date\":321, \"source\": {\"source\":\"premium\", \"user\": " + User + "}}";
var deserializedJson = JsonConvert.DeserializeObject<ChatBoost>(json, JsonSerializerSettings);
Assert.NotNull(deserializedJson);
Assert.That(deserializedJson.Source.Source, Is.EqualTo(ChatBoostSourceType.Premium));
Assert.That(deserializedJson.Source, Is.TypeOf<ChatBoostSourcePremium>());
}

[Test]
public void ShouldDeserializeGiftCode()
{
const string json =
"{\"boost_id\":\"abc\",\"add_date\":123,\"expiration_date\":321, \"source\": {\"source\":\"gift_code\", \"user\": " +
User +
"}}";
var deserializedJson = JsonConvert.DeserializeObject<ChatBoost>(json, JsonSerializerSettings);
Assert.NotNull(deserializedJson);
Assert.That(deserializedJson.Source.Source, Is.EqualTo(ChatBoostSourceType.GiftCode));
Assert.That(deserializedJson.Source, Is.TypeOf<ChatBoostSourceGiftCode>());
}

[Test]
public void ShouldDeserializeGiveaway()
{
const string json = "{\"boost_id\":\"abc\",\"add_date\":123,\"expiration_date\":321, \"source\": {\"source\":\"giveaway\", \"giveaway_message_id\": 123}}";
var deserializedJson = JsonConvert.DeserializeObject<ChatBoost>(json, JsonSerializerSettings);
Assert.NotNull(deserializedJson);
Assert.That(deserializedJson.Source.Source, Is.EqualTo(ChatBoostSourceType.Giveaway));
Assert.That(deserializedJson.Source, Is.TypeOf<ChatBoostSourceGiveaway>());
}
}

0 comments on commit f5c4173

Please sign in to comment.