Skip to content

Commit d192d2b

Browse files
authored
Add the ability to mock AsyncSearch operations (#3437)
1 parent 55285b6 commit d192d2b

File tree

5 files changed

+171
-82
lines changed

5 files changed

+171
-82
lines changed

sdk/src/Services/DynamoDBv2/Custom/DataModel/AsyncSearch.cs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,38 @@
1313
* permissions and limitations under the License.
1414
*/
1515

16-
using Amazon.DynamoDBv2.DataModel;
1716
using Amazon.DynamoDBv2.DocumentModel;
1817

1918
namespace Amazon.DynamoDBv2.DataModel
2019
{
20+
/// <summary>
21+
/// Interface retrieving search results (Query or Scan)
22+
/// from DynamoDB.
23+
/// </summary>
24+
public partial interface IAsyncSearch<T>
25+
{
26+
/// <summary>
27+
/// Flag that, if true, indicates that the search is done
28+
/// </summary>
29+
bool IsDone { get; }
30+
31+
/// <summary>
32+
/// Pagination token corresponding to the item where the search operation stopped,
33+
/// inclusive of the previous result set. Use this value to start a new
34+
/// operation to resume search from the next item.
35+
/// </summary>
36+
string PaginationToken { get; }
37+
}
38+
2139
/// <summary>
2240
/// A strongly-typed object for retrieving search results (Query or Scan)
2341
/// from DynamoDB.
2442
/// </summary>
25-
public partial class AsyncSearch<T>
43+
public partial class AsyncSearch<T> : IAsyncSearch<T>
2644
{
27-
#region Constructor
45+
private Search _documentSearch { get; set; }
46+
private DynamoDBContext _sourceContext { get; set; }
47+
private DynamoDBFlatConfig _config { get; set; }
2848

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

3757
internal AsyncSearch(DynamoDBContext source, DynamoDBContext.ContextSearch contextSearch)
3858
{
39-
SourceContext = source;
40-
DocumentSearch = contextSearch.Search;
41-
Config = contextSearch.FlatConfig;
59+
_sourceContext = source;
60+
_documentSearch = contextSearch.Search;
61+
_config = contextSearch.FlatConfig;
4262
}
4363

44-
#endregion
45-
46-
#region Private members
47-
48-
private Search DocumentSearch { get; set; }
49-
private DynamoDBContext SourceContext { get; set; }
50-
private DynamoDBFlatConfig Config { get; set; }
51-
52-
#endregion
53-
54-
#region Public properties
55-
56-
/// <summary>
57-
/// Flag that, if true, indicates that the search is done
58-
/// </summary>
64+
/// <inheritdoc/>
5965
public virtual bool IsDone
6066
{
6167
get
6268
{
63-
return DocumentSearch.IsDone;
69+
return _documentSearch.IsDone;
6470
}
6571
}
6672

67-
/// <summary>
68-
/// Pagination token corresponding to the item where the search operation stopped,
69-
/// inclusive of the previous result set. Use this value to start a new
70-
/// operation to resume search from the next item.
71-
/// </summary>
73+
/// <inheritdoc/>
7274
public virtual string PaginationToken
7375
{
7476
get
7577
{
76-
return DocumentSearch.PaginationToken;
78+
return _documentSearch.PaginationToken;
7779
}
7880
}
79-
80-
#endregion
8181
}
8282
}

sdk/src/Services/DynamoDBv2/Custom/DataModel/_async/AsyncSearch.Async.cs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,10 @@
1818
using System.Threading;
1919
using System.Threading.Tasks;
2020

21-
using Amazon.Runtime.Internal;
22-
using Amazon.DynamoDBv2.DocumentModel;
23-
using Amazon.DynamoDBv2.DataModel;
24-
2521
namespace Amazon.DynamoDBv2.DataModel
2622
{
27-
/// <summary>
28-
/// A strongly-typed object for retrieving search results (Query or Scan)
29-
/// from DynamoDB.
30-
/// </summary>
31-
/// <typeparam name="T"></typeparam>
32-
public partial class AsyncSearch<T>
23+
public partial interface IAsyncSearch<T>
3324
{
34-
#region Async public
35-
3625
/// <summary>
3726
/// Initiates the asynchronous execution to get the next set of results from DynamoDB.
3827
///
@@ -45,12 +34,7 @@ public partial class AsyncSearch<T>
4534
/// A Task that can be used to poll or wait for results, or both.
4635
/// Results will include the next set of result items from DynamoDB.
4736
/// </returns>
48-
public virtual async Task<List<T>> GetNextSetAsync(CancellationToken cancellationToken = default(CancellationToken))
49-
{
50-
var documents = await DocumentSearch.GetNextSetHelperAsync(cancellationToken).ConfigureAwait(false);
51-
List<T> items = SourceContext.FromDocumentsHelper<T>(documents, this.Config).ToList();
52-
return items;
53-
}
37+
Task<List<T>> GetNextSetAsync(CancellationToken cancellationToken = default(CancellationToken));
5438

5539
/// <summary>
5640
/// Initiates the asynchronous execution to get all the remaining results from DynamoDB.
@@ -60,13 +44,25 @@ public partial class AsyncSearch<T>
6044
/// A Task that can be used to poll or wait for results, or both.
6145
/// Results will include the remaining result items from DynamoDB.
6246
/// </returns>
63-
public virtual async Task<List<T>> GetRemainingAsync(CancellationToken cancellationToken = default(CancellationToken))
47+
Task<List<T>> GetRemainingAsync(CancellationToken cancellationToken = default(CancellationToken));
48+
}
49+
50+
public partial class AsyncSearch<T> : IAsyncSearch<T>
51+
{
52+
/// <inheritdoc/>
53+
public virtual async Task<List<T>> GetNextSetAsync(CancellationToken cancellationToken = default(CancellationToken))
6454
{
65-
var documents = await DocumentSearch.GetRemainingHelperAsync(cancellationToken).ConfigureAwait(false);
66-
List<T> items = SourceContext.FromDocumentsHelper<T>(documents, this.Config).ToList();
55+
var documents = await _documentSearch.GetNextSetHelperAsync(cancellationToken).ConfigureAwait(false);
56+
List<T> items = _sourceContext.FromDocumentsHelper<T>(documents, this._config).ToList();
6757
return items;
6858
}
6959

70-
#endregion
60+
/// <inheritdoc/>
61+
public virtual async Task<List<T>> GetRemainingAsync(CancellationToken cancellationToken = default(CancellationToken))
62+
{
63+
var documents = await _documentSearch.GetRemainingHelperAsync(cancellationToken).ConfigureAwait(false);
64+
List<T> items = _sourceContext.FromDocumentsHelper<T>(documents, this._config).ToList();
65+
return items;
66+
}
7167
}
7268
}

sdk/src/Services/DynamoDBv2/Custom/DataModel/_async/Context.Async.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -245,29 +245,29 @@ public Task ExecuteBatchGetAsync(params IBatchGet[] batches)
245245
#region Scan async
246246

247247
/// <inheritdoc/>
248-
public AsyncSearch<T> ScanAsync<T>(IEnumerable<ScanCondition> conditions)
248+
public IAsyncSearch<T> ScanAsync<T>(IEnumerable<ScanCondition> conditions)
249249
{
250250
var scan = ConvertScan<T>(conditions, null);
251251
return FromSearchAsync<T>(scan);
252252
}
253253

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

262262
/// <inheritdoc/>
263-
public AsyncSearch<T> ScanAsync<T>(IEnumerable<ScanCondition> conditions, ScanConfig scanConfig)
263+
public IAsyncSearch<T> ScanAsync<T>(IEnumerable<ScanCondition> conditions, ScanConfig scanConfig)
264264
{
265265
var scan = ConvertScan<T>(conditions, scanConfig?.ToDynamoDBOperationConfig());
266266
return FromSearchAsync<T>(scan);
267267
}
268268

269269
/// <inheritdoc/>
270-
public AsyncSearch<T> FromScanAsync<T>(ScanOperationConfig scanConfig)
270+
public IAsyncSearch<T> FromScanAsync<T>(ScanOperationConfig scanConfig)
271271
{
272272
if (scanConfig == null) throw new ArgumentNullException("scanConfig");
273273

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

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

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

288288
/// <inheritdoc/>
289-
public AsyncSearch<T> FromScanAsync<T>(ScanOperationConfig scanConfig, FromScanConfig fromScanConfig)
289+
public IAsyncSearch<T> FromScanAsync<T>(ScanOperationConfig scanConfig, FromScanConfig fromScanConfig)
290290
{
291291
if (scanConfig == null) throw new ArgumentNullException("scanConfig");
292292

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

301301
/// <inheritdoc/>
302-
public AsyncSearch<T> QueryAsync<T>(object hashKeyValue)
302+
public IAsyncSearch<T> QueryAsync<T>(object hashKeyValue)
303303
{
304304
var query = ConvertQueryByValue<T>(hashKeyValue, null, null);
305305
return FromSearchAsync<T>(query);
306306
}
307307

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

316316
/// <inheritdoc/>
317-
public AsyncSearch<T> QueryAsync<T>(object hashKeyValue, QueryConfig queryConfig)
317+
public IAsyncSearch<T> QueryAsync<T>(object hashKeyValue, QueryConfig queryConfig)
318318
{
319319
var query = ConvertQueryByValue<T>(hashKeyValue, null, queryConfig?.ToDynamoDBOperationConfig());
320320
return FromSearchAsync<T>(query);
321321
}
322322

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

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

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

354354
/// <inheritdoc/>
355-
public AsyncSearch<T> FromQueryAsync<T>(QueryOperationConfig queryConfig)
355+
public IAsyncSearch<T> FromQueryAsync<T>(QueryOperationConfig queryConfig)
356356
{
357357
if (queryConfig == null) throw new ArgumentNullException("queryConfig");
358358

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

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

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

373373
/// <inheritdoc/>
374-
public AsyncSearch<T> FromQueryAsync<T>(QueryOperationConfig queryConfig, FromQueryConfig fromQueryConfig)
374+
public IAsyncSearch<T> FromQueryAsync<T>(QueryOperationConfig queryConfig, FromQueryConfig fromQueryConfig)
375375
{
376376
if (queryConfig == null) throw new ArgumentNullException("queryConfig");
377377

0 commit comments

Comments
 (0)