Skip to content

Commit b00e388

Browse files
Added unit tests for bigquery
1 parent 0738fd1 commit b00e388

File tree

7 files changed

+879
-29
lines changed

7 files changed

+879
-29
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*-
2+
* #%L
3+
* athena-google-bigquery
4+
* %%
5+
* Copyright (C) 2019 - 2025 Amazon Web Services
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.amazonaws.athena.connectors.google.bigquery;
21+
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
25+
import java.util.HashMap;
26+
import java.util.Map;
27+
28+
import static com.amazonaws.athena.connector.lambda.connection.EnvironmentConstants.PROJECT_ID;
29+
import static com.amazonaws.athena.connector.lambda.connection.EnvironmentConstants.SECRET_NAME;
30+
import static com.amazonaws.athena.connectors.google.bigquery.BigQueryConstants.ENV_BIG_QUERY_CREDS_SM_ID;
31+
import static com.amazonaws.athena.connectors.google.bigquery.BigQueryConstants.GCP_PROJECT_ID;
32+
import static org.junit.Assert.assertEquals;
33+
import static org.junit.Assert.assertNull;
34+
35+
public class BigQueryEnvironmentPropertiesTest {
36+
37+
private Map<String, String> connectionProperties;
38+
private BigQueryEnvironmentProperties bigQueryEnvironmentProperties;
39+
private static final String SECRET_NAME_VALUE = "bigQuery_Secret";
40+
41+
@Before
42+
public void setup() {
43+
connectionProperties = new HashMap<>();
44+
connectionProperties.put(SECRET_NAME, SECRET_NAME_VALUE);
45+
bigQueryEnvironmentProperties = new BigQueryEnvironmentProperties();
46+
}
47+
48+
@Test
49+
public void testConnectionPropertiesToEnvironment_When_ProjectId_NotPresent() {
50+
Map<String, String> result = bigQueryEnvironmentProperties.connectionPropertiesToEnvironment(connectionProperties);
51+
assertNull(result.get(GCP_PROJECT_ID));
52+
assertEquals(SECRET_NAME_VALUE, result.get(ENV_BIG_QUERY_CREDS_SM_ID));
53+
}
54+
55+
@Test
56+
public void testConnectionPropertiesToEnvironment_When_ProjectId_Present() {
57+
final String expectedValue = "testProject";
58+
connectionProperties.put(PROJECT_ID, expectedValue);
59+
Map<String, String> result = bigQueryEnvironmentProperties.connectionPropertiesToEnvironment(connectionProperties);
60+
assertEquals(expectedValue, result.get(GCP_PROJECT_ID));
61+
assertEquals(SECRET_NAME_VALUE, result.get(ENV_BIG_QUERY_CREDS_SM_ID));
62+
}
63+
64+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*-
2+
* #%L
3+
* athena-google-bigquery
4+
* %%
5+
* Copyright (C) 2019 - 2025 Amazon Web Services
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.amazonaws.athena.connectors.google.bigquery;
21+
22+
import com.google.cloud.bigquery.BigQueryException;
23+
import org.junit.Test;
24+
import software.amazon.awssdk.services.athena.model.AthenaException;
25+
26+
import static com.amazonaws.athena.connectors.google.bigquery.BigQueryExceptionFilter.EXCEPTION_FILTER;
27+
import static org.junit.Assert.assertFalse;
28+
import static org.junit.Assert.assertTrue;
29+
30+
public class BigQueryExceptionFilterTest {
31+
32+
@Test
33+
public void testIsMatch_When_Rate_Exceeded()
34+
{
35+
boolean match = EXCEPTION_FILTER.isMatch(AthenaException.builder().message("Rate exceeded").build());
36+
assertTrue(match);
37+
}
38+
39+
@Test
40+
public void testIsMatch_When_Exceeded_Rate_Limits()
41+
{
42+
boolean match = EXCEPTION_FILTER.isMatch(new BigQueryException(1, "Exceeded rate limits"));
43+
assertTrue(match);
44+
}
45+
46+
@Test
47+
public void testIsMatch_When_Other_ErrorMessage()
48+
{
49+
boolean match = EXCEPTION_FILTER.isMatch(AthenaException.builder().message("other").build());
50+
assertFalse(match);
51+
}
52+
53+
}

athena-google-bigquery/src/test/java/com/amazonaws/athena/connectors/google/bigquery/BigQueryFederationExpressionParserTest.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public class BigQueryFederationExpressionParserTest
5454
private static final Logger LOGGER = LoggerFactory.getLogger(BigQueryFederationExpressionParserTest.class);
5555
private static final String COLUMN_QUOTE_CHAR = "\"";
5656
private static final String CONSTANT_QUOTE_CHAR = "'";
57+
private static final String INPUT_ARGUMENT1 = "110";
58+
private static final String INPUT_ARGUMENT2 = "120";
5759

5860
BlockAllocator blockAllocator;
5961
ArrowType intType = new ArrowType.Int(32, true);
@@ -128,6 +130,78 @@ public void testCreateSqlForComplexExpressionContent_UnaryFunction()
128130
assertEquals(negateClause, "(-110)");
129131
}
130132

