Skip to content

Commit 1bf6a6a

Browse files
committed
Add the ability to mock document model operations in DynamoDB (#3446)
* Add the ability to mock DocumentBatchGet and MultiTableDocumentBatchGet operations * Add the ability to mock DocumentBatchWrite and MultiTableDocumentBatchWrite operations * Add the ability to mock DocumentTransactGet and MultiTableDocumentTransactGet operations * Add the ability to mock DocumentTransactWrite and MultiTableDocumentTransactWrite operations * Add the ability to mock Search operations in Document model * Add the ability to mock Table operations in Document model * Fix typos
1 parent 587df54 commit 1bf6a6a

36 files changed

+2159
-1031
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,10 @@ internal BatchWrite(DynamoDBContext context, Type valuesType, DynamoDBFlatConfig
128128
}
129129

130130
Table table = _context.GetTargetTable(_storageConfig, _config);
131-
DocumentBatch = table.CreateBatchWrite();
131+
132+
// Table.CreateBatchWrite() returns the IDocumentBatchWrite interface.
133+
// But since we rely on the internal behavior of DocumentBatchWrite, we instantiate it via the constructor.
134+
DocumentBatch = new DocumentBatchWrite(table);
132135
}
133136

134137
/// <inheritdoc/>

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ internal Table GetUnconfiguredTable(string tableName, bool disableFetchingTableM
192192

193193
var emptyConfig = new TableConfig(tableName, conversion: null, consumer: Table.DynamoDBConsumer.DataModel,
194194
storeAsEpoch: null, isEmptyStringValueEnabled: false, metadataCachingMode: Config.MetadataCachingMode);
195-
table = Table.LoadTable(Client, emptyConfig);
195+
table = Table.LoadTable(Client, emptyConfig) as Table;
196196
tablesMap[tableName] = table;
197197

198198
return table;
@@ -1183,7 +1183,9 @@ private ContextSearch ConvertScan<T>(IEnumerable<ScanCondition> conditions, Dyna
11831183
IndexName = flatConfig.IndexName,
11841184
ConsistentRead = flatConfig.ConsistentRead.GetValueOrDefault(false)
11851185
};
1186-
Search scan = table.Scan(scanConfig);
1186+
1187+
// table.Scan() returns the ISearch interface but we explicitly cast it to a Search object since we rely on its internal behavior
1188+
Search scan = table.Scan(scanConfig) as Search;
11871189
return new ContextSearch(scan, flatConfig);
11881190
}
11891191

@@ -1192,7 +1194,9 @@ private ContextSearch ConvertFromScan<T>(ScanOperationConfig scanConfig, DynamoD
11921194
DynamoDBFlatConfig flatConfig = new DynamoDBFlatConfig(operationConfig, Config);
11931195
ItemStorageConfig storageConfig = StorageConfigCache.GetConfig<T>(flatConfig);
11941196
Table table = GetTargetTable(storageConfig, flatConfig);
1195-
Search search = table.Scan(scanConfig);
1197+
1198+
// table.Scan() returns the ISearch interface but we explicitly cast it to a Search object since we rely on its internal behavior
1199+
Search search = table.Scan(scanConfig) as Search;
11961200
return new ContextSearch(search, flatConfig);
11971201
}
11981202

@@ -1201,7 +1205,9 @@ private ContextSearch ConvertFromQuery<T>(QueryOperationConfig queryConfig, Dyna
12011205
DynamoDBFlatConfig flatConfig = new DynamoDBFlatConfig(operationConfig, Config);
12021206
ItemStorageConfig storageConfig = StorageConfigCache.GetConfig<T>(flatConfig);
12031207
Table table = GetTargetTable(storageConfig, flatConfig);
1204-
Search search = table.Query(queryConfig);
1208+
1209+
// table.Query() returns the ISearch interface but we explicitly cast it to a Search object since we rely on its internal behavior
1210+
Search search = table.Query(queryConfig) as Search;
12051211
return new ContextSearch(search, flatConfig);
12061212
}
12071213

