8000 Cesql v1 fixes by Cali0707 · Pull Request #641 · cloudevents/sdk-java · GitHub
[go: up one dir, main page]

Skip to content

Cesql v1 fixes #641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test: updated test files to test v1 spec
Signed-off-by: Calum Murray <cmurray@redhat.com>
  • Loading branch information
Cali0707 committed Jun 19, 2024
commit d88a3cf016f8946355613d2744916bbdc5aa839f
23 changes: 18 additions & 5 deletions sql/src/test/java/io/cloudevents/sql/CustomFunctionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.cloudevents.core.test.Data;
import io.cloudevents.sql.impl.functions.BaseFunction;
import io.cloudevents.sql.impl.functions.InfallibleOneArgumentFunction;
import io.cloudevents.sql.impl.runtime.EvaluationResult;
import io.cloudevents.sql.impl.runtime.EvaluationRuntimeBuilder;
import org.junit.jupiter.api.Test;

Expand All @@ -21,6 +22,7 @@ void addSimpleFunction() {
.addFunction(new InfallibleOneArgumentFunction<>(
"MY_STRING_PREDICATE",
String.class,
Boolean.class,
s -> s.length() % 2 == 0
))
.build();
Expand All @@ -37,12 +39,12 @@ void addSimpleFunction() {
Parser.parseDefault("MY_STRING_PREDICATE('abc', 'xyz')")
.evaluate(runtime, Data.V1_MIN)
)
.hasFailure(EvaluationException.ErrorKind.FUNCTION_DISPATCH);
.hasFailure(EvaluationException.ErrorKind.MISSING_FUNCTION);
assertThat(
Parser.parseDefault("MY_STRING_PR('abc', 'xyz')")
.evaluate(runtime, Data.V1_MIN)
)
.hasFailure(EvaluationException.ErrorKind.FUNCTION_DISPATCH);
.hasFailure(EvaluationException.ErrorKind.MISSING_FUNCTION);
}

