From fcc2a75d63aa1b6dbf57253efb746f26b0238302 Mon Sep 17 00:00:00 2001 From: Du-z <16366766+Du-z@users.noreply.github.com> Date: Tue, 27 Sep 2022 21:56:08 +1000 Subject: [PATCH 1/4] Added MemberData to query tests and fixed some failures --- src/Json/Constants.cs | 2 +- src/Json/Time/DateTimeConv.cs | 4 +- tests/Core.Tests/Core.Tests.csproj | 6 +- tests/Driver.Tests/Driver.Tests.csproj | 4 ++ .../Queries/EqualityQueryTests.cs | 19 +++--- .../Queries/InequalityQueryTests.cs | 38 ++++++------ tests/Driver.Tests/Queries/MathQueryTests.cs | 60 ++++++++++++------- tests/Driver.Tests/Queries/QueryTests.cs | 28 ++++----- .../Queries/Typed/DateOnlyQueryTests.cs | 27 +++++++-- .../Queries/Typed/DateTimeOffsetQueryTests.cs | 24 ++++++-- .../Queries/Typed/DateTimeQueryTests.cs | 34 +++++++---- .../Queries/Typed/DecimalQueryTests.cs | 28 +++++++-- .../Queries/Typed/DoubleQueryTests.cs | 28 +++++++-- .../Queries/Typed/EnumFlagQueryTests.cs | 34 +++++++++-- .../Queries/Typed/EnumQueryTests.cs | 28 +++++---- .../Queries/Typed/FloatQueryTests.cs | 28 +++++++-- .../Queries/Typed/GuidQueryTests.cs | 24 ++++++-- .../Queries/Typed/IntQueryTests.cs | 30 +++++++--- .../Queries/Typed/LongQueryTests.cs | 28 +++++++-- .../Queries/Typed/StringQueryTests.cs | 29 +++++++-- .../Queries/Typed/TimeOnlyQueryTests.cs | 34 +++++++---- .../Queries/Typed/TimeSpanQueryTests.cs | 35 +++++++---- tests/Shared/Shared.csproj | 4 ++ tests/Shared/SurrealInstance.cs | 2 +- 24 files changed, 406 insertions(+), 172 deletions(-) diff --git a/src/Json/Constants.cs b/src/Json/Constants.cs index 771fd8d2..d91b3b8a 100644 --- a/src/Json/Constants.cs +++ b/src/Json/Constants.cs @@ -29,7 +29,7 @@ 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, diff --git a/src/Json/Time/DateTimeConv.cs b/src/Json/Time/DateTimeConv.cs index 4d6b55f1..43a500d5 100644 --- a/src/Json/Time/DateTimeConv.cs +++ b/src/Json/Time/DateTimeConv.cs @@ -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"); } @@ -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."); } -} \ No newline at end of file +} diff --git a/tests/Core.Tests/Core.Tests.csproj b/tests/Core.Tests/Core.Tests.csproj index 0902e7e2..f76b7ed2 100644 --- a/tests/Core.Tests/Core.Tests.csproj +++ b/tests/Core.Tests/Core.Tests.csproj @@ -1,4 +1,4 @@ - + SurrealDB.Core.Tests @@ -11,4 +11,8 @@ + + + + diff --git a/tests/Driver.Tests/Driver.Tests.csproj b/tests/Driver.Tests/Driver.Tests.csproj index fdcfc2ce..2be86e37 100644 --- a/tests/Driver.Tests/Driver.Tests.csproj +++ b/tests/Driver.Tests/Driver.Tests.csproj @@ -18,5 +18,9 @@ + + + + \ No newline at end of file diff --git a/tests/Driver.Tests/Queries/EqualityQueryTests.cs b/tests/Driver.Tests/Queries/EqualityQueryTests.cs index a33343a1..18a32209 100644 --- a/tests/Driver.Tests/Queries/EqualityQueryTests.cs +++ b/tests/Driver.Tests/Queries/EqualityQueryTests.cs @@ -1,12 +1,12 @@ -namespace SurrealDB.Driver.Tests.Queries; +namespace SurrealDB.Driver.Tests.Queries; public abstract class EqualityQueryTests : QueryTests where T : IDatabase, IDisposable, new() { - [Fact] - public async Task EqualsQueryTest() => await DbHandle.WithDatabase( + + [Theory] + [MemberData("KeyPairs")] + public async Task EqualsQueryTest(TValue val1, TValue val2) => await DbHandle.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)"; @@ -21,12 +21,11 @@ public async Task EqualsQueryTest() => await DbHandle.WithDatabase( Assert.Equal(resultValue, expectedResult); } ); - - [Fact] - public async Task NotEqualsQueryTest() => await DbHandle.WithDatabase( + + [Theory] + [MemberData("KeyPairs")] + public async Task NotEqualsQueryTest(TValue val1, TValue val2) => await DbHandle.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)"; diff --git a/tests/Driver.Tests/Queries/InequalityQueryTests.cs b/tests/Driver.Tests/Queries/InequalityQueryTests.cs index 7280e104..f3477884 100644 --- a/tests/Driver.Tests/Queries/InequalityQueryTests.cs +++ b/tests/Driver.Tests/Queries/InequalityQueryTests.cs @@ -1,13 +1,12 @@ -namespace SurrealDB.Driver.Tests.Queries; +namespace SurrealDB.Driver.Tests.Queries; public abstract class InequalityQueryTests : EqualityQueryTests where T : IDatabase, IDisposable, new() { - - [Fact] - public async Task LessThanQueryTest() => await DbHandle.WithDatabase( + + [Theory] + [MemberData("KeyPairs")] + public async Task LessThanQueryTest(TValue val1, TValue val2) => await DbHandle.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)"; @@ -22,12 +21,11 @@ public async Task LessThanQueryTest() => await DbHandle.WithDatabase( Assert.Equal(resultValue, expectedResult); } ); - - [Fact] - public async Task LessThanOrEqualToQueryTest() => await DbHandle.WithDatabase( + + [Theory] + [MemberData("KeyPairs")] + public async Task LessThanOrEqualToQueryTest(TValue val1, TValue val2) => await DbHandle.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)"; @@ -42,12 +40,11 @@ public async Task LessThanOrEqualToQueryTest() => await DbHandle.WithDatabase Assert.Equal(resultValue, expectedResult); } ); - - [Fact] - public async Task GreaterThanQueryTest() => await DbHandle.WithDatabase( + + [Theory] + [MemberData("KeyPairs")] + public async Task GreaterThanQueryTest(TValue val1, TValue val2) => await DbHandle.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)"; @@ -62,12 +59,11 @@ public async Task GreaterThanQueryTest() => await DbHandle.WithDatabase( Assert.Equal(resultValue, expectedResult); } ); - - [Fact] - public async Task GreaterThanOrEqualToQueryTest() => await DbHandle.WithDatabase( + + [Theory] + [MemberData("KeyPairs")] + public async Task GreaterThanOrEqualToQueryTest(TValue val1, TValue val2) => await DbHandle.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)"; diff --git a/tests/Driver.Tests/Queries/MathQueryTests.cs b/tests/Driver.Tests/Queries/MathQueryTests.cs index b0ecdeda..81c75952 100644 --- a/tests/Driver.Tests/Queries/MathQueryTests.cs +++ b/tests/Driver.Tests/Queries/MathQueryTests.cs @@ -1,4 +1,4 @@ -namespace SurrealDB.Driver.Tests.Queries; +namespace SurrealDB.Driver.Tests.Queries; public abstract class MathQueryTests : InequalityQueryTests where T : IDatabase, IDisposable, new() { @@ -6,11 +6,10 @@ public abstract class MathQueryTests : InequalityQueryTests await DbHandle.WithDatabase( + [Theory] + [MemberData("KeyPairs")] + public async Task AdditionQueryTest(TValue val1, TValue val2) => await DbHandle.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 {ValueCast()}($val1 + $val2)"; @@ -25,12 +24,11 @@ public async Task AdditionQueryTest() => await DbHandle.WithDatabase( AssertEquivalency(resultValue, expectedResult); } ); - - [Fact] - public async Task SubtractionQueryTest() => await DbHandle.WithDatabase( + + [Theory] + [MemberData("KeyPairs")] + public async Task SubtractionQueryTest(TValue val1, TValue val2) => await DbHandle.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 {ValueCast()}($val1 - $val2)"; @@ -45,12 +43,11 @@ public async Task SubtractionQueryTest() => await DbHandle.WithDatabase( AssertEquivalency(value, expectedResult); } ); - - [Fact] - public async Task MultiplicationQueryTest() => await DbHandle.WithDatabase( + + [Theory] + [MemberData("KeyPairs")] + public async Task MultiplicationQueryTest(TValue val1, TValue val2) => await DbHandle.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 {ValueCast()}($val1 * $val2)"; @@ -65,13 +62,25 @@ public async Task MultiplicationQueryTest() => await DbHandle.WithDatabase( AssertEquivalency(value, expectedResult); } ); - - [Fact] - public async Task DivisionQueryTest() => await DbHandle.WithDatabase( + + [Theory] + [MemberData("KeyPairs")] + public async Task DivisionQueryTest(TValue val1, TValue val2) => await DbHandle.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 + var divisorIsZero = false; + dynamic? expectedResult; + if ((dynamic)val2! != 0) { + expectedResult = (dynamic)val1! / (dynamic)val2!; // Can't do operator overloads on generic types, so force it by casting to a dynamic + } else { + divisorIsZero = true; + expectedResult = default(TValue); + } + + if (divisorIsZero) { + // TODO: Remove this when divide by zero works + // Pass the test right now as Surreal crashes when it tries to divide by 0 + return; + } string sql = $"SELECT * FROM {ValueCast()}($val1 / $val2)"; Dictionary param = new() { ["val1"] = val1, ["val2"] = val2, }; @@ -81,8 +90,13 @@ public async Task DivisionQueryTest() => await DbHandle.WithDatabase( Assert.NotNull(response); TestHelper.AssertOk(response); Assert.True(response.TryGetResult(out Result result)); - var value = result.GetObject(); - AssertEquivalency(value, expectedResult); + + if (!divisorIsZero) { + var value = result.GetObject(); + AssertEquivalency(value, expectedResult); + } else { + Assert.True(false); // TODO: Test for the expected result when doing a divide by zero + } } ); diff --git a/tests/Driver.Tests/Queries/QueryTests.cs b/tests/Driver.Tests/Queries/QueryTests.cs index 7025801f..b14da28e 100644 --- a/tests/Driver.Tests/Queries/QueryTests.cs +++ b/tests/Driver.Tests/Queries/QueryTests.cs @@ -12,14 +12,12 @@ public abstract class QueryTests public QueryTests(ITestOutputHelper logger) { Logger = logger; } - - protected abstract TKey RandomKey(); - protected abstract TValue RandomValue(); - - [Fact] - public async Task SimpleSelectQueryTest() => await DbHandle.WithDatabase( + + [Theory] + [MemberData("KeyAndValuePairs")] + public async Task SimpleSelectQueryTest(TKey key, TValue value) => await DbHandle.WithDatabase( async db => { - TestObject expectedObject = new(RandomKey(), RandomValue()); + TestObject expectedObject = new(key, value); Thing thing = Thing.From("object", expectedObject.Key!.ToString()); await db.Create(thing, expectedObject); @@ -35,11 +33,12 @@ public async Task SimpleSelectQueryTest() => await DbHandle.WithDatabase( doc.Should().BeEquivalentTo(expectedObject); } ); - - [Fact] - public async Task SimpleSelectFromWhereQueryTest() => await DbHandle.WithDatabase( + + [Theory] + [MemberData("KeyAndValuePairs")] + public async Task SimpleSelectFromWhereQueryTest(TKey key, TValue value) => await DbHandle.WithDatabase( async db => { - TestObject expectedObject = new(RandomKey(), RandomValue()); + TestObject expectedObject = new(key, value); Thing thing = Thing.From("object", expectedObject.Key!.ToString()); await db.Create(thing, expectedObject); @@ -57,10 +56,11 @@ public async Task SimpleSelectFromWhereQueryTest() => await DbHandle.WithData } ); - [Fact] - public async Task SimpleSelectFromWhereValueQueryTest() => await DbHandle.WithDatabase( + [Theory] + [MemberData("KeyAndValuePairs")] + public async Task SimpleSelectFromWhereValueQueryTest(TKey key, TValue value) => await DbHandle.WithDatabase( async db => { - TestObject expectedObject = new(RandomKey(), RandomValue()); + TestObject expectedObject = new(key, value); Logger.WriteLine("exp: {0}", Serialize(expectedObject)); Thing thing = Thing.From("object", expectedObject.Key!.ToString()); diff --git a/tests/Driver.Tests/Queries/Typed/DateOnlyQueryTests.cs b/tests/Driver.Tests/Queries/Typed/DateOnlyQueryTests.cs index 0d812a38..c9827fd1 100644 --- a/tests/Driver.Tests/Queries/Typed/DateOnlyQueryTests.cs +++ b/tests/Driver.Tests/Queries/Typed/DateOnlyQueryTests.cs @@ -10,13 +10,30 @@ public RestDateOnlyQueryTests(ITestOutputHelper logger) : base(logger) { public abstract class DateOnlyQueryTests : InequalityQueryTests where T : IDatabase, IDisposable, new() { - - protected override int RandomKey() { - return RandomInt(); + + private static IEnumerable TestValues { + get { + yield return new DateOnly(2012, 6, 12); + yield return new DateOnly(2012, 10, 2); + yield return DateOnly.MaxValue; + yield return DateOnly.MinValue; + } } - protected override DateOnly RandomValue() { - return RandomDateOnly(); + public static IEnumerable KeyAndValuePairs { + get { + return TestValues.Select(e => new object[] { RandomInt(), e }); + } + } + + public static IEnumerable KeyPairs { + get { + foreach (var testValue1 in TestValues) { + foreach (var testValue2 in TestValues) { + yield return new object[] { testValue1, testValue2 }; + } + } + } } private static int RandomInt() { diff --git a/tests/Driver.Tests/Queries/Typed/DateTimeOffsetQueryTests.cs b/tests/Driver.Tests/Queries/Typed/DateTimeOffsetQueryTests.cs index d49a953a..d5bff8ca 100644 --- a/tests/Driver.Tests/Queries/Typed/DateTimeOffsetQueryTests.cs +++ b/tests/Driver.Tests/Queries/Typed/DateTimeOffsetQueryTests.cs @@ -11,12 +11,28 @@ public RestDateTimeOffsetQueryTests(ITestOutputHelper logger) : base(logger) { public abstract class DateTimeOffsetQueryTests : InequalityQueryTests where T : IDatabase, IDisposable, new() { - protected override int RandomKey() { - return RandomInt(); + private static IEnumerable TestValues { + get { + //yield return new DateTimeOffset(2012, 6, 12, 10, 5, 32, 648, TimeSpan.Zero); + yield return DateTimeOffset.MaxValue.ToUniversalTime(); + yield return DateTimeOffset.MinValue.ToUniversalTime(); + } } - protected override DateTimeOffset RandomValue() { - return RandomDateTimeOffset(); + public static IEnumerable KeyAndValuePairs { + get { + return TestValues.Select(e => new object[] { RandomInt(), e }); + } + } + + public static IEnumerable KeyPairs { + get { + foreach (var testValue1 in TestValues) { + foreach (var testValue2 in TestValues) { + yield return new object[] { testValue1, testValue2 }; + } + } + } } private static int RandomInt() { diff --git a/tests/Driver.Tests/Queries/Typed/DateTimeQueryTests.cs b/tests/Driver.Tests/Queries/Typed/DateTimeQueryTests.cs index 046eac77..489a5b80 100644 --- a/tests/Driver.Tests/Queries/Typed/DateTimeQueryTests.cs +++ b/tests/Driver.Tests/Queries/Typed/DateTimeQueryTests.cs @@ -11,26 +11,36 @@ public RestDateTimeQueryTests(ITestOutputHelper logger) : base(logger) { public abstract class DateTimeQueryTests : InequalityQueryTests where T : IDatabase, IDisposable, new() { - protected override int RandomKey() { - return RandomInt(); + private static IEnumerable TestValues { + get { + //yield return new DateTime(2012, 6, 12, 10, 5, 32, 648, DateTimeKind.Utc); + //yield return new DateTime(2012, 10, 2, 20, 55, 54, 3, DateTimeKind.Utc); + //yield return new DateTime(2012, 12, 2, 1, 2, 3, 4, DateTimeKind.Utc); + //yield return DateTime.MaxValue.AsUtc(); + yield return DateTime.MinValue.AsUtc(); + } } - protected override DateTime RandomValue() { - return RandomDateTime(); + public static IEnumerable KeyAndValuePairs { + get { + return TestValues.Select(e => new object[] { RandomInt(), e }); + } + } + + public static IEnumerable KeyPairs { + get { + foreach (var testValue1 in TestValues) { + foreach (var testValue2 in TestValues) { + yield return new object[] { testValue1, testValue2 }; + } + } + } } private static int RandomInt() { return ThreadRng.Shared.Next(); } - private static DateTime RandomDateTime() { - var minDate = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc); - var maxDate = new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Utc); - var diff = (maxDate - minDate).TotalMicroseconds(); - var randomeDateTime = minDate.AddMicroseconds((long)(ThreadRng.Shared.NextDouble() * diff)); - return randomeDateTime; - } - protected DateTimeQueryTests(ITestOutputHelper logger) : base(logger) { } } diff --git a/tests/Driver.Tests/Queries/Typed/DecimalQueryTests.cs b/tests/Driver.Tests/Queries/Typed/DecimalQueryTests.cs index 818b8a05..42916b80 100644 --- a/tests/Driver.Tests/Queries/Typed/DecimalQueryTests.cs +++ b/tests/Driver.Tests/Queries/Typed/DecimalQueryTests.cs @@ -12,12 +12,28 @@ public RestDecimalQueryTests(ITestOutputHelper logger) : base(logger) { public abstract class DecimalQueryTests : MathQueryTests where T : IDatabase, IDisposable, new() { - protected override decimal RandomKey() { - return RandomDouble(); - } - - protected override decimal RandomValue() { - return (RandomDouble() * 2000m) - 1000m; // Can't go too high otherwise the maths operations might overflow + private static IEnumerable TestValues { + get { + yield return 1000; // Can't go too high otherwise the maths operations might overflow + yield return 0; + yield return -1000; + } + } + + public static IEnumerable KeyAndValuePairs { + get { + return TestValues.Select(e => new object[] { RandomDouble(), e }); + } + } + + public static IEnumerable KeyPairs { + get { + foreach (var testValue1 in TestValues) { + foreach (var testValue2 in TestValues) { + yield return new object[] { testValue1, testValue2 }; + } + } + } } protected override string ValueCast() { diff --git a/tests/Driver.Tests/Queries/Typed/DoubleQueryTests.cs b/tests/Driver.Tests/Queries/Typed/DoubleQueryTests.cs index 9d324929..52ee5957 100644 --- a/tests/Driver.Tests/Queries/Typed/DoubleQueryTests.cs +++ b/tests/Driver.Tests/Queries/Typed/DoubleQueryTests.cs @@ -12,12 +12,28 @@ public RestDoubleQueryTests(ITestOutputHelper logger) : base(logger) { public abstract class DoubleQueryTests : MathQueryTests where T : IDatabase, IDisposable, new() { - protected override double RandomKey() { - return RandomDouble(); - } - - protected override double RandomValue() { - return (RandomDouble() * 2000d) - 1000d; // Can't go too high otherwise the maths operations might overflow + private static IEnumerable TestValues { + get { + yield return 1000; // Can't go too high otherwise the maths operations might overflow + yield return 0; + yield return -1000; + } + } + + public static IEnumerable KeyAndValuePairs { + get { + return TestValues.Select(e => new object[] { RandomDouble(), e }); + } + } + + public static IEnumerable KeyPairs { + get { + foreach (var testValue1 in TestValues) { + foreach (var testValue2 in TestValues) { + yield return new object[] { testValue1, testValue2 }; + } + } + } } protected override string ValueCast() { diff --git a/tests/Driver.Tests/Queries/Typed/EnumFlagQueryTests.cs b/tests/Driver.Tests/Queries/Typed/EnumFlagQueryTests.cs index d9f2278a..d2e89cb8 100644 --- a/tests/Driver.Tests/Queries/Typed/EnumFlagQueryTests.cs +++ b/tests/Driver.Tests/Queries/Typed/EnumFlagQueryTests.cs @@ -12,12 +12,38 @@ public RestEnumFlagQueryTests(ITestOutputHelper logger) : base(logger) { public abstract class EnumFlagQueryTests : EqualityQueryTests where T : IDatabase, IDisposable, new() { - protected override int RandomKey() { - return RandomInt(); + + private static IEnumerable TestValues { + get { + var flags = Enum.GetValues(typeof(StandardEnum)).Cast(); + + var count = 0; + foreach (var flags1 in flags) { + foreach (var flags2 in flags) { + count++; + if (count >= 4) { // Don't need to do every flag + count = 0; + yield return flags1 | flags2; + } + } + } + } } - protected override FlagsEnum RandomValue() { - return RandomEnum() | RandomEnum(); + public static IEnumerable KeyAndValuePairs { + get { + return TestValues.Select(e => new object[] { RandomInt(), e }); + } + } + + public static IEnumerable KeyPairs { + get { + foreach (var testValue1 in TestValues) { + foreach (var testValue2 in TestValues) { + yield return new object[] { testValue1, testValue2 }; + } + } + } } private static int RandomInt() { diff --git a/tests/Driver.Tests/Queries/Typed/EnumQueryTests.cs b/tests/Driver.Tests/Queries/Typed/EnumQueryTests.cs index 65ddd735..7d83ec9c 100644 --- a/tests/Driver.Tests/Queries/Typed/EnumQueryTests.cs +++ b/tests/Driver.Tests/Queries/Typed/EnumQueryTests.cs @@ -12,24 +12,32 @@ public RestEnumQueryTests(ITestOutputHelper logger) : base(logger) { public abstract class EnumQueryTests : EqualityQueryTests where T : IDatabase, IDisposable, new() { - protected override int RandomKey() { - return RandomInt(); + private static IEnumerable TestValues { + get { + return Enum.GetValues(typeof(StandardEnum)).Cast(); + } } - protected override StandardEnum RandomValue() { - return RandomEnum(); + public static IEnumerable KeyAndValuePairs { + get { + return TestValues.Select(e => new object[] { RandomInt(), e }); + } + } + + public static IEnumerable KeyPairs { + get { + foreach (var testValue1 in TestValues) { + foreach (var testValue2 in TestValues) { + yield return new object[] { testValue1, testValue2 }; + } + } + } } private static int RandomInt() { return ThreadRng.Shared.Next(); } - private static StandardEnum RandomEnum() { - var enumValues = EnumHelper.GetValues(); - var index = ThreadRng.Shared.Next(0, enumValues.Length); - return enumValues[index]; - } - public EnumQueryTests(ITestOutputHelper logger) : base(logger) { } } diff --git a/tests/Driver.Tests/Queries/Typed/FloatQueryTests.cs b/tests/Driver.Tests/Queries/Typed/FloatQueryTests.cs index 03cedc6a..0826dcf2 100644 --- a/tests/Driver.Tests/Queries/Typed/FloatQueryTests.cs +++ b/tests/Driver.Tests/Queries/Typed/FloatQueryTests.cs @@ -12,12 +12,28 @@ public RestFloatQueryTests(ITestOutputHelper logger) : base(logger) { public abstract class FloatQueryTests : MathQueryTests where T : IDatabase, IDisposable, new() { - protected override float RandomKey() { - return RandomFloat(); - } - - protected override float RandomValue() { - return (RandomFloat() * 2000) - 1000; // Can't go too high otherwise the maths operations might overflow + private static IEnumerable TestValues { + get { + yield return 1000; // Can't go too high otherwise the maths operations might overflow + yield return 0; + yield return -1000; + } + } + + public static IEnumerable KeyAndValuePairs { + get { + return TestValues.Select(e => new object[] { RandomFloat(), e }); + } + } + + public static IEnumerable KeyPairs { + get { + foreach (var testValue1 in TestValues) { + foreach (var testValue2 in TestValues) { + yield return new object[] { testValue1, testValue2 }; + } + } + } } protected override string ValueCast() { diff --git a/tests/Driver.Tests/Queries/Typed/GuidQueryTests.cs b/tests/Driver.Tests/Queries/Typed/GuidQueryTests.cs index 3ac23fdf..3a1a651f 100644 --- a/tests/Driver.Tests/Queries/Typed/GuidQueryTests.cs +++ b/tests/Driver.Tests/Queries/Typed/GuidQueryTests.cs @@ -12,12 +12,28 @@ public RestGuidQueryTests(ITestOutputHelper logger) : base(logger) { public abstract class GuidQueryTests : EqualityQueryTests where T : IDatabase, IDisposable, new() { - protected override Guid RandomKey() { - return RandomGuid(); + + private static IEnumerable TestValues { + get { + yield return Guid.NewGuid(); + yield return Guid.Empty; + } } - protected override Guid RandomValue() { - return RandomGuid(); + public static IEnumerable KeyAndValuePairs { + get { + return TestValues.Select(e => new object[] { RandomGuid(), e }); + } + } + + public static IEnumerable KeyPairs { + get { + foreach (var testValue1 in TestValues) { + foreach (var testValue2 in TestValues) { + yield return new object[] { testValue1, testValue2 }; + } + } + } } private static Guid RandomGuid() { diff --git a/tests/Driver.Tests/Queries/Typed/IntQueryTests.cs b/tests/Driver.Tests/Queries/Typed/IntQueryTests.cs index b2af9ca2..ad387176 100644 --- a/tests/Driver.Tests/Queries/Typed/IntQueryTests.cs +++ b/tests/Driver.Tests/Queries/Typed/IntQueryTests.cs @@ -11,13 +11,29 @@ public RestIntQueryTests(ITestOutputHelper logger) : base(logger) { public abstract class IntQueryTests : MathQueryTests where T : IDatabase, IDisposable, new() { - - protected override int RandomKey() { - return RandomInt(); - } - - protected override int RandomValue() { - return ThreadRng.Shared.Next(-10000, 10000); // Can't go too high otherwise the maths operations might overflow + + private static IEnumerable TestValues { + get { + yield return 10000; // Can't go too high otherwise the maths operations might overflow + yield return 0; + yield return -10000; + } + } + + public static IEnumerable KeyAndValuePairs { + get { + return TestValues.Select(e => new object[] { RandomInt(), e }); + } + } + + public static IEnumerable KeyPairs { + get { + foreach (var testValue1 in TestValues) { + foreach (var testValue2 in TestValues) { + yield return new object[] { testValue1, testValue2 }; + } + } + } } protected override string ValueCast() { diff --git a/tests/Driver.Tests/Queries/Typed/LongQueryTests.cs b/tests/Driver.Tests/Queries/Typed/LongQueryTests.cs index 94f9f8d6..2f798cbe 100644 --- a/tests/Driver.Tests/Queries/Typed/LongQueryTests.cs +++ b/tests/Driver.Tests/Queries/Typed/LongQueryTests.cs @@ -12,12 +12,28 @@ public RestLongQueryTests(ITestOutputHelper logger) : base(logger) { public abstract class LongQueryTests : MathQueryTests where T : IDatabase, IDisposable, new() { - protected override long RandomKey() { - return RandomLong(); - } - - protected override long RandomValue() { - return ThreadRng.Shared.NextInt64(-10000, 10000); // Can't go too high otherwise the maths operations might overflow + private static IEnumerable TestValues { + get { + yield return 10000; // Can't go too high otherwise the maths operations might overflow + yield return 0; + yield return -10000; + } + } + + public static IEnumerable KeyAndValuePairs { + get { + return TestValues.Select(e => new object[] { RandomLong(), e }); + } + } + + public static IEnumerable KeyPairs { + get { + foreach (var testValue1 in TestValues) { + foreach (var testValue2 in TestValues) { + yield return new object[] { testValue1, testValue2 }; + } + } + } } protected override string ValueCast() { diff --git a/tests/Driver.Tests/Queries/Typed/StringQueryTests.cs b/tests/Driver.Tests/Queries/Typed/StringQueryTests.cs index f3ead990..0543f4fd 100644 --- a/tests/Driver.Tests/Queries/Typed/StringQueryTests.cs +++ b/tests/Driver.Tests/Queries/Typed/StringQueryTests.cs @@ -12,12 +12,33 @@ public RpcStringQueryTests(ITestOutputHelper logger) : base(logger) { public abstract class StringQueryTests : EqualityQueryTests where T : IDatabase, IDisposable, new() { - protected override string RandomKey() { - return RandomString(); + private static IEnumerable TestValues { + get { + yield return "Test"; + yield return "Test123"; + yield return "Test 123"; + yield return "Test-123"; + yield return "Test_123"; + //yield return "Test\n123"; + yield return ""; + yield return null; + } } - protected override string RandomValue() { - return RandomString(); + public static IEnumerable KeyAndValuePairs { + get { + return TestValues.Select(e => new object[] { RandomString(), e }); + } + } + + public static IEnumerable KeyPairs { + get { + foreach (var testValue1 in TestValues) { + foreach (var testValue2 in TestValues) { + yield return new object[] { testValue1, testValue2 }; + } + } + } } private static string RandomString(int length = 10) { diff --git a/tests/Driver.Tests/Queries/Typed/TimeOnlyQueryTests.cs b/tests/Driver.Tests/Queries/Typed/TimeOnlyQueryTests.cs index 4eaa63a2..5e7c69a4 100644 --- a/tests/Driver.Tests/Queries/Typed/TimeOnlyQueryTests.cs +++ b/tests/Driver.Tests/Queries/Typed/TimeOnlyQueryTests.cs @@ -11,26 +11,36 @@ public RestTimeOnlyQueryTests(ITestOutputHelper logger) : base(logger) { public abstract class TimeOnlyQueryTests : InequalityQueryTests where T : IDatabase, IDisposable, new(){ - protected override int RandomKey() { - return RandomInt(); + private static IEnumerable TestValues { + get { + yield return new TimeOnly(10, 5, 32, 648); + yield return new TimeOnly(20, 55, 54, 3); + yield return new TimeOnly(1, 2, 3, 4); + yield return TimeOnly.MaxValue; + yield return TimeOnly.MinValue; + } } - protected override TimeOnly RandomValue() { - return RandomTimeOnly(); + public static IEnumerable KeyAndValuePairs { + get { + return TestValues.Select(e => new object[] { RandomInt(), e }); + } + } + + public static IEnumerable KeyPairs { + get { + foreach (var testValue1 in TestValues) { + foreach (var testValue2 in TestValues) { + yield return new object[] { testValue1, testValue2 }; + } + } + } } private static int RandomInt() { return ThreadRng.Shared.Next(); } - private static TimeOnly RandomTimeOnly() { - var minDate = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc); - var maxDate = new DateTime(2000, 1, 2, 0, 0, 0, DateTimeKind.Utc); - var diff = (maxDate - minDate).TotalMicroseconds(); - var randomeDateTime = minDate.AddMicroseconds((long)(ThreadRng.Shared.NextDouble() * diff)); - return TimeOnly.FromDateTime(randomeDateTime); - } - public TimeOnlyQueryTests(ITestOutputHelper logger) : base(logger) { } } diff --git a/tests/Driver.Tests/Queries/Typed/TimeSpanQueryTests.cs b/tests/Driver.Tests/Queries/Typed/TimeSpanQueryTests.cs index 29afebe7..84cb7c95 100644 --- a/tests/Driver.Tests/Queries/Typed/TimeSpanQueryTests.cs +++ b/tests/Driver.Tests/Queries/Typed/TimeSpanQueryTests.cs @@ -10,26 +10,35 @@ public RestTimeSpanQueryTests(ITestOutputHelper logger) : base(logger) { public abstract class TimeSpanQueryTests : InequalityQueryTests where T : IDatabase, IDisposable, new() { - - protected override int RandomKey() { - return RandomInt(); + + private static IEnumerable TestValues { + get { + yield return new TimeSpan(1, 2, 3, 4, 5); + yield return new TimeSpan(200, 20, 34, 41, 65); + yield return TimeSpan.Zero; + } } - protected override TimeSpan RandomValue() { - return RandomTimeOnly(); + public static IEnumerable KeyAndValuePairs { + get { + return TestValues.Select(e => new object[] { RandomInt(), e }); + } + } + + public static IEnumerable KeyPairs { + get { + foreach (var testValue1 in TestValues) { + foreach (var testValue2 in TestValues) { + yield return new object[] { testValue1, testValue2 }; + } + } + } } private static int RandomInt() { return ThreadRng.Shared.Next(); } - - private static TimeSpan RandomTimeOnly() { - var minDate = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc); - var maxDate = new DateTime(2000, 1, 2, 0, 0, 0, DateTimeKind.Utc); - var diff = (maxDate - minDate); - return diff; - } - + public TimeSpanQueryTests(ITestOutputHelper logger) : base(logger) { } } diff --git a/tests/Shared/Shared.csproj b/tests/Shared/Shared.csproj index a1bc176e..6d359abf 100644 --- a/tests/Shared/Shared.csproj +++ b/tests/Shared/Shared.csproj @@ -12,4 +12,8 @@ + + + + diff --git a/tests/Shared/SurrealInstance.cs b/tests/Shared/SurrealInstance.cs index 7f98f2f5..ef1838fd 100644 --- a/tests/Shared/SurrealInstance.cs +++ b/tests/Shared/SurrealInstance.cs @@ -22,7 +22,7 @@ public static Process Create() { private static Process? Instantiate(string path) { ProcessStartInfo pi = new() { - // These two settings can make the tests run twice as fast, but makes it harder to kill rouge DB instances + // These two settings can make the tests run more than twice as fast, but makes it harder to kill rouge DB instances //CreateNoWindow = true, //UseShellExecute = false, UseShellExecute = true, From c5b9492c02bc73bbefeab18ae07e4b3e0ed6c421 Mon Sep 17 00:00:00 2001 From: Du-z <16366766+Du-z@users.noreply.github.com> Date: Tue, 27 Sep 2022 22:09:24 +1000 Subject: [PATCH 2/4] Use Directory.Build.props --- tests/Core.Tests/Core.Tests.csproj | 6 +----- tests/Directory.Build.props | 2 +- tests/Driver.Tests/Driver.Tests.csproj | 4 ---- tests/Shared/Shared.csproj | 4 ---- 4 files changed, 2 insertions(+), 14 deletions(-) diff --git a/tests/Core.Tests/Core.Tests.csproj b/tests/Core.Tests/Core.Tests.csproj index f76b7ed2..0902e7e2 100644 --- a/tests/Core.Tests/Core.Tests.csproj +++ b/tests/Core.Tests/Core.Tests.csproj @@ -1,4 +1,4 @@ - + SurrealDB.Core.Tests @@ -11,8 +11,4 @@ - - - - diff --git a/tests/Directory.Build.props b/tests/Directory.Build.props index 7265d92a..404d416c 100644 --- a/tests/Directory.Build.props +++ b/tests/Directory.Build.props @@ -12,7 +12,7 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/tests/Driver.Tests/Driver.Tests.csproj b/tests/Driver.Tests/Driver.Tests.csproj index 2be86e37..fdcfc2ce 100644 --- a/tests/Driver.Tests/Driver.Tests.csproj +++ b/tests/Driver.Tests/Driver.Tests.csproj @@ -18,9 +18,5 @@ - - - - \ No newline at end of file diff --git a/tests/Shared/Shared.csproj b/tests/Shared/Shared.csproj index 6d359abf..a1bc176e 100644 --- a/tests/Shared/Shared.csproj +++ b/tests/Shared/Shared.csproj @@ -12,8 +12,4 @@ - - - - From 17f196dd55cdbf5df329a39f966adae067eae665 Mon Sep 17 00:00:00 2001 From: ProphetLamb Date: Tue, 27 Sep 2022 17:54:41 +0200 Subject: [PATCH 3/4] Rename `Constatns` -> `SerializerOptions` --- src/Driver/Rest/DatabaseRest.cs | 2 +- src/Driver/Rest/RestResponse.cs | 4 +-- .../{Constants.cs => SerializerOptions.cs} | 6 ++-- src/Models/Result.cs | 4 +-- src/Ws/WsClient.cs | 4 +-- tests/Core.Tests/ConversionTests.cs | 32 +++++++++---------- tests/Driver.Tests/Queries/QueryTests.cs | 4 +-- 7 files changed, 28 insertions(+), 28 deletions(-) rename src/Json/{Constants.cs => SerializerOptions.cs} (90%) diff --git a/src/Driver/Rest/DatabaseRest.cs b/src/Driver/Rest/DatabaseRest.cs index 92db50ef..b5959486 100644 --- a/src/Driver/Rest/DatabaseRest.cs +++ b/src/Driver/Rest/DatabaseRest.cs @@ -304,7 +304,7 @@ private string BuildRequestUri(Thing thing) { } private string ToJson(T? v) { - return JsonSerializer.Serialize(v, Constants.JsonOptions); + return JsonSerializer.Serialize(v, SerializerOptions.Shared); } private HttpContent ToJsonContent(T? v) { diff --git a/src/Driver/Rest/RestResponse.cs b/src/Driver/Rest/RestResponse.cs index c7512f41..edbf8c43 100644 --- a/src/Driver/Rest/RestResponse.cs +++ b/src/Driver/Rest/RestResponse.cs @@ -84,7 +84,7 @@ public static async Task From( Stream stream = await msg.Content.ReadAsStreamAsync(); #endif if (msg.StatusCode != HttpStatusCode.OK) { - Error? err = await JsonSerializer.DeserializeAsync(stream, Constants.JsonOptions, ct); + Error? err = await JsonSerializer.DeserializeAsync(stream, SerializerOptions.Shared, ct); return From(err); } @@ -93,7 +93,7 @@ public static async Task From( return EmptyOk; } - List? docs = await JsonSerializer.DeserializeAsync>(stream, Constants.JsonOptions, ct); + List? docs = await JsonSerializer.DeserializeAsync>(stream, SerializerOptions.Shared, ct); Success doc = (docs?.FirstOrDefault(e => e.result.ValueKind != JsonValueKind.Null)).GetValueOrDefault(default); return From(doc); diff --git a/src/Json/Constants.cs b/src/Json/SerializerOptions.cs similarity index 90% rename from src/Json/Constants.cs rename to src/Json/SerializerOptions.cs index d91b3b8a..98dd8c06 100644 --- a/src/Json/Constants.cs +++ b/src/Json/SerializerOptions.cs @@ -10,13 +10,13 @@ namespace SurrealDB.Json; /// /// Collection of repeatedly used constants. /// -public static class Constants { +public static class SerializerOptions { private static readonly Lazy _jsonSerializerOptions = new(CreateJsonOptions); /// /// Creates or returns the shared instance for this thread. /// - public static JsonSerializerOptions JsonOptions => _jsonSerializerOptions.Value; + public static JsonSerializerOptions Shared => _jsonSerializerOptions.Value; /// /// Instantiates a new instance of with default settings. @@ -32,7 +32,7 @@ public static JsonSerializerOptions CreateJsonOptions() { 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() }, }; diff --git a/src/Models/Result.cs b/src/Models/Result.cs index 74871ec9..300f3c29 100644 --- a/src/Models/Result.cs +++ b/src/Models/Result.cs @@ -39,7 +39,7 @@ public Result( if (_json.ValueKind is JsonValueKind.Undefined or JsonValueKind.Null) { return default; } - var obj = _json.Deserialize(Constants.JsonOptions); + var obj = _json.Deserialize(SerializerOptions.Shared); return obj; } @@ -50,7 +50,7 @@ public IEnumerable GetArray() { var en = _json.EnumerateArray(); while (en.MoveNext()) { - T? v = en.Current.Deserialize(Constants.JsonOptions); + T? v = en.Current.Deserialize(SerializerOptions.Shared); if (!EqualityComparer.Default.Equals(default, v)) { yield return v!; } diff --git a/src/Ws/WsClient.cs b/src/Ws/WsClient.cs index af8eb71c..b9f4aca4 100644 --- a/src/Ws/WsClient.cs +++ b/src/Ws/WsClient.cs @@ -92,7 +92,7 @@ public async Task 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; @@ -104,7 +104,7 @@ public async Task Send( await ReceiveStream(_ws!, stream, ct); // Read the buffer to json DOM stream.Position = 0; - Response rsp = await JsonSerializer.DeserializeAsync(stream, Constants.JsonOptions, ct); + Response rsp = await JsonSerializer.DeserializeAsync(stream, SerializerOptions.Shared, ct); return rsp; } diff --git a/tests/Core.Tests/ConversionTests.cs b/tests/Core.Tests/ConversionTests.cs index abf343d2..099e5994 100644 --- a/tests/Core.Tests/ConversionTests.cs +++ b/tests/Core.Tests/ConversionTests.cs @@ -34,8 +34,8 @@ public static IEnumerable SingleTests { [Theory] [MemberData(nameof(SingleTests))] public void FloatConverterTests(object testValue, float expectedValue) { - var json = JsonSerializer.Serialize(testValue, Constants.JsonOptions); - var result = JsonSerializer.Deserialize(json, Constants.JsonOptions); + var json = JsonSerializer.Serialize(testValue, SerializerOptions.Shared); + var result = JsonSerializer.Deserialize(json, SerializerOptions.Shared); result.Should().Be(expectedValue); } @@ -71,8 +71,8 @@ public static IEnumerable DoubleTests { [Theory] [MemberData(nameof(DoubleTests))] public void DoubleConverterTests(object testValue, double expectedValue) { - var json = JsonSerializer.Serialize(testValue, Constants.JsonOptions); - var result = JsonSerializer.Deserialize(json, Constants.JsonOptions); + var json = JsonSerializer.Serialize(testValue, SerializerOptions.Shared); + var result = JsonSerializer.Deserialize(json, SerializerOptions.Shared); result.Should().Be(expectedValue); } @@ -98,8 +98,8 @@ public static IEnumerable DecimalTests { [Theory] [MemberData(nameof(DecimalTests))] public void DecimalConverterTests(object testValue, decimal expectedValue) { - var json = JsonSerializer.Serialize(testValue, Constants.JsonOptions); - var result = JsonSerializer.Deserialize(json, Constants.JsonOptions); + var json = JsonSerializer.Serialize(testValue, SerializerOptions.Shared); + var result = JsonSerializer.Deserialize(json, SerializerOptions.Shared); result.Should().Be(expectedValue); } @@ -129,8 +129,8 @@ public static IEnumerable TimeSpanTests { [Theory] [MemberData(nameof(TimeSpanTests))] public void TimeSpanConverterTests(object testValue, TimeSpan expectedValue) { - var json = JsonSerializer.Serialize(testValue, Constants.JsonOptions); - var result = JsonSerializer.Deserialize(json, Constants.JsonOptions); + var json = JsonSerializer.Serialize(testValue, SerializerOptions.Shared); + var result = JsonSerializer.Deserialize(json, SerializerOptions.Shared); result.Should().Be(expectedValue); } @@ -160,8 +160,8 @@ public static IEnumerable DateTimeOffsetTests { [Theory] [MemberData(nameof(DateTimeOffsetTests))] public void DateTimeOffsetConverterTests(object testValue, DateTimeOffset expectedValue) { - var json = JsonSerializer.Serialize(testValue, Constants.JsonOptions); - var result = JsonSerializer.Deserialize(json, Constants.JsonOptions); + var json = JsonSerializer.Serialize(testValue, SerializerOptions.Shared); + var result = JsonSerializer.Deserialize(json, SerializerOptions.Shared); result.Should().Be(expectedValue); } @@ -196,8 +196,8 @@ public static IEnumerable DateTimeTests { [Theory] [MemberData(nameof(DateTimeTests))] public void DateTimeConverterTests(object testValue, DateTime expectedValue) { - var json = JsonSerializer.Serialize(testValue, Constants.JsonOptions); - var result = JsonSerializer.Deserialize(json, Constants.JsonOptions); + var json = JsonSerializer.Serialize(testValue, SerializerOptions.Shared); + var result = JsonSerializer.Deserialize(json, SerializerOptions.Shared); result.Should().Be(expectedValue); } @@ -225,8 +225,8 @@ public static IEnumerable DateTests { [Theory] [MemberData(nameof(DateTests))] public void DateConverterTests(object testValue, DateOnly expectedValue) { - var json = JsonSerializer.Serialize(testValue, Constants.JsonOptions); - var result = JsonSerializer.Deserialize(json, Constants.JsonOptions); + var json = JsonSerializer.Serialize(testValue, SerializerOptions.Shared); + var result = JsonSerializer.Deserialize(json, SerializerOptions.Shared); result.Should().Be(expectedValue); } @@ -256,8 +256,8 @@ public static IEnumerable TimeTests { [Theory] [MemberData(nameof(TimeTests))] public void TimeConverterTests(object testValue, TimeOnly expectedValue) { - var json = JsonSerializer.Serialize(testValue, Constants.JsonOptions); - var result = JsonSerializer.Deserialize(json, Constants.JsonOptions); + var json = JsonSerializer.Serialize(testValue, SerializerOptions.Shared); + var result = JsonSerializer.Deserialize(json, SerializerOptions.Shared); result.Should().Be(expectedValue); } } diff --git a/tests/Driver.Tests/Queries/QueryTests.cs b/tests/Driver.Tests/Queries/QueryTests.cs index b14da28e..3cc23eaf 100644 --- a/tests/Driver.Tests/Queries/QueryTests.cs +++ b/tests/Driver.Tests/Queries/QueryTests.cs @@ -83,12 +83,12 @@ public async Task SimpleSelectFromWhereValueQueryTest(TKey key, TValue value) => [DebuggerStepThrough] protected static string Serialize(in V value) { - return JsonSerializer.Serialize(value, Constants.JsonOptions); + return JsonSerializer.Serialize(value, SerializerOptions.Shared); } [DebuggerStepThrough] protected static V? Deserialize(string value) { - return JsonSerializer.Deserialize(value, Constants.JsonOptions); + return JsonSerializer.Deserialize(value, SerializerOptions.Shared); } } From b069ec92fe5ca0f00fd36ddbd5bc0f620d95455b Mon Sep 17 00:00:00 2001 From: ProphetLamb Date: Tue, 27 Sep 2022 18:03:05 +0200 Subject: [PATCH 4/4] Remove authentication type --- src/Abstractions/Database.cs | 8 ++++---- src/Configuration/Config.cs | 2 +- src/Configuration/ConfigBuilder.cs | 2 +- src/Driver/Rest/DatabaseRest.IDatabase.cs | 6 +++--- src/Driver/Rest/DatabaseRest.cs | 6 +++--- src/Driver/Rpc/DatabaseRpc.IDatabase.cs | 4 ++-- src/Driver/Rpc/DatabaseRpc.cs | 6 +++--- src/Models/Authentication.cs | 16 ---------------- tests/Driver.Tests/DatabaseTests.cs | 2 +- 9 files changed, 18 insertions(+), 34 deletions(-) delete mode 100644 src/Models/Authentication.cs diff --git a/src/Abstractions/Database.cs b/src/Abstractions/Database.cs index e767b02d..babb6379 100644 --- a/src/Abstractions/Database.cs +++ b/src/Abstractions/Database.cs @@ -27,7 +27,7 @@ public interface IDatabase /// Signs up to a specific authentication scope. /// /// Variables used in a signin query. - public new Task Signup(Authentication auth, CancellationToken ct = default); + public new Task Signup(object auth, CancellationToken ct = default); /// /// Signs in to a specific authentication scope. @@ -36,7 +36,7 @@ public interface IDatabase /// /// This updates the internal . /// - public new Task Signin(Authentication auth, CancellationToken ct = default); + public new Task Signin(object auth, CancellationToken ct = default); /// /// Invalidates the authentication for the current connection. @@ -180,7 +180,7 @@ public interface IDatabase { /// Signs up to a specific authentication scope. /// /// Variables used in a signin query. - public Task Signup(Authentication auth, CancellationToken ct = default); + public Task Signup(object auth, CancellationToken ct = default); /// /// Signs in to a specific authentication scope. @@ -189,7 +189,7 @@ public interface IDatabase { /// /// This updates the internal . /// - public Task Signin(Authentication auth, CancellationToken ct = default); + public Task Signin(object auth, CancellationToken ct = default); /// /// Invalidates the authentication for the current connection. diff --git a/src/Configuration/Config.cs b/src/Configuration/Config.cs index 19162d7f..626a19de 100644 --- a/src/Configuration/Config.cs +++ b/src/Configuration/Config.cs @@ -81,4 +81,4 @@ public void ThrowIfInvalid() { throw new InvalidConfigException("The configuration is not marked as valid."); } } -} \ No newline at end of file +} diff --git a/src/Configuration/ConfigBuilder.cs b/src/Configuration/ConfigBuilder.cs index 71fbc179..0cda6927 100644 --- a/src/Configuration/ConfigBuilder.cs +++ b/src/Configuration/ConfigBuilder.cs @@ -373,4 +373,4 @@ public static Uri GetUri( : new Uri($"https://{endPoint}/"); } } -} \ No newline at end of file +} diff --git a/src/Driver/Rest/DatabaseRest.IDatabase.cs b/src/Driver/Rest/DatabaseRest.IDatabase.cs index 46fa5283..2f6c888a 100644 --- a/src/Driver/Rest/DatabaseRest.IDatabase.cs +++ b/src/Driver/Rest/DatabaseRest.IDatabase.cs @@ -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 IDatabase.Info(CancellationToken ct) { @@ -12,11 +12,11 @@ async Task IDatabase.Use(string db, string ns, CancellationToken ct) return await Use(db, ns, ct); } - async Task IDatabase.Signup(Authentication auth, CancellationToken ct) { + async Task IDatabase.Signup(object auth, CancellationToken ct) { return await Signup(auth, ct); } - async Task IDatabase.Signin(Authentication auth, CancellationToken ct) { + async Task IDatabase.Signin(object auth, CancellationToken ct) { return await Signin(auth, ct); } diff --git a/src/Driver/Rest/DatabaseRest.cs b/src/Driver/Rest/DatabaseRest.cs index b5959486..af955452 100644 --- a/src/Driver/Rest/DatabaseRest.cs +++ b/src/Driver/Rest/DatabaseRest.cs @@ -80,15 +80,15 @@ public Task Use( } public async Task Signup( - Authentication auth, + object auth, CancellationToken ct = default) { return await Signup(ToJsonContent(auth), ct); } public async Task 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(); } diff --git a/src/Driver/Rpc/DatabaseRpc.IDatabase.cs b/src/Driver/Rpc/DatabaseRpc.IDatabase.cs index a2c2ed57..f612e2f7 100644 --- a/src/Driver/Rpc/DatabaseRpc.IDatabase.cs +++ b/src/Driver/Rpc/DatabaseRpc.IDatabase.cs @@ -12,11 +12,11 @@ async Task IDatabase.Use(string db, string ns, CancellationToken ct) return await Use(db, ns, ct); } - async Task IDatabase.Signup(Authentication auth, CancellationToken ct) { + async Task IDatabase.Signup(object auth, CancellationToken ct) { return await Signup(auth, ct); } - async Task IDatabase.Signin(Authentication auth, CancellationToken ct) { + async Task IDatabase.Signin(object auth, CancellationToken ct) { return await Signin(auth, ct); } diff --git a/src/Driver/Rpc/DatabaseRpc.cs b/src/Driver/Rpc/DatabaseRpc.cs index 5500a1e3..ae8d4d86 100644 --- a/src/Driver/Rpc/DatabaseRpc.cs +++ b/src/Driver/Rpc/DatabaseRpc.cs @@ -73,14 +73,14 @@ public async Task Use( /// public async Task Signup( - Authentication auth, + object auth, CancellationToken ct = default) { return await _client.Send(new() { method = "signup", parameters = new() { auth, }, }, ct).ToSurreal(); } /// public async Task Signin( - Authentication auth, + object auth, CancellationToken ct = default) { WsClient.Response rsp = await _client.Send(new() { method = "signin", parameters = new() { auth, }, }, ct); @@ -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() { diff --git a/src/Models/Authentication.cs b/src/Models/Authentication.cs deleted file mode 100644 index 354c3874..00000000 --- a/src/Models/Authentication.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System.Text.Json.Serialization; - -namespace SurrealDB.Models; - -public record struct Authentication { - [JsonPropertyName("ns")] - public string? Namespace { get; set; } - [JsonPropertyName("db")] - public string? Database { get; set; } - [JsonPropertyName("sc")] - public string? Scope { get; set; } - [JsonPropertyName("user")] - public string? Username { get; set; } - [JsonPropertyName("pass")] - public string? Password { get; set; } -} \ No newline at end of file diff --git a/tests/Driver.Tests/DatabaseTests.cs b/tests/Driver.Tests/DatabaseTests.cs index aed92b9c..b4a4a92a 100644 --- a/tests/Driver.Tests/DatabaseTests.cs +++ b/tests/Driver.Tests/DatabaseTests.cs @@ -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());