Skip to content

Commit 6895929

Browse files
committed
Add link to docs, and test that a property on the new operation-specific config overrides the context config.
1 parent 2c3afe6 commit 6895929

File tree

2 files changed

+115
-3
lines changed

2 files changed

+115
-3
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public class BatchGetConfig : BaseOperationConfig
3030
/// Property that directs <see cref="DynamoDBContext"/> to use consistent reads.
3131
/// If property is not set, behavior defaults to non-consistent reads.
3232
/// </summary>
33+
/// <remarks>
34+
/// Refer to the <see href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.ReadConsistency.html">
35+
/// Read Consistency</see> topic in the DynamoDB Developer Guide for more information.
36+
/// </remarks>
3337
public bool? ConsistentRead { get; set; }
3438

3539
/// <summary>

sdk/test/Services/DynamoDBv2/UnitTests/Custom/DataModelOperationSpecificConfigTests.cs

Lines changed: 111 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
using Amazon.DynamoDBv2.DataModel;
1+
using Amazon.DynamoDBv2;
2+
using Amazon.DynamoDBv2.DataModel;
3+
using Amazon.DynamoDBv2.Model;
24
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
using Moq;
36

47
namespace AWSSDK_DotNet.UnitTests
58
{
69
/// <summary>
7-
/// These tests serve as a reminder to developers and reviewers to
8-
/// ensure that new DynamoDB operation-specific properties are plumbed
10+
/// These tests that new DynamoDB operation-specific properties are plumbed
911
/// into the internal code paths correctly
1012
/// </summary>
1113
[TestClass]
@@ -27,6 +29,30 @@ public void BatchGetConfig()
2729
Assert.AreEqual(8, typeof(BatchGetConfig).GetProperties().Length);
2830
}
2931

32+
[TestMethod]
33+
public void BatchGetConfig_OverridesTableName()
34+
{
35+
var mockClient = new Mock<IAmazonDynamoDB>();
36+
mockClient.Setup(client => client.BatchGetItem(It.Is<BatchGetItemRequest>(request => request.RequestItems.ContainsKey("OperationPrefix-TableName"))))
37+
.Returns(new BatchGetItemResponse { Responses = new(), UnprocessedKeys = new() })
38+
.Verifiable();
39+
40+
// Set a prefix on the context config, but we'll override it on the operation config so we don't expect it to be used
41+
var context = new DynamoDBContext(mockClient.Object, new DynamoDBContextConfig {
42+
TableNamePrefix = "ContextPrefix-",
43+
DisableFetchingTableMetadata = true
44+
});
45+
46+
var batchGetConfig = new BatchGetConfig() { TableNamePrefix = "OperationPrefix-" };
47+
48+
var batchGet = context.CreateBatchGet<DataModel>(batchGetConfig);
49+
batchGet.AddKey("123");
50+
batchGet.Execute();
51+
52+
// We expect the setup with the correct prefix to have been called, otherwise an exception would have been thrown
53+
mockClient.VerifyAll();
54+
}
55+
3056
[TestMethod]
3157
public void BatchWriteConfig()
3258
{
@@ -35,6 +61,31 @@ public void BatchWriteConfig()
3561
Assert.AreEqual(8, typeof(BatchWriteConfig).GetProperties().Length);
3662
}
3763

