forked from databricks/databricks-jdbc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLInterpolatorTest.java
More file actions
142 lines (123 loc) · 6 KB
/
Copy pathSQLInterpolatorTest.java
File metadata and controls
142 lines (123 loc) · 6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package com.databricks.jdbc.common.util;
import static com.databricks.jdbc.TestConstants.TEST_STRING;
import static com.databricks.jdbc.api.impl.DatabricksPreparedStatementTest.getSqlParam;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import com.databricks.jdbc.api.impl.ImmutableSqlParameter;
import com.databricks.jdbc.exception.DatabricksValidationException;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
public class SQLInterpolatorTest {
@Test
public void testInterpolateSQLWithStrings() throws DatabricksValidationException {
String sql = "SELECT * FROM users WHERE name = ? AND city = ?";
Map<Integer, ImmutableSqlParameter> params = new HashMap<>();
params.put(1, getSqlParam(1, "Alice", DatabricksTypeUtil.STRING));
params.put(2, getSqlParam(2, "Wonderland", DatabricksTypeUtil.STRING));
String expected = "SELECT * FROM users WHERE name = 'Alice' AND city = 'Wonderland'";
assertEquals(expected, SQLInterpolator.interpolateSQL(sql, params));
}
@Test
public void testInterpolateSQLWithMixedTypes() throws DatabricksValidationException {
String sql = "INSERT INTO sales (id, amount, active) VALUES (?, ?, ?)";
Map<Integer, ImmutableSqlParameter> params = new HashMap<>();
params.put(1, getSqlParam(1, 101, DatabricksTypeUtil.INT));
params.put(2, getSqlParam(2, 19.95, DatabricksTypeUtil.FLOAT));
params.put(3, getSqlParam(3, true, DatabricksTypeUtil.BOOLEAN));
String expected = "INSERT INTO sales (id, amount, active) VALUES (101, 19.95, true)";
assertEquals(expected, SQLInterpolator.interpolateSQL(sql, params));
}
@Test
public void testInterpolateSQLWithNullValues() throws DatabricksValidationException {
String sql = "UPDATE products SET price = ? WHERE id = ?";
Map<Integer, ImmutableSqlParameter> params = new HashMap<>();
params.put(1, getSqlParam(1, null, DatabricksTypeUtil.NULL));
params.put(2, getSqlParam(2, 200, DatabricksTypeUtil.INT));
String expected = "UPDATE products SET price = NULL WHERE id = 200";
assertEquals(expected, SQLInterpolator.interpolateSQL(sql, params));
}
@Test
public void testParameterMismatch() {
String sql = "DELETE FROM log WHERE date = ?";
Map<Integer, ImmutableSqlParameter> params = new HashMap<>(); // no parameters added
assertThrows(
DatabricksValidationException.class,
() -> {
SQLInterpolator.interpolateSQL(sql, params);
});
}
@Test
public void testExtraParameters() {
String sql = "SELECT * FROM clients WHERE client_id = ?";
Map<Integer, ImmutableSqlParameter> params = new HashMap<>();
params.put(1, getSqlParam(1, 300, DatabricksTypeUtil.INT));
params.put(2, getSqlParam(2, TEST_STRING, DatabricksTypeUtil.STRING)); // extra parameter
assertThrows(
DatabricksValidationException.class,
() -> {
SQLInterpolator.interpolateSQL(sql, params);
});
}
@Test
public void testEscapedValues() throws DatabricksValidationException {
String sql = "UPDATE products SET price = ? WHERE id = ?";
Map<Integer, ImmutableSqlParameter> params = new HashMap<>();
params.put(1, getSqlParam(1, "O'Reilly", DatabricksTypeUtil.STRING));
params.put(2, getSqlParam(2, 200, DatabricksTypeUtil.INT));
String expected = "UPDATE products SET price = 'O''Reilly' WHERE id = 200";
assertEquals(expected, SQLInterpolator.interpolateSQL(sql, params));
}
@Test
public void testBinaryType() throws DatabricksValidationException {
String sql = "INSERT INTO sales (id, data) VALUES (?, ?)";
Map<Integer, ImmutableSqlParameter> params = new HashMap<>();
params.put(1, getSqlParam(1, 101, DatabricksTypeUtil.INT));
params.put(2, getSqlParam(2, "X'0102030405'", DatabricksTypeUtil.BINARY));
String expected = "INSERT INTO sales (id, data) VALUES (101, X'0102030405')";
assertEquals(expected, SQLInterpolator.interpolateSQL(sql, params));
}
private static Stream<Arguments> providePlaceholderQuotingTestCases() {
return Stream.of(
// Basic placeholder quoting
Arguments.of(
"SELECT * FROM table WHERE id = ?",
"SELECT * FROM table WHERE id = '?'",
"Basic placeholder quoting"),
// Multiple placeholders
Arguments.of(
"SELECT * FROM table WHERE id = ? AND name = ?",
"SELECT * FROM table WHERE id = '?' AND name = '?'",
"Multiple placeholders"),
// Already quoted placeholders
Arguments.of(
"SELECT * FROM table WHERE id = '?' AND name = ?",
"SELECT * FROM table WHERE id = '?' AND name = '?'",
"Already quoted placeholders"),
// Mixed quoted and unquoted placeholders
Arguments.of(
"SELECT * FROM table WHERE id = '?' AND name = ? AND age = '?'",
"SELECT * FROM table WHERE id = '?' AND name = '?' AND age = '?'",
"Mixed quoted and unquoted placeholders"),
// Null input
Arguments.of(null, null, "Null input"),
// Empty input
Arguments.of("", "", "Empty input"),
// No placeholders
Arguments.of("SELECT * FROM table", "SELECT * FROM table", "No placeholders"),
// Complex query with multiple conditions
Arguments.of(
"SELECT * FROM table WHERE id = ? AND (name = ? OR age = ?) AND status = ?",
"SELECT * FROM table WHERE id = '?' AND (name = '?' OR age = '?') AND status = '?'",
"Complex query with multiple conditions"));
}
@ParameterizedTest(name = "{2}")
@MethodSource("providePlaceholderQuotingTestCases")
public void testSurroundPlaceholdersWithQuotes(String input, String expected, String testName) {
assertEquals(expected, SQLInterpolator.surroundPlaceholdersWithQuotes(input), testName);
}
}