Skip to content

Commit

Permalink
Made the test suite great again.
Browse files Browse the repository at this point in the history
Improved separation between F# test cases and C# test cases to avoid breaking other tests due to differences with the serializer used (newtonsoft has limited support for discriminated unions).
  • Loading branch information
nkosi23 authored and jeremydmiller committed Dec 12, 2024
1 parent 14fedf5 commit 6022dbd
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 35 deletions.
18 changes: 4 additions & 14 deletions src/LinqTests/Acceptance/Support/DefaultQueryFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ public DefaultQueryFixture()
.Duplicate(x => x.NumberArray);
});

FSharpFriendlyStore = ProvisionStore("linq_querying", options =>
FSharpFriendlyStore = ProvisionStore("fsharp_linq_querying", options =>
{
options.RegisterFSharpOptionValueTypes();
var serializerOptions = JsonFSharpOptions.Default().WithUnwrapOption().ToJsonSerializerOptions();
options.UseSystemTextJsonForSerialization(serializerOptions);
});
}, isFsharpTest: true);

FSharpFriendlyStoreWithDuplicatedField = ProvisionStore("duplicate_fields", options =>
FSharpFriendlyStoreWithDuplicatedField = ProvisionStore("fsharp_duplicated_fields", options =>
{
options.Schema.For<Target>()
.Duplicate(x => x.Number)
Expand All @@ -47,7 +47,7 @@ public DefaultQueryFixture()
options.RegisterFSharpOptionValueTypes();
var serializerOptions = JsonFSharpOptions.Default().WithUnwrapOption().ToJsonSerializerOptions();
options.UseSystemTextJsonForSerialization(serializerOptions);
});
}, isFsharpTest: true);

SystemTextJsonStore = ProvisionStore("stj_linq", o =>
{
Expand All @@ -65,13 +65,3 @@ public DefaultQueryFixture()
public DocumentStore Store { get; set; }
}

public static class DefaultQueryFixtureExtensions
{
public static void UseFSharp(this DocumentStore store)
{
var o = store.Options;
o.RegisterFSharpOptionValueTypes();
var serializerOptions = JsonFSharpOptions.Default().WithUnwrapOption().ToJsonSerializerOptions();
o.UseSystemTextJsonForSerialization(serializerOptions);
}
}
16 changes: 13 additions & 3 deletions src/LinqTests/Acceptance/Support/LinqTestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,27 @@ public static IEnumerable<object[]> GetDescriptions()
return _descriptions.Select(x => new object[] { x });
}

protected async Task assertTestCase(string description, IDocumentStore store)
protected async Task assertTestCaseWithDocuments(string description, IDocumentStore store, Target[] documents)
{
var index = _descriptions.IndexOf(description);

var testCase = testCases[index];
await using var session = store.QuerySession();


var logger = new TestOutputMartenLogger(TestOutput);

session.Logger = logger;

await testCase.Compare(session, Fixture.Documents, logger);
await testCase.Compare(session, documents, logger);
}

protected Task assertTestCase(string description, IDocumentStore store)
{
return assertTestCaseWithDocuments(description, store, Fixture.Documents);
}

protected Task assertFSharpTestCase(string description, IDocumentStore store)
{
return assertTestCaseWithDocuments(description, store, Fixture.FSharpDocuments);
}
}
17 changes: 15 additions & 2 deletions src/LinqTests/Acceptance/Support/TargetSchemaFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ namespace LinqTests.Acceptance.Support;

public abstract class TargetSchemaFixture: IDisposable
{
/*
* Newtonsoft.Json does not support saving Discriminated Unions unwrapped (included f# options) which causes serialization-related errors.
* We must therefore only include F# data in F#-related tests to avoid false negatives.
*/
public readonly Target[] Documents = Target.GenerateRandomData(1000).ToArray();
public readonly Target[] FSharpDocuments = Target.GenerateRandomData(1000, includeFSharpUnionTypes: true).ToArray();

private readonly IList<DocumentStore> _stores = new List<DocumentStore>();

Expand All @@ -21,7 +26,7 @@ public void Dispose()
}
}

