Skip to content

Commit

Permalink
ES|QL: Add unit tests for now() function (#108498)
Browse files Browse the repository at this point in the history
  • Loading branch information
luigidellaquila committed May 10, 2024
1 parent 7ed58e7 commit fed8088
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 29 deletions.
2 changes: 1 addition & 1 deletion docs/reference/esql/functions/date-time-functions.asciidoc
Expand Up @@ -21,4 +21,4 @@ include::layout/date_extract.asciidoc[]
include::layout/date_format.asciidoc[]
include::layout/date_parse.asciidoc[]
include::layout/date_trunc.asciidoc[]
include::now.asciidoc[]
include::layout/now.asciidoc[]
5 changes: 5 additions & 0 deletions docs/reference/esql/functions/description/now.asciidoc
@@ -0,0 +1,5 @@
// This is generated by ESQL's AbstractFunctionTestCase. Do no edit it. See ../README.md for how to regenerate it.

*Description*

Returns current date and time.
22 changes: 22 additions & 0 deletions docs/reference/esql/functions/examples/now.asciidoc
@@ -0,0 +1,22 @@
// This is generated by ESQL's AbstractFunctionTestCase. Do no edit it. See ../README.md for how to regenerate it.

*Examples*

[source.merge.styled,esql]
----
include::{esql-specs}/date.csv-spec[tag=docsNow]
----
[%header.monospaced.styled,format=dsv,separator=|]
|===
include::{esql-specs}/date.csv-spec[tag=docsNow-result]
|===
To retrieve logs from the last hour:
[source.merge.styled,esql]
----
include::{esql-specs}/date.csv-spec[tag=docsNowWhere]
----
[%header.monospaced.styled,format=dsv,separator=|]
|===
include::{esql-specs}/date.csv-spec[tag=docsNowWhere-result]
|===

16 changes: 16 additions & 0 deletions docs/reference/esql/functions/kibana/definition/now.json
@@ -0,0 +1,16 @@
{
"comment" : "This is generated by ESQL's AbstractFunctionTestCase. Do no edit it. See ../README.md for how to regenerate it.",
"type" : "eval",
"name" : "now",
"description" : "Returns current date and time.",
"signatures" : [
{
"params" : [ ],
"returnType" : "datetime"
}
],
"examples" : [
"ROW current_date = NOW()",
"FROM sample_data\n| WHERE @timestamp > NOW() - 1 hour"
]
}
10 changes: 10 additions & 0 deletions docs/reference/esql/functions/kibana/docs/now.md
@@ -0,0 +1,10 @@
<!--
This is generated by ESQL's AbstractFunctionTestCase. Do no edit it. See ../README.md for how to regenerate it.
-->

### NOW
Returns current date and time.

```
ROW current_date = NOW()
```
15 changes: 15 additions & 0 deletions docs/reference/esql/functions/layout/now.asciidoc
@@ -0,0 +1,15 @@
// This is generated by ESQL's AbstractFunctionTestCase. Do no edit it. See ../README.md for how to regenerate it.

[discrete]
[[esql-now]]
=== `NOW`

*Syntax*

[.text-center]
image::esql/functions/signature/now.svg[Embedded,opts=inline]

include::../parameters/now.asciidoc[]
include::../description/now.asciidoc[]
include::../types/now.asciidoc[]
include::../examples/now.asciidoc[]
28 changes: 0 additions & 28 deletions docs/reference/esql/functions/now.asciidoc

This file was deleted.

3 changes: 3 additions & 0 deletions docs/reference/esql/functions/parameters/now.asciidoc
@@ -0,0 +1,3 @@
// This is generated by ESQL's AbstractFunctionTestCase. Do no edit it. See ../README.md for how to regenerate it.

*Parameters*
1 change: 1 addition & 0 deletions docs/reference/esql/functions/signature/now.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions docs/reference/esql/functions/types/now.asciidoc
@@ -0,0 +1,9 @@
// This is generated by ESQL's AbstractFunctionTestCase. Do no edit it. See ../README.md for how to regenerate it.

*Supported types*

[%header.monospaced.styled,format=dsv,separator=|]
|===
result
datetime
|===
@@ -0,0 +1,68 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.xpack.esql.expression.function.scalar.math;

import com.carrotsearch.randomizedtesting.annotations.Name;
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;

import org.elasticsearch.compute.data.Block;
import org.elasticsearch.compute.data.LongBlock;
import org.elasticsearch.xpack.esql.EsqlTestUtils;
import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier;
import org.elasticsearch.xpack.esql.expression.function.scalar.AbstractConfigurationFunctionTestCase;
import org.elasticsearch.xpack.esql.expression.function.scalar.date.Now;
import org.elasticsearch.xpack.esql.session.EsqlConfiguration;
import org.elasticsearch.xpack.ql.expression.Expression;
import org.elasticsearch.xpack.ql.tree.Source;
import org.elasticsearch.xpack.ql.type.DataTypes;
import org.hamcrest.Matcher;

import java.util.List;
import java.util.function.Supplier;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.matchesPattern;

public class NowTests extends AbstractConfigurationFunctionTestCase {
public NowTests(@Name("TestCase") Supplier<TestCaseSupplier.TestCase> testCaseSupplier) {
this.testCase = testCaseSupplier.get();
}

@ParametersFactory
public static Iterable<Object[]> parameters() {
return parameterSuppliersFromTypedData(
List.of(
new TestCaseSupplier(
"Now Test",
() -> new TestCaseSupplier.TestCase(
List.of(),
matchesPattern("LiteralsEvaluator\\[lit=.*\\]"),
DataTypes.DATETIME,
equalTo(EsqlTestUtils.TEST_CFG.now().toInstant().toEpochMilli())
)
)
)
);
}

@Override
protected Expression buildWithConfiguration(Source source, List<Expression> args, EsqlConfiguration configuration) {
return new Now(Source.EMPTY, configuration);
}

@Override
protected void assertSimpleWithNulls(List<Object> data, Block value, int nullBlock) {
assertThat(((LongBlock) value).asVector().getLong(0), equalTo(EsqlTestUtils.TEST_CFG.now().toInstant().toEpochMilli()));
}

@Override
protected Matcher<Object> allNullsMatcher() {
return equalTo(EsqlTestUtils.TEST_CFG.now().toInstant().toEpochMilli());
}

}

0 comments on commit fed8088

Please sign in to comment.