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

Added large document tests to Surreal DB #97

Merged
merged 4 commits into from
Oct 16, 2022
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
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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Du-z We have the Bogus package installed IIRC, this can be used to generate UNICODE dummy data, instead of a manual impl. Bogus is to .NET what faker is to JS

const string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[ThreadRng.Shared.Next(s.Length)]).ToArray());
}

}