Skip to content

Allow to remove TableNamePrefix on operation level #3476

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

Merged
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
7 changes: 2 additions & 5 deletions sdk/src/Services/DynamoDBv2/Custom/DataModel/Configs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ public class DynamoDBOperationConfig
/// <summary>
/// Property that directs <see cref="DynamoDBContext"/> to prefix all table names
/// with a specific string.
/// If property is null or empty, no prefix is used and default
/// table names are used.
/// If property is null, no prefix is used and default table names are used.
/// </summary>
public string TableNamePrefix { get; set; }

Expand Down Expand Up @@ -428,9 +427,7 @@ public DynamoDBFlatConfig(DynamoDBOperationConfig operationConfig, DynamoDBConte
bool retrieveDateTimeInUtc = operationConfig.RetrieveDateTimeInUtc ?? contextConfig.RetrieveDateTimeInUtc ?? false;
bool isEmptyStringValueEnabled = operationConfig.IsEmptyStringValueEnabled ?? contextConfig.IsEmptyStringValueEnabled ?? false;
DynamoDBEntryConversion conversion = operationConfig.Conversion ?? contextConfig.Conversion ?? DynamoDBEntryConversion.CurrentConversion;
string tableNamePrefix =
!string.IsNullOrEmpty(operationConfig.TableNamePrefix) ? operationConfig.TableNamePrefix :
!string.IsNullOrEmpty(contextConfig.TableNamePrefix) ? contextConfig.TableNamePrefix : string.Empty;
string tableNamePrefix = operationConfig.TableNamePrefix ?? contextConfig.TableNamePrefix ?? string.Empty;

// These properties can only be set at the operation level
bool disableFetchingTableMetadata = contextConfig.DisableFetchingTableMetadata ?? false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,31 @@ public void BatchGetConfig_OverridesTableName()
mockClient.VerifyAll();
}

[TestMethod]
public void BatchGetConfig_RemoveTablePrefix()
{
var mockClient = new Mock<IAmazonDynamoDB>();
mockClient.Setup(client => client.BatchGetItem(It.Is<BatchGetItemRequest>(request => request.RequestItems.ContainsKey("TableName"))))
.Returns(new BatchGetItemResponse { Responses = new(), UnprocessedKeys = new() })
.Verifiable();

// 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
var context = new DynamoDBContext(mockClient.Object, new DynamoDBContextConfig
{
TableNamePrefix = "ContextPrefix-",
DisableFetchingTableMetadata = true
});

var batchGetConfig = new BatchGetConfig() { TableNamePrefix = "" };

var batchGet = context.CreateBatchGet<DataModel>(batchGetConfig);
batchGet.AddKey("123", "Name");
batchGet.Execute();

// We expect the setup with the correct prefix to have been called, otherwise an exception would have been thrown
mockClient.VerifyAll();
}

[TestMethod]
public void BatchWriteConfig()
{
Expand Down Expand Up @@ -90,6 +115,31 @@ public void BatchWriteConfig_OverridesTableName()
mockClient.VerifyAll();
}

[TestMethod]
public void BatchWriteConfig_RemoveTablePrefix()
{
var mockClient = new Mock<IAmazonDynamoDB>();
mockClient.Setup(x => x.BatchWriteItem(It.Is<BatchWriteItemRequest>(x => x.RequestItems.ContainsKey("TableName"))))
.Returns(new BatchWriteItemResponse { UnprocessedItems = new() })
.Verifiable();

// 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
var context = new DynamoDBContext(mockClient.Object, new DynamoDBContextConfig
{
TableNamePrefix = "ContextPrefix-",
DisableFetchingTableMetadata = true
});

var batchWriteConfig = new BatchWriteConfig() { TableNamePrefix = "" };

var batchWrite = context.CreateBatchWrite<DataModel>(batchWriteConfig);
batchWrite.AddPutItem(new DataModel { Id = "123", Name = "Name" });
batchWrite.Execute();

// We expect the setup with the correct prefix to have been called, otherwise an exception would have been thrown
mockClient.VerifyAll();
}

[TestMethod]
public void TransactGetConfig()
{
Expand Down Expand Up @@ -123,6 +173,31 @@ public void TransactGetConfig_OverridesTableName()
mockClient.VerifyAll();
}

[TestMethod]
public void TransactGetConfig_RemoveTablePrefix()
{
var mockClient = new Mock<IAmazonDynamoDB>();
mockClient.Setup(x => x.TransactGetItems(It.Is<TransactGetItemsRequest>(x => x.TransactItems[0].Get.TableName == "TableName")))
.Returns(new TransactGetItemsResponse { Responses = new() })
.Verifiable();

// 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
var context = new DynamoDBContext(mockClient.Object, new DynamoDBContextConfig
{
TableNamePrefix = "ContextPrefix-",
DisableFetchingTableMetadata = true
});

var transactGetConfig = new TransactGetConfig() { TableNamePrefix = "" };

var transactGet = context.CreateTransactGet<DataModel>(transactGetConfig);
transactGet.AddKey("123", "Name");
transactGet.Execute();

// We expect the setup with the correct prefix to have been called, otherwise an exception would have been thrown
mockClient.VerifyAll();
}

