-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/v7.0.0' into v7.0.0
- Loading branch information
Showing
4 changed files
with
100 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
67 changes: 67 additions & 0 deletions
67
src/UnitTests/JsonConverters/ChatBoostSourceConverterTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>()); | ||
} | ||
} |