Skip to content

Commit e7e3d83

Browse files
authored
Improved unit tests assertions (#635)
* improved unit tests assertions * rebased * improves test assertions * improves test assertions continued * removed unnecessary for loops and improved essertions * removed unnecessary for loops and improved essertions continued * replaced contains() with assertEquals() * improved test assertions * results order might change, updated assertions * asserted executedLoops and removed unnecessary check * fixed failed test * fixed failed test
1 parent 96c6a39 commit e7e3d83

16 files changed

Lines changed: 146 additions & 103 deletions

src/test/java/com/teragrep/pth_10/AddtotalsTransformationTest.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,10 @@ void addtotals_noparams_test() {
106106
.map(r -> r.getAs(0).toString())
107107
.sorted()
108108
.collect(Collectors.toList());
109-
List<String> expected = Arrays.asList("36.0", "11.0", "1.0", "-9.0", "48.2");
109+
List<String> expected = Arrays.asList("-9.0", "1.0", "11.0", "36.0", "48.2");
110110
Assertions.assertEquals(5, res.size());
111111
Assertions.assertEquals(5, expected.size());
112-
113-
for (String r : res) {
114-
if (!expected.contains(r)) {
115-
Assertions.fail("Value <" + r + "> was not one of the expected values!");
116-
}
117-
}
112+
Assertions.assertEquals(expected, res);
118113
});
119114
}
120115

@@ -135,12 +130,7 @@ void addtotals_colParam_test() {
135130
List<Double> expected = Arrays.asList(-10d, 0d, 10d, 35d, 47.2d, 82.2d);
136131
Assertions.assertEquals(6, res.size());
137132
Assertions.assertEquals(6, expected.size());
138-
139-
for (Double r : res) {
140-
if (!expected.contains(r)) {
141-
Assertions.fail("Value <" + r + "> was not one of the expected values!");
142-
}
143-
}
133+
Assertions.assertEquals(expected, res);
144134
});
145135
}
146136

src/test/java/com/teragrep/pth_10/ConvertTransformationTest.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,18 @@ void testConvertCtimeAs() {
115115
.stream()
116116
.map(r -> r.getAs(0).toString())
117117
.collect(Collectors.toList());
118+
119+
Pattern p = Pattern.compile("\\d{2}/\\d{2}/\\d{4} \\d{2}:\\d{2}:\\d{2}");
120+
121+
int loopCount = 0;
118122
for (String s : listOfResults) {
119123
// match 00/00/0000 00:00:00
120-
Matcher m = Pattern.compile("\\d{2}/\\d{2}/\\d{4} \\d{2}:\\d{2}:\\d{2}").matcher(s);
124+
Matcher m = p.matcher(s);
125+
loopCount++;
121126

122127
Assertions.assertTrue(m.find());
123128
}
129+
Assertions.assertEquals(12, loopCount);
124130
});
125131
}
126132

@@ -157,9 +163,8 @@ void testConvertCtime() {
157163
"01/01/1970 00:00:04", "01/01/1970 00:00:03", "01/01/1970 00:00:02", "01/01/1970 00:00:01"
158164
);
159165

160-
for (int i = 0; i < listOfResults.size(); i++) {
161-
Assertions.assertEquals(expectedResults.get(i), listOfResults.get(i));
162-
}
166+
Assertions.assertEquals(12, listOfResults.size());
167+
Assertions.assertEquals(expectedResults, listOfResults);
163168
});
164169
}
165170

src/test/java/com/teragrep/pth_10/DedupTransformationTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,14 @@ public void dedupTest_Consecutive() {
152152
List<Row> listOfRaw = res.select("_raw", "offset").collectAsList();
153153
listOfRaw.sort(Comparator.comparingLong(r -> r.getAs("offset")));
154154
Assertions.assertEquals(10, listOfRaw.size());
155+
156+
int loopCount = 0;
155157
for (int i = 0; i < listOfRaw.size(); i = i + 2) {
158+
loopCount++;
156159
Assertions.assertEquals("1", listOfRaw.get(i).get(0).toString());
157160
Assertions.assertEquals("2", listOfRaw.get(i + 1).get(0).toString());
158161
}
162+
Assertions.assertEquals(5, loopCount);
159163
});
160164
}
161165

src/test/java/com/teragrep/pth_10/DynatraceTestAPICallback.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public HttpResponse handle(HttpRequest httpRequest) {
8181
invalidCount++;
8282
}
8383

84-
Assertions.assertFalse(invalidCount > 0, "Received invalid message: " + reqString);
84+
Assertions.assertEquals(0, invalidCount, "Received invalid message: " + reqString);
8585

8686
statusCode = 202;
8787