internal DocumentStore ProvisionStore(string schema, Action<StoreOptions> configure = null)
internal DocumentStore ProvisionStore(string schema, Action<StoreOptions> configure = null, bool isFsharpTest = false)
{
var store = DocumentStore.For(x =>
{
Expand All @@ -33,7 +38,15 @@ internal DocumentStore ProvisionStore(string schema, Action<StoreOptions> config

store.Advanced.Clean.CompletelyRemoveAll();

store.BulkInsert(Documents);

if (isFsharpTest)
{
store.BulkInsert(FSharpDocuments);
}
else
{
store.BulkInsert(Documents);
}

_stores.Add(store);

Expand Down
4 changes: 2 additions & 2 deletions src/LinqTests/Acceptance/where_clauses_fsharp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ static where_clauses_fsharp()
[MemberData(nameof(GetDescriptions))]
public Task run_query(string description)
{
return assertTestCase(description, Fixture.FSharpFriendlyStore);
return assertFSharpTestCase(description, Fixture.FSharpFriendlyStore);
}

[Theory]
[MemberData(nameof(GetDescriptions))]
public Task with_duplicated_fields(string description)
{
return assertTestCase(description, Fixture.FSharpFriendlyStoreWithDuplicatedField);
return assertFSharpTestCase(description, Fixture.FSharpFriendlyStoreWithDuplicatedField);
}
}
8 changes: 4 additions & 4 deletions src/LinqTests/Operators/is_one_of_operator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public void can_query_against_fsharp_guid_option_array_with_unwrapped_guid(Func<
private void can_query_against_array<T>(Func<T[], Expression<Func<Target, bool>>> isOneOf, Func<Target, T> select)
{

var targets = Target.GenerateRandomData(100).ToArray();
var targets = Target.GenerateRandomData(100, true).ToArray();
theStore.BulkInsert(targets);

var validValues = targets.Select(select).Distinct().Take(3).ToArray();
Expand Down Expand Up @@ -201,7 +201,7 @@ private void can_query_against_array_with_not_operator<T>(
Func<Target, T> select
)
{
var targets = Target.GenerateRandomData(100).ToArray();
var targets = Target.GenerateRandomData(100, true).ToArray();
theStore.BulkInsert(targets);

var validValues = targets.Select(select).Distinct().Take(3).ToArray();
Expand Down Expand Up @@ -240,7 +240,7 @@ public void can_query_against_fsharp_guid_option_list(Func<List<FSharpOption<Gui

private void can_query_against_list<T>(Func<List<T>, Expression<Func<Target, bool>>> isOneOf, Func<Target, T> select)
{
var targets = Target.GenerateRandomData(100).ToArray();
var targets = Target.GenerateRandomData(100, true).ToArray();
theStore.BulkInsert(targets);

var validValues = targets.Select(select).Distinct().Take(3).ToList();
Expand Down Expand Up @@ -288,7 +288,7 @@ private void can_query_against_list_with_not_operator<T>(
Func<Target, T> select
)
{
var targets = Target.GenerateRandomData(100).ToArray();
var targets = Target.GenerateRandomData(100, true).ToArray();
theStore.BulkInsert(targets);

var validValues = targets.Select(select).Distinct().Take(3).ToList();
Expand Down
30 changes: 20 additions & 10 deletions src/Marten.Testing/Documents/Target.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,32 @@ public class Target
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"
};

public static IEnumerable<Target> GenerateRandomData(int number)
public static IEnumerable<Target> GenerateRandomData(int number, bool includeFSharpUnionTypes = false)
{
var i = 0;
while (i < number)
{
yield return Random(true);
yield return Random(true, includeFSharpUnionTypes);

i++;
}
}

public static Target Random(bool deep = false)
public static Target Random(bool deep = false, bool includeFSharpUnionTypes = false)
{
var target = new Target();
target.String = _strings[_random.Next(0, 10)];
target.FSharpGuidOption = new FSharpOption<Guid>(Guid.NewGuid());
target.FSharpIntOption = new FSharpOption<int>(_random.Next(0, 10));
target.FSharpDateOption = new FSharpOption<DateTime>(DateTime.Now);
target.FSharpDateTimeOffsetOption = new FSharpOption<DateTimeOffset>(new DateTimeOffset(DateTime.UtcNow));
target.FSharpDecimalOption = new FSharpOption<decimal>(_random.Next(0, 10));
target.FSharpLongOption = new FSharpOption<long>(_random.Next(0, 10));
target.FSharpStringOption = new FSharpOption<string>(_strings[_random.Next(0, 10)]);

if (includeFSharpUnionTypes)
{
target.FSharpGuidOption = new FSharpOption<Guid>(Guid.NewGuid());
target.FSharpIntOption = new FSharpOption<int>(_random.Next(0, 10));
target.FSharpDateOption = new FSharpOption<DateTime>(DateTime.Now);
target.FSharpDateTimeOffsetOption = new FSharpOption<DateTimeOffset>(new DateTimeOffset(DateTime.UtcNow));
target.FSharpDecimalOption = new FSharpOption<decimal>(_random.Next(0, 10));
target.FSharpLongOption = new FSharpOption<long>(_random.Next(0, 10));
target.FSharpStringOption = new FSharpOption<string>(_strings[_random.Next(0, 10)]);
}

target.PaddedString = " " + target.String + " ";
target.AnotherString = _otherStrings[_random.Next(0, 10)];
Expand Down Expand Up @@ -107,6 +111,7 @@ public static Target Random(bool deep = false)
target.HowLong = TimeSpan.FromSeconds(target.Long);

target.Date = DateTime.Today.AddDays(_random.Next(-10000, 10000));
target.DateOffset = new DateTimeOffset(DateTime.Today.AddDays(_random.Next(-10000, 10000)));

if (value > 15)
{
Expand Down Expand Up @@ -213,6 +218,11 @@ public Target()
public TimeSpan HowLong { get; set; }
}

public class FSharpTarget: Target
{

}

public class Address
{
public Address()
Expand Down

0 comments on commit 6022dbd

Please sign in to comment.