@@ -1246,7 +1252,9 @@ private ContextSearch ConvertQueryHelper<T>(DynamoDBFlatConfig currentConfig, It
12461252
{
12471253
queryConfig.Select = SelectValues.AllProjectedAttributes;
12481254
}
1249-
Search query = table.Query(queryConfig);
1255+
1256+
// table.Query() returns the ISearch interface but we explicitly cast it to a Search object since we rely on its internal behavior
1257+
Search query = table.Query(queryConfig) as Search;
12501258

12511259
return new ContextSearch(query, currentConfig);
12521260
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,10 @@ internal TransactGet(DynamoDBContext context, DynamoDBFlatConfig config)
157157
_config = config;
158158
_storageConfig = context.StorageConfigCache.GetConfig<T>(config);
159159
var table = context.GetTargetTable(_storageConfig, config);
160-
DocumentTransaction = table.CreateTransactGet();
160+
161+
// Table.CreateTransactGet() returns the IDocumentTransactGet interface.
162+
// But since we rely on the internal behavior of DocumentTransactGet, we instantiate it via the constructor.
163+
DocumentTransaction = new DocumentTransactGet(table);
161164
}
162165

163166
private void ExecuteHelper()

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,10 @@ internal TransactWrite(DynamoDBContext context, DynamoDBFlatConfig config)
188188
_config = config;
189189
_storageConfig = context.StorageConfigCache.GetConfig<T>(config);
190190
Table table = _context.GetTargetTable(_storageConfig, _config);
191-
DocumentTransaction = table.CreateTransactWrite();
191+
192+
// table.CreateTransactWrite() return the IDocumentTransactWrite interface.
193+
// But since we rely on the internal behavior of DocumentTransactWrite, we instatiate it via the constructor.
194+
DocumentTransaction = new DocumentTransactWrite(table);
192195
}
193196

194197
/// <inheritdoc/>

sdk/src/Services/DynamoDBv2/Custom/DataModel/_bcl/Context.Sync.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,20 +348,20 @@ public IEnumerable<T> FromQuery<T>(QueryOperationConfig queryConfig, FromQueryCo
348348
#region Table methods
349349

350350
/// <inheritdoc/>
351-
public Table GetTargetTable<T>()
351+
public ITable GetTargetTable<T>()
352352
{
353353
return GetTargetTable<T>((GetTargetTableConfig)null);
354354
}
355355

356356
/// <inheritdoc/>
357357
[Obsolete("Use the GetTargetTable overload that takes GetTargetTableConfig instead, since DynamoDBOperationConfig contains properties that are not applicable to GetTargetTable.")]
358-
public Table GetTargetTable<T>(DynamoDBOperationConfig operationConfig = null)
358+
public ITable GetTargetTable<T>(DynamoDBOperationConfig operationConfig = null)
359359
{
360360
return GetTargetTableInternal<T>(new DynamoDBFlatConfig(operationConfig, Config));
361361
}
362362

363363
/// <inheritdoc/>
364-
public Table GetTargetTable<T>(GetTargetTableConfig getTargetTableConfig)
364+
public ITable GetTargetTable<T>(GetTargetTableConfig getTargetTableConfig)
365365
{
366366
return GetTargetTableInternal<T>(new DynamoDBFlatConfig(getTargetTableConfig?.ToDynamoDBOperationConfig(), Config));
367367
}

sdk/src/Services/DynamoDBv2/Custom/DataModel/_bcl/IDynamoDBContext.Sync.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ partial interface IDynamoDBContext
593593
/// </summary>
594594
/// <typeparam name="T">Type to retrieve table for</typeparam>
595595
/// <returns>Table object</returns>
596-
Table GetTargetTable<T>();
596+
ITable GetTargetTable<T>();
597597

598598
/// <summary>
599599
/// Retrieves the target table for the specified type
@@ -602,15 +602,15 @@ partial interface IDynamoDBContext
602602
/// <param name="operationConfig">Config object which can be used to override that table used.</param>
603603
/// <returns>Table object</returns>
604604
[Obsolete("Use the GetTargetTable overload that takes GetTargetTableConfig instead, since DynamoDBOperationConfig contains properties that are not applicable to GetTargetTable.")]
605-
Table GetTargetTable<T>(DynamoDBOperationConfig operationConfig = null);
605+
ITable GetTargetTable<T>(DynamoDBOperationConfig operationConfig = null);
606606

607607
/// <summary>
608608
/// Retrieves the target table for the specified type
609609
/// </summary>
610610
/// <typeparam name="T">Type to retrieve table for</typeparam>
611611
/// <param name="getTargetTableConfig">Config object that can be used to override properties on the table's context for this request.</param>
612612
/// <returns>Table object</returns>
613-
Table GetTargetTable<T>(GetTargetTableConfig getTargetTableConfig);
613+
ITable GetTargetTable<T>(GetTargetTableConfig getTargetTableConfig);
614614

615615
#endregion
616616
}

