Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support many more deserialization types #35

Merged
merged 2 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Fauna.Test/Serialization/Serializer.Classes.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ private class Person
public int Age { get; set; } = 61;
}

[FaunaObject]
private class ClassForDocument
{
[Field("user_field")] public string? UserField { get; set; }
}

[FaunaObject]
private class PersonWithAttributes
{
Expand All @@ -21,6 +27,7 @@ private class PersonWithAttributes
public string? Ignored { get; set; }
}


[FaunaObject]
private class ClassWithInvalidPropertyTypeHint
{
Expand Down
146 changes: 144 additions & 2 deletions Fauna.Test/Serialization/Serializer.Deserialize.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Fauna.Serialization;
using Fauna.Serialization.Attributes;
using Fauna.Types;
using NUnit.Framework;

Expand All @@ -8,7 +7,6 @@ namespace Fauna.Test.Serialization;
[TestFixture]
public partial class SerializerTests
{

[Test]
public void DeserializeValues()
{
Expand All @@ -32,6 +30,96 @@ public void DeserializeValues()
}
}

[Test]
public void DeserializeStringGeneric()
{
var result = Serializer.Deserialize<string>("\"hello\"");
Assert.AreEqual("hello", result);
}

[Test]
public void DeserializeNullableGeneric()
{
var result = Serializer.Deserialize<string?>("null");
Assert.IsNull(result);
}

[Test]
public void DeserializeIntGeneric()
{
var result = Serializer.Deserialize<int>(@"{""@int"":""42""}");
Assert.AreEqual(42, result);
}


[Test]
public void DeserializeDateGeneric()
{
var result = Serializer.Deserialize<DateTime>(@"{""@date"": ""2023-12-03""}");
Assert.AreEqual(new DateTime(2023, 12, 3), result);
}

[Test]
public void DeserializeTimeGeneric()
{
var result = Serializer.Deserialize<DateTime>(@"{""@time"": ""2023-12-03T05:52:10.000001-09:00""}");
var expected = new DateTime(2023, 12, 3, 14, 52, 10, 0, DateTimeKind.Utc).AddTicks(10).ToLocalTime();
Assert.AreEqual(expected, result);
}

[Test]
public void DeserializeDocument()
{
const string given = @"
{
""@doc"":{
""id"":""123"",
""coll"":{""@mod"":""MyColl""},
""ts"":{""@time"":""2023-12-15T01:01:01.0010010Z""},
""user_field"":""user_value""
}
}";

var actual = Serializer.Deserialize<Document>(given);
Assert.AreEqual("123", actual.Id);
Assert.AreEqual(new Module("MyColl"), actual.Collection);
Assert.AreEqual(DateTime.Parse("2023-12-15T01:01:01.0010010Z"), actual.Ts);
Assert.AreEqual("user_value", actual["user_field"]);
}

[Test]
public void DeserializeDocumentToClass()
{
const string given = @"
{
""@doc"":{
""id"":""123"",
""coll"":{""@mod"":""MyColl""},
""ts"":{""@time"":""2023-12-15T01:01:01.0010010Z""},
""user_field"":""user_value""
}
}";

var actual = Serializer.Deserialize<ClassForDocument>(given);
Assert.AreEqual("user_value", actual.UserField);
}

[Test]
public void DeserializeRef()
{
const string given = @"
{
""@ref"":{
""id"":""123"",
""coll"":{""@mod"":""MyColl""}
}
}";

var actual = Serializer.Deserialize<Ref>(given);
Assert.AreEqual("123", actual.Id);
Assert.AreEqual(new Module("MyColl"), actual.Collection);
}

[Test]
public void DeserializeObject()
{
Expand Down Expand Up @@ -105,6 +193,23 @@ public void DeserializeEscapedObject()
Assert.AreEqual(expected, result);
}

[Test]
public void DeserializeIntoGenericDictionary()
{
const string given = @"{
""k1"": { ""@int"": ""1"" },
""k2"": { ""@int"": ""2"" },
""k3"": { ""@int"": ""3"" }
}";
var expected = new Dictionary<string, int>()
{
{"k1", 1},
{"k2", 2},
{"k3", 3}
};
var actual = Serializer.Deserialize<Dictionary<string, int>>(given);
Assert.AreEqual(expected, actual);
}