src/test/java/com/teragrep/pth_10/EarliestLatestTest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -960,9 +960,7 @@ public void searchIssue382Test() {
960960
// test issue 382
961961
// case: index=* earliest=x latest=y abcdef and index=*abc* earliest=x latest=y abcdef match differently (data/no data)
962962
this.streamingTestUtil.performDPLTest("index=*g*", this.testFile, res -> {
963-
if (res.count() == 0) {
964-
Assertions.fail("(index=*g*) Expected result rows, instead got 0");
965-
}
963+
Assertions.assertNotEquals(0, res.count(), "(index=*g*) Expects result rows, should not get 0");
966964
});
967965
}
968966

@@ -975,9 +973,7 @@ public void searchIssue382Test2() {
975973
// test issue 382
976974
// index=* case
977975
this.streamingTestUtil.performDPLTest("index=*", this.testFile, res -> {
978-
if (res.count() == 0) {
979-
Assertions.fail("(index=*) Expected result rows, instead got 0");
980-
}
976+
Assertions.assertNotEquals(0, res.count(), "(index=*) Expects result rows, should not get 0");
981977
});
982978
}
983979

src/test/java/com/teragrep/pth_10/JoinTransformationTest.java

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@
5555
import org.slf4j.Logger;
5656
import org.slf4j.LoggerFactory;
5757

58+
import java.util.Arrays;
5859
import java.util.List;
60+
import java.util.Objects;
61+
import java.util.stream.Collectors;
5962

