|
| 1 | +using KeyLoad.Storage; |
| 2 | + |
| 3 | +namespace KeyLoad.FunctionalTests.StorageRecovery; |
| 4 | + |
| 5 | +internal sealed class KeySpacePartitionEncodingTests |
| 6 | +{ |
| 7 | + private const string Space = "document"; |
| 8 | + private const string PartitionKey = "Case"; |
| 9 | + private const int MaximumComponents = 256; |
| 10 | + private const int ScopeComponents = 5; |
| 11 | + private const string LiteralKey = "0150646F63756D656E7400005074656E616E740000506461746162617365000050646F6D61696E000050436173650000506974656D73000050494400FFC3A90000"; |
| 12 | + private static readonly PartitionRef Partition = new("tenant", "database", "domain", PartitionKey); |
| 13 | + |
| 14 | + [Test] |
| 15 | + public async Task PartitionScopeKeepsLiteralBytesCaseEscapesAndEveryDynamicComponent() |
| 16 | + { |
| 17 | + await Assert.That(KeySpace.Partition(Space, Partition, "items", "ID\0é") |
| 18 | + .SequenceEqual(Convert.FromHexString(LiteralKey))).IsTrue(); |
| 19 | + object?[] suffix = ["items", long.MinValue, 1.25m, double.MaxValue, DateTimeOffset.MaxValue, |
| 20 | + Guid.Parse("01234567-89ab-cdef-0123-456789abcdef"), true, null, MissingValue.Instance, new byte[] { 0, 255 }]; |
| 21 | + object?[] complete = [Space, "tenant", "database", "domain", PartitionKey, .. suffix]; |
| 22 | + var actual = KeySpace.Partition(Space, Partition, suffix); |
| 23 | + await Assert.That(actual.SequenceEqual(KeyCodec.Encode(complete))).IsTrue(); |
| 24 | + KeyCodecValidation.Validate(actual); |
| 25 | + await Assert.That(KeyCodec.Decode(actual).Length).IsEqualTo(ScopeComponents + suffix.Length); |
| 26 | + } |
| 27 | + |
| 28 | + [Test] |
| 29 | + public async Task ScopeAndSuffixShareOneComponentLimitAndPreserveOriginalRefusals() |
| 30 | + { |
| 31 | + object?[] suffix = [.. Enumerable.Repeat<object?>(MissingValue.Instance, MaximumComponents - ScopeComponents)]; |
| 32 | + var maximum = KeySpace.Partition(Space, Partition, suffix); |
| 33 | + await Assert.That(KeyCodec.Decode(maximum).Length).IsEqualTo(MaximumComponents); |
| 34 | + var excessive = Assert.ThrowsExactly<KeyLoadException>(() => KeySpace.Partition(Space, Partition, [.. suffix, null])); |
| 35 | + await Assert.That(excessive.Code).IsEqualTo(ErrorCode.ResourceExhausted); |
| 36 | + var unsupported = Assert.ThrowsExactly<KeyLoadException>(() => KeySpace.Partition(Space, Partition, new object())); |
| 37 | + await Assert.That(unsupported.Code).IsEqualTo(ErrorCode.UnsupportedCapability); |
| 38 | + _ = Assert.ThrowsExactly<ArgumentNullException>(() => KeySpace.Partition(Space, null!, suffix)); |
| 39 | + _ = Assert.ThrowsExactly<ArgumentNullException>(() => KeySpace.Partition(Space, Partition, (object?[])null!)); |
| 40 | + await Assert.That(KeySpace.Partition(Space, Partition, "items", "ID\0é") |
| 41 | + .SequenceEqual(Convert.FromHexString(LiteralKey))).IsTrue(); |
| 42 | + } |
| 43 | +} |
0 commit comments