Skip to content
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

ContainerBuilder : Adds public Constructor to create ContainerBuilder instance. #2241

Merged
merged 7 commits into from
Mar 22, 2021
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
21 changes: 14 additions & 7 deletions Microsoft.Azure.Cosmos/src/Fluent/Settings/ContainerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,22 @@ protected ContainerBuilder()
{
}

internal ContainerBuilder(
Database cosmosContainers,
CosmosClientContext clientContext,
/// <summary>
/// Creates an instance of ContainerBuilder .
/// </summary>
/// <param name="database"> The Microsoft.Azure.Cosmos.Database object.</param>
/// <param name="name"> Azure Cosmos container name to create. </param>
/// <param name="partitionKeyPath"> The path to the partition key. Example: /partitionKey </param>
public ContainerBuilder(
Database database,
string name,
string partitionKeyPath = null)
: base(name, partitionKeyPath)
string partitionKeyPath)
: base(
string.IsNullOrEmpty(name) ? throw new ArgumentNullException(nameof(name)) : name,
string.IsNullOrEmpty(partitionKeyPath) ? throw new ArgumentNullException(nameof(partitionKeyPath)) : partitionKeyPath)
{
this.database = cosmosContainers;
this.clientContext = clientContext;
this.database = database ?? throw new ArgumentNullException(nameof(database));
this.clientContext = database.Client.ClientContext;
this.containerUri = UriFactory.CreateDocumentCollectionUri(this.database.Id, name);
}

Expand Down
12 changes: 1 addition & 11 deletions Microsoft.Azure.Cosmos/src/Resource/Database/DatabaseCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -681,17 +681,7 @@ public override ContainerBuilder DefineContainer(
string name,
string partitionKeyPath)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentNullException(nameof(name));
}

if (string.IsNullOrEmpty(partitionKeyPath))
{
throw new ArgumentNullException(nameof(partitionKeyPath));
}

return new ContainerBuilder(this, this.ClientContext, name, partitionKeyPath);
return new ContainerBuilder(this, name, partitionKeyPath);
}

