Skip to content

Commit

Permalink
Add IPComparisonIT tests for comparison operators, rename modules a…
Browse files Browse the repository at this point in the history
…nd weblogs test index to make plural for consistency.

Signed-off-by: currantw <[email protected]>
  • Loading branch information
currantw committed Dec 18, 2024
1 parent 5298b38 commit 4718848
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.opensearch.sql.expression.env.Environment;

@ExtendWith(MockitoExtension.class)
public class IPFunctionTest {
public class IPFunctionsTest {

// IP range and address constants for testing.
private static final ExprValue IPv4Range = ExprValueUtils.stringValue("198.51.100.0/24");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public void dateFunctionNameCaseInsensitiveTest() {
public void ipTypeShouldPassJdbcFormatter() {
assertThat(
executeQuery(
"SELECT host FROM " + TestsConstants.TEST_INDEX_WEBLOG + " ORDER BY host", "jdbc"),
"SELECT host FROM " + TestsConstants.TEST_INDEX_WEBLOGS + " ORDER BY host", "jdbc"),
containsString("\"type\": \"ip\""));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ public enum Index {
getOrderIndexMapping(),
"src/test/resources/order.json"),
WEBLOG(
TestsConstants.TEST_INDEX_WEBLOG,
"weblog",
TestsConstants.TEST_INDEX_WEBLOGS,
"weblogs",
getWeblogsIndexMapping(),
"src/test/resources/weblogs.json"),
DATE(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -661,8 +661,8 @@ public enum Index {
getOrderIndexMapping(),
"src/test/resources/order.json"),
WEBLOG(
TestsConstants.TEST_INDEX_WEBLOG,
"weblog",
TestsConstants.TEST_INDEX_WEBLOGS,
"weblogs",
getWeblogsIndexMapping(),
"src/test/resources/weblogs.json"),
DATE(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class TestsConstants {
public static final String TEST_INDEX_BANK_CSV_SANITIZE = TEST_INDEX_BANK + "_csv_sanitize";
public static final String TEST_INDEX_BANK_RAW_SANITIZE = TEST_INDEX_BANK + "_raw_sanitize";
public static final String TEST_INDEX_ORDER = TEST_INDEX + "_order";
public static final String TEST_INDEX_WEBLOG = TEST_INDEX + "_weblog";
public static final String TEST_INDEX_WEBLOGS = TEST_INDEX + "_weblogs";
public static final String TEST_INDEX_DATE = TEST_INDEX + "_date";
public static final String TEST_INDEX_DATE_TIME = TEST_INDEX + "_datetime";
public static final String TEST_INDEX_DEEP_NESTED = TEST_INDEX + "_deep_nested";
Expand Down
145 changes: 145 additions & 0 deletions integ-test/src/test/java/org/opensearch/sql/ppl/IPComparisonIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.sql.ppl;

import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_WEBLOGS;
import static org.opensearch.sql.util.MatcherUtils.rows;
import static org.opensearch.sql.util.MatcherUtils.verifyDataRows;

import java.io.IOException;
import org.json.JSONObject;
import org.junit.Test;
import org.opensearch.sql.legacy.SQLIntegTestCase;

public class IPComparisonIT extends PPLIntegTestCase {

@Override
public void init() throws IOException {
loadIndex(SQLIntegTestCase.Index.WEBLOG);
}

@Test
public void test_equal() throws IOException {
JSONObject result;
final String operator = "=";

result = executeComparisonQuery(operator, "1.2.3.4");
verifyDataRows(result, rows("1.2.3.4"));

result = executeComparisonQuery(operator, "::ffff:1.2.3.4");
verifyDataRows(result, rows("1.2.3.4"));

result = executeComparisonQuery(operator, "::1");
verifyDataRows(result, rows("::1"));

result = executeComparisonQuery(operator, "0000:0000:0000:0000:0000:0000:0000:0001");
verifyDataRows(result, rows("::1"));
}

@Test
public void test_not_equal() throws IOException {
JSONObject result;
final String operator = "!=";

result = executeComparisonQuery(operator, "1.2.3.4");
verifyDataRows(
result, rows("::1"), rows("0.0.0.2"), rows("::3"), rows("1.2.3.5"), rows("::ffff:1234"));

result = executeComparisonQuery(operator, "::ffff:1.2.3.4");
verifyDataRows(
result, rows("::1"), rows("0.0.0.2"), rows("::3"), rows("1.2.3.5"), rows("::ffff:1234"));

result = executeComparisonQuery(operator, "::1");
verifyDataRows(
result,
rows("0.0.0.2"),
rows("::3"),
rows("1.2.3.4"),
rows("1.2.3.5"),
rows("::ffff:1234"));

result = executeComparisonQuery(operator, "0000:0000:0000:0000:0000:0000:0000:0001");
verifyDataRows(
result,
rows("0.0.0.2"),
rows("::3"),
rows("1.2.3.4"),
rows("1.2.3.5"),
rows("::ffff:1234"));
}

@Test
public void test_greater_than() throws IOException {
JSONObject result;
final String operator = ">";

result = executeComparisonQuery(operator, "1.2.3.3");
verifyDataRows(result, rows("1.2.3.4"), rows("1.2.3.5"));

result = executeComparisonQuery(operator, "1.2.3.4");
verifyDataRows(result, rows("1.2.3.5"));

result = executeComparisonQuery(operator, "1.2.3.5");
verifyDataRows(result);
}

@Test
public void test_greater_than_or_equal_to() throws IOException {
JSONObject result;
final String operator = ">=";

result = executeComparisonQuery(operator, "1.2.3.4");
verifyDataRows(result, rows("1.2.3.4"), rows("1.2.3.5"));

result = executeComparisonQuery(operator, "1.2.3.5");
verifyDataRows(result, rows("1.2.3.5"));

result = executeComparisonQuery(operator, "1.2.3.6");
verifyDataRows(result);
}

@Test
public void test_less_than() throws IOException {
JSONObject result;
final String operator = "<";

result = executeComparisonQuery(operator, "::4");
verifyDataRows(result, rows("::1"), rows("::3"));

result = executeComparisonQuery(operator, "::3");
verifyDataRows(result, rows("::1"));

result = executeComparisonQuery(operator, "::1");
verifyDataRows(result);
}

@Test
public void test_less_than_or_equal_to() throws IOException {
JSONObject result;
final String operator = "<=";

result = executeComparisonQuery(operator, "::3");
verifyDataRows(result, rows("::1"), rows("::3"));

result = executeComparisonQuery(operator, "::1");
verifyDataRows(result, rows("::1"));

result = executeComparisonQuery(operator, "::0");
verifyDataRows(result);
}

/**
* Executes a query comparison on the weblogs test index with the given comparison operator and IP
* address string, and returns the resulting {@link JSONObject};
*/
private JSONObject executeComparisonQuery(String comparisonOperator, String addressString)
throws IOException {
String formatString = "source=%s | where host %s '%s' | fields host";
String query =
String.format(formatString, TEST_INDEX_WEBLOGS, comparisonOperator, addressString);
return executeQuery(query);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package org.opensearch.sql.ppl;

import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_WEBLOG;
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_WEBLOGS;
import static org.opensearch.sql.util.MatcherUtils.rows;
import static org.opensearch.sql.util.MatcherUtils.schema;
import static org.opensearch.sql.util.MatcherUtils.verifyDataRows;
Expand All @@ -15,7 +15,7 @@
import org.json.JSONObject;
import org.junit.jupiter.api.Test;

public class IPFunctionIT extends PPLIntegTestCase {
public class IPFunctionsIT extends PPLIntegTestCase {

@Override
public void init() throws IOException {
Expand All @@ -32,7 +32,7 @@ public void test_cidrmatch() throws IOException {
executeQuery(
String.format(
"source=%s | where cidrmatch(host, '250.0.0.0/24') | fields host",
TEST_INDEX_WEBLOG));
TEST_INDEX_WEBLOGS));
verifySchema(result, schema("host", null, "ip"));
verifyDataRows(result);

Expand All @@ -41,7 +41,7 @@ public void test_cidrmatch() throws IOException {
executeQuery(
String.format(
"source=%s | where cidrmatch(host, '0.0.0.0/24') | fields host",
TEST_INDEX_WEBLOG));
TEST_INDEX_WEBLOGS));
verifySchema(result, schema("host", null, "ip"));
verifyDataRows(result, rows("0.0.0.2"));

Expand All @@ -50,7 +50,7 @@ public void test_cidrmatch() throws IOException {
executeQuery(
String.format(
"source=%s | where cidrmatch(host, '1.2.3.0/24') | fields host",
TEST_INDEX_WEBLOG));
TEST_INDEX_WEBLOGS));
verifySchema(result, schema("host", null, "ip"));
verifyDataRows(result, rows("1.2.3.4"), rows("1.2.3.5"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK;
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK_WITH_NULL_VALUES;
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_DOG;
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_WEBLOG;
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_WEBLOGS;
import static org.opensearch.sql.util.MatcherUtils.rows;
import static org.opensearch.sql.util.MatcherUtils.verifyOrder;

Expand Down Expand Up @@ -135,7 +135,7 @@ public void testSortStringField() throws IOException {
@Test
public void testSortIpField() throws IOException {
final JSONObject result =
executeQuery(String.format("source=%s | fields host | sort host", TEST_INDEX_WEBLOG));
executeQuery(String.format("source=%s | fields host | sort host", TEST_INDEX_WEBLOGS));
verifyOrder(
result,
rows("::1"),
Expand Down

0 comments on commit 4718848

Please sign in to comment.