public void Valid_Data_Is_Persisted() { _repoMock.Setup(x => x.CreateAsync(_model)).Returns(Task.FromResult(_model)); var response = _service.CreateAsync(_model).Result; Assert.Equal(StatusCode.Created, response.StatusCode); _repoMock.Verify(x => x.CreateAsync(It.IsAny <Profile>())); Assert.Equal(_myDateTime.UtcNow, response.Data.Created); Assert.Equal(_myDateTime.UtcNow, response.Data.Updated); }
public async void CreateAsync_ReturnsCreatedElement() { var repositoryMock = new Mock <ICrudRepository>(); repositoryMock.Setup(_ => _.CreateAsync(It.IsAny <TestEntity>())).ReturnsAsync(_entity); var service = new CrudService <TestEntity>(repositoryMock.Object); var result = await service.CreateAsync(_entity); Assert.Equal(_entity.Id, result.Id); repositoryMock.Verify(_ => _.CreateAsync(It.IsAny <TestEntity>()), Times.Once); repositoryMock.Verify(_ => _.SaveChanges(), 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"); }