64+
[TestMethod]
65+
public void BatchWriteConfig_OverridesTableName()
66+
{
67+
var mockClient = new Mock<IAmazonDynamoDB>();
68+
mockClient.Setup(x => x.BatchWriteItem(It.Is<BatchWriteItemRequest>(x => x.RequestItems.ContainsKey("OperationPrefix-TableName"))))
69+
.Returns(new BatchWriteItemResponse { UnprocessedItems = new() })
70+
.Verifiable();
71+
72+
// Set a prefix on the context config, but we'll override it on the operation config so we don't expect it to be used
73+
var context = new DynamoDBContext(mockClient.Object, new DynamoDBContextConfig
74+
{
75+
TableNamePrefix = "ContextPrefix-",
76+
DisableFetchingTableMetadata = true
77+
});
78+
79+
var batchWriteConfig = new BatchWriteConfig() { TableNamePrefix = "OperationPrefix-" };
80+
81+
var batchWrite = context.CreateBatchWrite<DataModel>(batchWriteConfig);
82+
batchWrite.AddPutItem(new DataModel { Id = "123" });
83+
batchWrite.Execute();
84+
85+
// We expect the setup with the correct prefix to have been called, otherwise an exception would have been thrown
86+
mockClient.VerifyAll();
87+
}
88+
3889
[TestMethod]
3990
public void TransactGetConfig()
4091
{
@@ -43,12 +94,69 @@ public void TransactGetConfig()
4394
Assert.AreEqual(7, typeof(TransactGetConfig).GetProperties().Length);
4495
}
4596

97+
[TestMethod]
98+
public void TransactGetConfig_OverridesTableName()
99+
{
100+
var mockClient = new Mock<IAmazonDynamoDB>();
101+
mockClient.Setup(x => x.TransactGetItems(It.Is<TransactGetItemsRequest>(x => x.TransactItems[0].Get.TableName == "OperationPrefix-TableName")))
102+
.Returns(new TransactGetItemsResponse { Responses = new() })
103+
.Verifiable();
104+
105+
// Set a prefix on the context config, but we'll override it on the operation config so we don't expect it to be used
106+
var context = new DynamoDBContext(mockClient.Object, new DynamoDBContextConfig
107+
{
108+
TableNamePrefix = "ContextPrefix-",
109+
DisableFetchingTableMetadata = true
110+
});
111+
112+
var transactGetConfig = new TransactGetConfig() { TableNamePrefix = "OperationPrefix-" };
113+
114+
var transactGet = context.CreateTransactGet<DataModel>(transactGetConfig);
115+
transactGet.AddKey("123");
116+
transactGet.Execute();
117+
118+
// We expect the setup with the correct prefix to have been called, otherwise an exception would have been thrown
119+
mockClient.VerifyAll();
120+
}
121+
46122
[TestMethod]
47123
public void TransactWriteConfig()
48124
{
49125
// If this fails because you've added a property, be sure to add it to
50126
// `ToDynamoDBOperationConfig` before updating this unit test
51127
Assert.AreEqual(7, typeof(TransactWriteConfig).GetProperties().Length);
52128
}
129+
130+
[TestMethod]
131+
public void TransactWriteConfig_OverridesTableName()
132+
{
133+
var mockClient = new Mock<IAmazonDynamoDB>();
134+
mockClient.Setup(x => x.TransactWriteItems(It.Is<TransactWriteItemsRequest>(x => x.TransactItems[0].Update.TableName == "OperationPrefix-TableName")))
135+
.Returns(new TransactWriteItemsResponse())
136+
.Verifiable();
137+
138+
// Set a prefix on the context config, but we'll override it on the operation config so we don't expect it to be used
139+
var context = new DynamoDBContext(mockClient.Object, new DynamoDBContextConfig
140+
{
141+
TableNamePrefix = "ContextPrefix-",
142+
DisableFetchingTableMetadata = true
143+
});
144+
145+
var transactWriteConfig = new TransactWriteConfig { TableNamePrefix = "OperationPrefix-" };
146+
147+
var transactWrite = context.CreateTransactWrite<DataModel>(transactWriteConfig);
148+
transactWrite.AddSaveItem(new DataModel { Id = "123" });
149+
transactWrite.Execute();
150+
151+
// We expect the setup with the correct prefix to have been called, otherwise an exception would have been thrown
152+
mockClient.VerifyAll();
153+
}
154+
155+
[DynamoDBTable("TableName")]
156+
private class DataModel
157+
{
158+
[DynamoDBHashKey]
159+
public string Id { get; set; }
160+
}
53161
}
54162
}

0 commit comments

Comments
 (0)