예제 #1
0
        public async Task AddAsync_Creates_IfTrue_AndNotFound(string partitionKeyPath, int collectionThroughput)
        {
            // Arrange
            var             mockService = new Mock <ICosmosDBService>(MockBehavior.Strict);
            CosmosDBContext context     = CosmosDBTestUtility.CreateContext(mockService.Object, partitionKeyPath: partitionKeyPath,
                                                                            throughput: collectionThroughput, createIfNotExists: true);
            var collector = new CosmosDBAsyncCollector <Item>(context);

            mockService
            .SetupSequence(m => m.UpsertDocumentAsync(It.IsAny <Uri>(), It.IsAny <object>()))
            .Throws(CosmosDBTestUtility.CreateDocumentClientException(HttpStatusCode.NotFound))
            .Returns(Task.FromResult(new Document()));

            CosmosDBTestUtility.SetupDatabaseMock(mockService);
            CosmosDBTestUtility.SetupCollectionMock(mockService, partitionKeyPath, collectionThroughput);

            // Act
            await collector.AddAsync(new Item { Text = "hello!" });

            // Assert
            mockService.VerifyAll();

            // Verify that we upsert again after creation.
            mockService.Verify(m => m.UpsertDocumentAsync(It.IsAny <Uri>(), It.IsAny <object>()), Times.Exactly(2));
        }
예제 #2
0
        public async Task CreateIfNotExist_Succeeds()
        {
            // Arrange
            var             mockService = new Mock <ICosmosDBService>(MockBehavior.Strict);
            CosmosDBContext context     = CosmosDBTestUtility.CreateContext(mockService.Object);

            CosmosDBTestUtility.SetupDatabaseMock(mockService);
            CosmosDBTestUtility.SetupCollectionMock(mockService);

            // Act
            await CosmosDBUtility.CreateDatabaseAndCollectionIfNotExistAsync(context);

            // Assert
            mockService.VerifyAll();
        }
예제 #3
0
        public async Task CreateIfNotExists_DoesNotSetThroughput_IfZero()
        {
            // Arrange
            var mockService = new Mock <ICosmosDBService>(MockBehavior.Strict);
            var context     = CosmosDBTestUtility.CreateContext(mockService.Object, throughput: 0);

            CosmosDBTestUtility.SetupDatabaseMock(mockService);
            CosmosDBTestUtility.SetupCollectionMock(mockService);

            // Act
            await CosmosDBUtility.CreateDatabaseAndCollectionIfNotExistAsync(context);

            // Assert
            mockService.VerifyAll();
        }
예제 #4
0
        public async Task CreateIfNotExists_SetsPartitionKey_IfSpecified()
        {
            // Arrange
            string partitionKeyPath = "partitionKey";
            var    mockService      = new Mock <ICosmosDBService>(MockBehavior.Strict);
            var    context          = CosmosDBTestUtility.CreateContext(mockService.Object, partitionKeyPath: partitionKeyPath);

            CosmosDBTestUtility.SetupDatabaseMock(mockService);
            CosmosDBTestUtility.SetupCollectionMock(mockService, partitionKeyPath);

            // Act
            await CosmosDBUtility.CreateDatabaseAndCollectionIfNotExistAsync(context);

            // Assert
            mockService.VerifyAll();
        }
예제 #5
0
        public async Task AddAsync_CreatesDocument()
        {
            // Arrange
            var mockDocDBService = new Mock <ICosmosDBService>(MockBehavior.Strict);

            mockDocDBService
            .Setup(m => m.UpsertDocumentAsync(It.IsAny <Uri>(), It.IsAny <object>()))
            .ReturnsAsync(new Document());

            var context   = CosmosDBTestUtility.CreateContext(mockDocDBService.Object);
            var collector = new CosmosDBAsyncCollector <Item>(context);

            // Act
            await collector.AddAsync(new Item { Text = "hello!" });

            // Assert
            mockDocDBService.VerifyAll();
        }
