-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
LookupMatchesAnalyzerTest.cs
223 lines (194 loc) · 9.44 KB
/
LookupMatchesAnalyzerTest.cs
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
using NBi.Core.ResultSet;
using NBi.Core.ResultSet.Lookup;
using NBi.Core.Scalar.Comparer;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NBi.Testing.Core.ResultSet.Lookup
{
public class LookupMatchesAnalyzerTest
{
protected DataTable BuildDataTable(object[] keys, object[] values)
{
var ds = new DataSet();
var dt = ds.Tables.Add("myTable");
var keyCol = dt.Columns.Add("myKey");
var valueCol = dt.Columns.Add("myValue");
for (int i = 0; i < keys.Length; i++)
{
var dr = dt.NewRow();
dr.SetField<object>(keyCol, keys[i]);
dr.SetField<object>(valueCol, values[i]);
dt.Rows.Add(dr);
}
return dt;
}
protected DataTable BuildDataTable(object[] keys, object[] secondKeys, object[] values)
{
var ds = new DataSet();
var dt = ds.Tables.Add("myTable");
var keyCol = dt.Columns.Add("zero");
var secondKeyCol = dt.Columns.Add("one");
var valueCol = dt.Columns.Add("two");
for (int i = 0; i < keys.Length; i++)
{
var dr = dt.NewRow();
dr.SetField<object>(keyCol, keys[i]);
dr.SetField<object>(secondKeyCol, secondKeys[i]);
dr.SetField<object>(valueCol, values[i]);
dt.Rows.Add(dr);
}
return dt;
}
private ColumnMappingCollection BuildColumnMapping(int count, int shift = 0, ColumnType columnType = ColumnType.Text)
{
var mappings = new ColumnMappingCollection();
for (int i = 0; i < count; i++)
mappings.Add(new ColumnMapping(new ColumnOrdinalIdentifier(i + shift), new ColumnOrdinalIdentifier(i + shift), columnType));
return mappings;
}
[Test]
public void Execute_ReferenceLargerThanCandidateMatchingValue_NoViolation()
{
var candidate = BuildDataTable(new[] { "Key0", "Key1" }, new object[] { 0, 1 });
var reference = BuildDataTable(new[] { "Key0", "Key1", "Key2" }, new object[] { 0, 1, 1 });
var analyzer = new LookupMatchesAnalyzer(BuildColumnMapping(1), BuildColumnMapping(1,1));
var violations = analyzer.Execute(candidate, reference);
Assert.That(violations.Count(), Is.EqualTo(0));
}
[Test]
public void Execute_ReferenceLargerThanCandidateMatchingValueWhenNoToleranceApplied_OneViolation()
{
var candidate = BuildDataTable(new[] { "Key0", "Key1" }, new object[] { 0, 1 });
var reference = BuildDataTable(new[] { "Key0", "Key1", "Key2" }, new object[] { 0, 2, 1 });
var analyzer = new LookupMatchesAnalyzer(BuildColumnMapping(1), BuildColumnMapping(1, 1, ColumnType.Numeric));
var violations = analyzer.Execute(candidate, reference);
Assert.That(violations.Count(), Is.EqualTo(1));
}
[Test]
public void Execute_ReferenceLargerThanCandidateMatchingValueWhenToleranceApplied_NoViolation()
{
var candidate = BuildDataTable(new[] { "Key0", "Key1" }, new object[] { 0, 1 });
var reference = BuildDataTable(new[] { "Key0", "Key1", "Key2" }, new object[] { 0, 2, 1 });
var tolerances = new Dictionary<IColumnIdentifier, Tolerance>() { { new ColumnIdentifierFactory().Instantiate("#1"), new NumericAbsoluteTolerance(1, SideTolerance.Both) } };
var analyzer = new LookupMatchesAnalyzer(BuildColumnMapping(1), BuildColumnMapping(1, 1, ColumnType.Numeric), tolerances);
var violations = analyzer.Execute(candidate, reference);
Assert.That(violations.Count(), Is.EqualTo(0));
}
[Test]
public void Execute_ReferenceLargerThanCandidateDuplicateKeys_NoViolation()
{
var candidate = BuildDataTable(new[] { "Key0", "Key1" }, new object[] { 0, 1 });
var reference = BuildDataTable(new[] { "Key0", "Key1", "Key2", "Key1", "Key2" }, new object[] { 0, 2, 3, 1, 3 });
var analyzer = new LookupMatchesAnalyzer(BuildColumnMapping(1), BuildColumnMapping(1, 1));
var violations = analyzer.Execute(candidate, reference);
Assert.That(violations.Count(), Is.EqualTo(0));
}
[Test]
public void Execute_MissingKeyInReference_OneViolation()
{
var candidate = BuildDataTable(new[] { "Key0", "Key1" }, new object[] { 0, 1 });
var reference = BuildDataTable(new[] { "Key0", "Key2", "Key2", "Key0", "Key2" }, new object[] { 0, 1, 1, 1, 1 });
var analyzer = new LookupMatchesAnalyzer(BuildColumnMapping(1), BuildColumnMapping(1, 1));
var violations = analyzer.Execute(candidate, reference);
Assert.That(violations.Count(), Is.EqualTo(1));
}
[Test]
public void Execute_NotMatchValueInReference_OneViolation()
{
var candidate = BuildDataTable(new[] { "Key0", "Key1" }, new object[] { 0, 1 });
var reference = BuildDataTable(new[] { "Key0", "Key1", "Key1", "Key0", "Key2" }, new object[] { 0, 2, 3, 4, 5 });
var analyzer = new LookupMatchesAnalyzer(BuildColumnMapping(1), BuildColumnMapping(1, 1));
var violations = analyzer.Execute(candidate, reference);
Assert.That(violations.Count(), Is.EqualTo(1));
}
[Test]
public void Execute_MultipleKeysreferenceLargerThanCandidateDuplicateKeys_NoViolation()
{
var candidate = BuildDataTable(new[] { "Key0", "Key1" }, new[] { "Foo", "Bar" }, new object[] { 0, 1 });
var reference = BuildDataTable(new[] { "Key0", "Key1", "Key2" }, new[] { "Foo", "Bar", "Bar" }, new object[] { 0, 1, 2 });
var referencer = new LookupMatchesAnalyzer(BuildColumnMapping(2), BuildColumnMapping(1, 2));
var violations = referencer.Execute(candidate, reference);
Assert.That(violations.Count(), Is.EqualTo(0));
}
[Test]
[TestCase(1000)]
[TestCase(10000)]
[TestCase(100000)]
[TestCase(1000000)]
[Retry(3)]
[Parallelizable(ParallelScope.Self)]
public void Execute_LargeVolumeReference_Fast(int maxItem)
{
var candidate = BuildDataTable(new[] { "Key0", "Key1", "Key0" }, new[] { "Foo", "Bar", "Foo" }, new object[] { 1, 2, 3 });
var reference = new DataTable();
var idColumn = reference.Columns.Add("id");
var valueColumn = reference.Columns.Add("value");
var randomizer = new Random();
for (int i = 0; i < maxItem; i++)
{
var dr = reference.NewRow();
dr.SetField<object>(idColumn, i);
dr.SetField<object>(valueColumn, randomizer.Next().ToString());
reference.Rows.Add(dr);
}
reference.AcceptChanges();
var mappingKey = new ColumnMappingCollection
{
new ColumnMapping(new ColumnNameIdentifier("two"), new ColumnNameIdentifier("id"), ColumnType.Numeric)
};
var mappingValue = new ColumnMappingCollection
{
new ColumnMapping(new ColumnNameIdentifier("one"), new ColumnNameIdentifier("value"), ColumnType.Text)
};
var analyzer = new LookupMatchesAnalyzer(mappingKey, mappingValue);
var stopWatch = new Stopwatch();
stopWatch.Start();
analyzer.Execute(candidate, reference);
stopWatch.Stop();
Assert.That(stopWatch.Elapsed.TotalSeconds, Is.LessThan(20));
}
[Test]
[TestCase(1000)]
[TestCase(10000)]
[TestCase(100000)]
[TestCase(1000000)]
[Retry(3)]
[Parallelizable(ParallelScope.Self)]
public void Execute_LargeVolumeCandidate_Fast(int maxItem)
{
var reference = BuildDataTable(new[] { "Key0", "Key1", "Key0" }, new[] { "Foo", "Bar", "Foo" }, new object[] { 1, 2, 3 });
var candidate = new DataTable();
var idColumn = candidate.Columns.Add("id");
var valueColumn = candidate.Columns.Add("value");
var randomizer = new Random();
for (int i = 0; i < maxItem; i++)
{
var dr = candidate.NewRow();
dr.SetField<object>(idColumn, i);
dr.SetField<object>(valueColumn, randomizer.Next().ToString());
candidate.Rows.Add(dr);
}
candidate.AcceptChanges();
var mappingKey = new ColumnMappingCollection
{
new ColumnMapping(new ColumnNameIdentifier("id"), new ColumnNameIdentifier("two"), ColumnType.Numeric)
};
var mappingValue = new ColumnMappingCollection
{
new ColumnMapping(new ColumnNameIdentifier("value"), new ColumnNameIdentifier("one"), ColumnType.Text)
};
var analyzer = new LookupMatchesAnalyzer(mappingKey, mappingValue);
var stopWatch = new Stopwatch();
stopWatch.Start();
var violations = analyzer.Execute(candidate, reference);
stopWatch.Stop();
Assert.That(stopWatch.Elapsed.TotalSeconds, Is.LessThan(7));
}
}
}