[Test]
public void DeserializeIntoPoco()
Expand Down Expand Up @@ -142,4 +247,41 @@ public void DeserializeIntoPocoWithAttributes()
Assert.AreEqual(612, p.Age);
Assert.IsNull(p.Ignored);
}

[Test]
public void DeserializeIntoList()
{
const string given = @"[""item1"",""item2""]";
var expected = new List<object> { "item1", "item2" };
var p = Serializer.Deserialize(given);
Assert.AreEqual(expected, p);
}

[Test]
public void DeserializeIntoGenericListWithPrimitive()
{
const string given = @"[""item1"",""item2""]";
var expected = new List<string> { "item1", "item2" };
var p = Serializer.Deserialize<List<string>>(given);
Assert.AreEqual(expected, p);
}

[Test]
public void DeserializeIntoGenericListWithPocoWithAttributes()
{
const string given = @"[
{""first_name"":""Cleve"",""last_name"":""Stuart"",""age"":{""@int"":""100""}},
{""first_name"":""Darren"",""last_name"":""Cunningham"",""age"":{""@int"":""101""}}
]";
var peeps = Serializer.Deserialize<List<PersonWithAttributes>>(given);
var cleve = peeps[0];
var darren = peeps[1];
Assert.AreEqual("Cleve", cleve.FirstName);
Assert.AreEqual("Stuart", cleve.LastName);
Assert.AreEqual(100, cleve.Age);

Assert.AreEqual("Darren", darren.FirstName);
Assert.AreEqual("Cunningham", darren.LastName);
Assert.AreEqual(101, darren.Age);
}
}
1 change: 0 additions & 1 deletion Fauna.Test/Serialization/Serializer.Serialize.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Text.RegularExpressions;
using Fauna.Serialization;
using Fauna.Serialization.Attributes;
using Fauna.Types;
using NUnit.Framework;

Expand Down
4 changes: 2 additions & 2 deletions Fauna.Test/Serialization/Utf8FaunaReader.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,14 +312,14 @@ public void ReadSet()
var reader = new Utf8FaunaReader(s);
var expectedTokens = new List<Tuple<TokenType, object?>>
{
new(TokenType.StartSet, null),
new(TokenType.StartPage, null),
new(TokenType.FieldName, "data"),
new(TokenType.StartArray, null),
new(TokenType.Int, 99),
new(TokenType.EndArray, null),
new(TokenType.FieldName, "after"),
new(TokenType.String, "afterme"),
new(TokenType.EndSet, null)
new(TokenType.EndPage, null)
};

AssertReader(reader, expectedTokens);
Expand Down
1 change: 1 addition & 0 deletions Fauna.Test/Serialization/Utf8FaunaWriter.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
[TestFixture]
public class Utf8FaunaWriterTests
{
private Utf8FaunaWriter _writer;

Check warning on line 11 in Fauna.Test/Serialization/Utf8FaunaWriter.Tests.cs

View workflow job for this annotation

GitHub Actions / dotnet-test (6.0.x)

Non-nullable field '_writer' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 11 in Fauna.Test/Serialization/Utf8FaunaWriter.Tests.cs

View workflow job for this annotation

GitHub Actions / dotnet-test (6.0.x)

Non-nullable field '_writer' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 11 in Fauna.Test/Serialization/Utf8FaunaWriter.Tests.cs

View workflow job for this annotation

GitHub Actions / dotnet-test (8.0.x)

Non-nullable field '_writer' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 11 in Fauna.Test/Serialization/Utf8FaunaWriter.Tests.cs

View workflow job for this annotation

GitHub Actions / dotnet-test (8.0.x)

Non-nullable field '_writer' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
private MemoryStream _stream;

Check warning on line 12 in Fauna.Test/Serialization/Utf8FaunaWriter.Tests.cs

View workflow job for this annotation

GitHub Actions / dotnet-test (6.0.x)

Non-nullable field '_stream' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 12 in Fauna.Test/Serialization/Utf8FaunaWriter.Tests.cs

View workflow job for this annotation

GitHub Actions / dotnet-test (8.0.x)

Non-nullable field '_stream' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

[SetUp]
public void Init()
Expand All @@ -22,6 +22,7 @@
public void Cleanup()
{
_writer.Dispose();
_stream.Dispose();
}

private void AssertWriter(string expected)
Expand Down
Loading
Loading