Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix coverage & additional style
Browse files Browse the repository at this point in the history
Signed-off-by: YANGDB <yang.db.dev@gmail.com>
YANG-DB committed Oct 27, 2023
1 parent e3f13c5 commit 381214d
Showing 5 changed files with 143 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.opensearch.sql.data.type.ExprCoreType;
import org.opensearch.sql.data.type.ExprType;
@@ -33,7 +34,7 @@ public ExprType type() {

@Override
public String stringValue() {
return value.stream().map(Object::toString).reduce("", String::concat);
return value.stream().map(ExprValue::stringValue).collect(Collectors.joining(","));
}

@Override
@@ -43,7 +44,7 @@ public List<ExprValue> arrayValue() {

@Override
public String toString() {
return String.format("\"%s\"", stringValue());
return String.format("%s", stringValue());
}

@Override
Original file line number Diff line number Diff line change
@@ -5,7 +5,6 @@

package org.opensearch.sql.data.model;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
@@ -72,20 +71,25 @@ public ExprValue keyValue(String key) {
public boolean equal(ExprValue o) {
if (!(o instanceof ExprTupleValue)) {
return false;
} else {
ExprTupleValue other = (ExprTupleValue) o;
Iterator<Entry<String, ExprValue>> thisIterator = this.valueMap.entrySet().iterator();
Iterator<Entry<String, ExprValue>> otherIterator = other.valueMap.entrySet().iterator();
while (thisIterator.hasNext() && otherIterator.hasNext()) {
Entry<String, ExprValue> thisEntry = thisIterator.next();
Entry<String, ExprValue> otherEntry = otherIterator.next();
if (!(thisEntry.getKey().equals(otherEntry.getKey())
&& thisEntry.getValue().equals(otherEntry.getValue()))) {
return false;
}
}

ExprTupleValue other = (ExprTupleValue) o;
if (this.valueMap.size() != other.valueMap.size()) {
return false;
}

for (Map.Entry<String, ExprValue> entry : this.valueMap.entrySet()) {
String key = entry.getKey();
ExprValue value = entry.getValue();
if (!other.valueMap.containsKey(key)) {
return false;
}
ExprValue otherValue = other.valueMap.get(key);
if (!value.equals(otherValue)) {
return false;
}
return !(thisIterator.hasNext() || otherIterator.hasNext());
}
return true;
}

/** Only compare the size of the map. */
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.opensearch.sql.data.model;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.lessThan;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import org.junit.jupiter.api.Test;
import org.opensearch.sql.data.type.ExprCoreType;

public class ExprArrayValueTest {
@Test
public void testIsArray() {
ExprArrayValue exprArrayValue = new ExprArrayValue(Arrays.asList(new ExprStringValue("test")));
assertTrue(exprArrayValue.isArray());
}

@Test
public void testValue() {
List<ExprValue> value =
Arrays.asList(new ExprStringValue("test1"), new ExprStringValue("test2"));
ExprArrayValue exprArrayValue = new ExprArrayValue(value);
assertEquals(value, exprArrayValue.value());
}

@Test
public void testType() {
ExprArrayValue exprArrayValue = new ExprArrayValue(Arrays.asList(new ExprStringValue("test")));
assertEquals(ExprCoreType.ARRAY, exprArrayValue.type());
}

@Test
public void testStringValue() {
ExprArrayValue exprArrayValue =
new ExprArrayValue(
Arrays.asList(new ExprStringValue("test1"), new ExprStringValue("test2")));
assertEquals("test1,test2", exprArrayValue.stringValue());
}

@Test
public void testArrayValue() {
List<ExprValue> value =
Arrays.asList(new ExprStringValue("test1"), new ExprStringValue("test2"));
ExprArrayValue exprArrayValue = new ExprArrayValue(value);
assertEquals(value, exprArrayValue.arrayValue());
}

@Test
public void testToString() {
ExprArrayValue exprArrayValue = new ExprArrayValue(List.of(new ExprStringValue("test")));
assertEquals("test", exprArrayValue.toString());
}

@Test
public void testCompare() {
ExprArrayValue exprArrayValue1 = new ExprArrayValue(Arrays.asList(new ExprStringValue("a")));
ExprArrayValue exprArrayValue2 = new ExprArrayValue(Arrays.asList(new ExprStringValue("b")));
assertThat(exprArrayValue1.compare(exprArrayValue2), lessThan(0));
}

@Test
public void testEqual() {
ExprArrayValue exprArrayValue1 = new ExprArrayValue(Arrays.asList(new ExprStringValue("test")));
ExprArrayValue exprArrayValue2 = new ExprArrayValue(Arrays.asList(new ExprStringValue("test")));
assertTrue(exprArrayValue1.equal(exprArrayValue2));
}

@Test
public void testHashCode() {
ExprArrayValue exprArrayValue = new ExprArrayValue(List.of(new ExprStringValue("test")));
assertEquals(exprArrayValue.hashCode(), Objects.hashCode("test"));
}
}
Original file line number Diff line number Diff line change
@@ -10,9 +10,11 @@
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 static org.opensearch.sql.data.model.ExprTupleValue.fromExprValueMap;
import static org.opensearch.sql.utils.ComparisonUtil.compare;

import com.google.common.collect.ImmutableMap;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.opensearch.sql.exception.ExpressionEvaluationException;

@@ -30,6 +32,27 @@ public void tuple_compare_int() {
assertFalse(tupleValue.equals(intValue));
}

@Test
public void compare_tuple_with_same_key_different_order() {
assertEquals(
fromExprValueMap(
Map.of(
"column1",
new ExprStringValue("value1"),
"column2",
new ExprIntegerValue(123),
"column3",
ExprBooleanValue.of(true))),
fromExprValueMap(
Map.of(
"column2",
new ExprIntegerValue(123),
"column1",
new ExprStringValue("value1"),
"column3",
ExprBooleanValue.of(true))));
}

@Test
public void compare_tuple_with_different_key() {
ExprValue tupleValue1 = ExprValueUtils.tupleValue(ImmutableMap.of("value", 2));
@@ -44,8 +67,8 @@ public void compare_tuple_with_different_size() {
ExprValue tupleValue1 = ExprValueUtils.tupleValue(ImmutableMap.of("integer_value", 2));
ExprValue tupleValue2 =
ExprValueUtils.tupleValue(ImmutableMap.of("integer_value", 2, "float_value", 1f));
assertFalse(tupleValue1.equals(tupleValue2));
assertFalse(tupleValue2.equals(tupleValue1));
assertNotEquals(tupleValue1, tupleValue2);
assertNotEquals(tupleValue2, tupleValue1);
}

@Test
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@

import static java.lang.String.format;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.opensearch.sql.data.model.ExprTupleValue.fromExprValueMap;

import java.util.List;
@@ -45,28 +47,28 @@ void testJsonObject() {
new ExecutionEngine.Schema.Column("column3", "column3", ExprCoreType.BOOLEAN))),
handle.schema());

assertEquals(true, handle.hasNext());
assertEquals(
assertTrue(handle.hasNext());
assertTrue(
fromExprValueMap(
Map.of(
"column1",
new ExprStringValue("value1"),
"column2",
new ExprIntegerValue(123),
"column3",
ExprBooleanValue.of(true))),
handle.next());
assertEquals(
Map.of(
"column1",
new ExprStringValue("value1"),
"column2",
new ExprIntegerValue(123),
"column3",
ExprBooleanValue.of(true)))
.equal(handle.next()));
assertTrue(
fromExprValueMap(
Map.of(
"column1",
new ExprStringValue("value2"),
"column2",
new ExprIntegerValue(456),
"column3",
ExprBooleanValue.of(false))),
handle.next());
assertEquals(false, handle.hasNext());
Map.of(
"column1",
new ExprStringValue("value2"),
"column2",
new ExprIntegerValue(456),
"column3",
ExprBooleanValue.of(false)))
.equal(handle.next()));
assertFalse(handle.hasNext());
}

@Test

0 comments on commit 381214d

Please sign in to comment.