public void Count_Causes_ErrorResponse() { _repoMock.Setup(x => x.CountAsync()).ThrowsAsync(_exception); var response = _service.CountAsync().Result as BaseErrorResponse; Assert.Equal(StatusCode.Error, response.StatusCode); Assert.Equal(_exception.Message, response.Exception.Message); }
public void Count_Has_Result() { _repoMock.Setup(x => x.CountAsync()).Returns(Task.FromResult(1)); var response = _service.CountAsync().Result as CountResponse; Assert.Equal(StatusCode.Ok, response.StatusCode); Assert.Equal(1, response.Count); }
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"); }