133+
@Test
134+
public void testCreateSqlForComplexExpressionContent_DivideFunction()
135+
{
136+
FunctionName divideFunction = StandardFunctions.DIVIDE_FUNCTION_NAME.getFunctionName();
137+
String divClause = bigQueryExpressionParser.mapFunctionToDataSourceSyntax(divideFunction, intType, ImmutableList.of("`col1`", INPUT_ARGUMENT1));
138+
assertEquals("(`col1` / 110)", divClause);
139+
}
140+
141+
@Test
142+
public void testCreateSqlForComplexExpressionContent_MulFunction()
143+
{
144+
FunctionName mulFunction = StandardFunctions.MULTIPLY_FUNCTION_NAME.getFunctionName();
145+
String mulClause = bigQueryExpressionParser.mapFunctionToDataSourceSyntax(mulFunction, intType, ImmutableList.of("`col1`", INPUT_ARGUMENT1));
146+
assertEquals("(`col1` * 110)", mulClause);
147+
}
148+
149+
@Test
150+
public void testCreateSqlForComplexExpressionContent_EqualFunction()
151+
{
152+
FunctionName equalFunction = StandardFunctions.EQUAL_OPERATOR_FUNCTION_NAME.getFunctionName();
153+
String equalClause = bigQueryExpressionParser.mapFunctionToDataSourceSyntax(equalFunction, intType, ImmutableList.of("`col1`", INPUT_ARGUMENT1));
154+
assertEquals("(`col1` = 110)", equalClause);
155+
}
156+
157+
@Test
158+
public void testCreateSqlForComplexExpressionContent_GreaterEqlFunction()
159+
{
160+
FunctionName grtEqlFunction = StandardFunctions.GREATER_THAN_OR_EQUAL_OPERATOR_FUNCTION_NAME.getFunctionName();
161+
String grtEqlClause = bigQueryExpressionParser.mapFunctionToDataSourceSyntax(grtEqlFunction, intType, ImmutableList.of("`col1`", INPUT_ARGUMENT1));
162+
assertEquals("(`col1` >= 110)", grtEqlClause);
163+
}
164+
165+
@Test
166+
public void testCreateSqlForComplexExpressionContent_LessEqlFunction()
167+
{
168+
FunctionName lesEqlFunction = StandardFunctions.LESS_THAN_OR_EQUAL_OPERATOR_FUNCTION_NAME.getFunctionName();
169+
String lesEqlClause = bigQueryExpressionParser.mapFunctionToDataSourceSyntax(lesEqlFunction, intType, ImmutableList.of("`col1`", INPUT_ARGUMENT1));
170+
assertEquals("(`col1` <= 110)", lesEqlClause);
171+
}
172+
173+
@Test
174+
public void testCreateSqlForComplexExpressionContent_LikeFunction()
175+
{
176+
FunctionName likeFunction = StandardFunctions.LIKE_PATTERN_FUNCTION_NAME.getFunctionName();
177+
String likeClause = bigQueryExpressionParser.mapFunctionToDataSourceSyntax(likeFunction, intType, ImmutableList.of(INPUT_ARGUMENT1, INPUT_ARGUMENT2));
178+
assertEquals("(110 LIKE 120)", likeClause);
179+
}
180+
181+
@Test
182+
public void testCreateSqlForComplexExpressionContent_NotFunction()
183+
{
184+
FunctionName notFunction = StandardFunctions.NOT_FUNCTION_NAME.getFunctionName();
185+
String notClause = bigQueryExpressionParser.mapFunctionToDataSourceSyntax(notFunction, intType, ImmutableList.of(INPUT_ARGUMENT1));
186+
assertEquals("( NOT 110)", notClause);
187+
}
188+
189+
@Test
190+
public void testCreateSqlForComplexExpressionContent_IsDistinctFunction()
191+
{
192+
FunctionName isDistFunction = StandardFunctions.IS_DISTINCT_FROM_OPERATOR_FUNCTION_NAME.getFunctionName();
193+
String isDistClause = bigQueryExpressionParser.mapFunctionToDataSourceSyntax(isDistFunction, intType, ImmutableList.of(INPUT_ARGUMENT1, INPUT_ARGUMENT2));
194+
assertEquals("(110 IS DISTINCT FROM 120)", isDistClause);
195+
}
196+
197+
@Test
198+
public void testCreateSqlForComplexExpressionContent_NullIfFunction()
199+
{
200+
FunctionName nullIfFunction = StandardFunctions.NULLIF_FUNCTION_NAME.getFunctionName();
201+
String nullIfClause = bigQueryExpressionParser.mapFunctionToDataSourceSyntax(nullIfFunction, intType, ImmutableList.of(INPUT_ARGUMENT1, INPUT_ARGUMENT2));
202+
assertEquals("(NULLIF(110, 120))", nullIfClause);
203+
}
204+
131205
@Test(expected = IllegalArgumentException.class)
132206
public void testCreateSqlForComplexExpressionContent_InvalidBinaryInput()
133207
{

0 commit comments

Comments
 (0)