sdk/src/Services/DynamoDBv2/Custom/DataModel/_netstandard/Context.Sync.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@
1414
*/
1515

1616
using System;
17-
using System.Collections.Generic;
18-
using System.Linq;
1917

2018
using Amazon.DynamoDBv2.DocumentModel;
21-
using Amazon.DynamoDBv2.Model;
2219

2320
namespace Amazon.DynamoDBv2.DataModel
2421
{
@@ -27,20 +24,20 @@ public partial class DynamoDBContext : IDynamoDBContext
2724
#region Table methods
2825

2926
/// <inheritdoc/>
30-
public Table GetTargetTable<T>()
27+
public ITable GetTargetTable<T>()
3128
{
3229
return GetTargetTableInternal<T>(new DynamoDBFlatConfig(null, Config));
3330
}
3431

3532
/// <inheritdoc/>
3633
[Obsolete("Use the GetTargetTable overload that takes GetTargetTableConfig instead, since DynamoDBOperationConfig contains properties that are not applicable to GetTargetTable.")]
37-
public Table GetTargetTable<T>(DynamoDBOperationConfig operationConfig = null)
34+
public ITable GetTargetTable<T>(DynamoDBOperationConfig operationConfig = null)
3835
{
3936
return GetTargetTableInternal<T>(new DynamoDBFlatConfig(operationConfig, Config));
4037
}
4138

4239
/// <inheritdoc/>
43-
public Table GetTargetTable<T>(GetTargetTableConfig getTargetTableConfig)
40+
public ITable GetTargetTable<T>(GetTargetTableConfig getTargetTableConfig)
4441
{
4542
return GetTargetTableInternal<T>(new DynamoDBFlatConfig(getTargetTableConfig?.ToDynamoDBOperationConfig(), Config));
4643
}

sdk/src/Services/DynamoDBv2/Custom/DataModel/_netstandard/IDynamoDBContext.Sync.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414
*/
1515

1616
using System;
17-
using System.Collections.Generic;
1817

1918
using Amazon.DynamoDBv2.DocumentModel;
20-
using Amazon.DynamoDBv2.Model;
2119

2220
namespace Amazon.DynamoDBv2.DataModel
2321
{
@@ -34,22 +32,22 @@ partial interface IDynamoDBContext
3432
/// </summary>
3533
/// <typeparam name="T">Type to retrieve table for</typeparam>
3634
/// <returns>Table object</returns>
37-
Table GetTargetTable<T>();
35+
ITable GetTargetTable<T>();
3836

3937
/// <summary>
4038
/// Retrieves the target table for the specified type
4139
/// </summary>
4240
/// <typeparam name="T">Type to retrieve table for</typeparam>
4341
/// <returns>Table object</returns>
4442
[Obsolete("Use the GetTargetTable overload that takes GetTargetTableConfig instead, since DynamoDBOperationConfig contains properties that are not applicable to GetTargetTable.")]
45-
Table GetTargetTable<T>(DynamoDBOperationConfig operationConfig = null);
43+
ITable GetTargetTable<T>(DynamoDBOperationConfig operationConfig = null);
4644

4745
/// <summary>
4846
/// Retrieves the target table for the specified type
4947
/// </summary>
5048
/// <typeparam name="T">Type to retrieve table for</typeparam>
5149
/// <returns>Table object</returns>
52-
Table GetTargetTable<T>(GetTargetTableConfig getTargetTableConfig);
50+
ITable GetTargetTable<T>(GetTargetTableConfig getTargetTableConfig);
5351

5452
#endregion
5553
}

0 commit comments

Comments
 (0)