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)); }
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); }
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(); }
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()); }
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(); }
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(); }