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

Adding CreateIfNotExistsAsync to container builder #592

Merged
merged 4 commits into from
Jul 24, 2019
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
15 changes: 15 additions & 0 deletions Microsoft.Azure.Cosmos/src/Fluent/Settings/ContainerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ public async Task<ContainerResponse> CreateAsync(int? throughput = null)
return await this.database.CreateContainerAsync(containerProperties, throughput);
}

/// <summary>
/// Creates a container if it does not exist with the current fluent definition.
/// </summary>
/// <param name="throughput">Desired throughput for the container expressed in Request Units per second.</param>
/// <returns>An asynchronous Task representing the creation of a <see cref="Container"/> based on the Fluent definition.</returns>
/// <remarks>
/// <seealso href="https://docs.microsoft.com/azure/cosmos-db/request-units"/> for details on provision throughput.
/// </remarks>
public async Task<ContainerResponse> CreateIfNotExistsAsync(int? throughput = null)
{
ContainerProperties containerProperties = this.Build();

return await this.database.CreateContainerIfNotExistsAsync(containerProperties, throughput);
}

/// <summary>
/// Applies the current Fluent definition and creates a container configuration.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,20 @@ public async Task TestCleanup()
public async Task ContainerContractTest()
{
ContainerResponse response = await this.cosmosDatabase.CreateContainerAsync(new Guid().ToString(), "/id");
Assert.IsNotNull(response);
Assert.IsTrue(response.RequestCharge > 0);
Assert.IsNotNull(response.Headers);
Assert.IsNotNull(response.Headers.ActivityId);

ContainerProperties containerSettings = response.Resource;
Assert.IsNotNull(containerSettings.Id);
Assert.IsNotNull(containerSettings.ResourceId);
Assert.IsNotNull(containerSettings.ETag);
Assert.IsTrue(containerSettings.LastModified.HasValue);
ValidateCreateContainerResponseContract(response);
}

Assert.IsNotNull(containerSettings.PartitionKeyPath);
Assert.IsNotNull(containerSettings.PartitionKeyPathTokens);
Assert.AreEqual(1, containerSettings.PartitionKeyPathTokens.Length);
Assert.AreEqual("id", containerSettings.PartitionKeyPathTokens[0]);
[TestMethod]
public async Task ContainerBuilderContractTest()
{
ContainerResponse response = await this.cosmosDatabase.DefineContainer(new Guid().ToString(), "/id").CreateAsync();
ValidateCreateContainerResponseContract(response);

ContainerCore containerCore = response.Container as ContainerCore;
Assert.IsNotNull(containerCore);
Assert.IsNotNull(containerCore.LinkUri);
Assert.IsFalse(containerCore.LinkUri.ToString().StartsWith("/"));
response = await this.cosmosDatabase.DefineContainer(new Guid().ToString(), "/id").CreateIfNotExistsAsync();
ValidateCreateContainerResponseContract(response);

Assert.IsTrue(containerSettings.LastModified.Value > new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), containerSettings.LastModified.Value.ToString());
response = await this.cosmosDatabase.DefineContainer(response.Container.Id, "/id").CreateIfNotExistsAsync();
ValidateCreateContainerResponseContract(response);
}

[Ignore]
Expand Down Expand Up @@ -577,5 +569,31 @@ public async Task TimeToLivePropertyPath()
containerResponse = await container.DeleteContainerAsync();
Assert.AreEqual(HttpStatusCode.NoContent, containerResponse.StatusCode);
}

private void ValidateCreateContainerResponseContract(ContainerResponse containerResponse)
{
Assert.IsNotNull(containerResponse);
Assert.IsTrue(containerResponse.RequestCharge > 0);
Assert.IsNotNull(containerResponse.Headers);
Assert.IsNotNull(containerResponse.Headers.ActivityId);

ContainerProperties containerSettings = containerResponse.Resource;
Assert.IsNotNull(containerSettings.Id);
Assert.IsNotNull(containerSettings.ResourceId);
Assert.IsNotNull(containerSettings.ETag);
Assert.IsTrue(containerSettings.LastModified.HasValue);

Assert.IsNotNull(containerSettings.PartitionKeyPath);
Assert.IsNotNull(containerSettings.PartitionKeyPathTokens);
Assert.AreEqual(1, containerSettings.PartitionKeyPathTokens.Length);
Assert.AreEqual("id", containerSettings.PartitionKeyPathTokens[0]);

ContainerCore containerCore = containerResponse.Container as ContainerCore;
Assert.IsNotNull(containerCore);
Assert.IsNotNull(containerCore.LinkUri);
Assert.IsFalse(containerCore.LinkUri.ToString().StartsWith("/"));

Assert.IsTrue(containerSettings.LastModified.Value > new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), containerSettings.LastModified.Value.ToString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2109,6 +2109,13 @@
],
"MethodInfo": "System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.ContainerResponse] CreateAsync(System.Nullable`1[System.Int32])"
},
"System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.ContainerResponse] CreateIfNotExistsAsync(System.Nullable`1[System.Int32])[System.Runtime.CompilerServices.AsyncStateMachineAttribute(typeof(Microsoft.Azure.Cosmos.Fluent.ContainerBuilder+<CreateIfNotExistsAsync>d__10))]": {
"Type": "Method",
"Attributes": [
"AsyncStateMachineAttribute"
],
"MethodInfo": "System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.ContainerResponse] CreateIfNotExistsAsync(System.Nullable`1[System.Int32])"
},
"Void .ctor()": {
"Type": "Constructor",
"Attributes": [],
Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#544](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/544) Added continuation token support for LINQ
- [#557](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/557) Added trigger options to item request options
- [#571](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/571) Added a default JSON.net serializer with optional settings
- [#592](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/592) Added CreateIfNotExistsAsync to container builder

### Fixed

Expand Down