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 #63 from Surreal-Net/query_tests_member_data
Browse files Browse the repository at this point in the history
  • Loading branch information
ProphetLamb authored Sep 27, 2022
2 parents 81ce708 + b069ec9 commit bb61e62
Show file tree
Hide file tree
Showing 35 changed files with 440 additions and 234 deletions.
8 changes: 4 additions & 4 deletions src/Abstractions/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface IDatabase<TResponse>
/// Signs up to a specific authentication scope.
/// </summary>
/// <param name="auth"> Variables used in a signin query. </param>
public new Task<TResponse> Signup(Authentication auth, CancellationToken ct = default);
public new Task<TResponse> Signup(object auth, CancellationToken ct = default);

/// <summary>
/// Signs in to a specific authentication scope.
Expand All @@ -36,7 +36,7 @@ public interface IDatabase<TResponse>
/// <remarks>
/// This updates the internal <see cref="Config" />.
/// </remarks>
public new Task<TResponse> Signin(Authentication auth, CancellationToken ct = default);
public new Task<TResponse> Signin(object auth, CancellationToken ct = default);

/// <summary>
/// Invalidates the authentication for the current connection.
Expand Down Expand Up @@ -180,7 +180,7 @@ public interface IDatabase {
/// Signs up to a specific authentication scope.
/// </summary>
/// <param name="auth"> Variables used in a signin query. </param>
public Task<IResponse> Signup(Authentication auth, CancellationToken ct = default);
public Task<IResponse> Signup(object auth, CancellationToken ct = default);

/// <summary>
/// Signs in to a specific authentication scope.
Expand All @@ -189,7 +189,7 @@ public interface IDatabase {
/// <remarks>
/// This updates the internal <see cref="Config" />.
/// </remarks>
public Task<IResponse> Signin(Authentication auth, CancellationToken ct = default);
public Task<IResponse> Signin(object auth, CancellationToken ct = default);

/// <summary>
/// Invalidates the authentication for the current connection.
Expand Down
2 changes: 1 addition & 1 deletion src/Configuration/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@ public void ThrowIfInvalid() {
throw new InvalidConfigException("The configuration is not marked as valid.");
}
}
}
}
2 changes: 1 addition & 1 deletion src/Configuration/ConfigBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,4 @@ public static Uri GetUri(
: new Uri($"https://{endPoint}/");
}
}
}
}
6 changes: 3 additions & 3 deletions src/Driver/Rest/DatabaseRest.IDatabase.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using SurrealDB.Abstractions;
using SurrealDB.Models;

namespace SurrealDB.Driver.Rest;
namespace SurrealDB.Driver.Rest;

