using Expecto.CSharp;
using Expecto;
using Microsoft.FSharp.Core;
namespace BitBadger.Documents.Tests.CSharp;
using static Runner;
///
/// A test serializer that returns known values
///
internal class TestSerializer : IDocumentSerializer
{
public string Serialize(T it) => "{\"Overridden\":true}";
public T Deserialize(string it) => default!;
}
///
/// C# Tests for common functionality in BitBadger.Documents
///
public static class CommonCSharpTests
{
///
/// Unit tests for the OpSql property of the Comparison discriminated union
///
private static readonly Test OpTests = TestList("Comparison.OpSql",
[
TestCase("Equal succeeds", () =>
{
Expect.equal(Comparison.NewEqual("").OpSql, "=", "The Equals SQL was not correct");
}),
TestCase("Greater succeeds", () =>
{
Expect.equal(Comparison.NewGreater("").OpSql, ">", "The Greater SQL was not correct");
}),
TestCase("GreaterOrEqual succeeds", () =>
{
Expect.equal(Comparison.NewGreaterOrEqual("").OpSql, ">=", "The GreaterOrEqual SQL was not correct");
}),
TestCase("Less succeeds", () =>
{
Expect.equal(Comparison.NewLess("").OpSql, "<", "The Less SQL was not correct");
}),
TestCase("LessOrEqual succeeds", () =>
{
Expect.equal(Comparison.NewLessOrEqual("").OpSql, "<=", "The LessOrEqual SQL was not correct");
}),
TestCase("NotEqual succeeds", () =>
{
Expect.equal(Comparison.NewNotEqual("").OpSql, "<>", "The NotEqual SQL was not correct");
}),
TestCase("Between succeeds", () =>
{
Expect.equal(Comparison.NewBetween("", "").OpSql, "BETWEEN", "The Between SQL was not correct");
}),
TestCase("In succeeds", () =>
{
Expect.equal(Comparison.NewIn([]).OpSql, "IN", "The In SQL was not correct");
}),
TestCase("InArray succeeds", () =>
{
Expect.equal(Comparison.NewInArray("", []).OpSql, "?|", "The InArray SQL was not correct");
}),
TestCase("Exists succeeds", () =>
{
Expect.equal(Comparison.Exists.OpSql, "IS NOT NULL", "The Exists SQL was not correct");
}),
TestCase("NotExists succeeds", () =>
{
Expect.equal(Comparison.NotExists.OpSql, "IS NULL", "The NotExists SQL was not correct");
})
]);
///
/// Unit tests for the Field class
///
private static readonly Test FieldTests = TestList("Field",
[
TestCase("Equal succeeds", () =>
{
var field = Field.Equal("Test", 14);
Expect.equal(field.Name, "Test", "Field name incorrect");
Expect.equal(field.Comparison, Comparison.NewEqual(14), "Comparison incorrect");
}),
TestCase("Greater succeeds", () =>
{
var field = Field.Greater("Great", "night");
Expect.equal(field.Name, "Great", "Field name incorrect");
Expect.equal(field.Comparison, Comparison.NewGreater("night"), "Comparison incorrect");
}),
TestCase("GreaterOrEqual succeeds", () =>
{
var field = Field.GreaterOrEqual("Nice", 88L);
Expect.equal(field.Name, "Nice", "Field name incorrect");
Expect.equal(field.Comparison, Comparison.NewGreaterOrEqual(88L), "Comparison incorrect");
}),
TestCase("Less succeeds", () =>
{
var field = Field.Less("Lesser", "seven");
Expect.equal(field.Name, "Lesser", "Field name incorrect");
Expect.equal(field.Comparison, Comparison.NewLess("seven"), "Comparison incorrect");
}),
TestCase("LessOrEqual succeeds", () =>
{
var field = Field.LessOrEqual("Nobody", "KNOWS");
Expect.equal(field.Name, "Nobody", "Field name incorrect");
Expect.equal(field.Comparison, Comparison.NewLessOrEqual("KNOWS"), "Comparison incorrect");
}),
TestCase("NotEqual succeeds", () =>
{
var field = Field.NotEqual("Park", "here");
Expect.equal(field.Name, "Park", "Field name incorrect");
Expect.equal(field.Comparison, Comparison.NewNotEqual("here"), "Comparison incorrect");
}),
TestCase("Between succeeds", () =>
{
var field = Field.Between("Age", 18, 49);
Expect.equal(field.Name, "Age", "Field name incorrect");
Expect.equal(field.Comparison, Comparison.NewBetween(18, 49), "Comparison incorrect");
}),
TestCase("In succeeds", () =>
{
var field = Field.In("Here", [8, 16, 32]);
Expect.equal(field.Name, "Here", "Field name incorrect");
Expect.isTrue(field.Comparison.IsIn, "Comparison incorrect");
Expect.sequenceEqual(((Comparison.In)field.Comparison).Values, [8, 16, 32], "Value incorrect");
}),
TestCase("InArray succeeds", () =>
{
var field = Field.InArray("ArrayField", "table", ["x", "y", "z"]);
Expect.equal(field.Name, "ArrayField", "Field name incorrect");
Expect.isTrue(field.Comparison.IsInArray, "Comparison incorrect");
var it = (Comparison.InArray)field.Comparison;
Expect.equal(it.Table, "table", "Table name incorrect");
Expect.sequenceEqual(it.Values, ["x", "y", "z"], "Value incorrect");
}),
TestCase("Exists succeeds", () =>
{
var field = Field.Exists("Groovy");
Expect.equal(field.Name, "Groovy", "Field name incorrect");
Expect.isTrue(field.Comparison.IsExists, "Comparison incorrect");
}),
TestCase("NotExists succeeds", () =>
{
var field = Field.NotExists("Rad");
Expect.equal(field.Name, "Rad", "Field name incorrect");
Expect.isTrue(field.Comparison.IsNotExists, "Comparison incorrect");
}),
TestList("NameToPath",
[
TestCase("succeeds for PostgreSQL and a simple name", () =>
{
Expect.equal("data->>'Simple'", Field.NameToPath("Simple", Dialect.PostgreSQL, FieldFormat.AsSql),
"Path not constructed correctly");
}),
TestCase("succeeds for SQLite and a simple name", () =>
{
Expect.equal("data->>'Simple'", Field.NameToPath("Simple", Dialect.SQLite, FieldFormat.AsSql),
"Path not constructed correctly");
}),
TestCase("succeeds for PostgreSQL and a nested name", () =>
{
Expect.equal("data#>>'{A,Long,Path,to,the,Property}'",
Field.NameToPath("A.Long.Path.to.the.Property", Dialect.PostgreSQL, FieldFormat.AsSql),
"Path not constructed correctly");
}),
TestCase("succeeds for SQLite and a nested name", () =>
{
Expect.equal("data->'A'->'Long'->'Path'->'to'->'the'->>'Property'",
Field.NameToPath("A.Long.Path.to.the.Property", Dialect.SQLite, FieldFormat.AsSql),
"Path not constructed correctly");
})
]),
TestCase("WithParameterName succeeds", () =>
{
var field = Field.Equal("Bob", "Tom").WithParameterName("@name");
Expect.isSome(field.ParameterName, "The parameter name should have been filled");
Expect.equal("@name", field.ParameterName.Value, "The parameter name is incorrect");
}),
TestCase("WithQualifier succeeds", () =>
{
var field = Field.Equal("Bill", "Matt").WithQualifier("joe");
Expect.isSome(field.Qualifier, "The table qualifier should have been filled");
Expect.equal("joe", field.Qualifier.Value, "The table qualifier is incorrect");
}),
TestList("Path",
[
TestCase("succeeds for a PostgreSQL single field with no qualifier", () =>
{
var field = Field.GreaterOrEqual("SomethingCool", 18);
Expect.equal("data->>'SomethingCool'", field.Path(Dialect.PostgreSQL, FieldFormat.AsSql),
"The PostgreSQL path is incorrect");
}),
TestCase("succeeds for a PostgreSQL single field with a qualifier", () =>
{
var field = Field.Less("SomethingElse", 9).WithQualifier("this");
Expect.equal("this.data->>'SomethingElse'", field.Path(Dialect.PostgreSQL, FieldFormat.AsSql),
"The PostgreSQL path is incorrect");
}),
TestCase("succeeds for a PostgreSQL nested field with no qualifier", () =>
{
var field = Field.Equal("My.Nested.Field", "howdy");
Expect.equal("data#>>'{My,Nested,Field}'", field.Path(Dialect.PostgreSQL, FieldFormat.AsSql),
"The PostgreSQL path is incorrect");
}),
TestCase("succeeds for a PostgreSQL nested field with a qualifier", () =>
{
var field = Field.Equal("Nest.Away", "doc").WithQualifier("bird");
Expect.equal("bird.data#>>'{Nest,Away}'", field.Path(Dialect.PostgreSQL, FieldFormat.AsSql),
"The PostgreSQL path is incorrect");
}),
TestCase("succeeds for a SQLite single field with no qualifier", () =>
{
var field = Field.GreaterOrEqual("SomethingCool", 18);
Expect.equal("data->>'SomethingCool'", field.Path(Dialect.SQLite, FieldFormat.AsSql),
"The SQLite path is incorrect");
}),
TestCase("succeeds for a SQLite single field with a qualifier", () =>
{
var field = Field.Less("SomethingElse", 9).WithQualifier("this");
Expect.equal("this.data->>'SomethingElse'", field.Path(Dialect.SQLite, FieldFormat.AsSql),
"The SQLite path is incorrect");
}),
TestCase("succeeds for a SQLite nested field with no qualifier", () =>
{
var field = Field.Equal("My.Nested.Field", "howdy");
Expect.equal("data->'My'->'Nested'->>'Field'", field.Path(Dialect.SQLite, FieldFormat.AsSql),
"The SQLite path is incorrect");
}),
TestCase("succeeds for a SQLite nested field with a qualifier", () =>
{
var field = Field.Equal("Nest.Away", "doc").WithQualifier("bird");
Expect.equal("bird.data->'Nest'->>'Away'", field.Path(Dialect.SQLite, FieldFormat.AsSql),
"The SQLite path is incorrect");
})
])
]);
///
/// Unit tests for the FieldMatch enum
///
private static readonly Test FieldMatchTests = TestList("FieldMatch.ToString",
[
TestCase("succeeds for Any", () =>
{
Expect.equal(FieldMatch.Any.ToString(), "OR", "SQL for Any is incorrect");
}),
TestCase("succeeds for All", () =>
{
Expect.equal(FieldMatch.All.ToString(), "AND", "SQL for All is incorrect");
})
]);
///
/// Unit tests for the ParameterName class
///
private static readonly Test ParameterNameTests = TestList("ParameterName.Derive",
[
TestCase("succeeds with existing name", () =>
{
ParameterName name = new();
Expect.equal(name.Derive(FSharpOption.Some("@taco")), "@taco", "Name should have been @taco");
Expect.equal(name.Derive(FSharpOption.None), "@field0",
"Counter should not have advanced for named field");
}),
TestCase("Derive succeeds with non-existent name", () =>
{
ParameterName name = new();
Expect.equal(name.Derive(FSharpOption.None), "@field0",
"Anonymous field name should have been returned");
Expect.equal(name.Derive(FSharpOption.None), "@field1",
"Counter should have advanced from previous call");
Expect.equal(name.Derive(FSharpOption.None), "@field2",
"Counter should have advanced from previous call");
Expect.equal(name.Derive(FSharpOption.None), "@field3",
"Counter should have advanced from previous call");
})
]);
///
/// Unit tests for the AutoId enum
///
private static readonly Test AutoIdTests = TestList("AutoId",
[
TestCase("GenerateGuid succeeds", () =>
{
var autoId = AutoId.GenerateGuid();
Expect.isNotNull(autoId, "The GUID auto-ID should not have been null");
Expect.stringHasLength(autoId, 32, "The GUID auto-ID should have been 32 characters long");
Expect.equal(autoId, autoId.ToLowerInvariant(), "The GUID auto-ID should have been lowercase");
}),
TestCase("GenerateRandomString succeeds", () =>
{
foreach (var length in (int[]) [6, 8, 12, 20, 32, 57, 64])
{
var autoId = AutoId.GenerateRandomString(length);
Expect.isNotNull(autoId, $"Random string ({length}) should not have been null");
Expect.stringHasLength(autoId, length, $"Random string should have been {length} characters long");
Expect.equal(autoId, autoId.ToLowerInvariant(),
$"Random string ({length}) should have been lowercase");
}
}),
TestList("NeedsAutoId",
[
TestCase("succeeds when no auto ID is configured", () =>
{
Expect.isFalse(AutoId.NeedsAutoId(AutoId.Disabled, new object(), "id"),
"Disabled auto-ID never needs an automatic ID");
}),
TestCase("fails for any when the ID property is not found", () =>
{
try
{
_ = AutoId.NeedsAutoId(AutoId.Number, new { Key = "" }, "Id");
Expect.isTrue(false, "Non-existent ID property should have thrown an exception");
}
catch (InvalidOperationException)
{
// pass
}
}),
TestCase("succeeds for byte when the ID is zero", () =>
{
Expect.isTrue(AutoId.NeedsAutoId(AutoId.Number, new { Id = (sbyte)0 }, "Id"),
"Zero ID should have returned true");
}),
TestCase("succeeds for byte when the ID is non-zero", () =>
{
Expect.isFalse(AutoId.NeedsAutoId(AutoId.Number, new { Id = (sbyte)4 }, "Id"),
"Non-zero ID should have returned false");
}),
TestCase("succeeds for short when the ID is zero", () =>
{
Expect.isTrue(AutoId.NeedsAutoId(AutoId.Number, new { Id = (short)0 }, "Id"),
"Zero ID should have returned true");
}),
TestCase("succeeds for short when the ID is non-zero", () =>
{
Expect.isFalse(AutoId.NeedsAutoId(AutoId.Number, new { Id = (short)7 }, "Id"),
"Non-zero ID should have returned false");
}),
TestCase("succeeds for int when the ID is zero", () =>
{
Expect.isTrue(AutoId.NeedsAutoId(AutoId.Number, new { Id = 0 }, "Id"),
"Zero ID should have returned true");
}),
TestCase("succeeds for int when the ID is non-zero", () =>
{
Expect.isFalse(AutoId.NeedsAutoId(AutoId.Number, new { Id = 32 }, "Id"),
"Non-zero ID should have returned false");
}),
TestCase("succeeds for long when the ID is zero", () =>
{
Expect.isTrue(AutoId.NeedsAutoId(AutoId.Number, new { Id = 0L }, "Id"),
"Zero ID should have returned true");
}),
TestCase("succeeds for long when the ID is non-zero", () =>
{
Expect.isFalse(AutoId.NeedsAutoId(AutoId.Number, new { Id = 80L }, "Id"),
"Non-zero ID should have returned false");
}),
TestCase("fails for number when the ID is not a number", () =>
{
try
{
_ = AutoId.NeedsAutoId(AutoId.Number, new { Id = "" }, "Id");
Expect.isTrue(false, "Numeric ID against a string should have thrown an exception");
}
catch (InvalidOperationException)
{
// pass
}
}),
TestCase("succeeds for GUID when the ID is blank", () =>
{
Expect.isTrue(AutoId.NeedsAutoId(AutoId.Guid, new { Id = "" }, "Id"),
"Blank ID should have returned true");
}),
TestCase("succeeds for GUID when the ID is filled", () =>
{
Expect.isFalse(AutoId.NeedsAutoId(AutoId.Guid, new { Id = "abc" }, "Id"),
"Filled ID should have returned false");
}),
TestCase("fails for GUID when the ID is not a string", () =>
{
try
{
_ = AutoId.NeedsAutoId(AutoId.Guid, new { Id = 8 }, "Id");
Expect.isTrue(false, "String ID against a number should have thrown an exception");
}
catch (InvalidOperationException)
{
// pass
}
}),
TestCase("succeeds for RandomString when the ID is blank", () =>
{
Expect.isTrue(AutoId.NeedsAutoId(AutoId.RandomString, new { Id = "" }, "Id"),
"Blank ID should have returned true");
}),
TestCase("succeeds for RandomString when the ID is filled", () =>
{
Expect.isFalse(AutoId.NeedsAutoId(AutoId.RandomString, new { Id = "x" }, "Id"),
"Filled ID should have returned false");
}),
TestCase("fails for RandomString when the ID is not a string", () =>
{
try
{
_ = AutoId.NeedsAutoId(AutoId.RandomString, new { Id = 33 }, "Id");
Expect.isTrue(false, "String ID against a number should have thrown an exception");
}
catch (InvalidOperationException)
{
// pass
}
})
])
]);
///
/// Unit tests for the Configuration static class
///
private static readonly Test ConfigurationTests = TestList("Configuration",
[
TestCase("UseSerializer succeeds", () =>
{
try
{
Configuration.UseSerializer(new TestSerializer());
var serialized = Configuration.Serializer().Serialize(new SubDocument
{
Foo = "howdy",
Bar = "bye"
});
Expect.equal(serialized, "{\"Overridden\":true}", "Specified serializer was not used");
var deserialized = Configuration.Serializer()
.Deserialize