Skip to content

Commit

Permalink
ContainerBuilder : Adds public Constructor to create ContainerBuilder…
Browse files Browse the repository at this point in the history
… instance. (#2241)

* Make methods virtual and Expose ContainerBuilder constructor.

* Update ContainerBuilder.cs

* Fixes.

* Update ContainerBuilder.cs

Co-authored-by: j82w <j82w@users.noreply.github.com>
  • Loading branch information
kr-santosh and j82w authored Mar 22, 2021
1 parent 35d1744 commit 8703a87
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 27 deletions.
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

0 comments on commit 8703a87

Please sign in to comment.