-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added doctest, integ-tests, and unit tests
- Loading branch information
Showing
12 changed files
with
204 additions
and
3 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
core/src/test/java/org/opensearch/sql/expression/json/JsonFunctionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.expression.json; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.when; | ||
import static org.opensearch.sql.data.model.ExprValueUtils.LITERAL_FALSE; | ||
import static org.opensearch.sql.data.model.ExprValueUtils.LITERAL_TRUE; | ||
import static org.opensearch.sql.data.type.ExprCoreType.STRING; | ||
|
||
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.ExprValue; | ||
import org.opensearch.sql.data.model.ExprValueUtils; | ||
import org.opensearch.sql.expression.DSL; | ||
import org.opensearch.sql.expression.Expression; | ||
import org.opensearch.sql.expression.FunctionExpression; | ||
import org.opensearch.sql.expression.env.Environment; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class JsonFunctionsTest { | ||
|
||
private static final ExprValue JsonObject = ExprValueUtils.stringValue("{\"a\":\"1\",\"b\":\"2\"}"); | ||
private static final ExprValue JsonArray = ExprValueUtils.stringValue("[1, 2, 3, 4]"); | ||
private static final ExprValue JsonScalarString = ExprValueUtils.stringValue("\"abc\""); | ||
private static final ExprValue JsonEmptyString = ExprValueUtils.stringValue(""); | ||
private static final ExprValue JsonInvalidObject = ExprValueUtils.stringValue("{\"invalid\":\"json\", \"string\"}"); | ||
private static final ExprValue JsonInvalidScalar = ExprValueUtils.stringValue("abc"); | ||
|
||
@Mock private Environment<Expression, ExprValue> env; | ||
|
||
@Test | ||
public void json_valid_invalid_json_string() { | ||
assertEquals(LITERAL_FALSE, execute(JsonInvalidObject)); | ||
assertEquals(LITERAL_FALSE, execute(JsonInvalidScalar)); | ||
} | ||
|
||
@Test | ||
public void json_valid_valid_json_string() { | ||
assertEquals(LITERAL_TRUE, JsonObject); | ||
assertEquals(LITERAL_TRUE, JsonArray); | ||
assertEquals(LITERAL_TRUE, JsonScalarString); | ||
assertEquals(LITERAL_TRUE, JsonEmptyString); | ||
} | ||
|
||
private ExprValue execute(ExprValue jsonString) { | ||
final String fieldName = "json_string"; | ||
FunctionExpression exp = DSL.jsonValid(DSL.literal(jsonString)); | ||
|
||
when(DSL.ref(fieldName, STRING).valueOf(env)).thenReturn(jsonString); | ||
|
||
return exp.valueOf(env); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
==================== | ||
IP Address Functions | ||
==================== | ||
|
||
.. rubric:: Table of contents | ||
|
||
.. contents:: | ||
:local: | ||
:depth: 1 | ||
|
||
JSON_VALID | ||
---------- | ||
|
||
Description | ||
>>>>>>>>>>> | ||
|
||
Usage: `json_valid(json_string)` checks if `json_string` is a valid STRING string. | ||
|
||
Argument type: STRING | ||
|
||
Return type: BOOLEAN | ||
|
||
Example:: | ||
|
||
> source=json_test | where json_valid(json_string) | fields test_name, json_string | ||
fetched rows / total rows = 4/4 | ||
+--------------------+--------------------+ | ||
| test_name | json_string | | ||
|--------------------|--------------------| | ||
| json object | {"a":"1","b":"2"} | | ||
| json array | [1, 2, 3, 4] | | ||
| json scalar string | [1, 2, 3, 4] | | ||
| json empty string | [1, 2, 3, 4] | | ||
+--------------------+--------------------+ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{"test_name":"json object", "json_string":"{\"a\":\"1\",\"b\":\"2\"}"} | ||
{"test_name":"json array", "json_string":"[1, 2, 3, 4]"} | ||
{"test_name":"json scalar string", "json_string":"\"abc\""} | ||
{"test_name":"json empty string","json_string":""} | ||
{"test_name":"json invalid object", "json_string":"{\"invalid\":\"json\", \"string\"}"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
integ-test/src/test/java/org/opensearch/sql/ppl/JsonFunctionIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.ppl; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
import org.json.JSONObject; | ||
|
||
import javax.json.Json; | ||
|
||
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_JSON_TEST; | ||
import static org.opensearch.sql.util.MatcherUtils.rows; | ||
import static org.opensearch.sql.util.MatcherUtils.schema; | ||
import static org.opensearch.sql.util.MatcherUtils.verifyDataRows; | ||
import static org.opensearch.sql.util.MatcherUtils.verifySchema; | ||
|
||
public class JsonFunctionIT extends PPLIntegTestCase { | ||
@Override | ||
public void init() throws IOException { | ||
loadIndex(Index.JSON_TEST); | ||
} | ||
|
||
@Test | ||
public void test_json_valid() throws IOException { | ||
JSONObject result; | ||
|
||
result = | ||
executeQuery( | ||
String.format( | ||
"source=%s | where json_valid(json_string) | fields test_name", | ||
TEST_INDEX_JSON_TEST | ||
) | ||
); | ||
verifySchema(result, schema("test_name", null, "string")); | ||
verifyDataRows( | ||
result, | ||
rows("json object"), | ||
rows("json array"), | ||
rows("json scalar string"), | ||
rows("json empty string") | ||
); | ||
} | ||
|
||
@Test | ||
public void test_not_json_valid() throws IOException { | ||
JSONObject result; | ||
|
||
result = | ||
executeQuery( | ||
String.format( | ||
"source=%s | where not json_valid(json_string) | fields test_name", | ||
TEST_INDEX_JSON_TEST | ||
) | ||
); | ||
verifySchema(result, schema("test_name", null, "string")); | ||
verifyDataRows( | ||
result, | ||
rows("json invalid object") | ||
); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
integ-test/src/test/resources/indexDefinitions/json_test_index_mappping.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"mappings": { | ||
"properties": { | ||
"test_name": { | ||
"type": "text" | ||
}, | ||
"json_string": { | ||
"type": "text" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{"index":{"_id":"1"}} | ||
{"test_name":"json object", "json_string":"{\"a\":\"1\",\"b\":\"2\"}"} | ||
{"index":{"_id":"2"}} | ||
{"test_name":"json array", "json_string":"[1, 2, 3, 4]"} | ||
{"index":{"_id":"3"}} | ||
{"test_name":"json scalar string", "json_string":"\"abc\""} | ||
{"index":{"_id":"4"}} | ||
{"test_name":"json empty string","json_string":""} | ||
{"index":{"_id":"5"}} | ||
{"test_name":"json invalid object", "json_string":"{\"invalid\":\"json\", \"string\"}"} |