-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
93 additions
and
9 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
93 changes: 93 additions & 0 deletions
93
tests/PinguApps.Appwrite.Shared.Tests/Converters/NullableDateTimeConverterTests.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,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); | ||
} | ||
} |