Skip to content

Commit d8cae12

Browse files
committed
test: Add tests for handling distinct query errors
1 parent be9d81c commit d8cae12

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
3+
*
4+
* WSO2 Inc. licenses this file to you under the Apache License,
5+
* Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
* KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
19+
package org.ballerinalang.test.query;
20+
21+
import org.ballerinalang.test.BCompileUtil;
22+
import org.ballerinalang.test.BRunUtil;
23+
import org.ballerinalang.test.CompileResult;
24+
import org.testng.annotations.AfterClass;
25+
import org.testng.annotations.BeforeClass;
26+
import org.testng.annotations.Test;
27+
28+
public class QueryDistinctErrorTest {
29+
private CompileResult result;
30+
31+
@BeforeClass
32+
public void setup() {
33+
result = BCompileUtil.compile("test-src/query/query-distinct-error.bal");
34+
}
35+
36+
@Test
37+
public void testDistinctTypeMismatchError() {
38+
BRunUtil.invoke(result, "testDistinctTypeMismatchError");
39+
}
40+
41+
@Test
42+
public void testQueryDistinctError() {
43+
BRunUtil.invoke(result, "testQueryDistinctError");
44+
}
45+
46+
@AfterClass
47+
public void tearDown() {
48+
result = null;
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// Copyright (c) 2025 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2+
//
3+
// WSO2 Inc. licenses this file to you under the Apache License,
4+
// Version 2.0 (the "License"); you may not use this file except
5+
// in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing,
11+
// software distributed under the License is distributed on an
12+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13+
// KIND, either express or implied. See the License for the
14+
// specific language governing permissions and limitations
15+
// under the License.
16+
17+
type Album record {
18+
string id;
19+
string name;
20+
string artist;
21+
decimal price;
22+
};
23+
24+
type TypeMismatchError distinct error;
25+
26+
class MockStream {
27+
private boolean hasNext = true;
28+
29+
public isolated function next() returns record {| Album value; |}|error? {
30+
if self.hasNext {
31+
self.hasNext = false;
32+
return error TypeMismatchError("Error when iterating the SQL result. invalid value for record field 'artist': expected value of type 'string', found '()'");
33+
}
34+
return ();
35+
}
36+
}
37+
38+
function createMockStream() returns stream<Album, error?> {
39+
MockStream mockStream = new MockStream();
40+
return new (mockStream);
41+
}
42+
43+
public function testDistinctTypeMismatchError() {
44+
stream<Album, error?> albumStream = createMockStream();
45+
46+
int count = 0;
47+
error? e = from Album _ in albumStream do {
48+
count = count + 1;
49+
};
50+
51+
assertEquality(0, count);
52+
assertTrue(e is TypeMismatchError);
53+
if e is TypeMismatchError {
54+
assertEquality(e.message(), "Error when iterating the SQL result. invalid value for record field 'artist': expected value of type 'string', found '()'");
55+
}
56+
}
57+
58+
class EvenNumberGenerator {
59+
int i = 0;
60+
public isolated function next() returns record {|int value;|}|error? {
61+
self.i += 2;
62+
if (self.i > 10) {
63+
return error("No more even numbers available.");
64+
}
65+
return {value: self.i};
66+
}
67+
}
68+
69+
function testErrorFromQueryPipeline() returns string|error? {
70+
EvenNumberGenerator evenGen = new ();
71+
EvenNumberGenerator evenGen2 = new ();
72+
73+
stream<int, error?> evenNumberStream = new (evenGen);
74+
stream<int, error?> evenNumberStream2 = new (evenGen2);
75+
76+
error? err = from var i in evenNumberStream
77+
do {
78+
record {| int value; |}? result = check evenNumberStream2.next();
79+
int? intResult = result?.value;
80+
};
81+
82+
assertTrue(err is error);
83+
return "should reach here";
84+
}
85+
86+
function testErrorFromQueryBody() returns string|error? {
87+
EvenNumberGenerator evenGen = new ();
88+
EvenNumberGenerator evenGen2 = new ();
89+
90+
stream<int, error?> evenNumberStream = new (evenGen);
91+
stream<int, error?> evenNumberStream2 = new (evenGen2);
92+
93+
error? err = from var i in 1...10
94+
do {
95+
record {| int value; |}? result = check evenNumberStream2.next();
96+
int? intResult = result?.value;
97+
};
98+
99+
assertTrue(err is error);
100+
return "shouldn't reach here";
101+
}
102+
103+
function testQueryDistinctError() {
104+
assertEquality("should reach here", testErrorFromQueryPipeline());
105+
assertTrue(testErrorFromQueryBody() is error);
106+
}
107+
108+
// Assertions ---------------------------------------------------------------------------------------------------------
109+
110+
const ASSERTION_ERROR_REASON = "AssertionError";
111+
112+
function assertTrue(any|error actual) {
113+
assertEquality(true, actual);
114+
}
115+
116+
function assertEquality(any|error expected, any|error actual) {
117+
if expected is anydata && actual is anydata && expected == actual {
118+
return;
119+
}
120+
121+
if expected === actual {
122+
return;
123+
}
124+
125+
string expectedValAsString = expected is error ? expected.toString() : expected.toString();
126+
string actualValAsString = actual is error ? actual.toString() : actual.toString();
127+
if expectedValAsString == actualValAsString {
128+
return;
129+
}
130+
131+
panic error(ASSERTION_ERROR_REASON,
132+
message = "expected '" + expectedValAsString + "', found '" + actualValAsString + "'");
133+
}

0 commit comments

Comments
 (0)