Skip to content

Commit 97f3b17

Browse files
authored
Merge pull request #23 from ipvalverde/fix-observable-collection-bug
Fixes #21 for ObservableCollection properties
2 parents 3ce7be2 + c6f420a commit 97f3b17

6 files changed

Lines changed: 413 additions & 9 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
4+
using System.Linq.Expressions;
5+
using System.Text;
6+
using Xunit;
7+
8+
namespace EPPlus.DataExtractor.Tests
9+
{
10+
public class ExpressionHelperTests
11+
{
12+
class ModelWithObservableCollection
13+
{
14+
public ObservableCollection<string> CollectionProp { get; set; }
15+
}
16+
17+
[Fact]
18+
public void ValidatePropertyExpressionType_WithIncompatiblePropertyType_ShouldFail()
19+
{
20+
var collectionPropExpression = GetExpression<ModelWithObservableCollection, string>(m => m.CollectionProp);
21+
22+
Assert.Throws<ArgumentException>(() =>
23+
ExpressionExtensions.ValidatePropertyExpressionType<ModelWithObservableCollection, string, Collection<string>>(collectionPropExpression));
24+
}
25+
26+
class ModelWithCollection
27+
{
28+
public Collection<string> CollectionProp { get; set; }
29+
}
30+
31+
[Fact]
32+
public void ValidatePropertyExpressionType_WithSameCollectionPropertyType()
33+
{
34+
var collectionPropExpression = GetExpression<ModelWithCollection, string>(m => m.CollectionProp);
35+
36+
ExpressionExtensions.ValidatePropertyExpressionType<ModelWithCollection, string, Collection<string>>(collectionPropExpression);
37+
}
38+
39+
class ModelWithList
40+
{
41+
public List<string> CollectionProp { get; set; }
42+
}
43+
44+
[Fact]
45+
public void ValidatePropertyExpressionType_WithSameListPropertyType()
46+
{
47+
var collectionPropExpression = GetExpression<ModelWithList, string>(m => m.CollectionProp);
48+
49+
ExpressionExtensions.ValidatePropertyExpressionType<ModelWithList, string, List<string>>(collectionPropExpression);
50+
}
51+
52+
private static Expression<Func<TModel, Collection<TItem>>> GetExpression<TModel, TItem>(Expression<Func<TModel, Collection<TItem>>> property) => property;
53+
private static Expression<Func<TModel, List<TItem>>> GetExpression<TModel, TItem>(Expression<Func<TModel, List<TItem>>> property) => property;
54+
}
55+
}

