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

Added ListAddToLeftAsync overload method. #322

Merged
merged 1 commit into from
Sep 1, 2020
Merged
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
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using StackExchange.Redis;
using StackExchange.Redis.Extensions.Core.Models;
using System.Threading.Tasks;

namespace StackExchange.Redis.Extensions.Core.Abstractions
{
Expand All @@ -22,6 +18,16 @@ public partial interface IRedisDatabase
Task<long> ListAddToLeftAsync<T>(string key, T item, When when = When.Always, CommandFlags flag = CommandFlags.None)
where T : class;

/// <summary>
/// Lists the add to left asynchronous.
/// </summary>
/// <typeparam name="T">The type of the expected object.</typeparam>
/// <param name="key">The key.</param>
/// <param name="items">The items.</param>
/// <param name="flag">Behaviour markers associated with a given command</param>
Task<long> ListAddToLeftAsync<T>(string key, T[] items, CommandFlags flag = CommandFlags.None)
where T : class;

/// <summary>
/// Removes and returns the last element of the list stored at key.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Linq;
using System.Threading.Tasks;

using StackExchange.Redis.Extensions.Core.Abstractions;

namespace StackExchange.Redis.Extensions.Core.Implementations
Expand All @@ -21,6 +23,21 @@ public Task<long> ListAddToLeftAsync<T>(string key, T item, When when = When.Alw
return Database.ListLeftPushAsync(key, serializedItem, when, flags);
}

/// <inheritdoc/>
public Task<long> ListAddToLeftAsync<T>(string key, T[] items, CommandFlags flags = CommandFlags.None)
where T : class
{
if (string.IsNullOrEmpty(key))
throw new ArgumentException("key cannot be empty.", nameof(key));

if (items == null)
throw new ArgumentNullException(nameof(items), "item cannot be null.");

var serializedItems = items.Select(x => (RedisValue)Serializer.Serialize(x)).ToArray();

return Database.ListLeftPushAsync(key, serializedItems, flags);
}

/// <inheritdoc/>
public async Task<T> ListGetFromRightAsync<T>(string key, CommandFlags flags = CommandFlags.None)
where T : class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -768,10 +768,22 @@ public async Task ListAddToLeftGenericShouldThrowExceptionWhenKeyIsEmpty()
await Assert.ThrowsAsync<ArgumentException>(() => Sut.GetDbFromConfiguration().ListAddToLeftAsync(string.Empty, string.Empty));
}

[Fact]
public async Task ListAddToLeftArrayShouldThrowExceptionWhenKeyIsEmpty()
{
await Assert.ThrowsAsync<ArgumentException>(() => Sut.GetDbFromConfiguration().ListAddToLeftAsync(string.Empty, items: Array.Empty<TestClass<string>>()));
}

[Fact]
public async Task ListAddToLeftGenericShouldThrowExceptionWhenItemIsNull()
{
await Assert.ThrowsAsync<ArgumentNullException>(() => Sut.GetDbFromConfiguration().ListAddToLeftAsync<string>("MyList", null));
await Assert.ThrowsAsync<ArgumentNullException>(() => Sut.GetDbFromConfiguration().ListAddToLeftAsync<string>("MyList", item: null));
}

[Fact]
public async Task ListAddToLeftGenericShouldThrowExceptionWhenItemsIsNull()
{
await Assert.ThrowsAsync<ArgumentNullException>(() => Sut.GetDbFromConfiguration().ListAddToLeftAsync<string>("MyList", items: null));
}

[Fact]
Expand All @@ -791,18 +803,46 @@ public async Task ListAddToLeftGeneric_With_An_Existing_Key_Should_Return_Valid_
Assert.Equal(keys.Length, values.Count);
}

[Fact]
public async Task ListAddToLeftArray_With_An_Existing_Key_Should_Return_Valid_Data()
{
var values = Range(0, 5000).Select(_ => new TestClass<string>(Guid.NewGuid().ToString(), Guid.NewGuid().ToString())).ToArray();

const string key = "MyList";

await Sut.GetDbFromConfiguration().ListAddToLeftAsync(key, items: values);

var keys = await db.ListRangeAsync(key);

Assert.Equal(keys.Length, values.Length);
}

[Fact]
public async Task ListAddToLeftAsyncGenericShouldThrowExceptionWhenKeyIsEmpty()
{
await Assert.ThrowsAsync<ArgumentException>(
() => Sut.GetDbFromConfiguration().ListAddToLeftAsync(string.Empty, string.Empty));
}

[Fact]
public async Task ListAddToLeftAsyncArrayShouldThrowExceptionWhenKeyIsEmpty()
{
await Assert.ThrowsAsync<ArgumentException>(
() => Sut.GetDbFromConfiguration().ListAddToLeftAsync(string.Empty, items: Array.Empty<TestClass<string>>()));
}

[Fact]
public async Task ListAddToLeftAsyncGenericShouldThrowExceptionWhenItemIsNull()
{
await Assert.ThrowsAsync<ArgumentNullException>(
() => Sut.GetDbFromConfiguration().ListAddToLeftAsync<string>("MyList", null));
() => Sut.GetDbFromConfiguration().ListAddToLeftAsync<string>("MyList", item: null));
}

[Fact]
public async Task ListAddToLeftAsyncGenericShouldThrowExceptionWhenItemsIsNull()
{
await Assert.ThrowsAsync<ArgumentNullException>(
() => Sut.GetDbFromConfiguration().ListAddToLeftAsync<string>("MyList", items: null));
}

[Fact]
Expand All @@ -823,6 +863,20 @@ public async Task ListAddToLeftAsyncGeneric_With_An_Existing_Key_Should_Return_V
Assert.Equal(keys.Length, values.Count);
}

[Fact]
public async Task ListAddToLeftAsyncArray_With_An_Existing_Key_Should_Return_Valid_Data()
{
var values = Range(0, 5000).Select(_ => new TestClass<string>(Guid.NewGuid().ToString(), Guid.NewGuid().ToString())).ToArray();

const string key = "MyListAsync";

await Sut.GetDbFromConfiguration().ListAddToLeftAsync(key, items: values);

var keys = await db.ListRangeAsync(key);

Assert.Equal(keys.Length, values.Length);
}

[Fact]
public async Task ListGetFromRightGenericShouldThrowExceptionWhenKeyIsEmpty()
{
Expand Down