tests: add converter tests
This commit is contained in:
@@ -1,29 +0,0 @@
|
|||||||
namespace StevesBot.Worker.Tests.Integration;
|
|
||||||
|
|
||||||
public class DiscordEventConverterTests
|
|
||||||
{
|
|
||||||
private readonly JsonSerializerOptions _jsonSerializerOptions = new()
|
|
||||||
{
|
|
||||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
||||||
Converters =
|
|
||||||
{
|
|
||||||
new DiscordEventConverter()
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Deserialize_WhenCalledWithUnknownOpCode_ItShouldReturnADiscordEvent()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private string Serialize(object obj)
|
|
||||||
{
|
|
||||||
return JsonSerializer.Serialize(obj, _jsonSerializerOptions);
|
|
||||||
}
|
|
||||||
|
|
||||||
private T? Deserialize<T>(string json)
|
|
||||||
{
|
|
||||||
return JsonSerializer.Deserialize<T>(json, _jsonSerializerOptions);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
namespace StevesBot.Worker.Tests.Unit;
|
||||||
|
|
||||||
|
public class DiscordEventConverterTests
|
||||||
|
{
|
||||||
|
private readonly JsonSerializerOptions _options = new()
|
||||||
|
{
|
||||||
|
Converters =
|
||||||
|
{
|
||||||
|
new DiscordEventConverter()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
private readonly Type _discordEventType = typeof(DiscordEvent);
|
||||||
|
private readonly DiscordEventConverter _converter = new();
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Read_WhenCalledWithUnknownOpCode_ItShouldReturnDiscordEvent()
|
||||||
|
{
|
||||||
|
var data = new
|
||||||
|
{
|
||||||
|
op = 99,
|
||||||
|
s = null as int?,
|
||||||
|
t = null as string,
|
||||||
|
d = null as object
|
||||||
|
};
|
||||||
|
|
||||||
|
var result = Read(data);
|
||||||
|
|
||||||
|
result.Should().BeOfType<DiscordEvent>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Read_WhenCalledWithDispatchOpCodeAndNoType_ItShouldReturnDispatchEvent()
|
||||||
|
{
|
||||||
|
var data = new
|
||||||
|
{
|
||||||
|
op = DiscordOpCodes.Dispatch,
|
||||||
|
s = null as int?,
|
||||||
|
t = null as string,
|
||||||
|
d = null as object
|
||||||
|
};
|
||||||
|
|
||||||
|
var result = Read(data);
|
||||||
|
|
||||||
|
result.Should().BeOfType<DiscordEvent>();
|
||||||
|
}
|
||||||
|
|
||||||
|
private DiscordEvent? Read(object data)
|
||||||
|
{
|
||||||
|
var json = JsonSerializer.Serialize(data);
|
||||||
|
var utf8Json = Encoding.UTF8.GetBytes(json);
|
||||||
|
var reader = new Utf8JsonReader(utf8Json);
|
||||||
|
return _converter.Read(ref reader, _discordEventType, _options);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,12 @@ namespace StevesBot.Worker.Discord.Events;
|
|||||||
|
|
||||||
internal sealed class DiscordEventConverter : JsonConverter<DiscordEvent>
|
internal sealed class DiscordEventConverter : JsonConverter<DiscordEvent>
|
||||||
{
|
{
|
||||||
|
private readonly JsonSerializerOptions _jsonSerializerOptions = new()
|
||||||
|
{
|
||||||
|
ReferenceHandler = ReferenceHandler.IgnoreCycles,
|
||||||
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||||
|
};
|
||||||
|
|
||||||
private const string OpPropertyName = "op";
|
private const string OpPropertyName = "op";
|
||||||
|
|
||||||
public override DiscordEvent? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
public override DiscordEvent? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||||
@@ -15,7 +21,7 @@ internal sealed class DiscordEventConverter : JsonConverter<DiscordEvent>
|
|||||||
DiscordOpCodes.Dispatch => DeserializeDispatchEvent(root, options),
|
DiscordOpCodes.Dispatch => DeserializeDispatchEvent(root, options),
|
||||||
DiscordOpCodes.Hello => JsonSerializer.Deserialize<HelloDiscordEvent>(root.GetRawText(), options),
|
DiscordOpCodes.Hello => JsonSerializer.Deserialize<HelloDiscordEvent>(root.GetRawText(), options),
|
||||||
DiscordOpCodes.HeartbeatAck => JsonSerializer.Deserialize<HeartbeatAckDiscordEvent>(root.GetRawText(), options),
|
DiscordOpCodes.HeartbeatAck => JsonSerializer.Deserialize<HeartbeatAckDiscordEvent>(root.GetRawText(), options),
|
||||||
_ => JsonSerializer.Deserialize<DiscordEvent>(root.GetRawText(), options)
|
_ => JsonSerializer.Deserialize<DiscordEvent>(root.GetRawText(), _jsonSerializerOptions),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,14 +30,14 @@ internal sealed class DiscordEventConverter : JsonConverter<DiscordEvent>
|
|||||||
JsonSerializer.Serialize(writer, value, value.GetType(), options);
|
JsonSerializer.Serialize(writer, value, value.GetType(), options);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static DiscordEvent? DeserializeDispatchEvent(JsonElement root, JsonSerializerOptions options)
|
private DiscordEvent? DeserializeDispatchEvent(JsonElement root, JsonSerializerOptions options)
|
||||||
{
|
{
|
||||||
var type = root.GetProperty("t").GetString();
|
var type = root.GetProperty("t").GetString();
|
||||||
|
|
||||||
return type switch
|
return type switch
|
||||||
{
|
{
|
||||||
DiscordEventTypes.Ready => JsonSerializer.Deserialize<ReadyDiscordEvent>(root.GetRawText(), options),
|
DiscordEventTypes.Ready => JsonSerializer.Deserialize<ReadyDiscordEvent>(root.GetRawText(), options),
|
||||||
_ => JsonSerializer.Deserialize<DiscordEvent>(root.GetRawText(), options),
|
_ => JsonSerializer.Deserialize<DiscordEvent>(root.GetRawText(), _jsonSerializerOptions),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user