src/EPPlus.DataExtractor.Tests/WorksheetExtensionsTests.cs

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using OfficeOpenXml;
22
using System;
33
using System.Collections.Generic;
4+
using System.Collections.ObjectModel;
45
using System.IO;
56
using System.Linq;
67
using Xunit;
@@ -276,7 +277,7 @@ public void ExtractDataTransformingColumnsIntoRows_WithICollectionPropertyNotIni
276277
.Extract<RowDataWithColumnBeingRowWithUninitializedICollection>()
277278
.WithProperty(p => p.Name, "F")
278279
.WithProperty(p => p.Age, "G")
279-
.WithCollectionProperty(p => p.MoneyData,
280+
.WithInitializedCollectionProperty(p => p.MoneyData,
280281
item => item.Date, 1,
281282
item => item.ReceivedMoney, "H", "S")
282283
.GetData(2, 4);
@@ -295,7 +296,7 @@ public void ExtractDataTransformingColumnsIntoRows_WithICollectionProperty()
295296
.Extract<RowDataWithColumnBeingRowWithInitializedICollection>()
296297
.WithProperty(p => p.Name, "F")
297298
.WithProperty(p => p.Age, "G")
298-
.WithCollectionProperty(p => p.MoneyData,
299+
.WithInitializedCollectionProperty(p => p.MoneyData,
299300
item => item.Date, 1,
300301
item => item.ReceivedMoney, "H", "S")
301302
.GetData(2, 4)
@@ -449,7 +450,7 @@ public void ExtractDataTransformingAGroupOfColumnsIntoRows_WithICollectionProper
449450
.Extract<CarDealerBranch.CarDealerBranchRevenueWithICollection>()
450451
.WithProperty(p => p.BranchName, "A")
451452
.WithProperty(p => p.BranchLocation, "B")
452-
.WithCollectionProperty(p => p.RevenueByMonth,
453+
.WithInitializedCollectionProperty(p => p.RevenueByMonth,
453454
1,
454455
"C",
455456
cfg => cfg
@@ -496,7 +497,7 @@ public void ExtractDataTransformingAGroupOfColumnsIntoRows_WithICollectionProper
496497
.Extract<CarDealerBranch.CarDealerBranchRevenueWithUninitializedICollection>()
497498
.WithProperty(p => p.BranchName, "A")
498499
.WithProperty(p => p.BranchLocation, "B")
499-
.WithCollectionProperty(p => p.RevenueByMonth,
500+
.WithInitializedCollectionProperty(p => p.RevenueByMonth,
500501
1,
501502
"C",
502503
cfg => cfg
@@ -556,7 +557,7 @@ public void ExtractSimpleDataCollection_WithICollectionProperty()
556557
.Extract<MultiLingualUserDataWithICollection>()
557558
.WithProperty(p => p.FirstName, "B")
558559
.WithProperty(p => p.LastName, "A")
559-
.WithCollectionProperty(x => x.LanguagesSpoken, "C", "E")
560+
.WithInitializedCollectionProperty(x => x.LanguagesSpoken, "C", "E")
560561
// Read from row 2 to 4
561562
.GetData(2, 4)
562563
.ToList();
@@ -592,12 +593,75 @@ public void ExtractSimpleDataCollection_WithICollectionPropertyNotInitialized_Sh
592593
.Extract<MultiLingualUserDataWithUnintializedICollection>()
593594
.WithProperty(p => p.FirstName, "B")
594595
.WithProperty(p => p.LastName, "A")
595-
.WithCollectionProperty(x => x.LanguagesSpoken, "C", "E")
596+
.WithInitializedCollectionProperty(x => x.LanguagesSpoken, "C", "E")
596597
// Read from row 2 to 4
597598
.GetData(2, 4);
598599

599600
Assert.Throws<InvalidOperationException>(() => dataEnumerable.ToList());
600601
}
601602
}
603+
604+
public class MultiLingualUserDataWithObservableCollection :
605+
BaseMultiLingualUserData<ObservableCollection<string>>
606+
{
607+
public MultiLingualUserDataWithObservableCollection()
608+
{
609+
this.LanguagesSpoken = new ObservableCollection<string>();
610+
}
611+
}
612+
613+
[Fact]
614+
public void ExtractSimpleDataCollection_WithObservableCollectionPropertyInCollectionOverload_ShouldFail()
615+
{
616+
var fileInfo = GetSpreadsheetFileInfo();
617+
using (var package = new ExcelPackage(fileInfo))
618+
{
619+
var worksheet = package.Workbook.Worksheets["StringsCollectionWorksheet"];
620+
621+
var dataExtractorSetup = worksheet
622+
.Extract<MultiLingualUserDataWithObservableCollection>()
623+
.WithProperty(p => p.FirstName, "B")
624+
.WithProperty(p => p.LastName, "A");
625+
626+
Assert.Throws<ArgumentException>(() =>
627+
dataExtractorSetup.WithCollectionProperty(p => p.LanguagesSpoken, "C", "E"));
628+
}
629+
}
630+
631+
[Fact]
632+
public void ExtractSimpleDataCollection_WithObservableCollectionProperty()
633+
{
634+
var fileInfo = GetSpreadsheetFileInfo();
635+
using (var package = new ExcelPackage(fileInfo))
636+
{
637+
var worksheet = package.Workbook.Worksheets["StringsCollectionWorksheet"];
638+
639+
var items = worksheet
640+
.Extract<MultiLingualUserDataWithObservableCollection>()
641+
.WithProperty(p => p.FirstName, "B")
642+
.WithProperty(p => p.LastName, "A")
643+
.WithInitializedCollectionProperty(x => x.LanguagesSpoken, "C", "E")
644+
// Read from row 2 to 4
645+
.GetData(2, 4)
646+
.ToList();
647+
648+
// 3 rows should be read.
649+
Assert.Equal(3, items.Count);
650+
651+
// First record should have 2 languages
652+
Assert.Equal(2, items[0].LanguagesSpoken.Count);
653+
Assert.Contains("Spanish", items[0].LanguagesSpoken);
654+
Assert.Contains("Romanian", items[0].LanguagesSpoken);
655+
656+
// Second record should have 3 languages
657+
Assert.Equal(3, items[1].LanguagesSpoken.Count);
658+
Assert.Contains("English", items[1].LanguagesSpoken);
659+
Assert.Contains("Latin", items[1].LanguagesSpoken);
660+
Assert.Contains("Mandarin", items[1].LanguagesSpoken);
661+
662+
// Third record should have no languages
663+
Assert.Empty(items[2].LanguagesSpoken);
664+
}
665+
}
602666
}
603667
}

0 commit comments

Comments
 (0)