From b0e709c35604a1be859324d9279baeaa8e267d4b Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Mon, 15 Jul 2024 05:11:00 +0100 Subject: [PATCH] Shared now has 100% coverage --- .../Converters/NullableDateTimeConverter.cs | 9 -- .../NullableDateTimeConverterTests.cs | 93 +++++++++++++++++++ 2 files changed, 93 insertions(+), 9 deletions(-) create mode 100644 tests/PinguApps.Appwrite.Shared.Tests/Converters/NullableDateTimeConverterTests.cs diff --git a/src/PinguApps.Appwrite.Shared/Converters/NullableDateTimeConverter.cs b/src/PinguApps.Appwrite.Shared/Converters/NullableDateTimeConverter.cs index eddda37f..2719aeca 100644 --- a/src/PinguApps.Appwrite.Shared/Converters/NullableDateTimeConverter.cs +++ b/src/PinguApps.Appwrite.Shared/Converters/NullableDateTimeConverter.cs @@ -23,11 +23,6 @@ internal class NullableDateTimeConverter : JsonConverter throw new JsonException($"Unable to parse '{stringValue}' to DateTime."); } - if (reader.TokenType == JsonTokenType.Null) - { - return null; - } - throw new JsonException("Unexpected token type."); } @@ -37,9 +32,5 @@ public override void Write(Utf8JsonWriter writer, DateTime? value, JsonSerialize { writer.WriteStringValue(value.Value.ToString("o")); } - else - { - writer.WriteNullValue(); - } } } diff --git a/tests/PinguApps.Appwrite.Shared.Tests/Converters/NullableDateTimeConverterTests.cs b/tests/PinguApps.Appwrite.Shared.Tests/Converters/NullableDateTimeConverterTests.cs new file mode 100644 index 00000000..2c0fb17a --- /dev/null +++ b/tests/PinguApps.Appwrite.Shared.Tests/Converters/NullableDateTimeConverterTests.cs @@ -0,0 +1,93 @@ +using System.Text.Json; +using System.Text.Json.Serialization; +using PinguApps.Appwrite.Shared.Converters; + +namespace PinguApps.Appwrite.Shared.Tests.Converters; + +public class NullableDateTimeConverterTests +{ + private readonly JsonSerializerOptions _options; + + public NullableDateTimeConverterTests() + { + _options = new JsonSerializerOptions(); + _options.Converters.Add(new NullableDateTimeConverter()); + } + + [Fact] + public void Read_ValidDateString_ReturnsDateTime() + { + var json = "\"2023-01-01T00:00:00\""; + var result = JsonSerializer.Deserialize(json, _options); + Assert.NotNull(result); + Assert.Equal(new DateTime(2023, 1, 1), result.Value); + } + + [Fact] + public void Read_EmptyString_ReturnsNull() + { + var json = "\"\""; + var result = JsonSerializer.Deserialize(json, _options); + Assert.Null(result); + } + + [Fact] + public void Read_NullToken_ReturnsNull() + { + var json = "null"; + var result = JsonSerializer.Deserialize(json, _options); + Assert.Null(result); + } + + public class NullableDateTimeObject + { + [JsonPropertyName("x")] + [JsonConverter(typeof(NullableDateTimeConverter))] + public DateTime? X { get; set; } + } + + [Fact] + public void Read_NullTokenInObject_ReturnsNull() + { + var json = "{\"x\": null}"; + var result = JsonSerializer.Deserialize(json, _options); + Assert.NotNull(result); + Assert.Null(result.X); + } + + [Fact] + public void Read_InvalidDateString_ThrowsJsonException() + { + var json = "\"invalid-date\""; + Assert.Throws(() => JsonSerializer.Deserialize(json, _options)); + } + + [Fact] + public void Read_UnexpectedTokenType_ThrowsJsonException() + { + var json = "123"; + Assert.Throws(() => JsonSerializer.Deserialize(json, _options)); + } + + [Fact] + public void Write_NonNullDateTime_WritesExpectedString() + { + var dateTime = new DateTime(2023, 1, 1); + var json = JsonSerializer.Serialize(dateTime, _options); + Assert.Equal("\"2023-01-01T00:00:00.0000000\"", json); + } + + [Fact] + public void Write_NullDateTime_WritesNullValue() + { + var json = JsonSerializer.Serialize(null, _options); + Assert.Equal("null", json); + } + + [Fact] + public void Write_NullDateTimeInObject_WritesNullValue() + { + var json = JsonSerializer.Serialize(new NullableDateTimeObject(), _options); + Assert.Equal("{\"x\":null}", json); + } +}