Skip to content
This repository has been archived by the owner on Dec 18, 2023. It is now read-only.

Commit

Permalink
Merge pull request #97 from Surreal-Net/feature/large_document_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ProphetLamb authored Oct 16, 2022
2 parents 837ac40 + 1a34850 commit 63a4635
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 19 deletions.
82 changes: 63 additions & 19 deletions tests/Driver.Tests/Roundtrip/RoundTripTests.cs
Original file line number Diff line number Diff line change
@@ -1,67 +1,111 @@
using System.Collections;

using SurrealDB.Json;
using SurrealDB.Models.Result;

namespace SurrealDB.Driver.Tests.Roundtrip;

public class RpcRoundTripTests : RoundTripTests<DatabaseRpc> {
public RpcRoundTripTests(ITestOutputHelper logger) : base(logger) {
}
}

public class RestRoundTripTests : RoundTripTests<DatabaseRest> {
public RestRoundTripTests(ITestOutputHelper logger) : base(logger) {
}
}

[Collection("SurrealDBRequired")]
public abstract class RoundTripTests<T>
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<T>.WithDatabase(
private static IEnumerable<RoundTripObject> 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<object[]> Documents => DocumentsToTest.Select(e => new []{(object)e}).ToList();

[Theory]
[MemberData(nameof(Documents))]
public async Task CreateRoundTripTest(RoundTripObject document) => await DbHandle<T>.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>();
RoundTripObject.AssertAreEqual(Expected, returnedDocument);
RoundTripObject.AssertAreEqual(document, returnedDocument);
}
);

[Fact]
public async Task CreateAndSelectRoundTripTest() => await DbHandle<T>.WithDatabase(

[Theory]
[MemberData(nameof(Documents))]
public async Task CreateAndSelectRoundTripTest(RoundTripObject document) => await DbHandle<T>.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>();
RoundTripObject.AssertAreEqual(Expected, returnedDocument);
RoundTripObject.AssertAreEqual(document, returnedDocument);
}
);

[Fact]
public async Task CreateAndQueryRoundTripTest() => await DbHandle<T>.WithDatabase(

[Theory]
[MemberData(nameof(Documents))]
public async Task CreateAndQueryRoundTripTest(RoundTripObject document) => await DbHandle<T>.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);
response.Should().NotBeNull();
TestHelper.AssertOk(response);
response.TryGetFirstValue(out ResultValue result).Should().BeTrue();
var returnedDocument = result.GetObject<RoundTripObject>();
RoundTripObject.AssertAreEqual(Expected, returnedDocument);
RoundTripObject.AssertAreEqual(document, returnedDocument);
}
);

[Theory]
[MemberData(nameof(Documents))]
public async Task CreateAndParameterizedQueryRoundTripTest(RoundTripObject document) => await DbHandle<T>.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<T>.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<string, object?> param = new() { ["thing"] = thing, };
Expand All @@ -71,7 +115,7 @@ public async Task CreateAndParameterizedQueryRoundTripTest() => await DbHandle<T
TestHelper.AssertOk(response);
ResultValue result = response.FirstValue();
var returnedDocument = result.GetObject<RoundTripObject>();
RoundTripObject.AssertAreEqual(Expected, returnedDocument);
RoundTripObject.AssertAreEqual(document, returnedDocument);
});
}

Expand Down
9 changes: 9 additions & 0 deletions tests/Shared/TestHelper.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using SurrealDB.Common;

using DriverResponse = SurrealDB.Models.Result.DriverResponse;

namespace SurrealDB.Shared.Tests;
Expand Down Expand Up @@ -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());
}

}

0 comments on commit 63a4635

Please # to comment.