-
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.
Added tests for the new converters etc
- Loading branch information
Showing
5 changed files
with
876 additions
and
19 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
24 changes: 24 additions & 0 deletions
24
src/PinguApps.Appwrite.Shared/Converters/DocumentGenericConverterFactory.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,24 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using PinguApps.Appwrite.Shared.Responses; | ||
|
||
namespace PinguApps.Appwrite.Shared.Converters; | ||
public class DocumentGenericConverterFactory : JsonConverterFactory | ||
{ | ||
public override bool CanConvert(Type typeToConvert) | ||
{ | ||
// Ensure the type is a generic type of Document<> | ||
return typeToConvert.IsGenericType && typeToConvert.GetGenericTypeDefinition() == typeof(Document<>); | ||
} | ||
|
||
public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
// Extract the TData type from Doocument<TData> | ||
Type dataType = typeToConvert.GetGenericArguments()[0]; | ||
|
||
// Create a specific generic converter for Doocument<TData> | ||
var converterType = typeof(DocumentGenericConverter<>).MakeGenericType(dataType); | ||
return (JsonConverter?)Activator.CreateInstance(converterType); | ||
} | ||
} |
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
99 changes: 99 additions & 0 deletions
99
tests/PinguApps.Appwrite.Shared.Tests/Converters/DocumentGenericConverterFactoryTests.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,99 @@ | ||
using System.Text.Json; | ||
using PinguApps.Appwrite.Shared.Converters; | ||
using PinguApps.Appwrite.Shared.Responses; | ||
|
||
namespace PinguApps.Appwrite.Shared.Tests.Converters; | ||
public class DocumentGenericConverterFactoryTests | ||
{ | ||
private readonly DocumentGenericConverterFactory _factory; | ||
|
||
public DocumentGenericConverterFactoryTests() | ||
{ | ||
_factory = new DocumentGenericConverterFactory(); | ||
} | ||
|
||
public class TestData | ||
{ | ||
public string? Field1 { get; set; } | ||
} | ||
|
||
[Fact] | ||
public void CanConvert_DocumentGenericType_ReturnsTrue() | ||
{ | ||
// Arrange | ||
var typeToConvert = typeof(Document<TestData>); | ||
|
||
// Act | ||
var result = _factory.CanConvert(typeToConvert); | ||
|
||
// Assert | ||
Assert.True(result); | ||
} | ||
|
||
[Fact] | ||
public void CanConvert_NonGenericType_ReturnsFalse() | ||
{ | ||
// Arrange | ||
var typeToConvert = typeof(TestData); | ||
|
||
// Act | ||
var result = _factory.CanConvert(typeToConvert); | ||
|
||
// Assert | ||
Assert.False(result); | ||
} | ||
|
||
[Fact] | ||
public void CanConvert_DifferentGenericType_ReturnsFalse() | ||
{ | ||
// Arrange | ||
var typeToConvert = typeof(List<TestData>); | ||
|
||
// Act | ||
var result = _factory.CanConvert(typeToConvert); | ||
|
||
// Assert | ||
Assert.False(result); | ||
} | ||
|
||
[Fact] | ||
public void CreateConverter_DocumentGenericType_ReturnsConverter() | ||
{ | ||
// Arrange | ||
var typeToConvert = typeof(Document<TestData>); | ||
var options = new JsonSerializerOptions(); | ||
|
||
// Act | ||
var converter = _factory.CreateConverter(typeToConvert, options); | ||
|
||
// Assert | ||
Assert.NotNull(converter); | ||
Assert.IsType<DocumentGenericConverter<TestData>>(converter); | ||
} | ||
|
||
[Fact] | ||
public void CreateConverter_NonGenericType_ThrowsException() | ||
{ | ||
// Arrange | ||
var typeToConvert = typeof(TestData); | ||
var options = new JsonSerializerOptions(); | ||
|
||
// Act & Assert | ||
Assert.Throws<IndexOutOfRangeException>(() => _factory.CreateConverter(typeToConvert, options)); | ||
} | ||
|
||
[Fact] | ||
public void CreateConverter_DifferentGenericType_ReturnsConverterWithFirstGenericArgument() | ||
{ | ||
// Arrange | ||
var typeToConvert = typeof(List<TestData>); | ||
var options = new JsonSerializerOptions(); | ||
|
||
// Act | ||
var converter = _factory.CreateConverter(typeToConvert, options); | ||
|
||
// Assert | ||
Assert.NotNull(converter); | ||
Assert.IsType<DocumentGenericConverter<TestData>>(converter); | ||
} | ||
} |
Oops, something went wrong.