Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
# Changelog

## 5.3.1 (unreleased)
## 5.3.1 — 2026-10-05

### Fixed

- **`Filter(NotEquals, x => x.Member, null)` matched nothing.** The scalar path bound the null as a
parameter and emitted `JSON_EXTRACT(...) <> NULL`, which is never true in SQL, so the "member is
present" test silently returned no rows on every 5.x release. 4.x concatenated `<> ''`, which
behaved as `IS NOT NULL` by accident. The scalar path now emits `IS NOT NULL`, exactly as
`Equals` with null emits `IS NULL` and as the list-member path already did. An empty string is
present, so it still matches; `NotEquals` with a non-null value still excludes absent members.
(#34)
- **`NotEquals` with a date-time or numeric value could return the row it should exclude.** The
negation bound a `DateTime`/`DateTimeOffset` as culture-formatted text rather than in the
serializer's date format, so no stored value ever compared equal and every present row came
back; and it compared a numeric member without the `CAST(… as NUMERIC)` that `Equals` uses,
so a text value such as `"2"` on a numeric path excluded nothing. `Equals` and `NotEquals` now
share one type dispatch with only the operator swapped, on the scalar and list-member paths
alike. (#34)
- **A text value on a date-time path is compared as given.** Passing an already formatted
string — `Filter(FilterType.NotEquals, x => x.When, "2026-01-01T08:00:00Z")`, until now the
only way to make `NotEquals` on a date work — keeps returning the same rows. The same value
with `Equals` was compared against an empty string and matched nothing; it now matches the
rows storing that text.
- **`NotEquals` on a numeric member now treats non-numeric text as `0`,** as `Equals` already
did: both serializers write `double.NaN` as the string `"NaN"`, which `CAST(… as NUMERIC)`
reads as `0`, so `Filter(FilterType.NotEquals, x => x.Score, 0d)` no longer returns a row
storing `NaN`.
- **On a list member, `NotEquals` matches a document when *some* element differs from the
value,** not when no element equals it. That meaning is unchanged, but it is now observable
on lists whose elements hold strings or dates.
- **List-member filters raised `malformed JSON` on documents with string-valued elements.**
The `Filter(FilterType, x => x.Items, x => x.Member, value)` overload walks the list with
`JSON_TREE` and handed every node it yielded to `JSON_EACH`. `JSON_TREE` yields the scalar
Expand All @@ -13,7 +39,7 @@
predicate, so the error surfaced only for documents whose elements never match: a filter
appeared to work until the store held a row it had to reject. Only object and array nodes
are now handed to `JSON_EACH`; scalar leaves contribute nothing, and nested lists of objects
are still searched. Present since the overload was introduced.
are still searched. Present since the overload was introduced. (#35)
- **A string member whose text is JSON no longer matches as if it were an element.** A
string leaf that happened to parse — `Note = "{\"Amount\":99}"` — was searched like a list
element, so `Filter(FilterType.Equals, x => x.Items, x => x.Amount, 99)` returned a document
Expand All @@ -23,15 +49,6 @@
`Filter(FilterType.Equals, x => x.Tags, x => x, "red")` raised the same `malformed JSON`
for any document it had to reject. It now returns the documents holding the value, as the
same call over a list of numbers or booleans already did.
- **`NotEquals` on a `DateTime` list member compared against the culture-dependent
`ToString()` form.** `Filter(FilterType.NotEquals, x => x.Items, x => x.When, value)` rendered
the value as `DateTime.ToString()` (`1/1/2026 8:00:00 AM` under `en-US`) while the document
stores the serializer's format (`2026-01-01T08:00:00Z`), so no element ever compared equal and
every document with a non-empty list matched. The value is now formatted with the
serializer's `DateTimeSerializationFormat`, as `Equals` already was. `NotEquals` on a list
member matches a document when *some* element differs from the value, not when no element
equals it; that meaning is unchanged, but it is now observable on lists whose elements hold
strings or dates.

## 5.3.0 — 2026-09-02

Expand Down
82 changes: 82 additions & 0 deletions TychoDB.UnitTests/NotEqualsDateTimeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;

namespace TychoDB.UnitTests;

/// <summary>
/// <c>NotEquals</c> has to bind a date-time value in the form the serializer wrote it, exactly
/// as <c>Equals</c> does. Bound as <c>DateTime.ToString()</c> instead, the value never equals
/// any stored text, so the negation admits every row, including the one it exists to exclude.
/// </summary>
[TestClass]
public class NotEqualsDateTimeTests
{
private static readonly DateTime FirstDay = new(2026, 1, 1, 8, 0, 0, DateTimeKind.Utc);
private static readonly DateTime SecondDay = new(2026, 1, 2, 8, 0, 0, DateTimeKind.Utc);
private static readonly DateTime ThirdDay = new(2026, 1, 3, 8, 0, 0, DateTimeKind.Utc);

public static IEnumerable<object[]> Serializers
{
get
{
yield return new object[] { new SystemTextJsonSerializer(), "stj" };
yield return new object[] { new NewtonsoftJsonSerializer(), "nsj" };
}
}

[TestMethod]
[DynamicData(nameof(Serializers))]
public async Task NotEquals_OnDateTimeProperty_ExcludesTheEqualRow(IJsonSerializer jsonSerializer, string label)
{
using var db = await SeedAsync(jsonSerializer);

var ids = await ReadIdsAsync(db, FilterBuilder<Doc>.Create().Filter(FilterType.NotEquals, x => x.When, FirstDay));

ids.ShouldBe(new[] { 2, 3 }, label);
}

[TestMethod]
[DynamicData(nameof(Serializers))]
public async Task NotEquals_OnDateTimeOffsetProperty_ExcludesTheEqualRow(IJsonSerializer jsonSerializer, string label)
{
using var db = await SeedAsync(jsonSerializer);

var ids = await ReadIdsAsync(db, FilterBuilder<Doc>.Create().Filter(FilterType.NotEquals, x => x.WhenOffset, new DateTimeOffset(FirstDay)));

ids.ShouldBe(new[] { 2, 3 }, label);
}

private static async Task<Tycho> SeedAsync(IJsonSerializer jsonSerializer)
{
var db = new Tycho(Path.GetTempPath(), jsonSerializer, dbName: $"{Guid.NewGuid()}.db", rebuildCache: true, requireTypeRegistration: false);
await db.ConnectAsync();

var docs = new[]
{
new Doc { Id = 1, When = FirstDay, WhenOffset = new DateTimeOffset(FirstDay) },
new Doc { Id = 2, When = SecondDay, WhenOffset = new DateTimeOffset(SecondDay) },
new Doc { Id = 3, When = ThirdDay, WhenOffset = new DateTimeOffset(ThirdDay) },
};

await db.WriteObjectsAsync(docs, x => x.Id.ToString());

return db;
}

private static async Task<int[]> ReadIdsAsync(Tycho db, FilterBuilder<Doc> filter)
=> (await db.ReadObjectsAsync<Doc>(filter: filter)).Select(x => x.Id).OrderBy(x => x).ToArray();

public class Doc
{
public int Id { get; set; }

public DateTime When { get; set; }

public DateTimeOffset WhenOffset { get; set; }
}
}
156 changes: 156 additions & 0 deletions TychoDB.UnitTests/NotEqualsNullTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;

namespace TychoDB.UnitTests;

/// <summary>
/// <c>Filter(NotEquals, x => x.Member, null)</c> is the "member is present" test, mirroring
/// <c>Equals</c> with null as the "member is absent" test. Binding the null as a parameter renders
/// <c>&lt;&gt; NULL</c>, which is never true in SQL, so the filter silently matched nothing; the
/// scalar path has to emit <c>IS NOT NULL</c> exactly as the list-member path already does.
/// </summary>
[TestClass]
public class NotEqualsNullTests
{
public class Inner
{
public string Label { get; set; }
}

public class Doc
{
public long Id { get; set; }

public DateTime? When { get; set; }

public string Text { get; set; }

public bool? Flag { get; set; }

public Inner Nested { get; set; }
}

public static IEnumerable<object[]> Serializers
{
get
{
yield return new object[] { new NewtonsoftJsonSerializer(), "newtonsoft" };
yield return new object[] { new SystemTextJsonSerializer(), "stj" };
}
}

private static async Task<(Tycho Db, string Path)> SeedAsync(IJsonSerializer jsonSerializer)
{
var path = Path.Combine(Path.GetTempPath(), "tycho-notequals-null-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(path);

var db =
new Tycho(path, jsonSerializer, requireTypeRegistration: true)
.AddTypeRegistrationWithCustomKeySelector<Doc>(x => x.Id)
.Connect();

await db.WriteObjectsAsync(
new[]
{
new Doc { Id = 1, When = new DateTime(2026, 1, 1), Text = "a", Flag = true, Nested = new Inner { Label = "x" } },
new Doc { Id = 2, When = null, Text = null, Flag = null, Nested = null },
new Doc { Id = 3, When = new DateTime(2026, 1, 3), Text = string.Empty, Flag = false, Nested = new Inner { Label = null } },
new Doc { Id = 4, When = null, Text = "d", Flag = null, Nested = null },
});

return (db, path);
}

private static async Task<long[]> IdsAsync(Tycho db, FilterBuilder<Doc> filter)
=> (await db.ReadObjectsAsync<Doc>(filter: filter)).Select(x => x.Id).OrderBy(x => x).ToArray();

[TestMethod]
[DynamicData(nameof(Serializers))]
public async Task NotEqualsNull_OnNullableDateTime_ReturnsRowsWithAValue(IJsonSerializer jsonSerializer, string label)
{
var (db, path) = await SeedAsync(jsonSerializer);
using var scoped = db;

var present = await IdsAsync(db, FilterBuilder<Doc>.Create().Filter(FilterType.NotEquals, x => x.When, null));
var absent = await IdsAsync(db, FilterBuilder<Doc>.Create().Filter(FilterType.Equals, x => x.When, null));

present.ShouldBe(new long[] { 1, 3 }, label);
absent.ShouldBe(new long[] { 2, 4 }, label);

db.Disconnect();
Directory.Delete(path, true);
}

[TestMethod]
[DynamicData(nameof(Serializers))]
public async Task NotEqualsNull_OnString_KeepsEmptyStrings(IJsonSerializer jsonSerializer, string label)
{
var (db, path) = await SeedAsync(jsonSerializer);
using var scoped = db;

var present = await IdsAsync(db, FilterBuilder<Doc>.Create().Filter(FilterType.NotEquals, x => x.Text, null));
var presentAndNonEmpty =
await IdsAsync(
db,
FilterBuilder<Doc>.Create()
.Filter(FilterType.NotEquals, x => x.Text, null)
.And()
.Filter(FilterType.NotEquals, x => x.Text, string.Empty));

present.ShouldBe(new long[] { 1, 3, 4 }, label);
presentAndNonEmpty.ShouldBe(new long[] { 1, 4 }, label);

db.Disconnect();
Directory.Delete(path, true);
}

[TestMethod]
[DynamicData(nameof(Serializers))]
public async Task NotEqualsNull_OnNullableBool_ReturnsRowsWithAValue(IJsonSerializer jsonSerializer, string label)
{
var (db, path) = await SeedAsync(jsonSerializer);
using var scoped = db;

var present = await IdsAsync(db, FilterBuilder<Doc>.Create().Filter(FilterType.NotEquals, x => x.Flag, null));

present.ShouldBe(new long[] { 1, 3 }, label);

db.Disconnect();
Directory.Delete(path, true);
}

[TestMethod]
[DynamicData(nameof(Serializers))]
public async Task NotEqualsNull_OnNestedObject_ReturnsRowsWhereObjectIsPresent(IJsonSerializer jsonSerializer, string label)
{
var (db, path) = await SeedAsync(jsonSerializer);
using var scoped = db;

var present = await IdsAsync(db, FilterBuilder<Doc>.Create().Filter(FilterType.NotEquals, x => x.Nested, null));

present.ShouldBe(new long[] { 1, 3 }, label);

db.Disconnect();
Directory.Delete(path, true);
}

[TestMethod]
[DynamicData(nameof(Serializers))]
public async Task NotEquals_WithValue_StillExcludesNulls(IJsonSerializer jsonSerializer, string label)
{
var (db, path) = await SeedAsync(jsonSerializer);
using var scoped = db;

var notA = await IdsAsync(db, FilterBuilder<Doc>.Create().Filter(FilterType.NotEquals, x => x.Text, "a"));

notA.ShouldBe(new long[] { 3, 4 }, label);

db.Disconnect();
Directory.Delete(path, true);
}
}
Loading
Loading