6063
/**
6164
* Tests for the new ProcessingStack implementation Uses streaming datasets
@@ -213,17 +216,21 @@ public void joinTypeLeftMax3Test() {
213216
"Batch handler dataset contained an unexpected column arrangement !"
214217
);
215218

216-
List<Row> listOfRows = ds.collectAsList();
217-
218219
// 3 rows should be not null, since only three subsearch matches are requested using max=3
219-
int notNulls = 0;
220-
for (Row r : listOfRows) {
221-
if (r.getAs("R_a") != null) {
222-
notNulls++;
223-
}
224-
}
225-
226-
Assertions.assertEquals(3, notNulls, "subsearch limit 3, so 3 should be not null");
220+
final List<String> listOfRows = ds
221+
.select("R_a")
222+
.orderBy("offset")
223+
.collectAsList()
224+
.stream()
225+
.map(r -> r.get(0))
226+
.filter(Objects::nonNull)
227+
.map(Object::toString)
228+
.collect(Collectors.toList());
229+
230+
Assertions.assertEquals(3, listOfRows.size());
231+
final List<String> expected = Arrays.asList("1", "2", "1");
232+
Assertions.assertEquals(expected, listOfRows);
233+
227234
}
228235
);
229236
}
@@ -305,14 +312,16 @@ public void join0MaxOverwriteExplicitTest() {
305312
"Batch handler dataset contained an unexpected column arrangement !"
306313
);
307314

308-
Assertions.assertEquals(10, ds.count(), "Should return 10 rows");
309-
310-
List<Row> listOfAColumn = ds.select("a").collectAsList();
315+
final List<String> listOfAColumn = ds
316+
.select("a")
317+
.collectAsList()
318+
.stream()
319+
.map(r -> r.get(0))
320+
.filter(Objects::nonNull)
321+
.map(Object::toString)
322+
.collect(Collectors.toList());
311323

312-
for (Row r : listOfAColumn) {
313-
String val = r.getString(0);
314-
Assertions.assertTrue(val != null, "All rows should have a valid value (non-null) !");
315-
}
324+
Assertions.assertEquals(10, listOfAColumn.size(), "Should have 10 rows");
316325
}
317326
);
318327
}

src/test/java/com/teragrep/pth_10/MakeresultsTransformationTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ public void makeresults_Annotate_Test() {
121121
// assert all of them to be null
122122
rows.forEach(row -> {
123123
Assertions.assertEquals(6, row.length());
124+
124125
for (int i = 0; i < row.length(); i++) {
125126
Assertions.assertEquals(this.streamingTestUtil.getCtx().nullValue.value(), row.get(i));
126127
}

src/test/java/com/teragrep/pth_10/RangemapTransformationTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,15 @@ public void testrangemapMultiValue() {
201201
Assertions.assertEquals(1, result.size());
202202
List<String> resultList = result.get(0).getList(0);
203203
Assertions.assertEquals(2, resultList.size());
204-
List<String> expected = Arrays.asList("lo", "hi");
204+
List<String> expectedList = Arrays.asList("lo", "hi");
205205

206+
int executedLoops = 0;
206207
for (String res : resultList) {
207-
if (!expected.contains(res)) {
208-
Assertions.fail("Expected values did not contain result value: " + res);
209-
}
208+
Assertions
209+
.assertTrue(expectedList.contains(res), "Expected values did not contain result value: " + res);
210+
executedLoops++;
210211
}
212+
Assertions.assertEquals(2, executedLoops);
211213
}
212214
);
213215
}

src/test/java/com/teragrep/pth_10/RegexTransformationTest.java

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
5454
import org.slf4j.Logger;
5555
import org.slf4j.LoggerFactory;
5656

57+
import java.util.List;
58+
import java.util.stream.Collectors;
59+
5760
/**
5861
* Tests for RegexTransformation Uses streaming datasets
5962
*
@@ -117,8 +120,18 @@ public void testRegexFieldNotEqual() {
117120
)
118121
public void testRegexFieldEqual() {
119122
streamingTestUtil.performDPLTest("index=index_A | regex _raw = \"data data\"", testFile, ds -> {
120-
int size = ds.collectAsList().size();
121-
Assertions.assertTrue(size > 1);
123+
List<String> resList = ds
124+
.select("_raw")
125+
.distinct()
126+
.collectAsList()
127+
.stream()
128+
.map(r -> r.get(0))
129+
.map(Object::toString)
130+
.collect(Collectors.toList());
131+
132+
Assertions.assertEquals(1, resList.size());
133+
Assertions.assertEquals("data data", resList.get(0));
134+
122135
});
123136
}
124137

@@ -129,8 +142,17 @@ public void testRegexFieldEqual() {
129142
)
130143
public void testRegexWithString() {
131144
streamingTestUtil.performDPLTest("index=index_A | regex \"data data\"", testFile, ds -> {
132-
int size = ds.collectAsList().size();
133-
Assertions.assertTrue(size > 1);
145+
List<String> resList = ds
146+
.select("_raw")
147+
.distinct()
148+
.collectAsList()
149+
.stream()
150+
.map(r -> r.get(0))
151+
.map(Object::toString)
152+
.collect(Collectors.toList());
153+
154+
Assertions.assertEquals(1, resList.size());
155+
Assertions.assertEquals("data data", resList.get(0));
134156
});
135157
}
136158

@@ -141,8 +163,17 @@ public void testRegexWithString() {
141163
)
142164
public void testRegexMatchedPattern() {
143165
streamingTestUtil.performDPLTest("index=index_A | regex \"^[d|D][a|z][t|T][a|B]\\s.{4}$\"", testFile, ds -> {
144-
int size = ds.collectAsList().size();
145-
Assertions.assertTrue(size > 1);
166+
List<String> resList = ds
167+
.select("_raw")
168+
.distinct()
169+
.collectAsList()
170+
.stream()
171+
.map(r -> r.get(0))
172+
.map(Object::toString)
173+
.collect(Collectors.toList());
174+
175+
Assertions.assertEquals(1, resList.size());
176+
Assertions.assertEquals("data data", resList.get(0));
146177
});
147178
}
148179

src/test/java/com/teragrep/pth_10/TeragrepTransformationTest.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,7 @@ public void tgHdfsLoadEmptyCsvDatasetTest() {
198198
)
199199
public void tgHdfsLoadCustomCsvTest() {
200200
String dir = testResourcesPath.concat("/csv/hdfs.csv");
201-
if (!Files.exists(Paths.get(dir))) {
202-
Assertions.fail("Expected file does not exist: " + dir);
203-
}
201+
Assertions.assertTrue(Files.exists(Paths.get(dir)), "Expected file does not exist: " + dir);
204202

205203
streamingTestUtil
206204
.performDPLTest("| teragrep exec hdfs load " + dir + " format=CSV header=FALSE", testFile, ds -> {
@@ -233,9 +231,7 @@ public void tgHdfsLoadCustomCsvTest() {
233231
)
234232
public void tgHdfsLoadCustomCsvWithHeaderTest() {
235233
String dir = testResourcesPath.concat("/csv/hdfs.csv");
236-
if (!Files.exists(Paths.get(dir))) {
237-
Assertions.fail("Expected file does not exist: " + dir);
238-
}
234+
Assertions.assertTrue(Files.exists(Paths.get(dir)), "Expected file does not exist: " + dir);
239235

240236
streamingTestUtil
241237
.performDPLTest("| teragrep exec hdfs load " + dir + " format=CSV header=TRUE", testFile, ds -> {
@@ -268,9 +264,7 @@ public void tgHdfsLoadCustomCsvWithHeaderTest() {
268264
)
269265
public void tgHdfsLoadCustomCsvWithProvidedSchemaTest() {
270266
String dir = testResourcesPath.concat("/csv/hdfs.csv");
271-
if (!Files.exists(Paths.get(dir))) {
272-
Assertions.fail("Expected file does not exist: " + dir);
273-
}
267+
Assertions.assertTrue(Files.exists(Paths.get(dir)), "Expected file does not exist: " + dir);
274268

275269
streamingTestUtil
276270
.performDPLTest(

0 commit comments

Comments
 (0)