Skip to content

C#: Add command batch tests. #3870

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
44 changes: 44 additions & 0 deletions csharp/tests/Valkey.Glide.IntegrationTests/BatchTestUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0

using Valkey.Glide.Pipeline;

using gs = Valkey.Glide.GlideString;

namespace Valkey.Glide.IntegrationTests;

internal class BatchTestUtils
{
public static List<(object? expected, string test)> CreateStringTest(IBatch batch, bool isAtomic)
{
List<(object? expected, string test)> testData = [];
string prefix = isAtomic ? "{stringKey}-" : "";
string key1 = $"{prefix}1-{Guid.NewGuid()}";

string value1 = $"value-1-{Guid.NewGuid()}";

_ = batch.Set(key1, value1);
testData.Add(("OK", "Set(key1, value1)"));
_ = batch.Get(key1);
testData.Add((new gs(value1), "Get(key1)"));

return testData;
}

public static TheoryData<BatchTestData> GetTestClientWithAtomic =>
[.. TestConfiguration.TestClients.SelectMany(r => new[] { true, false }.SelectMany(isAtomic =>
new BatchTestData[] {
new("String commands", r.Data, CreateStringTest, isAtomic),
}))];
}

internal delegate List<(object? expected, string test)> BatchTestDataProvider(IBatch batch, bool isAtomic);

internal record BatchTestData(string TestName, BaseClient Client, BatchTestDataProvider TestDataProvider, bool IsAtomic)
{
public string TestName = TestName;
public BaseClient Client = Client;
public BatchTestDataProvider TestDataProvider = TestDataProvider;
public bool IsAtomic = IsAtomic;

public override string? ToString() => $"{TestName} {Client} IsAtomic = {IsAtomic}";
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0

using Valkey.Glide.Pipeline;

namespace Valkey.Glide.IntegrationTests;

public class SharedCommandTests(TestConfiguration config)
Expand Down Expand Up @@ -46,4 +48,35 @@ public async Task GetReturnsEmptyString(BaseClient client)
string value = string.Empty;
await GetAndSetValues(client, key, value);
}

[Theory(DisableDiscoveryEnumeration = true)]
[MemberData(nameof(BatchTestUtils.GetTestClientWithAtomic), MemberType = typeof(BatchTestUtils))]
internal async Task BatchTest(BatchTestData testData)
{
IBatch batch = testData.Client is GlideClient ? new Batch(testData.IsAtomic) : new ClusterBatch(testData.IsAtomic);
List<(object? expected, string test)> expectedInfo = testData.TestDataProvider(batch, testData.IsAtomic);

object?[] actualResult = testData.Client switch
{
GlideClient client => (await client.Exec((Batch)batch, true))!,
GlideClusterClient client => (await client.Exec((ClusterBatch)batch, true))!,
_ => throw new NotImplementedException()
};

Assert.Equal(expectedInfo.Count, actualResult.Length);
List<string> failedChecks = [];
for (int i = 0; i < actualResult.Length; i++)
{
try
{
// TODO use assertDeepEquals
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to keep this comment or are you planning to make the change of line 73 in this PR?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do in another PR. We need to do a couple other changes before we can use assertDeepEquals

Assert.Equal(expectedInfo[i].expected, actualResult[i]);
}
catch (Exception e)
{
failedChecks.Add($"{expectedInfo[i].test} failed: {e.Message}");
}
}
Assert.Empty(failedChecks);
}
}
Loading