Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add better logging for dictionary tests #569

Merged
merged 1 commit into from
Sep 6, 2024
Merged
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
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:");
rishtigupta marked this conversation as resolved.
Show resolved Hide resolved
foreach (var kvp in hitResponse.ValueDictionaryByteArrayByteArray!)
{
_output.WriteLine($"Key: {BitConverter.ToString(kvp.Key)} | Value: {BitConverter.ToString(kvp.Value)}");
}

// Log if specific keys are missing
rishtigupta marked this conversation as resolved.
Show resolved Hide resolved
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)}");
rishtigupta marked this conversation as resolved.
Show resolved Hide resolved
}
return sb.ToString();
}
}
Loading