Skip to content

Commit

Permalink
Groom datetime functions unit tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Yury-Fridlyand <[email protected]>
  • Loading branch information
Yury-Fridlyand committed Jan 17, 2023
1 parent b11b239 commit 2ca51bb
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 611 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
import static org.opensearch.sql.utils.DateTimeFormatters.DATE_TIME_FORMATTER_LONG_YEAR;
import static org.opensearch.sql.utils.DateTimeFormatters.DATE_TIME_FORMATTER_SHORT_YEAR;
import static org.opensearch.sql.utils.DateTimeFormatters.DATE_TIME_FORMATTER_STRICT_WITH_TZ;
import static org.opensearch.sql.utils.DateTimeUtils.UTC_ZONE_ID;
import static org.opensearch.sql.utils.DateTimeUtils.extractDate;
import static org.opensearch.sql.utils.DateTimeUtils.extractDateTime;
import static org.opensearch.sql.utils.DateTimeUtils.UTC_ZONE_ID;

import java.math.BigDecimal;
import java.math.RoundingMode;
Expand Down Expand Up @@ -112,6 +112,7 @@ public void register(BuiltinFunctionRepository repository) {
repository.register(datediff());
repository.register(datetime());
repository.register(date_add());
repository.register(date_format());
repository.register(date_sub());
repository.register(day());
repository.register(dayName());
Expand Down Expand Up @@ -149,11 +150,10 @@ public void register(BuiltinFunctionRepository repository) {
repository.register(time_to_sec());
repository.register(timediff());
repository.register(timestamp());
repository.register(to_days());
repository.register(utc_date());
repository.register(utc_time());
repository.register(utc_timestamp());
repository.register(date_format());
repository.register(to_days());
repository.register(unix_timestamp());
repository.register(week(BuiltinFunctionName.WEEK));
repository.register(week(BuiltinFunctionName.WEEK_OF_YEAR));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,18 @@ public class MathematicalFunction {
*/
public static void register(BuiltinFunctionRepository repository) {
repository.register(abs());
repository.register(acos());
repository.register(asin());
repository.register(atan());
repository.register(atan2());
repository.register(cbrt());
repository.register(ceil());
repository.register(ceiling());
repository.register(conv());
repository.register(cos());
repository.register(cot());
repository.register(crc32());
repository.register(degrees());
repository.register(euler());
repository.register(exp());
repository.register(floor());
Expand All @@ -65,24 +72,17 @@ public static void register(BuiltinFunctionRepository repository) {
repository.register(log10());
repository.register(log2());
repository.register(mod());
repository.register(pi());
repository.register(pow());
repository.register(power());
repository.register(radians());
repository.register(rand());
repository.register(round());
repository.register(sign());
repository.register(sqrt());
repository.register(truncate());
repository.register(pi());
repository.register(rand());
repository.register(acos());
repository.register(asin());
repository.register(atan());
repository.register(atan2());
repository.register(cos());
repository.register(cot());
repository.register(degrees());
repository.register(radians());
repository.register(sin());
repository.register(sqrt());
repository.register(tan());
repository.register(truncate());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,26 @@

package org.opensearch.sql.expression.datetime;


import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.opensearch.sql.data.model.ExprValueUtils.nullValue;
import static org.opensearch.sql.data.type.ExprCoreType.DATETIME;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.sql.data.model.ExprDatetimeValue;
import org.opensearch.sql.data.model.ExprValue;
import org.opensearch.sql.expression.DSL;
import org.opensearch.sql.expression.Expression;
import org.opensearch.sql.expression.ExpressionTestBase;
import org.opensearch.sql.expression.FunctionExpression;
import org.opensearch.sql.expression.env.Environment;



@ExtendWith(MockitoExtension.class)
class ConvertTZTest extends ExpressionTestBase {

@Mock
Environment<Expression, ExprValue> env;

@Test
public void invalidDate() {
FunctionExpression expr = DSL.convert_tz(DSL.datetime(
DSL.literal("2021-04-31 10:00:00")),
DSL.literal("+00:00"),
DSL.literal("+00:00"));
assertEquals(DATETIME, expr.type());
assertEquals(nullValue(), expr.valueOf(env));
assertEquals(nullValue(), expr.valueOf());
}

@Test
Expand All @@ -48,7 +35,7 @@ public void conversionFromNoOffset() {
DSL.literal("+00:00"),
DSL.literal("+10:00"));
assertEquals(DATETIME, expr.type());
assertEquals(new ExprDatetimeValue("2008-05-16 08:00:00"), expr.valueOf(env));
assertEquals(new ExprDatetimeValue("2008-05-16 08:00:00"), expr.valueOf());
}

@Test
Expand All @@ -58,7 +45,7 @@ public void conversionToInvalidInput3Over() {
DSL.literal("+00:00"),
DSL.literal("+16:00"));
assertEquals(DATETIME, expr.type());
assertEquals(nullValue(), expr.valueOf(env));
assertEquals(nullValue(), expr.valueOf());
}

@Test
Expand All @@ -68,7 +55,7 @@ public void conversionToInvalidInput3Under() {
DSL.literal("+00:00"),
DSL.literal("-16:00"));
assertEquals(DATETIME, expr.type());
assertEquals(nullValue(), expr.valueOf(env));
assertEquals(nullValue(), expr.valueOf());
}

@Test
Expand All @@ -78,7 +65,7 @@ public void conversionFromPositiveToPositive() {
DSL.literal("+15:00"),
DSL.literal("+01:00"));
assertEquals(DATETIME, expr.type());
assertEquals(nullValue(), expr.valueOf(env));
assertEquals(nullValue(), expr.valueOf());
}

@Test
Expand All @@ -88,7 +75,7 @@ public void invalidInput2Under() {
DSL.literal("-15:00"),
DSL.literal("+01:00"));
assertEquals(DATETIME, expr.type());
assertEquals(nullValue(), expr.valueOf(env));
assertEquals(nullValue(), expr.valueOf());
}

@Test
Expand All @@ -98,7 +85,7 @@ public void invalidInput3Over() {
DSL.literal("-12:00"),
DSL.literal("+15:00"));
assertEquals(DATETIME, expr.type());
assertEquals(nullValue(), expr.valueOf(env));
assertEquals(nullValue(), expr.valueOf());
}

