public async Task <IEnumerable <T> > ExecuteSelectAllAsync <T>(string sql, Func <IDatabaseResultReader, T> mappingFunc) where T : class, IRestResource { try { using (IDatabaseConnection connection = _sqlClassProvider.CreateSqlConnection(_connectionString)) using (IDatabaseCommand command = _sqlClassProvider.CreateSqlCommand(sql, connection)) { await connection.OpenAsync().ConfigureAwait(false); using (IDatabaseResultReader reader = await command.ExecuteReaderAsync().ConfigureAwait(false)) { List <T> results = new List <T>(); while (await reader.ReadAsync().ConfigureAwait(false)) { results.Add(mappingFunc(reader)); } return(results); } } } catch (InvalidCastException ex) { throw InvalidConfigurationException.DataTypeMismatch(typeof(T).Name, ex); } }
public void CreateSqlCommand_NullSqlParameter_ThrowsArgumentNullException() { // Arrange ISqlClassProvider sut = CreateSut(); TestDelegate result = () => sut.CreateSqlCommand(null, Substitute.For <IDatabaseConnection>()); // Act // Assert Assert.Throws <ArgumentNullException>(result); }
public void CreateSqlCommand_NullSqlConnectionParameter_ThrowsArgumentNullException() { // Arrange ISqlClassProvider sut = CreateSut(); string sql = "SELECT * FROM TABLE"; TestDelegate result = () => sut.CreateSqlCommand(sql, null); // Act // Assert Assert.Throws <ArgumentNullException>(result); }
public void CreateSqlCommand_CorrectParameters_ReturnsIDatabaseCommandInstance() { // Arrange ISqlClassProvider sut = CreateSut(); string sql = "SELECT * FROM TABLE"; // Act var result = sut.CreateSqlCommand(sql, Substitute.For <IDatabaseConnection>()); // Assert Assert.IsInstanceOf <IDatabaseCommand>(result); }