Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maxchistt committed Jul 15, 2023
1 parent 0cab479 commit 687abc6
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ public void JsonSettingsTest1()
{
Settings.JsonSettings.StrictTypeSerialization = false;
Enumerable = new JsonEnumerable<string>();
Enumerable.AddRange(TestValues.Strings);
Assert.That(Enumerable.JsonString == JsonConvert.SerializeObject(TestValues.Strings));
Enumerable.AddRange(TestValues.StringsList);
Assert.That(Enumerable.JsonString == JsonConvert.SerializeObject(TestValues.StringsList));

Settings.JsonSettings.StrictTypeSerialization = true;
Enumerable = new JsonEnumerable<string>();
Enumerable.AddRange(TestValues.Strings);
Assert.That(Enumerable.JsonString != JsonConvert.SerializeObject(TestValues.Strings));
Enumerable.AddRange(TestValues.StringsList);
Assert.That(Enumerable.JsonString != JsonConvert.SerializeObject(TestValues.StringsList));

Settings.JsonSettings.StrictTypeSerialization = true;
Settings.JsonSettings.AllowChangeStrictParamAfterItUsed = false;
Expand Down
35 changes: 35 additions & 0 deletions JsonProperty.EFCore.Tests/GeneralTests/TypeConversionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,40 @@ public void SubitemComplexTypeConversionTestV3()
}
});
}

[Test, Order(4)]
public void SubitemComplexTypeConversionTestV4()
{
Console.WriteLine(nameof(SubitemComplexTypeConversionTestV4));
Assert.DoesNotThrow(() =>
{
var prod_listLast = list.Deserialize().Last() as Product;
object Test1Res_firstParamValue = prod_listLast.Parameters.Deserialize().First().Value;

if (Test1Res_firstParamValue.GetType() == typeof(object))
Assert.Fail($"{nameof(Test1Res_firstParamValue)} is object");

if (Test1Res_firstParamValue is JsonElement jsonElement)
{
Assert.That(jsonElement.ValueKind, Is.EqualTo(JsonValueKind.Number), "JsonElement is number fail");
}
Assert.That(Test1Res_firstParamValue, Is.Not.TypeOf<string>(), "Is.Not.TypeOf<string>() fail");
Assert.That(Test1Res_firstParamValue.GetType(), Is.AnyOf(typeof(float), typeof(double), typeof(decimal)), "type is any of numeric fail");
Assert.That(Test1Res_firstParamValue, Is.Not.NaN, "Is.Not.NaN fail");
Assert.IsTrue(decimal.TryParse(Test1Res_firstParamValue?.ToString(), out decimal _), "not parsable to decimal");

if (TestComplexTypeConversionStrictly)
{
Assert.IsAssignableFrom<decimal>(Test1Res_firstParamValue, "IsAssignableFrom<decimal> failed");
Assert.That(Test1Res_firstParamValue, Is.AssignableTo<decimal>(), "Is.AssignableTo<decimal>() failed");

Assert.That(Test1Res_firstParamValue, Is.InstanceOf(typeof(decimal)), "lastParamValue is decimal test failed");
}
else
{
Console.WriteLine($"!!! {nameof(SubitemComplexTypeConversionTestV4)} test is disabled by {nameof(TestComplexTypeConversionStrictly)} = {TestComplexTypeConversionStrictly} const");
}
});
}
}
}
10 changes: 9 additions & 1 deletion JsonProperty.EFCore.Tests/Shared/TestValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ namespace JsonProperty.EFCore.Tests.Shared
{
internal static class TestValues
{
public static IImmutableList<string> Strings { get; } = ImmutableArray.Create("Item1", "Item2", "Item3", "Item4");
public static IImmutableList<string> StringsList { get; } =
ImmutableArray.Create(new string[] {
"Item1", "Item2", "Item3", "Item4"
});

public static IImmutableDictionary<string, string> StringsDictionary { get; } =
ImmutableDictionary.ToImmutableDictionary(new Dictionary<string, string> {
{ "Item1", "Item1" }, { "Item2", "Item2" }, { "Item3", "Item3" }, { "Item4", "Item4" }
});
}
}
41 changes: 41 additions & 0 deletions JsonProperty.EFCore.Tests/TestsByType/DictionaryTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using JsonProperty.EFCore.Tests.Shared;