@Test
Expand All @@ -55,7 +57,7 @@ void addVariadicFunction() {
Parser.parseDefault("MY_STRING_FN('abc')")
.evaluate(runtime, Data.V1_MIN)
)
.hasFailure(EvaluationException.ErrorKind.FUNCTION_DISPATCH);
.hasFailure(EvaluationException.ErrorKind.MISSING_FUNCTION);
assertThat(
Parser.parseDefault("MY_STRING_FN('abc', 'b')")
.evaluate(runtime, Data.V1_MIN)
Expand Down Expand Up @@ -85,6 +87,7 @@ void addSimpleFunctionAndVariadicFunction() {
.addFunction(new InfallibleOneArgumentFunction<>(
"MY_STRING_FN",
String.class,
Boolean.class,
s -> s.length() % 2 == 0
))
.addFunction(new VariadicMockFunction("MY_STRING_FN", 2, Type.STRING))
Expand Down Expand Up @@ -126,6 +129,7 @@ void cannotAddVariadicWithFixedArgsLowerThanMaxArgsOverload() {
.addFunction(new InfallibleOneArgumentFunction<>(
"MY_STRING_FN",
String.class,
Boolean.class,
s -> s.length() % 2 == 0
));

Expand Down Expand Up @@ -153,20 +157,23 @@ void addSimpleFunctionFails() {
.addFunction(new InfallibleOneArgumentFunction<>(
"MY_STRING_FN",
String.class,
Boolean.class,
s -> s.length() % 2 == 0
));

assertThatThrownBy(() -> runtime.addFunction(
new InfallibleOneArgumentFunction<>(
"MY_STRING_FN",
String.class,
Boolean.class,
s -> s.length() % 2 == 0
)
)).isInstanceOf(IllegalArgumentException.class);
assertThatThrownBy(() -> runtime.addFunction(
new InfallibleOneArgumentFunction<>(
"MY_STRING_FN",
Integer.class,
Boolean.class,
s -> s % 2 == 0
)
)).isInstanceOf(IllegalArgumentException.class);
Expand All @@ -178,6 +185,7 @@ void customFunctionSpecTest() {
.addFunction(new InfallibleOneArgumentFunction<>(
"MY_STRING_PREDICATE",
String.class,
Boolean.class,
s -> s.length() % 2 == 0
))
.build();
Expand Down Expand Up @@ -211,8 +219,8 @@ private VariadicMockFunction(String name, int fixedArgs, Type argsType) {
}

@Override
public Object invoke(EvaluationContext ctx, EvaluationRuntime evaluationRuntime, CloudEvent event, List<Object> arguments) {
return arguments.size();
public EvaluationResult invoke(EvaluationContext ctx, EvaluationRuntime evaluationRuntime, CloudEvent event, List<Object> arguments) {
return new EvaluationResult(arguments.size());
}

@Override
Expand All @@ -229,6 +237,11 @@ public int arity() {
public boolean isVariadic() {
return true;
}

@Override
public Type returnType() {
return Type.INTEGER;
}
}

}
6 changes: 3 additions & 3 deletions sql/src/test/java/io/cloudevents/sql/TCKTestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ public CloudEvent getTestInputEvent() {
public EvaluationException.ErrorKind getEvaluationExceptionErrorKind() {
switch (this.error) {
case CAST:
return EvaluationException.ErrorKind.INVALID_CAST;
return EvaluationException.ErrorKind.CAST;
case MATH:
return EvaluationException.ErrorKind.MATH;
case MISSING_FUNCTION:
return EvaluationException.ErrorKind.FUNCTION_DISPATCH;
return EvaluationException.ErrorKind.MISSING_FUNCTION;
case MISSING_ATTRIBUTE:
return EvaluationException.ErrorKind.MISSING_ATTRIBUTE;
case FUNCTION_EVALUATION:
return EvaluationException.ErrorKind.FUNCTION_EXECUTION;
return EvaluationException.ErrorKind.FUNCTION_EVALUATION;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,6 @@
import static org.assertj.core.api.Assertions.assertThat;

public class ConstantFoldingTest {

@Test
void withBinaryExpression() {
Expression expression = Parser.getDefault().parse("1 + 2");
assertThat(expression)
.isInstanceOf(ExpressionImpl.class);

ExpressionInternal internal = ((ExpressionImpl) expression).getExpressionInternal();
assertThat(internal)
.isInstanceOf(ValueExpression.class)
.extracting(v -> ((ValueExpression) v).getValue())
.isEqualTo(3);
}

@Test
void withUnaryExpression() {
Expression expression = Parser.getDefault().parse("-1");
Expand Down
3 changes: 2 additions & 1 deletion sql/src/test/resources/tck/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The `error` values could be any of the following:
* `parse`: Error while parsing the expression
* `math`: Math error while evaluating a math operator
* `cast`: Casting error
* `missingAttribute`: Addressed a missing attribute
* `missingFunction`: Addressed a missing function
* `functionEvaluation`: Error while evaluating a function
* `missingAttribute`: Error due to a missing attribute
* `generic`: A generic error
16 changes: 16 additions & 0 deletions sql/src/test/resources/tck/binary_comparison_operators.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ tests:
- name: abc is equal to abc
expression: "'abc' = 'abc'"
result: true
- name: Equals operator returns false when encountering a missing attribute
expression: missing = 2
result: false
error: missingAttribute

- name: True is not equal to false
expression: TRUE != FALSE
Expand All @@ -37,6 +41,10 @@ tests:
- name: abc is not equal to abc
expression: "'abc' != 'abc'"
result: false
- name: Not equal operator returns false when encountering a missing attribute
expression: missing != 2
result: false
error: missingAttribute

- name: True is not equal to false (diamond operator)
expression: TRUE <> FALSE
Expand All @@ -56,6 +64,10 @@ tests:
- name: abc is not equal to abc (diamond operator)
expression: "'abc' <> 'abc'"
result: false
- name: Diamond operator returns false when encountering a missing attribute
expression: missing <> 2
result: false
error: missingAttribute

- name: 1 is less or equal than 2
expression: 2 <= 2
Expand All @@ -81,6 +93,10 @@ tests:
- name: 2 is greater than 2
expression: 2 > 2
result: false
- name: Less than or equal operator returns false when encountering a missing attribute
expression: missing <= 2
result: false
error: missingAttribute

- name: implicit casting with string as right type
expression: "true = ' 10000 ;TRUE'"
Expand Down
14 changes: 14 additions & 0 deletions sql/src/test/resources/tck/binary_logical_operators.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ tests:
- name: True and true
expression: TRUE AND TRUE
result: true
- name: AND operator is short circuit evaluated
expression: "false and (1 != 1 / 0)"
result: false
- name: AND operator is NOT short circuit evaluated when the first operand evaluates to true
expression: "true and (1 = 1 / 0)"
error: math
result: false

- name: False or false
expression: FALSE OR FALSE
Expand All @@ -25,6 +32,13 @@ tests:
- name: True or true
expression: TRUE OR TRUE
result: true
- name: OR operator is short circuit evaluated
expression: "true or (1 != 1 / 0)"
result: true
- name: OR operator is NOT short circuit evaluated when the first operand evaluates to false
expression: "false or (1 = 1 / 0)"
error: math
result: false

- name: False xor false
expression: FALSE XOR FALSE
Expand Down
17 changes: 10 additions & 7 deletions sql/src/test/resources/tck/binary_math_operators.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ tests:
expression: 5 % 0
result: 0
error: math
- name: Missing attribute in division results in missing attribute error, not divide by 0 error
expression: missing / 0
result: 0
error: missingAttribute
- name: Missing attribute in modulo results in missing attribute error, not divide by 0 error
expression: missing % 0
result: 0
error: missingAttribute

- name: Positive plus positive number
expression: 4 + 1
Expand Down Expand Up @@ -50,11 +58,6 @@ tests:
- name: Implicit casting, with both values string
expression: "'5' + '3'"
result: 8
- name: Implicit casting, with invalid boolean value
- name: Implicit casting, with boolean value
expression: "5 + TRUE"
result: 5
error: cast
- name: Implicit casting, with invalid string value
expression: "'5avc4' + 10"
result: 10
error: cast
result: 6
48 changes: 14 additions & 34 deletions sql/src/test/resources/tck/casting_functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ tests:
- name: Cast identity -1
expression: INT(-1)
result: -1
- name: Invalid cast from boolean to int
- name: Cast from TRUE to int
expression: INT(TRUE)
result: 1
- name: Cast from FALSE to int
expression: INT(FALSE)
result: 0
error: cast
- name: Invalid cast from string to int
expression: INT('ABC')
result: 0
Expand All @@ -37,10 +39,18 @@ tests:
expression: BOOL('ABC')
result: false
error: cast
- name: Invalid cast from int to boolean
- name: Cast from 1 to boolean
expression: BOOL(1)
result: true
- name: Cast from 0 to boolean
expression: BOOL(0)
result: false
error: cast
- name: Cast from 100 to boolean
expression: BOOL(100)
result: true
- name: Cast from -50 to boolean
expression: BOOL(-50)
result: true

- name: Cast TRUE to string
expression: STRING(TRUE)
Expand All @@ -57,33 +67,3 @@ tests:
- name: Cast identity "abc"
expression: STRING("abc")
result: "abc"

- name: "'true' is a boolean"
expression: IS_BOOL('true')
result: true
- name: "'FALSE' is a boolean"
expression: IS_BOOL('FALSE')
result: true
- name: 1 is not a boolean
expression: IS_BOOL(1)
result: false
- name: "'abc' is not a boolean"
expression: IS_BOOL('abc')
result: false

- name: "'-1' is an int"
expression: IS_INT('-1')
result: true
- name: "'1' is an int"
expression: IS_INT('1')
result: true
- name: true is not an int
expression: IS_INT(TRUE)
result: false
- name: "'abc' is not an int"
expression: IS_INT('abc')
result: false

- name: IS_STRING does not exists
expression: IS_STRING('ABC')
error: missingFunction
2 changes: 1 addition & 1 deletion sql/src/test/resources/tck/context_attributes_access.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ tests:
id: myId
source: localhost.localdomain
type: myType
result: ""
result: false
error: missingAttribute
- name: Access to optional boolean extension
expression: mybool
Expand Down
5 changes: 5 additions & 0 deletions sql/src/test/resources/tck/integer_builtin_functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ tests:
- name: ABS (3)
expression: ABS(0)
result: 0
- name: ABS overflow
expression: ABS(-2147483648)
result: 2147483647
error: math

11 changes: 11 additions & 0 deletions sql/src/test/resources/tck/like_expression.yaml
8000
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,14 @@ tests:
- name: With type coercion from bool (4)
expression: "FALSE LIKE 'fal%'"
result: true

- name: Invalid string literal in comparison causes parse error
expression: "x LIKE 123"
result: false
error: parse
eventOverrides:
x: "123"
- name: Missing attribute returns empty string
expression: "missing LIKE 'missing'"
result: false
error: missingAttribute
8 changes: 6 additions & 2 deletions sql/src/test/resources/tck/negate_operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ tests:
expression: --'10'
result: 10

- name: Invalid boolean cast
- name: Minus with boolean cast
expression: -TRUE
result: -1

- name: Minus with missing attribute
expression: -missing
result: 0
error: cast
error: missingAttribute
8 changes: 6 additions & 2 deletions sql/src/test/resources/tck/not_operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@ tests:

- name: Invalid int cast
expression: NOT 10
result: true
error: cast
result: false

- name: Not missing attribute
expression: NOT missing
result: false
error: missingAttribute
Loading
0