Skip to content

Commit 705a4c9

Browse files
committed
adds tests for column comparators
1 parent 87606bc commit 705a4c9

File tree

2 files changed

+255
-3
lines changed

2 files changed

+255
-3
lines changed

src/main/java/io/github/vmzakharov/ecdataframe/dataframe/compare/ComparisonResult.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ public double dDelta()
3535
return this.delta();
3636
}
3737

38-
/*
39-
used to populate the delta value in case one of the values being compared is null. Otherwise, populating delta is
40-
delegated to the relevant type specific subclass
38+
/**
39+
* Sets the value of the "difference" or delta value between the values being compared.
40+
* It is used in this class to populate the delta value in case one of the values being compared is null.
41+
* Otherwise, populating delta is delegated to the relevant type-specific subclass
42+
* @param newDelta the calculated value of the "difference" or delta between the values being compared
4143
*/
4244
abstract protected void delta(int newDelta);
4345

@@ -61,6 +63,9 @@ public boolean noNulls()
6163
return this.nullSide == NO_NULLS;
6264
}
6365

66+
/*
67+
TODO: Fix visibility mismatch between the method and the return type
68+
*/
6469
public NullSide nullSide()
6570
{
6671
return this.nullSide;
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
package io.github.vmzakharov.ecdataframe.dataframe;
2+
3+
import io.github.vmzakharov.ecdataframe.dataframe.compare.ComparisonResult;
4+
import org.junit.jupiter.api.BeforeAll;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.math.BigDecimal;
8+
import java.time.LocalDate;
9+
import java.time.LocalDateTime;
10+
11+
import static org.junit.jupiter.api.Assertions.*;
12+
13+
public class DataFrameColumnComparatorTest
14+
{
15+
public static final double TOLERANCE = 0.00000001;
16+
17+
public static DataFrame df1;
18+
public static DataFrame df2;
19+
20+
@BeforeAll
21+
public static void initializeDataFrames()
22+
{
23+
df1 = new DataFrame("df1")
24+
.addStringColumn("string1")
25+
.addLongColumn("long1").addIntColumn("int1")
26+
.addDoubleColumn("double1").addFloatColumn("float1")
27+
.addBooleanColumn("boolean1")
28+
.addDateColumn("date1").addDateTimeColumn("dateTime1")
29+
.addDecimalColumn("decimal1")
30+
.addRow("a", 10L, null, 13.25, 41.0f, null, LocalDate.of(2020, 10, 20), null, BigDecimal.valueOf(123, 2))
31+
.addRow("b", null, 22, 23.25, null, false, null, LocalDateTime.of(2020, 10, 20, 16, 32), BigDecimal.valueOf(456, 2))
32+
.addRow(null, 30L, 23, null, 43.0f, false, LocalDate.of(2020, 10, 24), LocalDateTime.of(2020, 11, 20, 17, 33), null)
33+
.addRow("d", 40L, 24, 43.25, 44.5f, true, LocalDate.of(2020, 10, 26), LocalDateTime.of(2020, 12, 20, 18, 34), BigDecimal.valueOf(901, 2))
34+
;
35+
36+
df2 = new DataFrame("df2")
37+
.addStringColumn("string2")
38+
.addLongColumn("long2").addIntColumn("int2")
39+
.addDoubleColumn("double2").addFloatColumn("float2")
40+
.addBooleanColumn("boolean2")
41+
.addDateColumn("date2").addDateTimeColumn("dateTime2")
42+
.addDecimalColumn("decimal2")
43+
.addRow("a", 10L, 21, 13.25, 41.0f, true, LocalDate.of(2020, 10, 20), LocalDateTime.of(2020, 9, 20, 15, 31), BigDecimal.valueOf(123, 2))
44+
.addRow(null, 20L, 22, null, 42.0f, false, null, LocalDateTime.of(2020, 10, 20, 16, 32), BigDecimal.valueOf(456, 2))
45+
.addRow("c", null, 23, 33.50, null, false, LocalDate.of(2020, 10, 24), null, BigDecimal.valueOf(789, 2))
46+
.addRow("d", 40L, null, 43.25, 44.5f, null, LocalDate.of(2020, 10, 26), LocalDateTime.of(2020, 12, 20, 18, 34), null)
47+
;
48+
}
49+
50+
@Test
51+
public void stringComparator()
52+
{
53+
DfCellComparator cellComparator = df1.columnComparator(df2, "string1", "string2");
54+
this.valueEqualsValue(cellComparator.compare(0, 0));
55+
this.valueAndNull(cellComparator.compare(0, 1));
56+
this.nullAndValue(cellComparator.compare(2, 0));
57+
this.bothAreNulls(cellComparator.compare(2, 1));
58+
this.leftGreaterThanRight(cellComparator.compare(3, 0), "d".compareTo("a"));
59+
this.leftLessThanRight(cellComparator.compare(0, 3), "a".compareTo("d"));
60+
}
61+
62+
@Test
63+
public void longComparator()
64+
{
65+
DfCellComparator cellComparator = df1.columnComparator(df2, "long1", "long2");
66+
this.valueEqualsValue(cellComparator.compare(0, 0));
67+
this.valueAndNull(cellComparator.compare(0, 2));
68+
this.nullAndValue(cellComparator.compare(1, 0));
69+
this.bothAreNulls(cellComparator.compare(1, 2));
70+
this.leftGreaterThanRight(cellComparator.compare(3, 0), 40L - 10L);
71+
this.leftLessThanRight(cellComparator.compare(0, 3), 10L - 40L);
72+
}
73+
74+
@Test
75+
public void intComparator()
76+
{
77+
DfCellComparator cellComparator = df1.columnComparator(df2, "int1", "int2");
78+
this.valueEqualsValue(cellComparator.compare(1, 1));
79+
this.valueAndNull(cellComparator.compare(1, 3));
80+
this.nullAndValue(cellComparator.compare(0, 1));
81+
this.bothAreNulls(cellComparator.compare(0, 3));
82+
this.leftGreaterThanRight(cellComparator.compare(2, 1), 23 - 22);
83+
this.leftLessThanRight(cellComparator.compare(1, 2), 22 - 23);
84+
}
85+
86+
@Test
87+
public void doubleComparator()
88+
{
89+
DfCellComparator cellComparator = df1.columnComparator(df2, "double1", "double2");
90+
this.valueEqualsValue(cellComparator.compare(0, 0));
91+
this.valueAndNull(cellComparator.compare(0, 1));
92+
this.nullAndValue(cellComparator.compare(2, 0));
93+
this.bothAreNulls(cellComparator.compare(2, 1));
94+
this.leftGreaterThanRight(cellComparator.compare(3, 0), 43.25 - 13.25);
95+
this.leftLessThanRight(cellComparator.compare(0, 3), 13.25 - 43.25);
96+
}
97+
98+
@Test
99+
public void floatComparator()
100+
{
101+
DfCellComparator cellComparator = df1.columnComparator(df2, "float1", "float2");
102+
this.valueEqualsValue(cellComparator.compare(0, 0));
103+
this.valueAndNull(cellComparator.compare(0, 2));
104+
this.nullAndValue(cellComparator.compare(1, 0));
105+
this.bothAreNulls(cellComparator.compare(1, 2));
106+
this.leftGreaterThanRight(cellComparator.compare(3, 0), 44.5 - 41.0);
107+
this.leftLessThanRight(cellComparator.compare(0, 3), 41.0 - 44.5);
108+
}
109+
110+
@Test
111+
public void booleanComparator()
112+
{
113+
DfCellComparator cellComparator = df1.columnComparator(df2, "boolean1", "boolean2");
114+
this.valueEqualsValue(cellComparator.compare(2, 2));
115+
this.valueAndNull(cellComparator.compare(1, 3));
116+
this.nullAndValue(cellComparator.compare(0, 1));
117+
this.bothAreNulls(cellComparator.compare(0, 3));
118+
this.leftGreaterThanRight(cellComparator.compare(3, 1), Boolean.compare(true, false));
119+
this.leftLessThanRight(cellComparator.compare(1, 0), Boolean.compare(false, true));
120+
}
121+
122+
@Test
123+
public void dateComparator()
124+
{
125+
DfCellComparator cellComparator = df1.columnComparator(df2, "date1", "date2");
126+
this.valueEqualsValue(cellComparator.compare(0, 0));
127+
this.valueAndNull(cellComparator.compare(0, 1));
128+
this.nullAndValue(cellComparator.compare(1, 0));
129+
this.bothAreNulls(cellComparator.compare(1, 1));
130+
this.leftGreaterThanRight(cellComparator.compare(2, 0), LocalDate.of(2020, 10, 24).compareTo(LocalDate.of(2020, 10, 20)));
131+
this.leftLessThanRight(cellComparator.compare(0, 2), LocalDate.of(2020, 10, 20).compareTo(LocalDate.of(2020, 10, 24)));
132+
}
133+
134+
@Test
135+
public void dateTimeComparator()
136+
{
137+
DfCellComparator cellComparator = df1.columnComparator(df2, "dateTime1", "dateTime2");
138+
this.valueEqualsValue(cellComparator.compare(1, 1));
139+
this.valueAndNull(cellComparator.compare(1, 2));
140+
this.nullAndValue(cellComparator.compare(0, 1));
141+
this.bothAreNulls(cellComparator.compare(0, 2));
142+
this.leftGreaterThanRight(cellComparator.compare(3, 1), LocalDateTime.of(2020, 12, 20, 18, 34).compareTo(LocalDateTime.of(2020, 10, 20, 16, 32)));
143+
this.leftLessThanRight(cellComparator.compare(1, 3), LocalDateTime.of(2020, 10, 20, 16, 32).compareTo(LocalDateTime.of(2020, 12, 20, 18, 34)));
144+
}
145+
146+
@Test
147+
public void decimalComparator()
148+
{
149+
DfCellComparator cellComparator = df1.columnComparator(df2, "decimal1", "decimal2");
150+
this.valueEqualsValue(cellComparator.compare(0, 0));
151+
this.valueAndNull(cellComparator.compare(0, 3));
152+
this.nullAndValue(cellComparator.compare(2, 0));
153+
this.bothAreNulls(cellComparator.compare(2, 3));
154+
this.leftGreaterThanRight(
155+
cellComparator.compare(1, 0),
156+
BigDecimal.valueOf(456, 2).subtract(BigDecimal.valueOf(123, 2)).doubleValue());
157+
this.leftLessThanRight(
158+
cellComparator.compare(0, 1),
159+
BigDecimal.valueOf(123, 2).subtract(BigDecimal.valueOf(456, 2)).doubleValue());
160+
}
161+
162+
private void valueEqualsValue(ComparisonResult result)
163+
{
164+
assertEquals(0, result.compared());
165+
assertEquals(0.0, result.dDelta());
166+
assertEquals(0, result.delta());
167+
assertTrue(result.noNulls());
168+
assertFalse(result.leftIsNull());
169+
assertFalse(result.rightIsNull());
170+
assertFalse(result.bothAreNulls());
171+
}
172+
173+
private void valueAndNull(ComparisonResult result)
174+
{
175+
assertTrue(result.compared() > 0);
176+
assertEquals(1.0, result.dDelta());
177+
assertEquals(1, result.delta());
178+
assertFalse(result.noNulls());
179+
assertFalse(result.leftIsNull());
180+
assertTrue(result.rightIsNull());
181+
assertFalse(result.bothAreNulls());
182+
}
183+
184+
private void nullAndValue(ComparisonResult result)
185+
{
186+
assertTrue(result.compared() < 0);
187+
assertEquals(-1.0, result.dDelta());
188+
assertEquals(-1, result.delta());
189+
assertFalse(result.noNulls());
190+
assertTrue(result.leftIsNull());
191+
assertFalse(result.rightIsNull());
192+
assertFalse(result.bothAreNulls());
193+
}
194+
195+
private void bothAreNulls(ComparisonResult result)
196+
{
197+
assertEquals(0, result.compared());
198+
assertEquals(0.0, result.dDelta());
199+
assertEquals(0, result.delta());
200+
assertFalse(result.noNulls());
201+
assertTrue(result.bothAreNulls());
202+
assertTrue(result.leftIsNull());
203+
assertTrue(result.rightIsNull());
204+
}
205+
206+
private void leftGreaterThanRight(ComparisonResult result, long expectedDelta)
207+
{
208+
this.leftGreaterThanRight(result, expectedDelta, expectedDelta);
209+
}
210+
211+
private void leftGreaterThanRight(ComparisonResult result, double expectedDoubleDelta)
212+
{
213+
this.leftGreaterThanRight(result, Math.round(expectedDoubleDelta), expectedDoubleDelta);
214+
}
215+
216+
private void leftGreaterThanRight(ComparisonResult result, long expectedDelta, double expectedDoubleDelta)
217+
{
218+
assertTrue(result.compared() > 0);
219+
assertEquals(expectedDelta, result.delta());
220+
assertEquals(expectedDoubleDelta, result.dDelta(), TOLERANCE);
221+
assertTrue(result.noNulls());
222+
assertFalse(result.leftIsNull());
223+
assertFalse(result.rightIsNull());
224+
assertFalse(result.bothAreNulls());
225+
}
226+
227+
private void leftLessThanRight(ComparisonResult result, long expectedDelta)
228+
{
229+
this.leftLessThanRight(result, expectedDelta, expectedDelta);
230+
}
231+
232+
private void leftLessThanRight(ComparisonResult result, double expectedDoubleDelta)
233+
{
234+
this.leftLessThanRight(result, Math.round(expectedDoubleDelta), expectedDoubleDelta);
235+
}
236+
237+
private void leftLessThanRight(ComparisonResult result, long expectedDelta, double expectedDoubleDelta)
238+
{
239+
assertTrue(result.compared() < 0);
240+
assertEquals(expectedDelta, result.delta());
241+
assertEquals(expectedDoubleDelta, result.dDelta(), TOLERANCE);
242+
assertTrue(result.noNulls());
243+
assertFalse(result.leftIsNull());
244+
assertFalse(result.rightIsNull());
245+
assertFalse(result.bothAreNulls());
246+
}
247+
}

0 commit comments

Comments
 (0)