Skip to content

Commit

Permalink
Add the ability to mock AsyncSearch operations (#3437)
Browse files Browse the repository at this point in the history
  • Loading branch information
96malhar authored and philasmar committed Aug 26, 2024
1 parent ea25913 commit 1de686b
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 82 deletions.
60 changes: 30 additions & 30 deletions sdk/src/Services/DynamoDBv2/Custom/DataModel/AsyncSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,38 @@
* permissions and limitations under the License.
*/

using Amazon.DynamoDBv2.DataModel;
using Amazon.DynamoDBv2.DocumentModel;

namespace Amazon.DynamoDBv2.DataModel
{
/// <summary>
/// Interface retrieving search results (Query or Scan)
/// from DynamoDB.
/// </summary>
public partial interface IAsyncSearch<T>
{
/// <summary>
/// Flag that, if true, indicates that the search is done
/// </summary>
bool IsDone { get; }

/// <summary>
/// Pagination token corresponding to the item where the search operation stopped,
/// inclusive of the previous result set. Use this value to start a new
/// operation to resume search from the next item.
/// </summary>
string PaginationToken { get; }
}

/// <summary>
/// A strongly-typed object for retrieving search results (Query or Scan)
/// from DynamoDB.
/// </summary>
public partial class AsyncSearch<T>
public partial class AsyncSearch<T> : IAsyncSearch<T>
{
#region Constructor
private Search _documentSearch { get; set; }
private DynamoDBContext _sourceContext { get; set; }
private DynamoDBFlatConfig _config { get; set; }

/// <summary>
/// This constructor is used for mocking. Users that want to mock AsyncSearch can create a subclass of AsyncSearch and make a public parameterless constructor.
Expand All @@ -36,47 +56,27 @@ protected AsyncSearch()

internal AsyncSearch(DynamoDBContext source, DynamoDBContext.ContextSearch contextSearch)
{
SourceContext = source;
DocumentSearch = contextSearch.Search;
Config = contextSearch.FlatConfig;
_sourceContext = source;
_documentSearch = contextSearch.Search;
_config = contextSearch.FlatConfig;
}

#endregion

#region Private members

private Search DocumentSearch { get; set; }
private DynamoDBContext SourceContext { get; set; }
private DynamoDBFlatConfig Config { get; set; }

#endregion

#region Public properties

/// <summary>
/// Flag that, if true, indicates that the search is done
/// </summary>
/// <inheritdoc/>
public virtual bool IsDone
{
get
{
return DocumentSearch.IsDone;
return _documentSearch.IsDone;
}
}

/// <summary>
/// Pagination token corresponding to the item where the search operation stopped,
/// inclusive of the previous result set. Use this value to start a new
/// operation to resume search from the next item.
/// </summary>
/// <inheritdoc/>
public virtual string PaginationToken
{
get
{
return DocumentSearch.PaginationToken;
return _documentSearch.PaginationToken;
}
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,10 @@
using System.Threading;
using System.Threading.Tasks;

using Amazon.Runtime.Internal;
using Amazon.DynamoDBv2.DocumentModel;
using Amazon.DynamoDBv2.DataModel;

namespace Amazon.DynamoDBv2.DataModel
{
/// <summary>
/// A strongly-typed object for retrieving search results (Query or Scan)
/// from DynamoDB.
/// </summary>
/// <typeparam name="T"></typeparam>
public partial class AsyncSearch<T>
public partial interface IAsyncSearch<T>
{
#region Async public

/// <summary>
/// Initiates the asynchronous execution to get the next set of results from DynamoDB.
///
Expand All @@ -45,12 +34,7 @@ public partial class AsyncSearch<T>
/// A Task that can be used to poll or wait for results, or both.
/// Results will include the next set of result items from DynamoDB.
/// </returns>
public virtual async Task<List<T>> GetNextSetAsync(CancellationToken cancellationToken = default(CancellationToken))
{
var documents = await DocumentSearch.GetNextSetHelperAsync(cancellationToken).ConfigureAwait(false);
List<T> items = SourceContext.FromDocumentsHelper<T>(documents, this.Config).ToList();
return items;
}
Task<List<T>> GetNextSetAsync(CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Initiates the asynchronous execution to get all the remaining results from DynamoDB.
Expand All @@ -60,13 +44,25 @@ public partial class AsyncSearch<T>
/// A Task that can be used to poll or wait for results, or both.
/// Results will include the remaining result items from DynamoDB.
/// </returns>
public virtual async Task<List<T>> GetRemainingAsync(CancellationToken cancellationToken = default(CancellationToken))
Task<List<T>> GetRemainingAsync(CancellationToken cancellationToken = default(CancellationToken));
}

public partial class AsyncSearch<T> : IAsyncSearch<T>
{
/// <inheritdoc/>
public virtual async Task<List<T>> GetNextSetAsync(CancellationToken cancellationToken = default(CancellationToken))
{
var documents = await DocumentSearch.GetRemainingHelperAsync(cancellationToken).ConfigureAwait(false);
List<T> items = SourceContext.FromDocumentsHelper<T>(documents, this.Config).ToList();
var documents = await _documentSearch.GetNextSetHelperAsync(cancellationToken).ConfigureAwait(false);
List<T> items = _sourceContext.FromDocumentsHelper<T>(documents, this._config).ToList();
return items;
}

#endregion
/// <inheritdoc/>
public virtual async Task<List<T>> GetRemainingAsync(CancellationToken cancellationToken = default(CancellationToken))
{
var documents = await _documentSearch.GetRemainingHelperAsync(cancellationToken).ConfigureAwait(false);
List<T> items = _sourceContext.FromDocumentsHelper<T>(documents, this._config).ToList();
return items;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -245,29 +245,29 @@ public Task ExecuteBatchGetAsync(params IBatchGet[] batches)
#region Scan async

/// <inheritdoc/>
public AsyncSearch<T> ScanAsync<T>(IEnumerable<ScanCondition> conditions)
public IAsyncSearch<T> ScanAsync<T>(IEnumerable<ScanCondition> conditions)
{
var scan = ConvertScan<T>(conditions, null);
return FromSearchAsync<T>(scan);
}

/// <inheritdoc/>
[Obsolete("Use the ScanAsync overload that takes ScanConfig instead, since DynamoDBOperationConfig contains properties that are not applicable to ScanAsync.")]
public AsyncSearch<T> ScanAsync<T>(IEnumerable<ScanCondition> conditions, DynamoDBOperationConfig operationConfig = null)
public IAsyncSearch<T> ScanAsync<T>(IEnumerable<ScanCondition> conditions, DynamoDBOperationConfig operationConfig = null)
{
var scan = ConvertScan<T>(conditions, operationConfig);
return FromSearchAsync<T>(scan);
}

/// <inheritdoc/>
public AsyncSearch<T> ScanAsync<T>(IEnumerable<ScanCondition> conditions, ScanConfig scanConfig)
public IAsyncSearch<T> ScanAsync<T>(IEnumerable<ScanCondition> conditions, ScanConfig scanConfig)
{
var scan = ConvertScan<T>(conditions, scanConfig?.ToDynamoDBOperationConfig());
return FromSearchAsync<T>(scan);
}

/// <inheritdoc/>
public AsyncSearch<T> FromScanAsync<T>(ScanOperationConfig scanConfig)
public IAsyncSearch<T> FromScanAsync<T>(ScanOperationConfig scanConfig)
{
if (scanConfig == null) throw new ArgumentNullException("scanConfig");

Expand All @@ -277,7 +277,7 @@ public AsyncSearch<T> FromScanAsync<T>(ScanOperationConfig scanConfig)

/// <inheritdoc/>
[Obsolete("Use the FromScanAsync overload that takes ScanConfig instead, since DynamoDBOperationConfig contains properties that are not applicable to FromScanAsync.")]
public AsyncSearch<T> FromScanAsync<T>(ScanOperationConfig scanConfig, DynamoDBOperationConfig operationConfig = null)
public IAsyncSearch<T> FromScanAsync<T>(ScanOperationConfig scanConfig, DynamoDBOperationConfig operationConfig = null)
{
if (scanConfig == null) throw new ArgumentNullException("scanConfig");

Expand All @@ -286,7 +286,7 @@ public AsyncSearch<T> FromScanAsync<T>(ScanOperationConfig scanConfig, DynamoDBO
}

/// <inheritdoc/>
public AsyncSearch<T> FromScanAsync<T>(ScanOperationConfig scanConfig, FromScanConfig fromScanConfig)
public IAsyncSearch<T> FromScanAsync<T>(ScanOperationConfig scanConfig, FromScanConfig fromScanConfig)
{
if (scanConfig == null) throw new ArgumentNullException("scanConfig");

Expand All @@ -299,29 +299,29 @@ public AsyncSearch<T> FromScanAsync<T>(ScanOperationConfig scanConfig, FromScanC
#region Query async

/// <inheritdoc/>
public AsyncSearch<T> QueryAsync<T>(object hashKeyValue)
public IAsyncSearch<T> QueryAsync<T>(object hashKeyValue)
{
var query = ConvertQueryByValue<T>(hashKeyValue, null, null);
return FromSearchAsync<T>(query);
}

/// <inheritdoc/>
[Obsolete("Use the QueryAsync overload that takes QueryConfig instead, since DynamoDBOperationConfig contains properties that are not applicable to QueryAsync.")]
public AsyncSearch<T> QueryAsync<T>(object hashKeyValue, DynamoDBOperationConfig operationConfig = null)
public IAsyncSearch<T> QueryAsync<T>(object hashKeyValue, DynamoDBOperationConfig operationConfig = null)
{
var query = ConvertQueryByValue<T>(hashKeyValue, null, operationConfig);
return FromSearchAsync<T>(query);
}

/// <inheritdoc/>
public AsyncSearch<T> QueryAsync<T>(object hashKeyValue, QueryConfig queryConfig)
public IAsyncSearch<T> QueryAsync<T>(object hashKeyValue, QueryConfig queryConfig)
{
var query = ConvertQueryByValue<T>(hashKeyValue, null, queryConfig?.ToDynamoDBOperationConfig());
return FromSearchAsync<T>(query);
}

/// <inheritdoc/>
public AsyncSearch<T> QueryAsync<T>(object hashKeyValue, QueryOperator op, IEnumerable<object> values)
public IAsyncSearch<T> QueryAsync<T>(object hashKeyValue, QueryOperator op, IEnumerable<object> values)
{
if (values == null)
throw new ArgumentNullException("values");
Expand All @@ -332,7 +332,7 @@ public AsyncSearch<T> QueryAsync<T>(object hashKeyValue, QueryOperator op, IEnum

/// <inheritdoc/>
[Obsolete("Use the QueryAsync overload that takes QueryConfig instead, since DynamoDBOperationConfig contains properties that are not applicable to QueryAsync.")]
public AsyncSearch<T> QueryAsync<T>(object hashKeyValue, QueryOperator op, IEnumerable<object> values, DynamoDBOperationConfig operationConfig = null)
public IAsyncSearch<T> QueryAsync<T>(object hashKeyValue, QueryOperator op, IEnumerable<object> values, DynamoDBOperationConfig operationConfig = null)
{
if (values == null)
throw new ArgumentNullException("values");
Expand All @@ -342,7 +342,7 @@ public AsyncSearch<T> QueryAsync<T>(object hashKeyValue, QueryOperator op, IEnum
}

/// <inheritdoc/>
public AsyncSearch<T> QueryAsync<T>(object hashKeyValue, QueryOperator op, IEnumerable<object> values, QueryConfig queryConfig)
public IAsyncSearch<T> QueryAsync<T>(object hashKeyValue, QueryOperator op, IEnumerable<object> values, QueryConfig queryConfig)
{
if (values == null)
throw new ArgumentNullException("values");
Expand All @@ -352,7 +352,7 @@ public AsyncSearch<T> QueryAsync<T>(object hashKeyValue, QueryOperator op, IEnum
}

/// <inheritdoc/>
public AsyncSearch<T> FromQueryAsync<T>(QueryOperationConfig queryConfig)
public IAsyncSearch<T> FromQueryAsync<T>(QueryOperationConfig queryConfig)
{
if (queryConfig == null) throw new ArgumentNullException("queryConfig");

Expand All @@ -362,7 +362,7 @@ public AsyncSearch<T> FromQueryAsync<T>(QueryOperationConfig queryConfig)

/// <inheritdoc/>
[Obsolete("Use the FromQueryAsync overload that takes QueryConfig instead, since DynamoDBOperationConfig contains properties that are not applicable to FromQueryAsync.")]
public AsyncSearch<T> FromQueryAsync<T>(QueryOperationConfig queryConfig, DynamoDBOperationConfig operationConfig = null)
public IAsyncSearch<T> FromQueryAsync<T>(QueryOperationConfig queryConfig, DynamoDBOperationConfig operationConfig = null)
{
if (queryConfig == null) throw new ArgumentNullException("queryConfig");

Expand All @@ -371,7 +371,7 @@ public AsyncSearch<T> FromQueryAsync<T>(QueryOperationConfig queryConfig, Dynamo
}

/// <inheritdoc/>
public AsyncSearch<T> FromQueryAsync<T>(QueryOperationConfig queryConfig, FromQueryConfig fromQueryConfig)
public IAsyncSearch<T> FromQueryAsync<T>(QueryOperationConfig queryConfig, FromQueryConfig fromQueryConfig)
{
if (queryConfig == null) throw new ArgumentNullException("queryConfig");

Expand Down
Loading

0 comments on commit 1de686b

Please sign in to comment.