Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue-65: Improve test assertions #429

Merged
merged 3 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions src/test/java/com/teragrep/pth10/CatalystVisitorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
*/
package com.teragrep.pth10;

import org.apache.spark.sql.types.DataTypes;
import org.apache.spark.sql.types.MetadataBuilder;
import org.apache.spark.sql.types.StructField;
import org.apache.spark.sql.types.StructType;
import com.teragrep.pth10.ast.DPLParserCatalystContext;
import com.teragrep.pth10.ast.DPLTimeFormat;
import org.apache.spark.sql.Row;
Expand Down Expand Up @@ -204,15 +208,25 @@ void searchQueryWithAggrTest() {
void searchQueryWithIndexEarliestTest() {
this.streamingTestUtil
.performDPLTest("index = cinnamon _index_earliest=\"04/16/2020:10:25:40\"", this.testFile, res -> {
String e = "[_raw: string, _time: string ... 6 more fields]";
final StructType expectedSchema = new StructType(new StructField[] {
new StructField("_raw", DataTypes.StringType, true, new MetadataBuilder().build()),
new StructField("_time", DataTypes.StringType, true, new MetadataBuilder().build()),
new StructField("host", DataTypes.StringType, true, new MetadataBuilder().build()),
new StructField("index", DataTypes.StringType, true, new MetadataBuilder().build()),
new StructField("offset", DataTypes.LongType, true, new MetadataBuilder().build()),
new StructField("partition", DataTypes.StringType, true, new MetadataBuilder().build()),
new StructField("source", DataTypes.StringType, true, new MetadataBuilder().build()),
new StructField("sourcetype", DataTypes.StringType, true, new MetadataBuilder().build()),
});

// check schema
Assertions.assertEquals(e, res.toString());
Assertions.assertEquals(expectedSchema, res.schema());

String logicalPart = this.streamingTestUtil.getCtx().getSparkQuery();
// check column for archive query i.e. only logical part'
DPLTimeFormat tf = new DPLTimeFormat("MM/dd/yyyy:HH:mm:ss");
long indexEarliestEpoch = Assertions.assertDoesNotThrow(() -> tf.getEpoch("04/16/2020:10:25:40"));
e = "(RLIKE(index, (?i)^cinnamon$) AND (_time >= from_unixtime(" + indexEarliestEpoch
String e = "(RLIKE(index, (?i)^cinnamon$) AND (_time >= from_unixtime(" + indexEarliestEpoch
+ ", yyyy-MM-dd HH:mm:ss)))";
Assertions.assertEquals(e, logicalPart);
});
Expand All @@ -228,17 +242,28 @@ void searchQueryWithStringTest() {
String testFile = "src/test/resources/subsearchData*.jsonl";

this.streamingTestUtil.performDPLTest("index=index_A \"(1)(enTIty)\"", testFile, res -> {
String e = "StructType(StructField(_raw,StringType,true),StructField(_time,StringType,true),StructField(host,StringType,true),StructField(index,StringType,true),StructField(offset,LongType,true),StructField(origin,StringType,true),StructField(partition,StringType,true),StructField(source,StringType,true),StructField(sourcetype,StringType,true))";
String resSchema = res.schema().toString();
Assertions.assertEquals(e, resSchema);

final StructType expectedSchema = new StructType(new StructField[] {
new StructField("_raw", DataTypes.StringType, true, new MetadataBuilder().build()),
new StructField("_time", DataTypes.StringType, true, new MetadataBuilder().build()),
new StructField("host", DataTypes.StringType, true, new MetadataBuilder().build()),
new StructField("index", DataTypes.StringType, true, new MetadataBuilder().build()),
new StructField("offset", DataTypes.LongType, true, new MetadataBuilder().build()),
new StructField("origin", DataTypes.StringType, true, new MetadataBuilder().build()),
new StructField("partition", DataTypes.StringType, true, new MetadataBuilder().build()),
new StructField("source", DataTypes.StringType, true, new MetadataBuilder().build()),
new StructField("sourcetype", DataTypes.StringType, true, new MetadataBuilder().build()),
});

Assertions.assertEquals(expectedSchema, res.schema());
// Check result count
List<Row> lst = res.collectAsList();
// check result count
Assertions.assertEquals(1, lst.size());

// get logical part
String logicalPart = this.streamingTestUtil.getCtx().getSparkQuery();
e = "(RLIKE(index, (?i)^index_A$) AND RLIKE(_raw, (?i)^.*\\Q(1)(enTIty)\\E.*))";
String e = "(RLIKE(index, (?i)^index_A$) AND RLIKE(_raw, (?i)^.*\\Q(1)(enTIty)\\E.*))";
Assertions.assertEquals(e, logicalPart);
});
}
Expand Down
29 changes: 19 additions & 10 deletions src/test/java/com/teragrep/pth10/chartTransformationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ public void testChartCountAs() {
String query = "index = index_A | chart count(_raw) as count";

this.streamingTestUtil.performDPLTest(query, this.testFile, res -> {
String[] expectedColumns = new String[] {
"count"
};
Assertions.assertArrayEquals(expectedColumns, res.columns()); // check that the schema is correct
final StructType expectedSchema = new StructType(new StructField[] {
new StructField("count", DataTypes.LongType, true, new MetadataBuilder().build())
});
Assertions.assertEquals(expectedSchema, res.schema()); // check that the schema is correct

List<String> resultList = res
.select("count")
Expand All @@ -115,9 +115,12 @@ void testChartCountWithAsAndSplitBy() {
String q = "( index = index_A OR index = index_B ) _index_earliest=\"04/16/2003:10:25:40\" | chart count(_raw) as count by offset";

this.streamingTestUtil.performDPLTest(q, this.testFile, res -> {
final StructType expectedSchema = new StructType(new StructField[] {
new StructField("offset", DataTypes.LongType, true, new MetadataBuilder().build()),
new StructField("count", DataTypes.LongType, true, new MetadataBuilder().build())
});

String e = "[offset: bigint, count: bigint]"; // At least schema is correct
Assertions.assertEquals(e, res.toString());
Assertions.assertEquals(expectedSchema, res.schema()); // At least schema is correct

// 3 first rows are earlier than where _index_earliest is set to
List<String> expectedValues = new ArrayList<>();
Expand Down Expand Up @@ -149,8 +152,12 @@ void testChartCountAsFieldIsUsable() {

this.streamingTestUtil.performDPLTest(q, this.testFile, res -> {

String e = "[offset: bigint, count: bigint]"; // At least schema is correct
Assertions.assertEquals(e, res.toString());
final StructType expectedSchema = new StructType(new StructField[] {
new StructField("offset", DataTypes.LongType, true, new MetadataBuilder().build()),
new StructField("count", DataTypes.LongType, true, new MetadataBuilder().build())
});

Assertions.assertEquals(expectedSchema, res.schema()); // At least schema is correct

List<String> expectedValues = new ArrayList<>();
// Only first 5 rows have index: index_A
Expand Down Expand Up @@ -181,9 +188,11 @@ void testChartCount() {
String q = "index = index_B _index_earliest=\"04/16/2003:10:25:40\" | chart count(_raw)";

this.streamingTestUtil.performDPLTest(q, this.testFile, res -> {
final StructType expectedSchema = new StructType(new StructField[] {
new StructField("count(_raw)", DataTypes.LongType, true, new MetadataBuilder().build())
});

String e = "[count(_raw): bigint]"; // At least schema is correct
Assertions.assertEquals(e, res.toString());
Assertions.assertEquals(expectedSchema, res.schema()); // At least schema is correct

List<String> expectedValues = new ArrayList<>();
expectedValues.add("5"); // only last 5 rows have index: index_B
Expand Down
Loading