예제 #6
0
        public async Task AddAsync_DoesNotCreate_IfUpsertSucceeds()
        {
            // Arrange
            var mockDocDBService = new Mock <ICosmosDBService>(MockBehavior.Strict);
            var context          = CosmosDBTestUtility.CreateContext(mockDocDBService.Object);

            context.ResolvedAttribute.CreateIfNotExists = true;
            var collector = new CosmosDBAsyncCollector <Item>(context);

            mockDocDBService
            .Setup(m => m.UpsertDocumentAsync(It.IsAny <Uri>(), It.IsAny <object>()))
            .Returns(Task.FromResult(new Document()));

            //// Act
            await collector.AddAsync(new Item { Text = "hello!" });

            // Assert
            mockDocDBService.VerifyAll();
        }
        public async Task ConvertAsync_RethrowsException_IfNotFound()
        {
            var builder = CreateBuilder <Item>(out Mock <ICosmosDBService> mockService);

            mockService
            .Setup(m => m.ExecuteNextAsync <Item>(_expectedUri, It.IsAny <SqlQuerySpec>(), It.IsAny <string>()))
            .ThrowsAsync(CosmosDBTestUtility.CreateDocumentClientException((HttpStatusCode)404));

            CosmosDBAttribute attribute = new CosmosDBAttribute(DatabaseName, CollectionName)
            {
                SqlQuery           = string.Empty,
                SqlQueryParameters = new SqlParameterCollection()
            };

            var ex = await Assert.ThrowsAsync <DocumentClientException>(() => builder.ConvertAsync(attribute, CancellationToken.None));

            Assert.Equal("NotFound", ex.Error.Code);

            mockService.Verify(m => m.ExecuteNextAsync <Item>(_expectedUri, It.IsAny <SqlQuerySpec>(), It.IsAny <string>()), Times.Once());
        }
예제 #8
0
        public async Task CreateIfNotExist_Rethrows()
        {
            // Arrange
            var             mockService = new Mock <ICosmosDBService>(MockBehavior.Strict);
            CosmosDBContext context     = CosmosDBTestUtility.CreateContext(mockService.Object);

            CosmosDBTestUtility.SetupDatabaseMock(mockService);

            // overwrite the default setup with one that throws
            mockService
            .Setup(m => m.CreateDatabaseIfNotExistsAsync(It.Is <Database>(d => d.Id == CosmosDBTestUtility.DatabaseName)))
            .ThrowsAsync(CosmosDBTestUtility.CreateDocumentClientException(HttpStatusCode.BadRequest));

            // Act
            await Assert.ThrowsAsync <DocumentClientException>(
                () => CosmosDBUtility.CreateDatabaseAndCollectionIfNotExistAsync(context));

            // Assert
            mockService.VerifyAll();
        }
예제 #9
0
        public async Task AddAsync_ThrowsWithCustomMessage_IfNotFound()
        {
            // Arrange
            var mockDocDBService = new Mock <ICosmosDBService>(MockBehavior.Strict);

            mockDocDBService
            .Setup(m => m.UpsertDocumentAsync(It.IsAny <Uri>(), It.IsAny <object>()))
            .ThrowsAsync(CosmosDBTestUtility.CreateDocumentClientException(HttpStatusCode.NotFound));

            var context   = CosmosDBTestUtility.CreateContext(mockDocDBService.Object, createIfNotExists: false);
            var collector = new CosmosDBAsyncCollector <Item>(context);

            // Act
            var ex = await Assert.ThrowsAsync <InvalidOperationException>(() => collector.AddAsync(new Item {
                Text = "hello!"
            }));

            // Assert
            Assert.Contains(CosmosDBTestUtility.CollectionName, ex.Message);
            Assert.Contains(CosmosDBTestUtility.DatabaseName, ex.Message);
            mockDocDBService.VerifyAll();
        }