public async Task AddAsync_Creates_IfTrue_AndNotFound(string partitionKeyPath, int collectionThroughput) { // Arrange var mockService = new Mock <IDocumentDBService>(MockBehavior.Strict); DocumentDBContext context = CreateContext(mockService.Object, partitionKeyPath: partitionKeyPath, throughput: collectionThroughput, createIfNotExists: true); var collector = new DocumentDBAsyncCollector <Item>(context); mockService .SetupSequence(m => m.UpsertDocumentAsync(It.IsAny <Uri>(), It.IsAny <object>())) .Throws(DocumentDBTestUtility.CreateDocumentClientException(HttpStatusCode.NotFound)) .Returns(Task.FromResult(new Document())); SetupDatabaseMock(mockService); 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 AddAsync_Retries_IfThrottled() { // Arrange var databases = new Database[] { new Database { Id = DatabaseName } }; var collections = new DocumentCollection[] { new DocumentCollection { Id = CollectionName } }; var mockDocDBService = new Mock <IDocumentDBService>(MockBehavior.Strict); var context = CreateContext(mockDocDBService.Object); var collector = new DocumentDBAsyncCollector <Item>(context); mockDocDBService .SetupSequence(m => m.CreateDocumentAsync(It.IsAny <Uri>(), It.IsAny <object>())) .Throws(DocumentDBTestUtility.CreateDocumentClientException((HttpStatusCode)429)) .Throws(DocumentDBTestUtility.CreateDocumentClientException((HttpStatusCode)429)) .Throws(DocumentDBTestUtility.CreateDocumentClientException((HttpStatusCode)429)) .Returns(Task.FromResult(new Document())); // Act await collector.AddAsync(new Item { Text = "hello!" }); // Assert mockDocDBService.VerifyAll(); }
public async Task CreateIfNotExist_Succeeds_IfDbAndCollectionExist() { // Arrange var mockService = new Mock <IDocumentDBService>(MockBehavior.Strict); DocumentDBContext context = CreateContext(mockService.Object); SetupDatabaseMock(mockService, databaseExists: true); SetupCollectionMock(mockService, collectionExists: true); // Act await DocumentDBAsyncCollector <Item> .CreateIfNotExistAsync(context); // Assert mockService.VerifyAll(); }
public async Task CreateIfNotExists_DoesNotSetThroughput_IfZero() { // Arrange string partitionKeyPath = "partitionKey"; var mockService = new Mock <IDocumentDBService>(MockBehavior.Strict); SetupCollectionMock(mockService, partitionKeyPath); // Act await DocumentDBAsyncCollector <Item> .CreateDocumentCollectionIfNotExistsAsync(mockService.Object, DatabaseName, CollectionName, partitionKeyPath, 0); // Assert mockService.VerifyAll(); }
public async Task AddAsync_CreatesDocument() { // Arrange var mockDocDBService = new Mock <IDocumentDBService>(MockBehavior.Strict); mockDocDBService .Setup(m => m.UpsertDocumentAsync(It.IsAny <Uri>(), It.IsAny <object>())) .ReturnsAsync(new Document()); var context = CreateContext(mockDocDBService.Object); var collector = new DocumentDBAsyncCollector <Item>(context); // Act await collector.AddAsync(new Item { Text = "hello!" }); // Assert mockDocDBService.VerifyAll(); }
public async Task AddAsync_DoesNotCreate_IfUpsertSucceeds() { // Arrange var mockDocDBService = new Mock <IDocumentDBService>(MockBehavior.Strict); var context = CreateContext(mockDocDBService.Object); context.ResolvedAttribute.CreateIfNotExists = true; var collector = new DocumentDBAsyncCollector <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 AddAsync_Throws_IfExceptionIsNotTooManyRequests() { // Arrange var mockDocDBService = new Mock <IDocumentDBService>(MockBehavior.Strict); mockDocDBService .Setup(m => m.UpsertDocumentAsync(It.IsAny <Uri>(), It.IsAny <object>())) .ThrowsAsync(DocumentDBTestUtility.CreateDocumentClientException(HttpStatusCode.NotFound)); var context = CreateContext(mockDocDBService.Object); var collector = new DocumentDBAsyncCollector <Item>(context); // Act await Assert.ThrowsAsync <DocumentClientException>(() => collector.AddAsync(new Item { Text = "hello!" })); // Assert mockDocDBService.VerifyAll(); }
public async Task CreateIfNotExist_Throws_IfExceptionIsNotConflict() { // Arrange var mockService = new Mock <IDocumentDBService>(MockBehavior.Strict); DocumentDBContext context = CreateContext(mockService.Object); SetupDatabaseMock(mockService); // overwrite the default setup with one that throws mockService .Setup(m => m.CreateDatabaseAsync(It.Is <Database>(d => d.Id == DatabaseName))) .ThrowsAsync(DocumentDBTestUtility.CreateDocumentClientException(HttpStatusCode.BadRequest)); // Act await Assert.ThrowsAsync <DocumentClientException>( () => DocumentDBAsyncCollector <Item> .CreateIfNotExistAsync(context)); // Assert mockService.VerifyAll(); }