#if PREVIEW
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2910,6 +2910,11 @@
"AsyncStateMachineAttribute"
],
"MethodInfo": "System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.ContainerResponse] CreateIfNotExistsAsync(System.Nullable`1[System.Int32], System.Threading.CancellationToken);IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"Void .ctor(Microsoft.Azure.Cosmos.Database, System.String, System.String)": {
"Type": "Constructor",
"Attributes": [],
"MethodInfo": "[Void .ctor(Microsoft.Azure.Cosmos.Database, System.String, System.String), Void .ctor(Microsoft.Azure.Cosmos.Database, System.String, System.String)]"
}
},
"NestedTypes": {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public class ContainerDefinitionForCreateTests
public async Task MissingPKForCreateThrows()
{
Mock<Database> mockContainers = new Mock<Database>();

Mock<CosmosClient> mockClient = new Mock<CosmosClient>();
mockContainers.Setup(m => m.Client).Returns(mockClient.Object);
ContainerBuilder containerFluentDefinitionForCreate = new ContainerBuilder(
mockContainers.Object,
GetContext(),
containerName,
null);

Expand All @@ -53,6 +53,8 @@ public async Task MissingPKForReplace_CallsReadAsync()
.ReturnsAsync(mockContainerResponse.Object);

Mock<Database> mockContainers = new Mock<Database>();
Mock<CosmosClient> mockClient = new Mock<CosmosClient>();
mockContainers.Setup(m => m.Client).Returns(mockClient.Object);
mockContainers
.Setup(c => c.CreateContainerAsync(
It.Is<ContainerProperties>((settings) => settings.PartitionKeyPath.Equals(partitionKey)),
Expand All @@ -65,7 +67,6 @@ public async Task MissingPKForReplace_CallsReadAsync()

ContainerBuilder containerFluentDefinitionForCreate = new ContainerBuilder(
mockContainers.Object,
GetContext(),
containerName,
null);

Expand All @@ -79,6 +80,8 @@ public async Task WithThroughput()
{
Mock<ContainerResponse> mockContainerResponse = new Mock<ContainerResponse>();
Mock<Database> mockContainers = new Mock<Database>();
Mock<CosmosClient> mockClient = new Mock<CosmosClient>();
mockContainers.Setup(m => m.Client).Returns(mockClient.Object);
mockContainers
.Setup(c => c.CreateContainerAsync(
It.IsAny<ContainerProperties>(),
Expand All @@ -92,7 +95,6 @@ public async Task WithThroughput()

ContainerBuilder containerFluentDefinitionForCreate = new ContainerBuilder(
mockContainers.Object,
GetContext(),
containerName,
partitionKey);

Expand All @@ -111,6 +113,8 @@ public async Task WithTimeToLivePropertyPath()
{
Mock<ContainerResponse> mockContainerResponse = new Mock<ContainerResponse>();
Mock<Database> mockContainers = new Mock<Database>();
Mock<CosmosClient> mockClient = new Mock<CosmosClient>();
mockContainers.Setup(m => m.Client).Returns(mockClient.Object);
mockContainers
.Setup(c => c.CreateContainerAsync(
#pragma warning disable CS0612 // Type or member is obsolete
Expand All @@ -126,7 +130,6 @@ public async Task WithTimeToLivePropertyPath()

ContainerBuilder containerFluentDefinitionForCreate = new ContainerBuilder(
mockContainers.Object,
GetContext(),
containerName,
partitionKey);

Expand All @@ -148,6 +151,8 @@ public async Task WithDefaultTimeToLiveTimeSpan()
{
Mock<ContainerResponse> mockContainerResponse = new Mock<ContainerResponse>();
Mock<Database> mockContainers = new Mock<Database>();
Mock<CosmosClient> mockClient = new Mock<CosmosClient>();
mockContainers.Setup(m => m.Client).Returns(mockClient.Object);
mockContainers
.Setup(c => c.CreateContainerAsync(
It.Is<ContainerProperties>((settings) => settings.DefaultTimeToLive.Equals((int)timeToLive.TotalSeconds)),
Expand All @@ -161,7 +166,6 @@ public async Task WithDefaultTimeToLiveTimeSpan()

ContainerBuilder containerFluentDefinitionForCreate = new ContainerBuilder(
mockContainers.Object,
GetContext(),
containerName,
partitionKey);

Expand All @@ -181,6 +185,8 @@ public async Task WithDefaultTimeToLiveInt()
{
Mock<ContainerResponse> mockContainerResponse = new Mock<ContainerResponse>();
Mock<Database> mockContainers = new Mock<Database>();
Mock<CosmosClient> mockClient = new Mock<CosmosClient>();
mockContainers.Setup(m => m.Client).Returns(mockClient.Object);
mockContainers
.Setup(c => c.CreateContainerAsync(
It.Is<ContainerProperties>((settings) => settings.DefaultTimeToLive.Equals((int)timeToLive.TotalSeconds)),
Expand All @@ -194,7 +200,6 @@ public async Task WithDefaultTimeToLiveInt()

ContainerBuilder containerFluentDefinitionForCreate = new ContainerBuilder(
mockContainers.Object,
GetContext(),
containerName,
partitionKey);

Expand All @@ -214,6 +219,8 @@ public async Task WithIndexingPolicy()
{
Mock<ContainerResponse> mockContainerResponse = new Mock<ContainerResponse>();
Mock<Database> mockContainers = new Mock<Database>();
Mock<CosmosClient> mockClient = new Mock<CosmosClient>();
mockContainers.Setup(m => m.Client).Returns(mockClient.Object);
mockContainers
.Setup(c => c.CreateContainerAsync(
It.Is<ContainerProperties>((settings) => IndexingMode.None.Equals(settings.IndexingPolicy.IndexingMode) && !settings.IndexingPolicy.Automatic),
Expand All @@ -227,7 +234,6 @@ public async Task WithIndexingPolicy()

ContainerBuilder containerFluentDefinitionForCreate = new ContainerBuilder(
mockContainers.Object,
GetContext(),
containerName,
partitionKey);

Expand All @@ -250,6 +256,8 @@ public async Task WithUniqueKey()
{
Mock<ContainerResponse> mockContainerResponse = new Mock<ContainerResponse>();
Mock<Database> mockContainers = new Mock<Database>();
Mock<CosmosClient> mockClient = new Mock<CosmosClient>();
mockContainers.Setup(m => m.Client).Returns(mockClient.Object);
mockContainers
.Setup(c => c.CreateContainerAsync(
It.Is<ContainerProperties>((settings) => settings.UniqueKeyPolicy.UniqueKeys.Count == 1 && path.Equals(settings.UniqueKeyPolicy.UniqueKeys[0].Paths[0])),
Expand All @@ -263,7 +271,6 @@ public async Task WithUniqueKey()

ContainerBuilder containerFluentDefinitionForCreate = new ContainerBuilder(
mockContainers.Object,
GetContext(),
containerName,
partitionKey);

Expand Down