@Test
Expand All @@ -108,7 +95,7 @@ public void conversionToPositiveEdge() {
DSL.literal("+00:00"),
DSL.literal("+14:00"));
assertEquals(DATETIME, expr.type());
assertEquals(new ExprDatetimeValue("2008-05-16 12:00:00"), expr.valueOf(env));
assertEquals(new ExprDatetimeValue("2008-05-16 12:00:00"), expr.valueOf());
}

@Test
Expand All @@ -118,7 +105,7 @@ public void conversionToNegativeEdge() {
DSL.literal("+00:01"),
DSL.literal("-13:59"));
assertEquals(DATETIME, expr.type());
assertEquals(new ExprDatetimeValue("2008-05-15 08:00:00"), expr.valueOf(env));
assertEquals(new ExprDatetimeValue("2008-05-15 08:00:00"), expr.valueOf());
}

@Test
Expand All @@ -128,7 +115,7 @@ public void invalidInput2() {
DSL.literal("+)()"),
DSL.literal("+12:00"));
assertEquals(DATETIME, expr.type());
assertEquals(nullValue(), expr.valueOf(env));
assertEquals(nullValue(), expr.valueOf());
}

@Test
Expand All @@ -138,7 +125,7 @@ public void invalidInput3() {
DSL.literal("+00:00"),
DSL.literal("test"));
assertEquals(DATETIME, expr.type());
assertEquals(nullValue(), expr.valueOf(env));
assertEquals(nullValue(), expr.valueOf());
}

@Test
Expand All @@ -148,7 +135,7 @@ public void invalidInput1() {
DSL.literal("+00:00"),
DSL.literal("+00:00"));
assertEquals(DATETIME, expr.type());
assertEquals(nullValue(), expr.valueOf(env));
assertEquals(nullValue(), expr.valueOf());
}

@Test
Expand All @@ -158,7 +145,7 @@ public void invalidDateFeb30() {
DSL.literal("+00:00"),
DSL.literal("+00:00"));
assertEquals(DATETIME, expr.type());
assertEquals(nullValue(), expr.valueOf(env));
assertEquals(nullValue(), expr.valueOf());
}

@Test
Expand All @@ -168,7 +155,7 @@ public void invalidDateApril31() {
DSL.literal("+00:00"),
DSL.literal("+00:00"));
assertEquals(DATETIME, expr.type());
assertEquals(nullValue(), expr.valueOf(env));
assertEquals(nullValue(), expr.valueOf());
}

@Test
Expand All @@ -178,6 +165,6 @@ public void invalidMonth13() {
DSL.literal("+00:00"),
DSL.literal("+00:00"));
assertEquals(DATETIME, expr.type());
assertEquals(nullValue(), expr.valueOf(env));
assertEquals(nullValue(), expr.valueOf());
}
}
Loading

0 comments on commit 2ca51bb

Please sign in to comment.