|
3 | 3 | // See the LICENSE file in the project root for more information. |
4 | 4 |
|
5 | 5 | using System; |
| 6 | +using System.Collections.Generic; |
6 | 7 | using System.Globalization; |
7 | 8 | using System.IO; |
8 | 9 | using System.Linq; |
@@ -1019,5 +1020,189 @@ public void TestMixedDataTypesInCsv() |
1019 | 1020 | Assert.Equal("", emptyColumn[i]); |
1020 | 1021 | } |
1021 | 1022 | } |
| 1023 | + |
| 1024 | + public readonly struct LoadCsvVerifyingHelper |
| 1025 | + { |
| 1026 | + private readonly int _columnCount; |
| 1027 | + private readonly long _rowCount; |
| 1028 | + private readonly string[] _columnNames; |
| 1029 | + private readonly Type[] _columnTypes; |
| 1030 | + private readonly object[][] _cells; |
| 1031 | + |
| 1032 | + public LoadCsvVerifyingHelper(int columnCount, long rowCount, string[] columnNames, Type[] columnTypes, object[][] cells) |
| 1033 | + { |
| 1034 | + _columnCount = columnCount; |
| 1035 | + _rowCount = rowCount; |
| 1036 | + _columnNames = columnNames; |
| 1037 | + _columnTypes = columnTypes; |
| 1038 | + _cells = cells; |
| 1039 | + |
| 1040 | + } |
| 1041 | + |
| 1042 | + public void VerifyLoadCsv(DataFrame df) |
| 1043 | + { |
| 1044 | + Assert.Equal(_rowCount, df.Rows.Count); |
| 1045 | + Assert.Equal(_columnCount, df.Columns.Count); |
| 1046 | + |
| 1047 | + for (int j = 0; j < _columnCount; j++) |
| 1048 | + { |
| 1049 | + Assert.True(_columnTypes[j] == df.Columns[j].DataType); |
| 1050 | + Assert.Equal(_columnNames[j], df.Columns[j].Name); |
| 1051 | + |
| 1052 | + } |
| 1053 | + |
| 1054 | + VerifyColumnTypes(df); |
| 1055 | + |
| 1056 | + for (int i = 0; i < _rowCount; i++) |
| 1057 | + { |
| 1058 | + Assert.Equal(_cells[i], df.Rows[i]); |
| 1059 | + } |
| 1060 | + } |
| 1061 | + } |
| 1062 | + |
| 1063 | + public static IEnumerable<object[]> CsvWithTextQualifiers_TestData() |
| 1064 | + { |
| 1065 | + yield return new object[] // Comma Separators in Data |
| 1066 | + { |
| 1067 | + @"Name,Age,Description |
| 1068 | +Paul,34,""Paul lives in Vermont, VA."" |
| 1069 | +Victor,29,""Victor: Funny guy"" |
| 1070 | +Maria,31,", |
| 1071 | + ',', |
| 1072 | + new Type[] { typeof(string), typeof(int), typeof(string) }, |
| 1073 | + new LoadCsvVerifyingHelper( |
| 1074 | + 3, |
| 1075 | + 3, |
| 1076 | + new string[] { "Name", "Age", "Description" }, |
| 1077 | + new Type[] { typeof(string), typeof(int), typeof(string) }, |
| 1078 | + new object[][] |
| 1079 | + { |
| 1080 | + new object[] { "Paul", 34, "Paul lives in Vermont, VA." }, |
| 1081 | + new object[] { "Victor", 29, "Victor: Funny guy" }, |
| 1082 | + new object[] { "Maria", 31, "" } |
| 1083 | + } |
| 1084 | + ) |
| 1085 | + }; |
| 1086 | + yield return new object[] // Colon Separators in Data |
| 1087 | + { |
| 1088 | + @"Name:Age:Description |
| 1089 | +Paul:34:""Paul lives in Vermont, VA."" |
| 1090 | +Victor:29:""Victor: Funny guy"" |
| 1091 | +Maria:31:", |
| 1092 | + ':', |
| 1093 | + new Type[] { typeof(string), typeof(int), typeof(string) }, |
| 1094 | + new LoadCsvVerifyingHelper( |
| 1095 | + 3, |
| 1096 | + 3, |
| 1097 | + new string[] { "Name", "Age", "Description" }, |
| 1098 | + new Type[] { typeof(string), typeof(int), typeof(string) }, |
| 1099 | + new object[][] |
| 1100 | + { |
| 1101 | + new object[] { "Paul", 34, "Paul lives in Vermont, VA." }, |
| 1102 | + new object[] { "Victor", 29, "Victor: Funny guy" }, |
| 1103 | + new object[] { "Maria", 31, "" } |
| 1104 | + } |
| 1105 | + ) |
| 1106 | + }; |
| 1107 | + yield return new object[] // Comma Separators in Header |
| 1108 | + { |
| 1109 | + @"""Na,me"",Age,Description |
| 1110 | +Paul,34,""Paul lives in Vermont, VA."" |
| 1111 | +Victor,29,""Victor: Funny guy"" |
| 1112 | +Maria,31,", |
| 1113 | + ',', |
| 1114 | + new Type[] { typeof(string), typeof(int), typeof(string) }, |
| 1115 | + new LoadCsvVerifyingHelper( |
| 1116 | + 3, |
| 1117 | + 3, |
| 1118 | + new string[] { "Na,me", "Age", "Description" }, |
| 1119 | + new Type[] { typeof(string), typeof(int), typeof(string) }, |
| 1120 | + new object[][] |
| 1121 | + { |
| 1122 | + new object[] { "Paul", 34, "Paul lives in Vermont, VA." }, |
| 1123 | + new object[] { "Victor", 29, "Victor: Funny guy" }, |
| 1124 | + new object[] { "Maria", 31, "" } |
| 1125 | + } |
| 1126 | + ) |
| 1127 | + }; |
| 1128 | + yield return new object[] // Newlines In Data |
| 1129 | + { |
| 1130 | + @"Name,Age,Description |
| 1131 | +Paul,34,""Paul lives in Vermont |
| 1132 | +VA."" |
| 1133 | +Victor,29,""Victor: Funny guy"" |
| 1134 | +Maria,31,", |
| 1135 | + ',', |
| 1136 | + new Type[] { typeof(string), typeof(int), typeof(string) }, |
| 1137 | + new LoadCsvVerifyingHelper( |
| 1138 | + 3, |
| 1139 | + 3, |
| 1140 | + new string[] { "Name", "Age", "Description" }, |
| 1141 | + new Type[] { typeof(string), typeof(int), typeof(string) }, |
| 1142 | + new object[][] |
| 1143 | + { |
| 1144 | + new object[] { "Paul", 34, @"Paul lives in Vermont |
| 1145 | +VA." }, |
| 1146 | + new object[] { "Victor", 29, "Victor: Funny guy" }, |
| 1147 | + new object[] { "Maria", 31, "" } |
| 1148 | + } |
| 1149 | + ) |
| 1150 | + }; |
| 1151 | + yield return new object[] // Newlines In Header |
| 1152 | + { |
| 1153 | + @"""Na |
| 1154 | +me"":Age:Description |
| 1155 | +Paul:34:""Paul lives in Vermont, VA."" |
| 1156 | +Victor:29:""Victor: Funny guy"" |
| 1157 | +Maria:31:", |
| 1158 | + ':', |
| 1159 | + new Type[] { typeof(string), typeof(int), typeof(string) }, |
| 1160 | + new LoadCsvVerifyingHelper( |
| 1161 | + 3, |
| 1162 | + 3, |
| 1163 | + new string[] { @"Na |
| 1164 | +me", "Age", "Description" }, |
| 1165 | + new Type[] { typeof(string), typeof(int), typeof(string) }, |
| 1166 | + new object[][] |
| 1167 | + { |
| 1168 | + new object[] { "Paul", 34, "Paul lives in Vermont, VA." }, |
| 1169 | + new object[] { "Victor", 29, "Victor: Funny guy" }, |
| 1170 | + new object[] { "Maria", 31, "" } |
| 1171 | + } |
| 1172 | + ) |
| 1173 | + }; |
| 1174 | + } |
| 1175 | + |
| 1176 | + [Theory] |
| 1177 | + [MemberData(nameof(CsvWithTextQualifiers_TestData))] |
| 1178 | + public void TestLoadCsvWithTextQualifiersFromStream(string data, char separator, Type[] dataTypes, LoadCsvVerifyingHelper helper) |
| 1179 | + { |
| 1180 | + DataFrame df = DataFrame.LoadCsv(GetStream(data), dataTypes: dataTypes, separator: separator); |
| 1181 | + helper.VerifyLoadCsv(df); |
| 1182 | + } |
| 1183 | + |
| 1184 | + [Theory] |
| 1185 | + [MemberData(nameof(CsvWithTextQualifiers_TestData))] |
| 1186 | + public void TestLoadCsvWithTextQualifiersFromString(string data, char separator, Type[] dataTypes, LoadCsvVerifyingHelper helper) |
| 1187 | + { |
| 1188 | + DataFrame df = DataFrame.LoadCsvFromString(data, dataTypes: dataTypes, separator: separator); |
| 1189 | + helper.VerifyLoadCsv(df); |
| 1190 | + } |
| 1191 | + |
| 1192 | + [Theory] |
| 1193 | + [MemberData(nameof(CsvWithTextQualifiers_TestData))] |
| 1194 | + public void TestWriteCsvWithTextQualifiers(string data, char separator, Type[] dataTypes, LoadCsvVerifyingHelper helper) |
| 1195 | + { |
| 1196 | + DataFrame df = DataFrame.LoadCsv(GetStream(data), dataTypes: dataTypes, separator: separator); |
| 1197 | + |
| 1198 | + using MemoryStream csvStream = new MemoryStream(); |
| 1199 | + DataFrame.WriteCsv(df, csvStream, separator: separator); |
| 1200 | + |
| 1201 | + // We are verifying that WriteCsv works by reading the result back to a DataFrame and verifying correctness, |
| 1202 | + // ensuring no information loss |
| 1203 | + csvStream.Seek(0, SeekOrigin.Begin); |
| 1204 | + DataFrame df2 = DataFrame.LoadCsv(csvStream, dataTypes: dataTypes, separator: separator); |
| 1205 | + helper.VerifyLoadCsv(df2); |
| 1206 | + } |
1022 | 1207 | } |
1023 | 1208 | } |
0 commit comments