Skip to content

Commit

Permalink
Serialize dicts, lists, and add more testing
Browse files Browse the repository at this point in the history
  • Loading branch information
pnwpedro committed Dec 13, 2023
1 parent 3f7562d commit d486e5b
Show file tree
Hide file tree
Showing 5 changed files with 393 additions and 95 deletions.
138 changes: 138 additions & 0 deletions Fauna.Test/Serialization/Serializer.Classes.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
using Fauna.Serialization;
using Fauna.Serialization.Attributes;

namespace Fauna.Test.Serialization;

public partial class SerializerTests
{
private class Person
{
public string? FirstName { get; set; } = "Baz";
public string? LastName { get; set; } = "Luhrmann";
public int Age { get; set; } = 61;
}

[FaunaObject]
private class PersonWithAttributes
{
[Field("first_name")] public string? FirstName { get; set; } = "Baz";
[Field("last_name")] public string? LastName { get; set; } = "Luhrmann";
[Field("age", FaunaType.Long)] public int Age { get; set; } = 61;
public string? Ignored { get; set; }
}

private class ThingWithStringOverride
{
private const string Name = "TheThing";

public override string ToString()
{
return Name;
}
}

[FaunaObject]
private class PersonWithTypeOverrides
{
// Long Conversions
[Field("short_to_long", FaunaType.Long)] public short? ShortToLong { get; set; } = 1;
[Field("int_to_long", FaunaType.Long)] public int? IntToLong { get; set; } = 2;
[Field("long_to_long", FaunaType.Long)] public long? LongToLong { get; set; } = 3L;

// Int Conversions
[Field("short_to_int", FaunaType.Int)] public short? ShortToInt { get; set; } = 4;
[Field("int_to_int", FaunaType.Int)] public int? IntToInt { get; set; } = 5;

// Double Conversions
[Field("short_to_double", FaunaType.Double)] public short? ShortToDouble { get; set; } = 6;
[Field("int_to_double", FaunaType.Double)] public int? IntToDouble { get; set; } = 7;
[Field("long_to_double", FaunaType.Double)] public long? LongToDouble { get; set; } = 8L;
[Field("decimal_to_double", FaunaType.Double)] public decimal? DecimalToDouble { get; set; } = 9.2M;
[Field("double_to_double", FaunaType.Double)] public double? DoubleToDouble { get; set; } = 10.1d;

// Bool conversions
[Field("true_to_true", FaunaType.Boolean)] public bool? TrueToTrue { get; set; } = true;
[Field("false_to_false", FaunaType.Boolean)] public bool? FalseToFalse { get; set; } = false;

// String conversions
[Field("class_to_string", FaunaType.String)]
public ThingWithStringOverride? ThingToString { get; set; } = new();
[Field("string_to_string", FaunaType.String)] public string? StringToString { get; set; } = "aString";

// Date conversions
[Field("datetime_to_date", FaunaType.Date)]
public DateTime? DateTimeToDate { get; set; } = new DateTime(2023, 12, 13, 12, 12, 12, 1, 1, DateTimeKind.Utc);
[Field("dateonly_to_date", FaunaType.Date)]
public DateOnly? DateOnlyToDate { get; set; } = new DateOnly(2023, 12, 13);
[Field("datetimeoffset_to_date", FaunaType.Date)]
public DateTimeOffset? DateTimeOffsetToDate { get; set; } = new DateTimeOffset(new DateTime(2023, 12, 13, 12, 12, 12, 1, 1, DateTimeKind.Utc));

// Time conversions
[Field("datetime_to_time", FaunaType.Time)]
public DateTime? DateTimeToTime { get; set; } = new DateTime(2023, 12, 13, 12, 12, 12, 1, 1, DateTimeKind.Utc);
[Field("datetimeoffset_to_time", FaunaType.Time)]
public DateTimeOffset? DateTimeOffsetToTime { get; set; } = new DateTimeOffset(new DateTime(2023, 12, 13, 12, 12, 12, 1, 1, DateTimeKind.Utc));
}


[FaunaObject]
private class PersonWithIntConflict
{
[Field("@int")] public string? Field { get; set; } = "not";
}

[FaunaObject]
private class PersonWithLongConflict
{
[Field("@long")] public string? Field { get; set; } = "not";
}

[FaunaObject]
private class PersonWithDoubleConflict
{
[Field("@double")] public string? Field { get; set; } = "not";
}

[FaunaObject]
private class PersonWithModConflict
{
[Field("@mod")] public string? Field { get; set; } = "not";
}

[FaunaObject]
private class PersonWithRefConflict
{
[Field("@ref")] public string? Field { get; set; } = "not";
}

[FaunaObject]
private class PersonWithDocConflict
{
[Field("@doc")] public string? Field { get; set; } = "not";
}

[FaunaObject]
private class PersonWithObjectConflict
{
[Field("@object")] public string? Field { get; set; } = "not";
}

[FaunaObject]
private class PersonWithSetConflict
{
[Field("@set")] public string? Field { get; set; } = "not";
}

[FaunaObject]
private class PersonWithTimeConflict
{
[Field("@time")] public string? Field { get; set; } = "not";
}

[FaunaObject]
private class PersonWithDateConflict
{
[Field("@date")] public string? Field { get; set; } = "not";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,9 @@
namespace Fauna.Test.Serialization;

[TestFixture]
public class SerializerTests
public partial class SerializerTests
{

private class TestPerson
{
public string? FirstName { get; set; }
public string? LastName { get; set; }
public int Age { get; set; }
}

[FaunaObject]
private class TestPersonWithAttributes
{
[Field("first_name")]
public string? FirstName { get; set; }
[Field("last_name")]
public string? LastName { get; set; }
[Field("age")]
public int Age { get; set; }
public string? Ignored { get; set; }
}


[Test]
public void DeserializeValues()
{
Expand Down Expand Up @@ -132,34 +112,34 @@ public void DeserializeIntoPoco()

const string given = """
{
"FirstName": "Baz",
"LastName": "Luhrmann",
"Age": { "@int": "61" }
"FirstName": "Baz2",
"LastName": "Luhrmann2",
"Age": { "@int": "612" }
}
""";

var p = Serializer.Deserialize<TestPerson>(given);
Assert.AreEqual("Baz", p.FirstName);
Assert.AreEqual("Luhrmann", p.LastName);
Assert.AreEqual(61, p.Age);
var p = Serializer.Deserialize<Person>(given);
Assert.AreEqual("Baz2", p.FirstName);

Check warning on line 122 in Fauna.Test/Serialization/Serializer.Deserialize.Tests.cs

View workflow job for this annotation

GitHub Actions / dotnet-test (8.0.x)

Dereference of a possibly null reference.
Assert.AreEqual("Luhrmann2", p.LastName);
Assert.AreEqual(612, p.Age);
}

[Test]
public void DeserializeIntoPocoWithAttributes()
{
const string given = """
{
"first_name": "Baz",
"last_name": "Luhrmann",
"age": { "@int": "61" },
"first_name": "Baz2",
"last_name": "Luhrmann2",
"age": { "@int": "612" },
"Ignored": "should be null"
}
""";

var p = Serializer.Deserialize<TestPersonWithAttributes>(given);
Assert.AreEqual("Baz", p.FirstName);
Assert.AreEqual("Luhrmann", p.LastName);
Assert.AreEqual(61, p.Age);
var p = Serializer.Deserialize<PersonWithAttributes>(given);
Assert.AreEqual("Baz2", p.FirstName);

Check warning on line 140 in Fauna.Test/Serialization/Serializer.Deserialize.Tests.cs

View workflow job for this annotation

GitHub Actions / dotnet-test (8.0.x)

Dereference of a possibly null reference.
Assert.AreEqual("Luhrmann2", p.LastName);
Assert.AreEqual(612, p.Age);
Assert.IsNull(p.Ignored);
}
}
Loading

0 comments on commit d486e5b

Please sign in to comment.