[TestMethod]
public void TransactWriteConfig()
{
Expand Down Expand Up @@ -156,6 +231,31 @@ public void TransactWriteConfig_OverridesTableName()
mockClient.VerifyAll();
}

[TestMethod]
public void TransactWriteConfig_RemoveTablePrefix()
{
var mockClient = new Mock<IAmazonDynamoDB>();
mockClient.Setup(x => x.TransactWriteItems(It.Is<TransactWriteItemsRequest>(x => x.TransactItems[0].Update.TableName == "TableName")))
.Returns(new TransactWriteItemsResponse())
.Verifiable();

// 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
var context = new DynamoDBContext(mockClient.Object, new DynamoDBContextConfig
{
TableNamePrefix = "ContextPrefix-",
DisableFetchingTableMetadata = true
});

var transactWriteConfig = new TransactWriteConfig { TableNamePrefix = "" };

var transactWrite = context.CreateTransactWrite<DataModel>(transactWriteConfig);
transactWrite.AddSaveItem(new DataModel { Id = "123" });
transactWrite.Execute();

// We expect the setup with the correct prefix to have been called, otherwise an exception would have been thrown
mockClient.VerifyAll();
}

[TestMethod]
public void QueryConfig()
{
Expand Down Expand Up @@ -188,6 +288,30 @@ public void QueryConfig_OverridesTableName()
mockClient.VerifyAll();
}

[TestMethod]
public void QueryConfig_RemoveTablePrefix()
{
var mockClient = new Mock<IAmazonDynamoDB>();
mockClient.Setup(client => client.Query(It.Is<QueryRequest>(request => request.TableName == "TableName")))
.Returns(new QueryResponse { Items = new() })
.Verifiable();

// 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
var context = new DynamoDBContext(mockClient.Object, new DynamoDBContextConfig
{
TableNamePrefix = "ContextPrefix-",
DisableFetchingTableMetadata = true
});

var queryConfig = new QueryConfig() { TableNamePrefix = "" };

var query = context.Query<DataModel>("123", queryConfig);
query.ToList();

// We expect the setup with the correct prefix to have been called, otherwise an exception would have been thrown
mockClient.VerifyAll();
}

[TestMethod]
public void FromQueryConfig()
{
Expand Down Expand Up @@ -225,6 +349,35 @@ public void FromQueryConfig_OverridesTableName()
mockClient.VerifyAll();
}

[TestMethod]
public void FromQueryConfig_RemoveTablePrefix()
{
var mockClient = new Mock<IAmazonDynamoDB>();
mockClient.Setup(client => client.Query(It.Is<QueryRequest>(request => request.TableName == "TableName")))
.Returns(new QueryResponse { Items = new() })
.Verifiable();

// 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
var context = new DynamoDBContext(mockClient.Object, new DynamoDBContextConfig
{
TableNamePrefix = "ContextPrefix-",
DisableFetchingTableMetadata = true
});

var queryOperationConfig = new QueryOperationConfig()
{
Filter = new QueryFilter("Id", QueryOperator.Equal, "123")
};

var fromQueryConfig = new FromQueryConfig() { TableNamePrefix = "" };

var query = context.FromQuery<DataModel>(queryOperationConfig, fromQueryConfig);
query.ToList();

// We expect the setup with the correct prefix to have been called, otherwise an exception would have been thrown
mockClient.VerifyAll();
}

[TestMethod]
public void ScanConfig()
{
Expand Down Expand Up @@ -257,6 +410,30 @@ public void ScanConfig_OverridesTableName()
mockClient.VerifyAll();
}

[TestMethod]
public void ScanConfig_RemoveTablePrefix()
{
var mockClient = new Mock<IAmazonDynamoDB>();
mockClient.Setup(client => client.Scan(It.Is<ScanRequest>(request => request.TableName == "TableName")))
.Returns(new ScanResponse { Items = new() })
.Verifiable();

// 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
var context = new DynamoDBContext(mockClient.Object, new DynamoDBContextConfig
{
TableNamePrefix = "ContextPrefix-",
DisableFetchingTableMetadata = true
});

var scanConfig = new ScanConfig() { TableNamePrefix = "" };

var scan = context.Scan<DataModel>(new ScanCondition[0], scanConfig);
scan.ToList();

// We expect the setup with the correct prefix to have been called, otherwise an exception would have been thrown
mockClient.VerifyAll();
}

