Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created April 7, 2017 01:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcomartin/b26a3742a7ef111ba81e4bbb1d823172 to your computer and use it in GitHub Desktop.
Save dcomartin/b26a3742a7ef111ba81e4bbb1d823172 to your computer and use it in GitHub Desktop.
using System;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Shouldly;
using Xunit;
namespace Demo
{
public class OptimtimisticConcurrencyTests
{
private readonly DocumentClient _client;
private const string EndpointUrl = "https://localhost:8081";
private const string AuthorizationKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";
private const string DatabaseId = "ConcurrencyDemo";
private const string CollectionId = "Customers";
public OptimtimisticConcurrencyTests()
{
_client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey);
}
[Fact]
public async Task Should_insert_multiple_customers()
{
// Setup our Database and add a new Customer
var dbSetup = new DatabaseSetup(_client);
await dbSetup.Init(DatabaseId, CollectionId);
dynamic[] customers = {
new Customer(Guid.NewGuid().ToString(), "Test1 Customer1"),
new Customer(Guid.NewGuid().ToString(), "Test1 Customer2")
};
var sproc = dbSetup.Client.CreateStoredProcedureQuery(dbSetup.Collection.SelfLink).Where(x => x.Id == "sp_bulkinsert").AsEnumerable().First();
var imported = await dbSetup.Client.ExecuteStoredProcedureAsync<int>(sproc.SelfLink, new dynamic[] { customers });
imported.Response.ShouldBe(2);
}
[Fact]
public async Task Should_not_insert_any()
{
// Setup our Database and add a new Customer
var dbSetup = new DatabaseSetup(_client);
await dbSetup.Init(DatabaseId, CollectionId);
string customer1Id = Guid.NewGuid().ToString();
dynamic[] customers = {
new Customer(customer1Id, "Test2 Customer1"),
new Customer(Guid.NewGuid().ToString(), "")
};
var sproc = dbSetup.Client.CreateStoredProcedureQuery(dbSetup.Collection.SelfLink).Where(x => x.Id == "sp_bulkinsert").AsEnumerable().First();
await dbSetup.Client.ExecuteStoredProcedureAsync<int>(sproc.SelfLink, new dynamic[] {customers}).ShouldThrowAsync<DocumentClientException>();
var document = (from f in dbSetup.Client.CreateDocumentQuery(dbSetup.Collection.SelfLink)
where f.Id == customer1Id
select f).AsEnumerable().FirstOrDefault();
document.ShouldBeNull();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment