public void Document_Is_Deleted() { _repoMock.Setup(x => x.GetByIdAsync(_model.Id)).Returns(Task.FromResult(_responseModel)); _repoMock.Setup(x => x.DeleteAsync(_model.Id)).Returns(Task.FromResult(true)); var response = _service.DeleteAsync(_model.Id).Result; Assert.Equal(StatusCode.NoContent, response.StatusCode); _repoMock.Verify(x => x.DeleteAsync(It.IsAny <object>())); }
public async void DeleteAsync_ThrowsEntityNotFoundException() { var repositoryMock = new Mock <ICrudRepository>(); repositoryMock.Setup(_ => _.GetByIdAsync <TestEntity>(It.IsAny <Guid>())).ThrowsAsync(new EntityNotFoundException()); var service = new CrudService <TestEntity>(repositoryMock.Object); await Assert.ThrowsAsync <EntityNotFoundException>(() => service.DeleteAsync(Guid.Empty)); }
public async void DeleteAsync_ReturnsDeletedElement() { var repositoryMock = new Mock <ICrudRepository>(); repositoryMock.Setup(_ => _.GetByIdAsync <TestEntity>(_entity.Id)).ReturnsAsync(_entity); var service = new CrudService <TestEntity>(repositoryMock.Object); var result = await service.DeleteAsync(_entity.Id); Assert.Equal(_entity.Id, result.Id); repositoryMock.Verify(_ => _.GetByIdAsync <TestEntity>(It.IsAny <Guid>()), Times.Once()); repositoryMock.Verify(_ => _.DeleteAsync <TestEntity>(It.IsAny <Guid>()), Times.Once); repositoryMock.Verify(_ => _.SaveChanges(), Times.Once); }
public async Task Delete_Resturns_WhenEntityDeleted() { //Arrage var context = new Mock <DbContext>(); context.Setup(x => x.Set <Entity>().FindAsync(It.IsAny <int>())).ReturnsAsync(new Mock <Entity>().Object); var service = new CrudService <Entity>(context.Object); //Act await service.DeleteAsync(default(int)); //Assert context.Verify(x => x.Remove(It.IsAny <Entity>()), Times.Once); context.Verify(x => x.SaveChangesAsync(It.IsAny <CancellationToken>()), Times.Once); }
public void Delete_ThrowsDbUpdateException_WhenCannotDelete() { //Arrage var context = new Mock <DbContext>(); var entity = new Mock <Entity>().Object; context.Setup(x => x.Set <Entity>().FindAsync(It.IsAny <int>())).ReturnsAsync(entity); context.Setup(x => x.SaveChangesAsync(It.IsAny <CancellationToken>())).ThrowsAsync(new DbUpdateException()); var service = new CrudService <Entity>(context.Object); //Act Func <Task> func = () => service.DeleteAsync(default(int)); //Assert Assert.ThrowsAsync <DbUpdateException>(func); context.Verify(x => x.Remove(It.IsAny <Entity>()), Times.Once); }
private void Run() { // get config values for CosmosDB var endpointUrl = Configuration["AppSettings:EndpointUrl"]; var authorizationKey = Configuration["AppSettings:AuthorizationKey"]; var databaseName = Configuration["AppSettings:DatabaseName"]; var collectionName = typeof(Profile).Name; // instantiate IRepository DocumentClient documentClient = new DocumentClient(new Uri(endpointUrl), authorizationKey); IRepository <Profile> repository = new DocumentDbRepository <Profile>(documentClient, databaseName, collectionName); // instantiate service. Note: Inherit from ICrudservice to add more functionality & use the repository var service = new CrudService <Profile>(repository); // create 1 var milo = new Profile { Id = "1", FirstName = "Milo", LastName = "Aukerman", Email = "*****@*****.**", Occupation = "" }; var createResponse = service.CreateAsync(milo).Result; if (createResponse.Success) { System.Console.WriteLine($"CreateAsync {createResponse.StatusCode}"); ShowObject(createResponse); } // create with duplicate ID var bill = new Profile { Id = "2", FirstName = "Bill", LastName = "Stevenson", Email = "*****@*****.**" }; createResponse = service.CreateAsync(bill).Result; System.Console.WriteLine($"CreateAsync {createResponse.StatusCode}"); if (createResponse is ConflictResponse <Profile> typedConflictRes) { ShowObject(typedConflictRes); } var stephen = new Profile { // id will be generated by database server FirstName = "Stephen", LastName = "Egerton", Email = "*****@*****.**", Occupation = "Guitarist" }; createResponse = service.CreateAsync(stephen).Result; if (createResponse.Success) { System.Console.WriteLine($"CreateAsync {createResponse.StatusCode}"); ShowObject(createResponse); } // get var getResponse = service.GetByIdAsync("1").Result; System.Console.WriteLine($"\nGetByIdAsync {getResponse.StatusCode}"); if (getResponse is GetResponse <Profile> typedGetRes) { ShowObject(typedGetRes); } // update var docToUpdate = service.GetByIdAsync("1").Result as GetResponse <Profile>; var modifiedModel = docToUpdate.Data; modifiedModel.Occupation = "Singer"; var updateResponse = service.UpdateAsync(modifiedModel).Result; System.Console.WriteLine($"\nUpdateAsync {updateResponse.StatusCode}"); ShowObject(updateResponse); var nonExistingDoc = new Profile { Id = "Not-a-real-id" }; updateResponse = service.UpdateAsync(nonExistingDoc).Result; System.Console.WriteLine($"\nUpdateAsync {updateResponse.StatusCode}"); if (updateResponse is UpdateNotFoundResponse <Profile> typedNotFoundRes) { ShowObject(typedNotFoundRes); } // get all var responseQueryable = service.GetAllAsync().Result; System.Console.WriteLine($"\nGetAllAsync {responseQueryable.Success} {responseQueryable.StatusCode}"); responseQueryable.Data.ToList().ForEach(ShowObject); // query responseQueryable = service.QueryAsync("SELECT * FROM Profile p WHERE p.firstName = 'Milo'").Result; System.Console.Write($"\nQueryAsync {responseQueryable.Success} {responseQueryable.StatusCode}"); if (responseQueryable is QueryResponse <Profile> typedQueryRes) { typedQueryRes.Data.ToList().ForEach(ShowObject); } // count var countResponse = service.CountAsync().Result; System.Console.WriteLine($"CountAsync {countResponse.StatusCode}"); ShowObject(countResponse); // count by query countResponse = service.CountAsync("SELECT * FROM Profile p WHERE p.firstName = 'Milo'").Result; System.Console.WriteLine($"\nCountAsync by SQL {countResponse.StatusCode}"); ShowObject(countResponse); // first or default by query var firstResponse = service.FirstOrDefaultAsync("SELECT * FROM Profile p WHERE p.firstName = 'Milo'").Result; System.Console.WriteLine($"\nFirstOrDefaultAsync {firstResponse.StatusCode}"); ShowObject(firstResponse); // delete var responseDelete = service.DeleteAsync("1").Result; System.Console.WriteLine($"\nDeleteAsync {responseDelete.StatusCode}"); ShowObject(responseDelete); // delete responseDelete = service.DeleteAsync("Not-a-real-ID").Result; System.Console.WriteLine($"\nDeleteAsync {responseDelete.StatusCode}"); if (responseDelete is DeleteNotFoundResponse typedNotFoundDelRes) { ShowObject(responseDelete); } AddStoredProc(documentClient, databaseName, collectionName, "my_sproc"); var responseSproc = service.ExecuteStoredProcedureAsync <string>("my_sproc", "Milo").Result; System.Console.WriteLine($"\nExecuteStoredProcedureAsync {responseSproc.StatusCode}"); ShowObject(responseSproc); // cleanup System.Console.WriteLine($"\nDeleting {databaseName}"); documentClient.DeleteDatabaseAsync(UriFactory.CreateDatabaseUri(databaseName)).Wait(); System.Console.WriteLine($"\nDone"); }