diff --git a/tests/Driver.Tests/Roundtrip/RoundTripTests.cs b/tests/Driver.Tests/Roundtrip/RoundTripTests.cs index f7e4831b..ff88ec83 100644 --- a/tests/Driver.Tests/Roundtrip/RoundTripTests.cs +++ b/tests/Driver.Tests/Roundtrip/RoundTripTests.cs @@ -1,52 +1,93 @@ +using System.Collections; + +using SurrealDB.Json; using SurrealDB.Models.Result; namespace SurrealDB.Driver.Tests.Roundtrip; public class RpcRoundTripTests : RoundTripTests { + public RpcRoundTripTests(ITestOutputHelper logger) : base(logger) { + } } public class RestRoundTripTests : RoundTripTests { + public RestRoundTripTests(ITestOutputHelper logger) : base(logger) { + } } [Collection("SurrealDBRequired")] public abstract class RoundTripTests where T : IDatabase, IDisposable, new() { + protected readonly ITestOutputHelper Logger; + + public RoundTripTests(ITestOutputHelper logger) { + Logger = logger; + } - protected RoundTripObject Expected = new(); - [Fact] - public async Task CreateRoundTripTest() => await DbHandle.WithDatabase( + private static IEnumerable DocumentsToTest { + get { + yield return new RoundTripObject(); + + for (int i = 4; i < 9; i++) { + var arraySize = 2 << i; + var largeDocument = new RoundTripObject { + StringArray = new string[arraySize], + }; + + for (int n = 0; n < largeDocument.StringArray.Length; n++) { + largeDocument.StringArray[n] = TestHelper.RandomString(10); + } + + yield return largeDocument; + } + } + } + + public static List Documents => DocumentsToTest.Select(e => new []{(object)e}).ToList(); + + [Theory] + [MemberData(nameof(Documents))] + public async Task CreateRoundTripTest(RoundTripObject document) => await DbHandle.WithDatabase( async db => { + Logger.WriteLine("Document size in bytes: {0}", JsonSerializer.Serialize(document, SerializerOptions.Shared).Length * sizeof(char)); + Thing thing = new("object", ThreadRng.Shared.Next()); - var response = await db.Create(thing, Expected); + var response = await db.Create(thing, document); TestHelper.AssertOk(response); ResultValue result = response.FirstValue(); var returnedDocument = result.GetObject(); - RoundTripObject.AssertAreEqual(Expected, returnedDocument); + RoundTripObject.AssertAreEqual(document, returnedDocument); } ); - - [Fact] - public async Task CreateAndSelectRoundTripTest() => await DbHandle.WithDatabase( + + [Theory] + [MemberData(nameof(Documents))] + public async Task CreateAndSelectRoundTripTest(RoundTripObject document) => await DbHandle.WithDatabase( async db => { + Logger.WriteLine("Document size in bytes: {0}", JsonSerializer.Serialize(document, SerializerOptions.Shared).Length * sizeof(char)); + Thing thing = new("object", ThreadRng.Shared.Next()); - await db.Create(thing, Expected); + await db.Create(thing, document); var response = await db.Select(thing); TestHelper.AssertOk(response); ResultValue result = response.FirstValue(); var returnedDocument = result.GetObject(); - RoundTripObject.AssertAreEqual(Expected, returnedDocument); + RoundTripObject.AssertAreEqual(document, returnedDocument); } ); - - [Fact] - public async Task CreateAndQueryRoundTripTest() => await DbHandle.WithDatabase( + + [Theory] + [MemberData(nameof(Documents))] + public async Task CreateAndQueryRoundTripTest(RoundTripObject document) => await DbHandle.WithDatabase( async db => { + Logger.WriteLine("Document size in bytes: {0}", JsonSerializer.Serialize(document, SerializerOptions.Shared).Length * sizeof(char)); + Thing thing = new("object", ThreadRng.Shared.Next()); - await db.Create(thing, Expected); + await db.Create(thing, document); string sql = $"SELECT * FROM \"{thing}\""; var response = await db.Query(sql, null); @@ -54,14 +95,17 @@ public async Task CreateAndQueryRoundTripTest() => await DbHandle.WithDatabas TestHelper.AssertOk(response); response.TryGetFirstValue(out ResultValue result).Should().BeTrue(); var returnedDocument = result.GetObject(); - RoundTripObject.AssertAreEqual(Expected, returnedDocument); + RoundTripObject.AssertAreEqual(document, returnedDocument); } ); + + [Theory] + [MemberData(nameof(Documents))] + public async Task CreateAndParameterizedQueryRoundTripTest(RoundTripObject document) => await DbHandle.WithDatabase(async db => { + Logger.WriteLine("Document size in bytes: {0}", JsonSerializer.Serialize(document, SerializerOptions.Shared).Length * sizeof(char)); - [Fact] - public async Task CreateAndParameterizedQueryRoundTripTest() => await DbHandle.WithDatabase(async db => { Thing thing = new("object", ThreadRng.Shared.Next()); - var rsp = await db.Create(thing, Expected); + var rsp = await db.Create(thing, document); rsp.HasErrors.Should().BeFalse(); string sql = "SELECT * FROM $thing"; Dictionary param = new() { ["thing"] = thing, }; @@ -71,7 +115,7 @@ public async Task CreateAndParameterizedQueryRoundTripTest() => await DbHandle(); - RoundTripObject.AssertAreEqual(Expected, returnedDocument); + RoundTripObject.AssertAreEqual(document, returnedDocument); }); } diff --git a/tests/Shared/TestHelper.cs b/tests/Shared/TestHelper.cs index dcf05630..6fedcf46 100644 --- a/tests/Shared/TestHelper.cs +++ b/tests/Shared/TestHelper.cs @@ -1,3 +1,5 @@ +using SurrealDB.Common; + using DriverResponse = SurrealDB.Models.Result.DriverResponse; namespace SurrealDB.Shared.Tests; @@ -59,4 +61,11 @@ public static void AssertError( Exception ex = new(message); throw ex; } + + public static string RandomString(int length = 10) { + const string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + return new string(Enumerable.Repeat(chars, length) + .Select(s => s[ThreadRng.Shared.Next(s.Length)]).ToArray()); + } + }