Created
April 7, 2017 01:46
-
-
Save dcomartin/b26a3742a7ef111ba81e4bbb1d823172 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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