Exemple #1
0
        public async Task Should_Throw_With_PreconditionFailed()
        {
            // Setup our Database and add a new Customer
            var dbSetup = new DatabaseSetup(_client);
            await dbSetup.Init(DatabaseId, CollectionId);

            var addCustomer = new Customer(Guid.NewGuid().ToString(), "Demo");
            await dbSetup.AddCustomer(addCustomer);

            // Fetch out the Document (Customer)
            var document = (from f in dbSetup.Client.CreateDocumentQuery(dbSetup.Collection.SelfLink)
                            where f.Id == addCustomer.Id
                            select f).AsEnumerable().FirstOrDefault();

            // Cast the Document to our Customer & make a data change
            var editCustomer = (Customer)(dynamic)document;

            editCustomer.Name = "Changed";

            // Using Access Conditions gives us the ability to use the ETag from our fetched document for optimistic concurrency.
            var ac = new AccessCondition {
                Condition = document.ETag, Type = AccessConditionType.IfMatch
            };

            // Replace our document, which will succeed with the correct ETag
            await dbSetup.Client.ReplaceDocumentAsync(document.SelfLink, editCustomer,
                                                      new RequestOptions { AccessCondition = ac });

            // Replace again, which will fail since our (same) ETag is now invalid
            var ex = await dbSetup.Client.ReplaceDocumentAsync(document.SelfLink, editCustomer,
                                                               new RequestOptions { AccessCondition = ac }).ShouldThrowAsync <DocumentClientException>();

            ex.StatusCode.ShouldBe(HttpStatusCode.PreconditionFailed);
        }