Skip to content

Commit

Permalink
chore: add better logging for dictionary tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rishtigupta committed Sep 6, 2024
1 parent 8f9eb8f commit e8f7479
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 deletions tests/Integration/Momento.Sdk.Tests/Cache/DictionaryTest.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Momento.Sdk.Internal.ExtensionMethods;
using Momento.Sdk.Requests;
using Xunit.Abstractions;

namespace Momento.Sdk.Tests.Integration.Cache;

[Collection("CacheClient")]
public class DictionaryTest : TestBase
{
public DictionaryTest(CacheClientFixture fixture) : base(fixture)
private readonly ITestOutputHelper _output;

public DictionaryTest(CacheClientFixture fixture, ITestOutputHelper output) : base(fixture)
{
_output = output;
}

[Theory]
Expand Down Expand Up @@ -913,18 +918,36 @@ public async Task DictionaryFetchAsync_HasContentByteArrayByteArray_HappyPath()
Assert.True(fetchResponse is CacheDictionaryFetchResponse.Hit, $"Unexpected response: {fetchResponse}");

var hitResponse = (CacheDictionaryFetchResponse.Hit)fetchResponse;
// Exercise byte array dictionary structural equality comparer
Assert.True(hitResponse.ValueDictionaryByteArrayByteArray!.ContainsKey(field1), $"Could not find key {field1} in dictionary byte array: {hitResponse.ValueDictionaryByteArrayByteArray!}");
Assert.True(hitResponse.ValueDictionaryByteArrayByteArray!.ContainsKey(field2), $"Could not find key {field2} in dictionary byte array: {hitResponse.ValueDictionaryByteArrayByteArray!}");
Assert.Equal(2, hitResponse.ValueDictionaryByteArrayByteArray!.Count);

// Print dictionary content to output for debugging
_output.WriteLine("Hit Response Dictionary Contents:");
foreach (var kvp in hitResponse.ValueDictionaryByteArrayByteArray!)
{
_output.WriteLine($"Key: {BitConverter.ToString(kvp.Key)} | Value: {BitConverter.ToString(kvp.Value)}");
}

// Log if specific keys are missing
if (!hitResponse.ValueDictionaryByteArrayByteArray!.ContainsKey(field1))
{
_output.WriteLine($"Warning: Key {BitConverter.ToString(field1)} was not found in the dictionary.");
}

if (!hitResponse.ValueDictionaryByteArrayByteArray!.ContainsKey(field2))
{
_output.WriteLine($"Warning: Key {BitConverter.ToString(field2)} was not found in the dictionary.");
}

// Exercise DictionaryEquals extension
Assert.True(hitResponse.ValueDictionaryByteArrayByteArray!.DictionaryEquals(contentDictionary), $"Expected DictionaryEquals to return true for these dictionaries: {hitResponse.ValueDictionaryByteArrayByteArray!} AND {contentDictionary}");
var actualDictionaryString = DictionaryToString(hitResponse.ValueDictionaryByteArrayByteArray!);
var expectedDictionaryString = DictionaryToString(contentDictionary);
Assert.True(hitResponse.ValueDictionaryByteArrayByteArray!.DictionaryEquals(contentDictionary),
$"Dictionary contents do not match. Actual dictionary: {actualDictionaryString} | Expected dictionary: {expectedDictionaryString}");

// Test field caching behavior
Assert.Same(hitResponse.ValueDictionaryByteArrayByteArray, hitResponse.ValueDictionaryByteArrayByteArray);
}

[Fact]
public async Task DictionaryDeleteAsync_DictionaryDoesNotExist_Noop()
{
Expand Down Expand Up @@ -1161,4 +1184,14 @@ public async Task DictionaryLengthAsync_HappyPath()
var hitResponse = (CacheDictionaryLengthResponse.Hit)lengthResponse;
Assert.Equal(1, hitResponse.Length);
}

private string DictionaryToString(IDictionary<byte[], byte[]> dictionary)
{
var sb = new StringBuilder();
foreach (var kvp in dictionary)
{
sb.AppendLine($"Key: {BitConverter.ToString(kvp.Key)} | Value: {BitConverter.ToString(kvp.Value)}");
}
return sb.ToString();
}
}

0 comments on commit e8f7479

Please sign in to comment.