-
Notifications
You must be signed in to change notification settings - Fork 294
Expand file tree
/
Copy pathExcelQueryFactoryTests.cs
More file actions
393 lines (322 loc) · 15.7 KB
/
ExcelQueryFactoryTests.cs
File metadata and controls
393 lines (322 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
using System.Linq;
using NUnit.Framework;
using System;
using System.IO;
using System.Data;
using LinqToExcel.Domain;
namespace LinqToExcel.Tests
{
using LinqToExcel.Query;
[Author("Paul Yoder", "paulyoder@gmail.com")]
[Category("Unit")]
[TestFixture]
public class ExcelQueryFactoryTests
{
private string _excelFileName;
private string _excelFileWithBuiltinWorksheets;
private string _excelFileWithNamedRanges;
[SetUp]
public void s()
{
var excelFilesDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ExcelFiles");
_excelFileName = Path.Combine(excelFilesDirectory, "Companies.xls");
_excelFileWithBuiltinWorksheets = Path.Combine(excelFilesDirectory, "Companies.xlsx");
_excelFileWithNamedRanges = Path.Combine(excelFilesDirectory, "NamedRanges.xlsx");
}
[Test]
public void throw_argumentnullexception_when_filename_is_null()
{
var repo = new ExcelQueryFactory(new LogManagerFactory());
Assert.That(() => (from r in repo.Worksheet() select r).First(),
Throws.TypeOf<ArgumentNullException>());
}
[Test]
public void Constructor_sets_filename()
{
var repo = new ExcelQueryFactory(@"C:\spreadsheet.xls", new LogManagerFactory());
Assert.AreEqual(@"C:\spreadsheet.xls", repo.FileName);
}
[Test]
public void Constructor_defaults_UsePersistentConnection_to_false()
{
var repo = new ExcelQueryFactory(new LogManagerFactory());
Assert.AreEqual(false, repo.UsePersistentConnection);
}
[Test]
public void Constructor_defaults_ReadOnly_to_false()
{
var repo = new ExcelQueryFactory(new LogManagerFactory());
Assert.AreEqual(false, repo.ReadOnly);
}
[Test]
public void GetWorksheetNames_throws_exception_when_filename_not_set()
{
var factory = new ExcelQueryFactory(new LogManagerFactory());
Assert.That(() => factory.GetWorksheetNames(),
Throws.TypeOf<NullReferenceException>(), "FileName property is not set");
}
[Test]
public void GetColumnNames_throws_exception_when_filename_not_set()
{
var factory = new ExcelQueryFactory(new LogManagerFactory());
Assert.That(() => factory.GetColumnNames(""),
Throws.TypeOf<NullReferenceException>(), "FileName property is not set");
}
[Test]
public void GetWorksheetNames_returns_worksheet_names()
{
var excel = new ExcelQueryFactory(_excelFileName, new LogManagerFactory());
var worksheetNames = excel.GetWorksheetNames();
Assert.AreEqual(
"ColumnMappings, IMEX Table, Invalid Cast, More Companies, Null Dates, Range1, Sheet1, TrimSpaces",
string.Join(", ", worksheetNames.ToArray()));
}
[Test]
public void GetWorksheetNames_does_not_include_builtin_worksheets()
{
var excel = new ExcelQueryFactory(_excelFileWithBuiltinWorksheets, new LogManagerFactory());
var worksheetNames = excel.GetWorksheetNames();
Assert.AreEqual(
"AutoFiltered, ColumnMappings, MoreCompanies, NullCells, Paul's Worksheet, Sheet1",
string.Join(", ", worksheetNames.ToArray()));
}
[Test] //This test is no longer passing. I believe it has something to do with my computer settings
public void GetWorksheetNames_does_not_include_named_ranges()
{
var excel = new ExcelQueryFactory(_excelFileWithNamedRanges, new LogManagerFactory());
var worksheetNames = excel.GetWorksheetNames();
Assert.AreEqual(
"Tabelle1, Tabelle3, WS2",
string.Join(", ", worksheetNames.ToArray()));
}
[Test]
public void GetNamedRanges()
{
var excel = new ExcelQueryFactory(_excelFileWithNamedRanges, new LogManagerFactory());
var namedRanges = excel.GetNamedRanges(excel.GetWorksheetNames().First());
Assert.AreEqual(
"NameCell",
string.Join(", ", namedRanges.ToArray()));
}
[Test]
public void GetNamedRangeValue()
{
var excel = new ExcelQueryFactory(_excelFileWithNamedRanges, new LogManagerFactory());
var firstCellValue = excel.NamedRangeNoHeader("Tabelle1", "NameCell").First().First().Value;
Assert.AreEqual(
"NameCell",
firstCellValue);
}
[Test]
public void GetWorksheetNames_does_not_delete_apostrophes_in_middle_of_worksheet_name()
{
var excel = new ExcelQueryFactory(_excelFileWithBuiltinWorksheets, new LogManagerFactory());
var worksheetNames = excel.GetWorksheetNames();
Assert.IsTrue(worksheetNames.Any(x => x == "Paul's Worksheet"));
}
[Test]
public void GetColumnNames_returns_column_names()
{
var excel = new ExcelQueryFactory(_excelFileName, new LogManagerFactory());
var columnNames = excel.GetColumnNames("Sheet1");
Assert.AreEqual(
"Name, CEO, EmployeeCount, StartDate",
string.Join(", ", columnNames.ToArray()));
}
[Test]
public void StrictMapping_ClassStrict_throws_StrictMappingException_when_property_is_not_mapped_to_column()
{
var excel = new ExcelQueryFactory(_excelFileName, new LogManagerFactory());
excel.StrictMapping = StrictMappingType.ClassStrict;
var companies = (from x in excel.Worksheet<CompanyWithCity>()
select x);
Assert.That(() => companies.ToList(),
Throws.TypeOf<StrictMappingException>(), "'City' property is not mapped to a column");
}
[Test]
public void StrictMapping_ClassStrict_with_additional_unused_worksheet_columns_doesnt_throw_exception()
{
var excel = new ExcelQueryFactory(_excelFileName, new LogManagerFactory());
excel.StrictMapping = StrictMappingType.ClassStrict;
excel.AddMapping<Company>(x => x.IsActive, "Active");
var companies = (from c in excel.Worksheet<CompanyNullable>()
where c.Name == "ACME"
select c).ToList();
Assert.AreEqual(1, companies.Count);
}
[Test]
public void StrictMapping_ClassStrict_with_ignore_attribute_doesnt_throw_exception()
{
var excel = new ExcelQueryFactory(_excelFileName, new LogManagerFactory());
excel.StrictMapping = StrictMappingType.ClassStrict;
var companies = (from c in excel.Worksheet<CompanyIgnoreIsActive>("More Companies")
where c.Name == "ACME"
select c).ToList();
Assert.AreEqual(1, companies.Count);
}
[Test]
public void StrictMapping_WorksheetStrict_throws_StrictMappingException_when_column_is_not_mapped_to_property()
{
var excel = new ExcelQueryFactory(_excelFileName, new LogManagerFactory());
excel.StrictMapping = StrictMappingType.WorksheetStrict;
var companies = (from x in excel.Worksheet<Company>("Null Dates")
select x);
Assert.That(() => companies.ToList(),
Throws.TypeOf<StrictMappingException>(), "'City' column is not mapped to a property");
}
[Test]
public void StrictMapping_WorksheetStrict_with_additional_unused_class_properties_doesnt_throw_exception()
{
var excel = new ExcelQueryFactory(_excelFileName, new LogManagerFactory());
excel.StrictMapping = StrictMappingType.WorksheetStrict;
excel.AddMapping<Company>(x => x.IsActive, "Active");
var companies = (from c in excel.Worksheet<CompanyWithCity>()
where c.Name == "ACME"
select c).ToList();
Assert.AreEqual(1, companies.Count);
}
[Test]
public void StrictMapping_WorksheetStrict_with_ignore_attribute_throws_exception()
{
var excel = new ExcelQueryFactory(_excelFileName, new LogManagerFactory());
excel.StrictMapping = StrictMappingType.WorksheetStrict;
var companies = (from c in excel.Worksheet<CompanyIgnoreIsActive>("More Companies")
where c.Name == "ACME"
select c);
Assert.That(() => companies.ToList(),
Throws.TypeOf<StrictMappingException>(), "'Active' column is not mapped to a property");
}
[Test]
public void StrictMapping_Both_throws_StrictMappingException_when_property_is_not_mapped_to_column()
{
var excel = new ExcelQueryFactory(_excelFileName, new LogManagerFactory());
excel.StrictMapping = StrictMappingType.Both;
var companies = (from x in excel.Worksheet<CompanyWithCity>()
select x);
Assert.That(() => companies.ToList(),
Throws.TypeOf<StrictMappingException>(), "'City' column is not mapped to a property");
}
[Test]
public void StrictMapping_Both_throws_StrictMappingException_when_column_is_not_mapped_to_property()
{
var excel = new ExcelQueryFactory(_excelFileName, new LogManagerFactory());
excel.StrictMapping = StrictMappingType.Both;
var companies = (from x in excel.Worksheet<Company>("Null Dates")
select x);
Assert.That(() => companies.ToList(),
Throws.TypeOf<StrictMappingException>(), "'City' column is not mapped to a property");
}
[Test]
public void StrictMapping_Both_with_column_mappings_doesnt_throw_exception()
{
var excel = new ExcelQueryFactory(_excelFileName, new LogManagerFactory());
excel.StrictMapping = StrictMappingType.Both;
excel.AddMapping<Company>(x => x.IsActive, "Active");
var companies = (from c in excel.Worksheet<Company>("More Companies")
where c.Name == "ACME"
select c).ToList();
Assert.AreEqual(1, companies.Count);
}
[Test]
public void StrictMapping_Both_with_ignore_attribute_doesnt_throw_exception()
{
var excel = new ExcelQueryFactory(_excelFileName, new LogManagerFactory());
excel.StrictMapping = StrictMappingType.Both;
var companies = (from c in excel.Worksheet<CompanyIgnoreIsActive>("Sheet1")
where c.Name == "ACME"
select c).ToList();
Assert.AreEqual(1, companies.Count);
}
[Test]
public void StrictMapping_None_with_additional_worksheet_column_doesnt_throw_exception()
{
var excel = new ExcelQueryFactory(_excelFileName, new LogManagerFactory());
excel.StrictMapping = StrictMappingType.None;
excel.AddMapping<Company>(x => x.IsActive, "Active");
var companies = (from c in excel.Worksheet<Company>("Null Dates")
where c.Name == "ACME"
select c).ToList();
Assert.AreEqual(1, companies.Count);
}
[Test]
public void StrictMapping_None_with_additional_class_properties_doesnt_throw_exception()
{
var excel = new ExcelQueryFactory(_excelFileName, new LogManagerFactory());
excel.StrictMapping = StrictMappingType.None;
excel.AddMapping<Company>(x => x.IsActive, "Active");
var companies = (from c in excel.Worksheet<CompanyWithCity>()
where c.Name == "ACME"
select c).ToList();
Assert.AreEqual(1, companies.Count);
}
[Test]
public void StrictMapping_Not_Explicitly_Set_with_additional_worksheet_column_doesnt_throw_exception()
{
var excel = new ExcelQueryFactory(_excelFileName, new LogManagerFactory());
excel.AddMapping<Company>(x => x.IsActive, "Active");
var companies = (from c in excel.Worksheet<Company>("Null Dates")
where c.Name == "ACME"
select c).ToList();
Assert.AreEqual(1, companies.Count);
}
[Test]
public void StrictMapping_Not_Explicitly_Set_with_additional_class_properties_doesnt_throw_exception()
{
var excel = new ExcelQueryFactory(_excelFileName, new LogManagerFactory());
excel.AddMapping<Company>(x => x.IsActive, "Active");
var companies = (from c in excel.Worksheet<CompanyWithCity>()
where c.Name == "ACME"
select c).ToList();
Assert.AreEqual(1, companies.Count);
}
[Test]
public void TrimSpaces_Start_TrimsWhiteSpacesAtTheBeginning()
{
var excel = new ExcelQueryFactory(_excelFileName, new LogManagerFactory());
excel.TrimSpaces = TrimSpacesType.Start;
var companies = excel.Worksheet<Company>("TrimSpaces").ToList();
Assert.AreEqual("White Space In Front", companies[0].Name);
}
[Test]
public void TrimSpaces_End_TrimsWhiteSpacesAtTheEnd()
{
var excel = new ExcelQueryFactory(_excelFileName, new LogManagerFactory());
excel.TrimSpaces = TrimSpacesType.End;
var companies = excel.Worksheet<Company>("TrimSpaces").ToList();
Assert.AreEqual("White Space At End", companies[1].Name);
}
[Test]
public void TrimSpaces_Both_TrimsWhiteSpacesOnBothSides()
{
var excel = new ExcelQueryFactory(_excelFileName, new LogManagerFactory());
excel.TrimSpaces = TrimSpacesType.Both;
var companies = excel.Worksheet<Company>("TrimSpaces").ToList();
Assert.AreEqual("White Space On Both Sides", companies[2].Name);
}
[Test]
public void TrimSpaces_None_DoesntTrimWhitespace()
{
var excel = new ExcelQueryFactory(_excelFileName, new LogManagerFactory());
excel.TrimSpaces = TrimSpacesType.None;
var companies = excel.Worksheet<Company>("TrimSpaces").ToList();
Assert.AreEqual(" White Space On Both Sides ", companies[2].Name);
}
[Test]
public void Null_ConstantExpression_in_where_expression_should_not_throw_exception()
{
// The C# 6 compiler made some changes to the expression tree output.
// This changes are largely transparent to consumers if they adhere
// to the Liskov Substitution Principle. This test is to specifically
// address a bug in which the code didn't allow the derived TypedConstantExpression
// to be substituted for a ConstantExpression. This resulted in a downstream InvalidCastException
// when the LinqToExcel invoked Converter.ChangeType since TypedConstantExpression
// does not implement IConvertible.
var excel = new ExcelQueryFactory(_excelFileName);
excel.AddMapping<Company>(x => x.IsActive, "Active");
var companies = (from c in excel.Worksheet<CompanyNullable>()
where c.Name != null
select c).ToList();
Assert.IsNotNull(companies);
}
}
}