Path: redb/redb.Tests.Integration/Tests/Unit/RedbKeySerializerValueTupleTests.cs Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Tests.Integration/Tests/Unit/RedbKeySerializerValueTupleTests.cs ---- using System.Text; using redb.Core.Utils; namespace redb.Tests.Integration.Tests.Unit; /// /// A dictionary key that is a ValueTuple of eight or more fields. Such a tuple keeps its fields from the eighth on in a /// nested ValueTuple in the field Rest. The key text is the JSON of the tuple's fields: two keys that differ only from /// the eighth field on must give two texts, and the text must read back to the same key. No database required. /// public class RedbKeySerializerValueTupleTests { private static string Text(string json) => Convert.ToBase64String(Encoding.UTF8.GetBytes(json)); [Fact] public void EightFieldKeys_DifferingInTheEighthField_GiveDifferentTexts() { var first = (1, 2, 3, 4, 5, 6, 7, 8); var second = (1, 2, 3, 4, 5, 6, 7, 9); RedbKeySerializer.Serialize(first).Should().NotBe(RedbKeySerializer.Serialize(second), "the two keys differ in their eighth field"); } [Fact] public void EightFieldKey_ReadsBackAsWritten() { var key = (1, 2, 3, 4, 5, 6, 7, 8); RedbKeySerializer.Deserialize<(int, int, int, int, int, int, int, int)>(RedbKeySerializer.Serialize(key)) .Should().Be(key); } [Fact] public void EightFieldKey_WritesRestAsTheObjectOfItsOwnFields() { RedbKeySerializer.Serialize((1, 2, 3, 4, 5, 6, 7, 8)).Should().Be( Text("{\"Item1\":1,\"Item2\":2,\"Item3\":3,\"Item4\":4,\"Item5\":5,\"Item6\":6,\"Item7\":7,\"Rest\":{\"Item1\":8}}")); } [Fact] public void NineFieldKey_OfMixedTypes_ReadsBackAsWritten() { var key = (1, "two", 3L, 4.5m, true, 'f', (short)7, Guid.Parse("8c0e7e0f-1d7a-4c56-9f6c-3b1a2c4d5e6f"), "nine"); RedbKeySerializer.Deserialize<(int, string, long, decimal, bool, char, short, Guid, string)>( RedbKeySerializer.Serialize(key)) .Should().Be(key); } [Fact] public void SixteenFieldKey_RestInsideRest_ReadsBackAsWritten_AndTheLastFieldCounts() { var key = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16); var other = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17); RedbKeySerializer.Deserialize<(int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int)>( RedbKeySerializer.Serialize(key)) .Should().Be(key); RedbKeySerializer.Serialize(key).Should().NotBe(RedbKeySerializer.Serialize(other), "the sixteenth field sits in the Rest of the Rest and must reach the text"); } [Fact] public void SevenFieldKey_ReadsBackAsWritten() { var key = (1, 2, 3, 4, 5, 6, 7); RedbKeySerializer.Deserialize<(int, int, int, int, int, int, int)>(RedbKeySerializer.Serialize(key)) .Should().Be(key); } [Fact] public void KeysOfUpToSevenFields_GiveTheTextTheyAlwaysGave() { RedbKeySerializer.Serialize((1, "a")).Should().Be(Text("{\"Item1\":1,\"Item2\":\"a\"}")); RedbKeySerializer.Serialize((1, 2, 3, 4, 5, 6, 7)).Should().Be( Text("{\"Item1\":1,\"Item2\":2,\"Item3\":3,\"Item4\":4,\"Item5\":5,\"Item6\":6,\"Item7\":7}")); } [Fact] public void StoredEightFieldKey_WithEmptyRest_ReadsAsBefore_TailAtDefault() { var stored = Text("{\"Item1\":1,\"Item2\":2,\"Item3\":3,\"Item4\":4,\"Item5\":5,\"Item6\":6,\"Item7\":7,\"Rest\":{}}"); RedbKeySerializer.Deserialize<(int, int, int, int, int, int, int, int)>(stored) .Should().Be((1, 2, 3, 4, 5, 6, 7, 0)); } // A ValueTuple nested in a field of the key, not as Rest: System.Text.Json writes no fields, so the nested tuple // must be taken apart the same way as the key itself. [Fact] public void NestedTupleKeys_DifferingInTheNestedTuple_GiveDifferentTexts() { RedbKeySerializer.Serialize(((1, 2), 3)).Should().NotBe(RedbKeySerializer.Serialize(((9, 9), 3)), "the two keys differ in their nested tuple"); } [Fact] public void NestedTupleKey_ReadsBackAsWritten() { var key = ((1, "a"), 3, (4, (5L, 6))); RedbKeySerializer.Deserialize<((int, string), int, (int, (long, int)))>(RedbKeySerializer.Serialize(key)) .Should().Be(key); } [Fact] public void NestedTupleKey_WritesTheNestedTupleAsTheObjectOfItsFields() { RedbKeySerializer.Serialize(((1, 2), 3)).Should().Be( Text("{\"Item1\":{\"Item1\":1,\"Item2\":2},\"Item2\":3}")); } [Fact] public void StoredNestedTupleKey_WithEmptyNestedPart_ReadsAsBefore_Defaults() { var stored = Text("{\"Item1\":{},\"Item2\":3}"); RedbKeySerializer.Deserialize<((int, int), int)>(stored).Should().Be(((0, 0), 3)); } // Tuple<> is a class with properties; System.Text.Json writes and reads it. A ValueTuple inside it is not. [Fact] public void ReferenceTupleKey_ReadsBackAsWritten_EightItemsIncluded() { var pair = Tuple.Create(1, "a"); var eight = Tuple.Create(1, 2, 3, 4, 5, 6, 7, 8); RedbKeySerializer.Deserialize>(RedbKeySerializer.Serialize(pair)).Should().Be(pair); RedbKeySerializer.Deserialize>>(RedbKeySerializer.Serialize(eight)) .Should().Be(eight); RedbKeySerializer.Serialize(eight).Should().NotBe(RedbKeySerializer.Serialize(Tuple.Create(1, 2, 3, 4, 5, 6, 7, 9))); } [Fact] public void ReferenceTupleKey_WithANestedValueTuple_KeepsIt() { var key = Tuple.Create((1, 2), 3); RedbKeySerializer.Serialize(key).Should().NotBe(RedbKeySerializer.Serialize(Tuple.Create((9, 9), 3))); RedbKeySerializer.Deserialize>(RedbKeySerializer.Serialize(key)).Should().Be(key); } public sealed record KeyWithTuple(string Name, (int, int) Pair); public sealed record PlainKey(int A, string B); [Fact] public void ClassKey_WithAValueTupleProperty_KeepsIt() { var key = new KeyWithTuple("k", (1, 2)); RedbKeySerializer.Serialize(key).Should().NotBe(RedbKeySerializer.Serialize(new KeyWithTuple("k", (9, 9)))); RedbKeySerializer.Deserialize(RedbKeySerializer.Serialize(key)).Should().Be(key); } [Fact] public void KeysWithNoNestedTuple_GiveTheTextTheyAlwaysGave() { RedbKeySerializer.Serialize(new PlainKey(1, "x")).Should().Be(Text("{\"A\":1,\"B\":\"x\"}")); RedbKeySerializer.Serialize(Tuple.Create(1, "a")).Should().Be(Text("{\"Item1\":1,\"Item2\":\"a\"}")); } [Fact] public void DictionaryHash_FollowsTheKeyText_EightFieldKeysDifferingInTheEighthField() { var first = new Dictionary<(int, int, int, int, int, int, int, int), string> { [(1, 2, 3, 4, 5, 6, 7, 8)] = "v" }; var second = new Dictionary<(int, int, int, int, int, int, int, int), string> { [(1, 2, 3, 4, 5, 6, 7, 9)] = "v" }; var firstHash = RedbHash.ComputeForProps(first); var secondHash = RedbHash.ComputeForProps(second); firstHash.Should().NotBeNull(); (firstHash == secondHash).Should().BeFalse("the two keys differ in their eighth field"); } }