namespace JsonProperty.EFCore.Tests.TestsByType
{
internal class DictionaryTest
{
public JsonDictionary<string, string> Dictionary { get; set; }

[SetUp]
public void Setup()
{
Dictionary = new();
}

[Test]
public void TestDictionary()
{
Console.WriteLine(nameof(TestDictionary));
Assert.DoesNotThrow(() =>
{
Dictionary.Add(TestValues.StringsDictionary.ElementAt(0));
Dictionary.Edit(en => en.Append(TestValues.StringsDictionary.ElementAt(1)));
Dictionary.AddRange(new Dictionary<string, string>(new[]{
TestValues.StringsDictionary.ElementAt(2),TestValues.StringsDictionary.ElementAt(3)
}));

for (int i = 0; i < TestValues.StringsList.Count; i++)
{
Assert.IsTrue(TestValues.StringsDictionary.ElementAt(i).Value == Dictionary.Deserialize().ElementAt(i).Value, "1) TestValues.StringsDictionary.ElementAt(i).Value == Dictionary.Deserialize().ElementAt(i).Value");
}

Dictionary.Serialize(new Dictionary<string, string>(TestValues.StringsDictionary));

for (int i = 0; i < TestValues.StringsList.Count; i++)
{
Assert.IsTrue(TestValues.StringsDictionary.ElementAt(i).Value == Dictionary.VirtualDictionary.ElementAt(i).Value, "2) TestValues.StringsDictionary.ElementAt(i).Value == Dictionary.VirtualDictionary.ElementAt(i).Value");
}
});
}
}
}
17 changes: 8 additions & 9 deletions JsonProperty.EFCore.Tests/TestsByType/EnumerableTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using JsonProperty.EFCore.Tests.Shared;
using System.Text;

namespace JsonProperty.EFCore.Tests.TestsByType
{
Expand All @@ -19,20 +18,20 @@ public void TestEnumerable()
Console.WriteLine(nameof(TestEnumerable));
Assert.DoesNotThrow(() =>
{
Enumerable.Add(TestValues.Strings[0]);
Enumerable.Edit(en => en.Append(TestValues.Strings[1]));
Enumerable.AddRange(new string[] { TestValues.Strings[2], TestValues.Strings[3] });
Enumerable.Add(TestValues.StringsList[0]);
Enumerable.Edit(en => en.Append(TestValues.StringsList[1]));
Enumerable.AddRange(new string[] { TestValues.StringsList[2], TestValues.StringsList[3] });

for (int i = 0; i < TestValues.Strings.Count; i++)
for (int i = 0; i < TestValues.StringsList.Count; i++)
{
Assert.IsTrue(TestValues.Strings[i] == Enumerable.Deserialize().ElementAt(i), "1) Assert.IsTrue(TestValues.Strings[i] == Enumerable.Deserialize().ElementAt(i)");
Assert.IsTrue(TestValues.StringsList[i] == Enumerable.Deserialize().ElementAt(i), "1) Assert.IsTrue(TestValues.Strings[i] == Enumerable.Deserialize().ElementAt(i)");
}

Enumerable.Serialize(TestValues.Strings);
Enumerable.Serialize(TestValues.StringsList);

for (int i = 0; i < TestValues.Strings.Count; i++)
for (int i = 0; i < TestValues.StringsList.Count; i++)
{
Assert.IsTrue(TestValues.Strings[i] == Enumerable.VirtualEnumerable.ElementAt(i), "2) TestValues.Strings[i] == Enumerable.VirtualEnumerable.ElementAt(i)");
Assert.IsTrue(TestValues.StringsList[i] == Enumerable.VirtualEnumerable.ElementAt(i), "2) TestValues.Strings[i] == Enumerable.VirtualEnumerable.ElementAt(i)");
}
});
}
Expand Down
45 changes: 45 additions & 0 deletions JsonProperty.EFCore.Tests/TestsByType/ListTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using JsonProperty.EFCore.Tests.Shared;

namespace JsonProperty.EFCore.Tests.TestsByType
{
internal class ListTest
{
public JsonList<string> List { get; set; }

[SetUp]
public void Setup()
{
List = new();
}

[Test]
public void TestList()
{
Console.WriteLine(nameof(TestList));
Assert.DoesNotThrow(() =>
{
List.Add(TestValues.StringsList[0]);

List.Edit(en =>
{
en.Add(TestValues.StringsList[1]);
return en;
});

List.AddRange(new string[] { TestValues.StringsList[2], TestValues.StringsList[3] });

for (int i = 0; i < TestValues.StringsList.Count; i++)
{
Assert.IsTrue(TestValues.StringsList[i] == List.Deserialize().ElementAt(i), "1) Assert.IsTrue(TestValues.Strings[i] == Enumerable.Deserialize().ElementAt(i)");
}

List.Serialize(TestValues.StringsList.ToList());

for (int i = 0; i < TestValues.StringsList.Count; i++)
{
Assert.IsTrue(TestValues.StringsList[i] == List.VirtualList.ElementAt(i), "2) TestValues.Strings[i] == Enumerable.VirtualEnumerable.ElementAt(i)");
}
});
}
}
}

0 comments on commit 687abc6

Please sign in to comment.