public async Task Should_execute_operations_with_same_partition_key_together()
        {
            var fakeContainer    = new FakeContainer();
            var fakeCosmosClient = new FakeCosmosClient(fakeContainer);
            var containerHolderHolderResolver = new ContainerHolderResolver(new FakeProvider(fakeCosmosClient),
                                                                            new ContainerInformation("fakeContainer", new PartitionKeyPath("/deep/down")), "fakeDatabase");

            var storageSession = new StorageSession(containerHolderHolderResolver, new ContextBag(), true);
            var firstOperation = new FakeOperation
            {
                PartitionKey = new PartitionKey("PartitionKey1")
            };

            storageSession.AddOperation(firstOperation);
            var secondOperation = new FakeOperation
            {
                PartitionKey = new PartitionKey("PartitionKey1")
            };

            storageSession.AddOperation(secondOperation);

            await((ICompletableSynchronizedStorageSession)storageSession).CompleteAsync();

            Assert.That(firstOperation.WasApplied, Is.True);
            Assert.That(secondOperation.WasApplied, Is.True);
            Assert.That(firstOperation.AppliedBatch, Is.EqualTo(secondOperation.AppliedBatch), "Operations with the same partition key must be in the same batch");
        }
        public void Should_execute_and_dispose_release_operations_with_different_partition_key_distinct_when_not_completed()
        {
            var fakeContainer    = new FakeContainer();
            var fakeCosmosClient = new FakeCosmosClient(fakeContainer);
            var containerHolderHolderResolver = new ContainerHolderResolver(new FakeProvider(fakeCosmosClient),
                                                                            new ContainerInformation("fakeContainer", new PartitionKeyPath("/deep/down")), "fakeDatabase");

            var storageSession        = new StorageSession(containerHolderHolderResolver, new ContextBag(), true);
            var firstReleaseOperation = new ReleaseLockOperation
            {
                PartitionKey = new PartitionKey("PartitionKey1")
            };

            storageSession.AddOperation(firstReleaseOperation);

            var secondReleaseOperation = new ReleaseLockOperation
            {
                PartitionKey = new PartitionKey("PartitionKey2")
            };

            storageSession.AddOperation(secondReleaseOperation);

            storageSession.Dispose();

            Assert.That(firstReleaseOperation.WasApplied, Is.True);
            Assert.That(secondReleaseOperation.WasApplied, Is.True);
            Assert.That(firstReleaseOperation.WasDisposed, Is.True);
            Assert.That(secondReleaseOperation.WasDisposed, Is.True);
            Assert.That(firstReleaseOperation.AppliedBatch, Is.Not.EqualTo(secondReleaseOperation.AppliedBatch), "Release operations with the different partition keys must be in different batches.");
        }
        public async Task Should_not_dispose_release_operations_when_operations_not_successful()
        {
            var fakeContainer    = new FakeContainer();
            var fakeCosmosClient = new FakeCosmosClient(fakeContainer);
            var containerHolderHolderResolver = new ContainerHolderResolver(new FakeProvider(fakeCosmosClient),
                                                                            new ContainerInformation("fakeContainer", new PartitionKeyPath("/deep/down")), "fakeDatabase");

            var storageSession = new StorageSession(containerHolderHolderResolver, new ContextBag(), true);
            var operation      = new ThrowOnApplyOperation
            {
                PartitionKey = new PartitionKey("PartitionKey1")
            };

            storageSession.AddOperation(operation);
            var releaseOperation = new ReleaseLockOperation
            {
                PartitionKey = new PartitionKey("PartitionKey1")
            };

            storageSession.AddOperation(releaseOperation);

            try
            {
                await((ICompletableSynchronizedStorageSession)storageSession).CompleteAsync();
            }
            catch
            {
                // ignored
            }

            Assert.That(releaseOperation.WasApplied, Is.False);
            Assert.That(releaseOperation.WasDisposed, Is.False);
        }
        public void Should_execute_and_dispose_release_operations_as_best_effort()
        {
            var fakeContainer = new FakeContainer
            {
                TransactionalBatchFactory = () => new ThrowsOnExecuteAsyncTransactionalBatch()
            };
            var fakeCosmosClient = new FakeCosmosClient(fakeContainer);
            var containerHolderHolderResolver = new ContainerHolderResolver(new FakeProvider(fakeCosmosClient),
                                                                            new ContainerInformation("fakeContainer", new PartitionKeyPath("/deep/down")), "fakeDatabase");

            var storageSession        = new StorageSession(containerHolderHolderResolver, new ContextBag(), true);
            var firstReleaseOperation = new ReleaseLockOperation
            {
                PartitionKey = new PartitionKey("PartitionKey1")
            };

            storageSession.AddOperation(firstReleaseOperation);

            var secondReleaseOperation = new ReleaseLockOperation
            {
                PartitionKey = new PartitionKey("PartitionKey2")
            };

            storageSession.AddOperation(secondReleaseOperation);

            Assert.DoesNotThrow(() => storageSession.Dispose());

            Assert.That(firstReleaseOperation.WasApplied, Is.True);
            Assert.That(secondReleaseOperation.WasApplied, Is.True);
            Assert.That(firstReleaseOperation.WasDisposed, Is.True);
            Assert.That(secondReleaseOperation.WasDisposed, Is.True);
        }