public sealed partial class DatabaseRest : IDatabase {
async Task<IResponse> IDatabase.Info(CancellationToken ct) {
Expand All @@ -12,11 +12,11 @@ async Task<IResponse> IDatabase.Use(string db, string ns, CancellationToken ct)
return await Use(db, ns, ct);
}

async Task<IResponse> IDatabase.Signup(Authentication auth, CancellationToken ct) {
async Task<IResponse> IDatabase.Signup(object auth, CancellationToken ct) {
return await Signup(auth, ct);
}

async Task<IResponse> IDatabase.Signin(Authentication auth, CancellationToken ct) {
async Task<IResponse> IDatabase.Signin(object auth, CancellationToken ct) {
return await Signin(auth, ct);
}

Expand Down
8 changes: 4 additions & 4 deletions src/Driver/Rest/DatabaseRest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ public Task<RestResponse> Use(
}

public async Task<RestResponse> Signup(
Authentication auth,
object auth,
CancellationToken ct = default) {
return await Signup(ToJsonContent(auth), ct);
}

public async Task<RestResponse> Signin(
Authentication auth,
object auth,
CancellationToken ct = default) {
SetAuth(auth.Username, auth.Password);
// SetAuth(auth.Username, auth.Password);
HttpResponseMessage rsp = await _client.PostAsync("signin", ToJsonContent(auth), ct);
return await rsp.ToSurreal();
}
Expand Down Expand Up @@ -304,7 +304,7 @@ private string BuildRequestUri(Thing thing) {
}

private string ToJson<T>(T? v) {
return JsonSerializer.Serialize(v, Constants.JsonOptions);
return JsonSerializer.Serialize(v, SerializerOptions.Shared);
}

private HttpContent ToJsonContent<T>(T? v) {
Expand Down
4 changes: 2 additions & 2 deletions src/Driver/Rest/RestResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static async Task<RestResponse> From(
Stream stream = await msg.Content.ReadAsStreamAsync();
#endif
if (msg.StatusCode != HttpStatusCode.OK) {
Error? err = await JsonSerializer.DeserializeAsync<Error>(stream, Constants.JsonOptions, ct);
Error? err = await JsonSerializer.DeserializeAsync<Error>(stream, SerializerOptions.Shared, ct);
return From(err);
}

Expand All @@ -93,7 +93,7 @@ public static async Task<RestResponse> From(
return EmptyOk;
}

List<Success>? docs = await JsonSerializer.DeserializeAsync<List<Success>>(stream, Constants.JsonOptions, ct);
List<Success>? docs = await JsonSerializer.DeserializeAsync<List<Success>>(stream, SerializerOptions.Shared, ct);
Success doc = (docs?.FirstOrDefault(e => e.result.ValueKind != JsonValueKind.Null)).GetValueOrDefault(default);

return From(doc);
Expand Down
4 changes: 2 additions & 2 deletions src/Driver/Rpc/DatabaseRpc.IDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ async Task<IResponse> IDatabase.Use(string db, string ns, CancellationToken ct)
return await Use(db, ns, ct);
}

async Task<IResponse> IDatabase.Signup(Authentication auth, CancellationToken ct) {
async Task<IResponse> IDatabase.Signup(object auth, CancellationToken ct) {
return await Signup(auth, ct);
}

async Task<IResponse> IDatabase.Signin(Authentication auth, CancellationToken ct) {
async Task<IResponse> IDatabase.Signin(object auth, CancellationToken ct) {
return await Signin(auth, ct);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Driver/Rpc/DatabaseRpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ public async Task<RpcResponse> Use(

/// <inheritdoc />
public async Task<RpcResponse> Signup(
Authentication auth,
object auth,
CancellationToken ct = default) {
return await _client.Send(new() { method = "signup", parameters = new() { auth, }, }, ct).ToSurreal();
}

/// <inheritdoc />
public async Task<RpcResponse> Signin(
Authentication auth,
object auth,
CancellationToken ct = default) {
WsClient.Response rsp = await _client.Send(new() { method = "signin", parameters = new() { auth, }, }, ct);

Expand Down Expand Up @@ -184,7 +184,7 @@ private async Task SetAuth(
// TODO: Support jwt auth
_config.Username = user;
_config.Password = pass;
await Signin(new() { Username = user, Password = pass, }, ct);
await Signin(new{ user = user, pass = pass, }, ct);
}

public void Dispose() {
Expand Down
8 changes: 4 additions & 4 deletions src/Json/Constants.cs → src/Json/SerializerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ namespace SurrealDB.Json;
/// <summary>
/// Collection of repeatedly used constants.
/// </summary>
public static class Constants {
public static class SerializerOptions {
private static readonly Lazy<JsonSerializerOptions> _jsonSerializerOptions = new(CreateJsonOptions);

/// <summary>
/// Creates or returns the shared <see cref="JsonSerializerOptions"/> instance for this thread.
/// </summary>
public static JsonSerializerOptions JsonOptions => _jsonSerializerOptions.Value;
public static JsonSerializerOptions Shared => _jsonSerializerOptions.Value;

/// <summary>
/// Instantiates a new instance of <see cref="JsonSerializerOptions"/> with default settings.
Expand All @@ -29,10 +29,10 @@ public static JsonSerializerOptions CreateJsonOptions() {
WriteIndented = false,
NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.AllowNamedFloatingPointLiterals,
// This was throwing an exception when set to JsonIgnoreCondition.Always
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault,
DefaultIgnoreCondition = JsonIgnoreCondition.Never,
// TODO: Remove this when the server is fixed, see: https://github.com/surrealdb/surrealdb/issues/137
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
IgnoreReadOnlyFields = false,
IgnoreReadOnlyProperties = false,
UnknownTypeHandling = JsonUnknownTypeHandling.JsonElement,
Converters = { new JsonStringEnumConverter(), new DecimalConv(), new DoubleConv(), new SingleConv(), new DateTimeConv() , new DateTimeOffsetConv(), new TimeSpanConv(), new TimeOnlyConv(), new DateOnlyConv() },
};
Expand Down
4 changes: 2 additions & 2 deletions src/Json/Time/DateTimeConv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static bool TryParse(string? s, out DateTime value) {
return false;
}

public static string ToString(in DateTimeOffset value) {
public static string ToString(in DateTime value) {
return value.ToString("O");
}

Expand All @@ -51,4 +51,4 @@ public static string ToString(in DateTimeOffset value) {
private static DateTime ThrowJsonTokenTypeInvalid() {
throw new JsonException("Cannot deserialize a non numeric non string token as a DateTime.");
}
}
}
16 changes: 0 additions & 16 deletions src/Models/Authentication.cs

This file was deleted.

4 changes: 2 additions & 2 deletions src/Models/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Result(
if (_json.ValueKind is JsonValueKind.Undefined or JsonValueKind.Null) {
return default;
}
var obj = _json.Deserialize<T>(Constants.JsonOptions);
var obj = _json.Deserialize<T>(SerializerOptions.Shared);
return obj;
}

Expand All @@ -50,7 +50,7 @@ public IEnumerable<T> GetArray<T>() {

var en = _json.EnumerateArray();
while (en.MoveNext()) {
T? v = en.Current.Deserialize<T>(Constants.JsonOptions);
T? v = en.Current.Deserialize<T>(SerializerOptions.Shared);
if (!EqualityComparer<T>.Default.Equals(default, v)) {
yield return v!;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Ws/WsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public async Task<Response> Send(

await using RecyclableMemoryStream stream = new(s_manager.Value);

await JsonSerializer.SerializeAsync(stream, req, Constants.JsonOptions, ct);
await JsonSerializer.SerializeAsync(stream, req, SerializerOptions.Shared, ct);
// Now Position = Length = EndOfMessage
// Write the buffer to the websocket
stream.Position = 0;
Expand All @@ -104,7 +104,7 @@ public async Task<Response> Send(
await ReceiveStream(_ws!, stream, ct);
// Read the buffer to json DOM
stream.Position = 0;
Response rsp = await JsonSerializer.DeserializeAsync<Response>(stream, Constants.JsonOptions, ct);
Response rsp = await JsonSerializer.DeserializeAsync<Response>(stream, SerializerOptions.Shared, ct);
return rsp;
}

Expand Down
32 changes: 16 additions & 16 deletions tests/Core.Tests/ConversionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public static IEnumerable<object[]> SingleTests {
[Theory]
[MemberData(nameof(SingleTests))]
public void FloatConverterTests(object testValue, float expectedValue) {
var json = JsonSerializer.Serialize(testValue, Constants.JsonOptions);
var result = JsonSerializer.Deserialize<float>(json, Constants.JsonOptions);
var json = JsonSerializer.Serialize(testValue, SerializerOptions.Shared);
var result = JsonSerializer.Deserialize<float>(json, SerializerOptions.Shared);
result.Should().Be(expectedValue);
}

Expand Down Expand Up @@ -71,8 +71,8 @@ public static IEnumerable<object[]> DoubleTests {
[Theory]
[MemberData(nameof(DoubleTests))]
public void DoubleConverterTests(object testValue, double expectedValue) {
var json = JsonSerializer.Serialize(testValue, Constants.JsonOptions);
var result = JsonSerializer.Deserialize<double>(json, Constants.JsonOptions);
var json = JsonSerializer.Serialize(testValue, SerializerOptions.Shared);
var result = JsonSerializer.Deserialize<double>(json, SerializerOptions.Shared);
result.Should().Be(expectedValue);
}

Expand All @@ -98,8 +98,8 @@ public static IEnumerable<object[]> DecimalTests {
[Theory]
[MemberData(nameof(DecimalTests))]
public void DecimalConverterTests(object testValue, decimal expectedValue) {
var json = JsonSerializer.Serialize(testValue, Constants.JsonOptions);
var result = JsonSerializer.Deserialize<decimal>(json, Constants.JsonOptions);
var json = JsonSerializer.Serialize(testValue, SerializerOptions.Shared);
var result = JsonSerializer.Deserialize<decimal>(json, SerializerOptions.Shared);
result.Should().Be(expectedValue);
}

Expand Down Expand Up @@ -129,8 +129,8 @@ public static IEnumerable<object[]> TimeSpanTests {
[Theory]
[MemberData(nameof(TimeSpanTests))]
public void TimeSpanConverterTests(object testValue, TimeSpan expectedValue) {
var json = JsonSerializer.Serialize(testValue, Constants.JsonOptions);
var result = JsonSerializer.Deserialize<TimeSpan>(json, Constants.JsonOptions);
var json = JsonSerializer.Serialize(testValue, SerializerOptions.Shared);
var result = JsonSerializer.Deserialize<TimeSpan>(json, SerializerOptions.Shared);
result.Should().Be(expectedValue);
}

Expand Down Expand Up @@ -160,8 +160,8 @@ public static IEnumerable<object[]> DateTimeOffsetTests {
[Theory]
[MemberData(nameof(DateTimeOffsetTests))]
public void DateTimeOffsetConverterTests(object testValue, DateTimeOffset expectedValue) {
var json = JsonSerializer.Serialize(testValue, Constants.JsonOptions);
var result = JsonSerializer.Deserialize<DateTimeOffset>(json, Constants.JsonOptions);
var json = JsonSerializer.Serialize(testValue, SerializerOptions.Shared);
var result = JsonSerializer.Deserialize<DateTimeOffset>(json, SerializerOptions.Shared);
result.Should().Be(expectedValue);
}

Expand Down Expand Up @@ -196,8 +196,8 @@ public static IEnumerable<object[]> DateTimeTests {
[Theory]
[MemberData(nameof(DateTimeTests))]
public void DateTimeConverterTests(object testValue, DateTime expectedValue) {
var json = JsonSerializer.Serialize(testValue, Constants.JsonOptions);
var result = JsonSerializer.Deserialize<DateTime>(json, Constants.JsonOptions);
var json = JsonSerializer.Serialize(testValue, SerializerOptions.Shared);
var result = JsonSerializer.Deserialize<DateTime>(json, SerializerOptions.Shared);
result.Should().Be(expectedValue);
}

Expand Down Expand Up @@ -225,8 +225,8 @@ public static IEnumerable<object[]> DateTests {
[Theory]
[MemberData(nameof(DateTests))]
public void DateConverterTests(object testValue, DateOnly expectedValue) {
var json = JsonSerializer.Serialize(testValue, Constants.JsonOptions);
var result = JsonSerializer.Deserialize<DateOnly>(json, Constants.JsonOptions);
var json = JsonSerializer.Serialize(testValue, SerializerOptions.Shared);
var result = JsonSerializer.Deserialize<DateOnly>(json, SerializerOptions.Shared);
result.Should().Be(expectedValue);
}

Expand Down Expand Up @@ -256,8 +256,8 @@ public static IEnumerable<object[]> TimeTests {
[Theory]
[MemberData(nameof(TimeTests))]
public void TimeConverterTests(object testValue, TimeOnly expectedValue) {
var json = JsonSerializer.Serialize(testValue, Constants.JsonOptions);
var result = JsonSerializer.Deserialize<TimeOnly>(json, Constants.JsonOptions);
var json = JsonSerializer.Serialize(testValue, SerializerOptions.Shared);
var result = JsonSerializer.Deserialize<TimeOnly>(json, SerializerOptions.Shared);
result.Should().Be(expectedValue);
}
}
2 changes: 1 addition & 1 deletion tests/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.1" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
2 changes: 1 addition & 1 deletion tests/Driver.Tests/DatabaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected override async Task Run(T db) {
IResponse infoResp = await db.Info();
AssertOk(infoResp);

IResponse signInStatus = await db.Signin(new() { Username = TestHelper.User, Password = TestHelper.Pass, });
IResponse signInStatus = await db.Signin(new{ user = TestHelper.User, pass = TestHelper.Pass, });

AssertOk(signInStatus);
//AssertOk(await db.Invalidate());
Expand Down
19 changes: 9 additions & 10 deletions tests/Driver.Tests/Queries/EqualityQueryTests.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace SurrealDB.Driver.Tests.Queries;
namespace SurrealDB.Driver.Tests.Queries;

public abstract class EqualityQueryTests<T, TKey, TValue> : QueryTests<T, TKey, TValue>
where T : IDatabase, IDisposable, new() {
[Fact]
public async Task EqualsQueryTest() => await DbHandle<T>.WithDatabase(

[Theory]
[MemberData("KeyPairs")]
public async Task EqualsQueryTest(TValue val1, TValue val2) => await DbHandle<T>.WithDatabase(
async db => {
var val1 = RandomValue();
var val2 = RandomValue();
var expectedResult = (dynamic)val1! == (dynamic)val2!; // Can't do operator overloads on generic types, so force it by casting to a dynamic

string sql = $"SELECT * FROM ($val1 = $val2)";
Expand All @@ -21,12 +21,11 @@ public async Task EqualsQueryTest() => await DbHandle<T>.WithDatabase(
Assert.Equal(resultValue, expectedResult);
}
);

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

[Theory]
[MemberData("KeyPairs")]
public async Task NotEqualsQueryTest(TValue val1, TValue val2) => await DbHandle<T>.WithDatabase(
async db => {
var val1 = RandomValue();
var val2 = RandomValue();
var expectedResult = (dynamic)val1! != (dynamic)val2!; // Can't do operator overloads on generic types, so force it by casting to a dynamic

string sql = $"SELECT * FROM ($val1 != $val2)";
Expand Down
Loading

0 comments on commit bb61e62

Please sign in to comment.