Ejemplo n.º 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));
        }
        private CosmosDBExtensionConfigProvider InitializeExtensionConfigProvider(string defaultConnStr, string optionsConnStr = null)
        {
            var options        = CosmosDBTestUtility.InitializeOptions(defaultConnStr, optionsConnStr);
            var factory        = new DefaultCosmosDBServiceFactory();
            var nameResolver   = new TestNameResolver();
            var configProvider = new CosmosDBExtensionConfigProvider(options, factory, _emptyConfig, nameResolver, NullLoggerFactory.Instance);

            var context = TestHelpers.CreateExtensionConfigContext(nameResolver);

            configProvider.Initialize(context);

            return(configProvider);
        }
Ejemplo n.º 3
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();
        }
Ejemplo n.º 4
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();
        }
Ejemplo n.º 5
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();
        }
Ejemplo n.º 6
0
        public async Task GetValueAsync_Throws_WhenErrorResponse()
        {
            // Arrange
            IValueBinder binder = CreateBinder <Item>(out Mock <ICosmosDBService> mockService);

            mockService
            .Setup(m => m.ReadDocumentAsync(_expectedUri, null))
            .ThrowsAsync(CosmosDBTestUtility.CreateDocumentClientException(HttpStatusCode.ServiceUnavailable));

            // Act
            // TODO: Fix this up so it exposes the real exception
            var ex = await Assert.ThrowsAsync <DocumentClientException>(() => binder.GetValueAsync());

            // Assert
            Assert.Equal(HttpStatusCode.ServiceUnavailable, ex.StatusCode);
        }
Ejemplo n.º 7
0
        public async Task GetValueAsync_DoesNotThrow_WhenResponseIsNotFound()
        {
            // Arrange
            IValueBinder binder = CreateBinder <Item>(out Mock <ICosmosDBService> mockService);

            mockService
            .Setup(m => m.ReadDocumentAsync(_expectedUri, null))
            .ThrowsAsync(CosmosDBTestUtility.CreateDocumentClientException(HttpStatusCode.NotFound));

            // Act
            var value = await binder.GetValueAsync();

            // Assert
            Assert.Null(value);
            mockService.VerifyAll();
        }
Ejemplo n.º 8
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();
        }
Ejemplo n.º 9
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());
        }
Ejemplo n.º 11
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();
        }
Ejemplo n.º 12
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();
        }