Skip to content

Commit

Permalink
Shared now has 100% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
pingu2k4 committed Jul 15, 2024
1 parent f6bed64 commit b0e709c
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ internal class NullableDateTimeConverter : JsonConverter<DateTime?>
throw new JsonException($"Unable to parse '{stringValue}' to DateTime.");
}

if (reader.TokenType == JsonTokenType.Null)
{
return null;
}

throw new JsonException("Unexpected token type.");
}

Expand All @@ -37,9 +32,5 @@ public override void Write(Utf8JsonWriter writer, DateTime? value, JsonSerialize
{
writer.WriteStringValue(value.Value.ToString("o"));
}
else
{
writer.WriteNullValue();
}
}
}
Original file line number Diff line number Diff line change
@@ -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<DateTime?>(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<DateTime?>(json, _options);
Assert.Null(result);
}

[Fact]
public void Read_NullToken_ReturnsNull()
{
var json = "null";
var result = JsonSerializer.Deserialize<DateTime?>(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<NullableDateTimeObject>(json, _options);
Assert.NotNull(result);
Assert.Null(result.X);
}

[Fact]
public void Read_InvalidDateString_ThrowsJsonException()
{
var json = "\"invalid-date\"";
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<DateTime?>(json, _options));
}

[Fact]
public void Read_UnexpectedTokenType_ThrowsJsonException()
{
var json = "123";
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<DateTime?>(json, _options));
}

[Fact]
public void Write_NonNullDateTime_WritesExpectedString()
{
var dateTime = new DateTime(2023, 1, 1);
var json = JsonSerializer.Serialize<DateTime?>(dateTime, _options);
Assert.Equal("\"2023-01-01T00:00:00.0000000\"", json);
}

[Fact]
public void Write_NullDateTime_WritesNullValue()
{
var json = JsonSerializer.Serialize<DateTime?>(null, _options);
Assert.Equal("null", json);
}

[Fact]
public void Write_NullDateTimeInObject_WritesNullValue()
{
var json = JsonSerializer.Serialize(new NullableDateTimeObject(), _options);
Assert.Equal("{\"x\":null}", json);
}
}

0 comments on commit b0e709c

Please sign in to comment.