Skip to content

Commit

Permalink
Add additional tests to increase code coverage
Browse files Browse the repository at this point in the history
Signed-off-by: currantw <[email protected]>
  • Loading branch information
currantw committed Dec 3, 2024
1 parent 5a8970e commit 357755b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public ExprIpValue(String s) {
IPAddress address = new IPAddressString(s, validationOptions).toAddress();

// Convert IPv6 mapped IPv4 addresses to IPv4
if (address.isIPv4Convertible()) address = address.toIPv4();
if (address.isIPv4Convertible()) {
address = address.toIPv4();
}

value = address;
} catch (AddressStringException e) {
Expand All @@ -55,7 +57,6 @@ public ExprType type() {

@Override
public int compare(ExprValue other) {
assert (other instanceof ExprIpValue);

// Map IPv4 addresses to IPv6 for comparison
IPv6Address ipv6Value = toIPv6Address(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
package org.opensearch.sql.data.model;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;
import org.junit.jupiter.api.Test;
import org.opensearch.sql.data.type.ExprCoreType;
import org.opensearch.sql.exception.ExpressionEvaluationException;
import org.opensearch.sql.exception.SemanticCheckException;

public class ExprIpValueTest {
Expand Down Expand Up @@ -74,6 +76,9 @@ public void testType() {

@Test
public void testCompare() {
Exception ex;

// Compare to IP address.
ipv4LesserStrings.forEach(
(s) -> assertTrue(exprIpv4Value.compareTo(ExprValueUtils.ipValue(s)) > 0));
ipv4EqualStrings.forEach(
Expand All @@ -86,12 +91,40 @@ public void testCompare() {
(s) -> assertEquals(0, exprIpv6Value.compareTo(ExprValueUtils.ipValue(s))));
ipv6GreaterStrings.forEach(
(s) -> assertTrue(exprIpv6Value.compareTo(ExprValueUtils.ipValue(s)) < 0));

// Compare to null/missing value.
ex =
assertThrows(
IllegalStateException.class,
() -> exprIpv4Value.compareTo(ExprValueUtils.LITERAL_NULL));
assertEquals("[BUG] Unreachable, Comparing with NULL or MISSING is undefined", ex.getMessage());

ex =
assertThrows(
IllegalStateException.class,
() -> exprIpv4Value.compareTo(ExprValueUtils.LITERAL_MISSING));
assertEquals("[BUG] Unreachable, Comparing with NULL or MISSING is undefined", ex.getMessage());

// Compare to other data type.
ex =
assertThrows(
ExpressionEvaluationException.class,
() -> exprIpv4Value.compareTo(ExprValueUtils.LITERAL_TRUE));
assertEquals("compare expected value have same type, but with [IP, BOOLEAN]", ex.getMessage());
}

@Test
public void testEquals() {
assertEquals(exprIpv4Value, exprIpv4Value);
assertNotEquals(exprIpv4Value, new Object());
assertNotEquals(exprIpv4Value, ExprValueUtils.LITERAL_NULL);
assertNotEquals(exprIpv4Value, ExprValueUtils.LITERAL_MISSING);

ipv4EqualStrings.forEach((s) -> assertEquals(exprIpv4Value, ExprValueUtils.ipValue(s)));
ipv6EqualStrings.forEach((s) -> assertEquals(exprIpv6Value, ExprValueUtils.ipValue(s)));

ipv4LesserStrings.forEach((s) -> assertNotEquals(exprIpv4Value, ExprValueUtils.ipValue(s)));
ipv6GreaterStrings.forEach((s) -> assertNotEquals(exprIpv6Value, ExprValueUtils.ipValue(s)));
}

@Test
Expand Down

0 comments on commit 357755b

Please sign in to comment.