[TestMethod]
public void FromScanConfig()
{
Expand Down Expand Up @@ -289,6 +466,30 @@ public void FromScanConfig_OverridesTableName()
mockClient.VerifyAll();
}

[TestMethod]
public void FromScanConfig_RemoveTablePrefix()
{
var mockClient = new Mock<IAmazonDynamoDB>();
mockClient.Setup(client => client.Scan(It.Is<ScanRequest>(request => request.TableName == "TableName")))
.Returns(new ScanResponse { Items = new() })
.Verifiable();

// 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
var context = new DynamoDBContext(mockClient.Object, new DynamoDBContextConfig
{
TableNamePrefix = "ContextPrefix-",
DisableFetchingTableMetadata = true
});

var fromScanConfig = new FromScanConfig() { TableNamePrefix = "" };

var scan = context.FromScan<DataModel>(new ScanOperationConfig(), fromScanConfig);
scan.ToList();

// We expect the setup with the correct prefix to have been called, otherwise an exception would have been thrown
mockClient.VerifyAll();
}

[TestMethod]
public void DeleteConfig()
{
Expand Down Expand Up @@ -320,6 +521,29 @@ public void DeleteConfig_OverridesTableName()
mockClient.VerifyAll();
}

[TestMethod]
public void DeleteConfig_RemoveTablePrefix()
{
var mockClient = new Mock<IAmazonDynamoDB>();
mockClient.Setup(client => client.DeleteItem(It.Is<DeleteItemRequest>(request => request.TableName == "TableName")))
.Returns(new DeleteItemResponse())
.Verifiable();

// 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
var context = new DynamoDBContext(mockClient.Object, new DynamoDBContextConfig
{
TableNamePrefix = "ContextPrefix-",
DisableFetchingTableMetadata = true
});

var deleteConfig = new DeleteConfig() { TableNamePrefix = "" };

context.Delete<DataModel>("123", "Name", deleteConfig);

// We expect the setup with the correct prefix to have been called, otherwise an exception would have been thrown
mockClient.VerifyAll();
}

[TestMethod]
public void SaveConfig()
{
Expand All @@ -345,7 +569,30 @@ public void SaveConfig_OverridesTableName()

var saveConfig = new SaveConfig() { TableNamePrefix = "OperationPrefix-" };

context.Save(new DataModel { Id = "123", Name = "Name"}, saveConfig);
context.Save(new DataModel { Id = "123", Name = "Name" }, saveConfig);

// We expect the setup with the correct prefix to have been called, otherwise an exception would have been thrown
mockClient.VerifyAll();
}

[TestMethod]
public void SaveConfig_RemoveTablePrefix()
{
var mockClient = new Mock<IAmazonDynamoDB>();
mockClient.Setup(client => client.UpdateItem(It.Is<UpdateItemRequest>(request => request.TableName == "TableName")))
.Returns(new UpdateItemResponse())
.Verifiable();

// 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
var context = new DynamoDBContext(mockClient.Object, new DynamoDBContextConfig
{
TableNamePrefix = "ContextPrefix-",
DisableFetchingTableMetadata = true
});

var saveConfig = new SaveConfig() { TableNamePrefix = "" };

context.Save(new DataModel { Id = "123", Name = "Name" }, saveConfig);

// We expect the setup with the correct prefix to have been called, otherwise an exception would have been thrown
mockClient.VerifyAll();
Expand All @@ -360,7 +607,7 @@ public void LoadConfig()
}

[TestMethod]
public void Loadonfig_OverridesTableName()
public void LoadConfig_OverridesTableName()
{
var mockClient = new Mock<IAmazonDynamoDB>();
mockClient.Setup(client => client.GetItem(It.Is<GetItemRequest>(request => request.TableName == "OperationPrefix-TableName")))
Expand All @@ -382,6 +629,29 @@ public void Loadonfig_OverridesTableName()
mockClient.VerifyAll();
}

[TestMethod]
public void LoadConfig_RemoveTablePrefix()
{
var mockClient = new Mock<IAmazonDynamoDB>();
mockClient.Setup(client => client.GetItem(It.Is<GetItemRequest>(request => request.TableName == "TableName")))
.Returns(new GetItemResponse())
.Verifiable();

// 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
var context = new DynamoDBContext(mockClient.Object, new DynamoDBContextConfig
{
TableNamePrefix = "ContextPrefix-",
DisableFetchingTableMetadata = true
});

var loadConfig = new LoadConfig() { TableNamePrefix = "" };

context.Load<DataModel>("123", "Name", loadConfig);

// We expect the setup with the correct prefix to have been called, otherwise an exception would have been thrown
mockClient.VerifyAll();
}

[TestMethod]
public void ToDocumentConfig()
{
Expand Down