public ISqlResult ExecuteQuery(string connectionString, string sql) { ISqlResult result = new SqlResult(); bool gettingColumnNames = true; using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); var cmd = new SqlCommand(sql, conn); var reader = cmd.ExecuteReader(); while(reader.Read()) { var newRow = new Dictionary<string,object>(); for(int i = 0; i < reader.FieldCount; i++) { if(gettingColumnNames) { gettingColumnNames = i < reader.FieldCount; result.ColumnNames.Add(reader.GetName(i)); } newRow.Add(reader.GetName(i), reader[i]); } result.Rows.Add(newRow); } } return result; }
public void EmptyStringReturnedWithEmptyObject() { var _testResult = new SqlResult(); var output = _testResult.PrintDataToText(); Assert.Equal("",output); }
public void ColumnsWithNoRowsReturnsEmptyString() { var _testResult = new SqlResult(); _testResult.ColumnNames.Add("col1"); var output = _testResult.PrintDataToText(); Assert.Equal("",output); }
public void ValidDataReturnsFormattedOutput() { var _testResult = new SqlResult(); _testResult.ColumnNames.Add("col1"); _testResult.Rows.Add(new Dictionary<string, object> {{"col1","test"}}); var output = _testResult.PrintDataToText(); Assert.Equal("col1 \n---- \ntest \n",output); }
public void RowsWithNoColumnNamesThrowsAnException() { var _testResult = new SqlResult(); _testResult.Rows.Add(new Dictionary<string, object> {{"test","test"}}); var result = Record.Exception(() => _testResult.PrintDataToText()); Assert.NotNull(result); Assert.IsType<Exception>(result); Assert.Equal("No column names were provided, so a result can't be displayed.", result.Message); }