Skip to content

Commit 0b32f4e

Browse files
committed
Update tests
1 parent add4df0 commit 0b32f4e

File tree

3 files changed

+114
-54
lines changed

3 files changed

+114
-54
lines changed

jdbc/src/test/java/tech/ydb/jdbc/YdbDriverProperitesTest.java

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public void acceptsURL(String url, boolean accept, String connectionString) thro
109109
@SuppressWarnings("UnstableApiUsage")
110110
@Test
111111
public void getPropertyInfoDefault() throws SQLException {
112-
String url = "jdbc:ydb:ydb-demo.testhost.org:2135/test/pr/testing/ci";
112+
String url = "jdbc:ydb:ydb-demo.testhost.org:2135/test/db";
113113

114114
Properties properties = new Properties();
115115
DriverPropertyInfo[] propertyInfo = driver.getPropertyInfo(url, properties);
@@ -122,13 +122,13 @@ public void getPropertyInfoDefault() throws SQLException {
122122
Assertions.assertEquals(expect, actual);
123123

124124
YdbProperties ydbProperties = YdbJdbcTools.from(url, properties);
125-
Assertions.assertEquals("grpc://ydb-demo.testhost.org:2135/test/pr/testing/ci",
125+
Assertions.assertEquals("grpc://ydb-demo.testhost.org:2135/test/db",
126126
ydbProperties.getConnectionProperties().getConnectionString());
127127
}
128128

129129
@Test
130130
public void getPropertyInfoAllFromUrl() throws SQLException {
131-
String url = "jdbc:ydb:ydb-demo.testhost.org:2135/test/pr/testing/ci?" +
131+
String url = "jdbc:ydb:ydb-demo.testhost.org:2135/test/db?" +
132132
customizedProperties().entrySet().stream()
133133
.map(e -> e.getKey() + "=" + e.getValue())
134134
.collect(Collectors.joining("&"));
@@ -141,15 +141,18 @@ public void getPropertyInfoAllFromUrl() throws SQLException {
141141
actual.forEach(logger::info);
142142

143143
List<String> expect = convertPropertyInfo(customizedPropertyInfo());
144-
Assertions.assertEquals(expect, actual);
144+
Assertions.assertEquals(expect.size(), actual.size(), "Wrong size of all properties");
145+
for (int idx = 0; idx < expect.size(); idx += 1) {
146+
Assertions.assertEquals(expect.get(idx), actual.get(idx), "Wrong parameter " + idx);
147+
}
145148

146149
YdbProperties ydbProperties = YdbJdbcTools.from(url, properties);
147150
checkCustomizedProperties(ydbProperties);
148151
}
149152

150153
@Test
151154
public void getPropertyInfoFromProperties() throws SQLException {
152-
String url = "jdbc:ydb:ydb-demo.testhost.org:2135/test/pr/testing/ci";
155+
String url = "jdbc:ydb:ydb-demo.testhost.org:2135/test/db";
153156

154157
Properties properties = customizedProperties();
155158
Properties copy = new Properties();
@@ -162,7 +165,10 @@ public void getPropertyInfoFromProperties() throws SQLException {
162165
actual.forEach(logger::info);
163166

164167
List<String> expect = convertPropertyInfo(customizedPropertyInfo());
165-
Assertions.assertEquals(expect, actual);
168+
Assertions.assertEquals(expect.size(), actual.size(), "Wrong size of all properties");
169+
for (int idx = 0; idx < expect.size(); idx += 1) {
170+
Assertions.assertEquals(expect.get(idx), actual.get(idx), "Wrong parameter " + idx);
171+
}
166172

167173
YdbProperties ydbProperties = YdbJdbcTools.from(url, properties);
168174
checkCustomizedProperties(ydbProperties);
@@ -186,7 +192,10 @@ public void getPropertyInfoOverwrite() throws SQLException {
186192

187193
// URL will always overwrite properties
188194
List<String> expect = convertPropertyInfo(defaultPropertyInfo("sas"));
189-
Assertions.assertEquals(expect, actual);
195+
Assertions.assertEquals(expect.size(), actual.size(), "Wrong size of default properties");
196+
for (int idx = 0; idx < expect.size(); idx += 1) {
197+
Assertions.assertEquals(expect.get(idx), actual.get(idx), "Wrong default parameter " + idx);
198+
}
190199

191200
YdbProperties ydbProperties = YdbJdbcTools.from(url, properties);
192201
Assertions.assertEquals("grpc://ydb-demo.testhost.org:2135/testing/ydb",
@@ -200,7 +209,7 @@ public void getTokenAs(String token, String expectValue) throws SQLException {
200209
token += TOKEN_FILE.getAbsolutePath();
201210
}
202211

203-
String url = "jdbc:ydb:ydb-demo.testhost.org:2135/test/pr/testing/ci?token=" + token;
212+
String url = "jdbc:ydb:ydb-demo.testhost.org:2135/test/db?token=" + token;
204213
Properties properties = new Properties();
205214
YdbProperties ydbProperties = YdbJdbcTools.from(url, properties);
206215

@@ -212,7 +221,7 @@ public void getTokenAs(String token, String expectValue) throws SQLException {
212221
@ParameterizedTest
213222
@MethodSource("unknownFiles")
214223
public void getTokenAsInvalid(String token, String expectException) {
215-
String url = "jdbc:ydb:ydb-demo.testhost.org:2135/test/pr/testing/ci?token=" + token;
224+
String url = "jdbc:ydb:ydb-demo.testhost.org:2135/test/db?token=" + token;
216225
ExceptionAssert.ydbConfiguration(expectException, () -> YdbJdbcTools.from(url, new Properties()));
217226
}
218227

@@ -222,7 +231,7 @@ public void getCaCertificateAs(String certificate, String expectValue) throws SQ
222231
if ("file:".equals(certificate)) {
223232
certificate += CERTIFICATE_FILE.getAbsolutePath();
224233
}
225-
String url = "jdbc:ydb:ydb-demo.testhost.org:2135/test/pr/testing/ci" +
234+
String url = "jdbc:ydb:ydb-demo.testhost.org:2135/test/db" +
226235
"?secureConnectionCertificate=" + certificate;
227236
Properties properties = new Properties();
228237
YdbProperties ydbProperties = YdbJdbcTools.from(url, properties);
@@ -235,15 +244,15 @@ public void getCaCertificateAs(String certificate, String expectValue) throws SQ
235244
@ParameterizedTest
236245
@MethodSource("unknownFiles")
237246
public void getCaCertificateAsInvalid(String certificate, String expectException) {
238-
String url = "jdbc:ydb:ydb-demo.testhost.org:2135/test/pr/testing/ci" +
247+
String url = "jdbc:ydb:ydb-demo.testhost.org:2135/test/db" +
239248
"?secureConnectionCertificate=" + certificate;
240249
ExceptionAssert.ydbConfiguration(expectException, () -> YdbJdbcTools.from(url, new Properties()));
241250
}
242251

243252
@ParameterizedTest
244253
@MethodSource("invalidDurationParams")
245254
public void invalidDuration(String param) {
246-
String url = "jdbc:ydb:ydb-demo.testhost.org:2135/test/pr/testing/ci?" + param + "=1bc";
255+
String url = "jdbc:ydb:ydb-demo.testhost.org:2135/test/db?" + param + "=1bc";
247256
ExceptionAssert.ydbConfiguration("Unable to convert property " + param +
248257
": Unable to parse value [1bc] -> [PT1BC] as Duration: Text cannot be parsed to a Duration",
249258
() -> YdbJdbcTools.from(url, new Properties()));
@@ -252,7 +261,7 @@ public void invalidDuration(String param) {
252261
@ParameterizedTest
253262
@MethodSource("invalidIntegerParams")
254263
public void invalidInteger(String param) {
255-
String url = "jdbc:ydb:ydb-demo.testhost.org:2135/test/pr/testing/ci?" + param + "=1bc";
264+
String url = "jdbc:ydb:ydb-demo.testhost.org:2135/test/db?" + param + "=1bc";
256265
ExceptionAssert.ydbConfiguration("Unable to convert property " + param +
257266
": Unable to parse value [1bc] as Integer: For input string: \"1bc\"",
258267
() -> YdbJdbcTools.from(url, new Properties()));
@@ -313,14 +322,17 @@ static DriverPropertyInfo[] defaultPropertyInfo(@Nullable String localDatacenter
313322
YdbOperationProperty.AUTOCOMMIT.toDriverPropertyInfo("true"),
314323
YdbOperationProperty.TRANSACTION_LEVEL.toDriverPropertyInfo("8"),
315324

316-
YdbOperationProperty.ENFORCE_SQL_V1.toDriverPropertyInfo("true"),
317-
YdbOperationProperty.ENFORCE_VARIABLE_PREFIX.toDriverPropertyInfo("true"),
318325
YdbOperationProperty.CACHE_CONNECTIONS_IN_DRIVER.toDriverPropertyInfo("true"),
319-
YdbOperationProperty.DETECT_SQL_OPERATIONS.toDriverPropertyInfo("true"),
320326

327+
YdbOperationProperty.ENFORCE_SQL_V1.toDriverPropertyInfo("false"),
328+
329+
YdbOperationProperty.DISABLE_DETECT_SQL_OPERATIONS.toDriverPropertyInfo("false"),
321330
YdbOperationProperty.DISABLE_PREPARE_DATAQUERY.toDriverPropertyInfo("false"),
322331
YdbOperationProperty.DISABLE_AUTO_PREPARED_BATCHES.toDriverPropertyInfo("false"),
323-
YdbOperationProperty.DISABLE_JDBC_PARAMETERS.toDriverPropertyInfo("false")
332+
YdbOperationProperty.DISABLE_JDBC_PARAMETERS.toDriverPropertyInfo("false"),
333+
YdbOperationProperty.DISABLE_JDBC_PARAMETERS_DECLARE.toDriverPropertyInfo("false"),
334+
335+
YdbOperationProperty.JDBC_SUPPORT_LEVEL.toDriverPropertyInfo("" + YdbConst.DEFAULT_JDBC_SUPPORT_LEVEL)
324336
};
325337
}
326338

@@ -350,14 +362,15 @@ static Properties customizedProperties() {
350362
properties.setProperty("autoCommit", "true");
351363
properties.setProperty("transactionLevel", "4");
352364

353-
properties.setProperty("enforceSqlV1", "false");
354-
properties.setProperty("enforceVariablePrefix", "false");
355365
properties.setProperty("cacheConnectionsInDriver", "false");
356-
properties.setProperty("detectSqlOperations", "false");
357366

358-
properties.setProperty("disableAutoPreparedBatches", "true");
367+
properties.setProperty("enforceSqlV1", "true");
359368
properties.setProperty("disablePrepareDataQuery", "true");
369+
properties.setProperty("disableAutoPreparedBatches", "true");
370+
properties.setProperty("disableDetectSqlOperations", "true");
360371
properties.setProperty("disableJdbcParameters", "true");
372+
properties.setProperty("disableJdbcParameterDeclare", "true");
373+
properties.setProperty("jdbcSupportLevel", "0");
361374
return properties;
362375
}
363376

@@ -386,19 +399,22 @@ static DriverPropertyInfo[] customizedPropertyInfo() {
386399
YdbOperationProperty.AUTOCOMMIT.toDriverPropertyInfo("true"),
387400
YdbOperationProperty.TRANSACTION_LEVEL.toDriverPropertyInfo("4"),
388401

389-
YdbOperationProperty.ENFORCE_SQL_V1.toDriverPropertyInfo("false"),
390-
YdbOperationProperty.ENFORCE_VARIABLE_PREFIX.toDriverPropertyInfo("false"),
391402
YdbOperationProperty.CACHE_CONNECTIONS_IN_DRIVER.toDriverPropertyInfo("false"),
392-
YdbOperationProperty.DETECT_SQL_OPERATIONS.toDriverPropertyInfo("false"),
403+
404+
YdbOperationProperty.ENFORCE_SQL_V1.toDriverPropertyInfo("true"),
405+
YdbOperationProperty.DISABLE_DETECT_SQL_OPERATIONS.toDriverPropertyInfo("true"),
393406
YdbOperationProperty.DISABLE_PREPARE_DATAQUERY.toDriverPropertyInfo("true"),
394407
YdbOperationProperty.DISABLE_AUTO_PREPARED_BATCHES.toDriverPropertyInfo("true"),
395-
YdbOperationProperty.DISABLE_JDBC_PARAMETERS.toDriverPropertyInfo("true")
408+
YdbOperationProperty.DISABLE_JDBC_PARAMETERS.toDriverPropertyInfo("true"),
409+
YdbOperationProperty.DISABLE_JDBC_PARAMETERS_DECLARE.toDriverPropertyInfo("true"),
410+
411+
YdbOperationProperty.JDBC_SUPPORT_LEVEL.toDriverPropertyInfo("0")
396412
};
397413
}
398414

399415
static void checkCustomizedProperties(YdbProperties properties) {
400416
YdbConnectionProperties conn = properties.getConnectionProperties();
401-
Assertions.assertEquals("grpc://ydb-demo.testhost.org:2135/test/pr/testing/ci",
417+
Assertions.assertEquals("grpc://ydb-demo.testhost.org:2135/test/db",
402418
conn.getConnectionString());
403419

404420
YdbOperationProperties ops = properties.getOperationProperties();
@@ -409,14 +425,8 @@ static void checkCustomizedProperties(YdbProperties properties) {
409425
Assertions.assertEquals(Duration.ofSeconds(6), ops.getSessionTimeout());
410426
Assertions.assertTrue(ops.isAutoCommit());
411427
Assertions.assertEquals(YdbConst.ONLINE_CONSISTENT_READ_ONLY, ops.getTransactionLevel());
412-
Assertions.assertFalse(ops.isEnforceSqlV1());
413-
Assertions.assertFalse(ops.isEnforceVariablePrefix());
414428
Assertions.assertFalse(ops.isCacheConnectionsInDriver());
415-
Assertions.assertFalse(ops.isDetectSqlOperations());
416-
417-
Assertions.assertTrue(ops.isAutoPreparedBatchesDisabled());
418-
Assertions.assertTrue(ops.isPrepareDataQueryDisabled());
419-
Assertions.assertTrue(ops.isJdbcParametersSupportDisabled());
429+
Assertions.assertEquals(0, ops.getJdbcSupportLevel());
420430
}
421431

422432
static String asString(DriverPropertyInfo info) {
@@ -450,18 +460,18 @@ public static Collection<Arguments> urlsToCheck() {
450460
true, "grpc://ydb-demo.testhost.org:2135"),
451461
Arguments.of("jdbc:ydb:ydb-demo.testhost.org",
452462
true, "grpc://ydb-demo.testhost.org"),
453-
Arguments.of("jdbc:ydb:ydb-demo.testhost.org:2135?database=test/pr/testing/ci",
454-
true, "grpc://ydb-demo.testhost.org:2135/test/pr/testing/ci"),
463+
Arguments.of("jdbc:ydb:ydb-demo.testhost.org:2135?database=test/db",
464+
true, "grpc://ydb-demo.testhost.org:2135/test/db"),
455465
Arguments.of("jdbc:ydb:grpcs://ydb-demo.testhost.org",
456466
true, "grpcs://ydb-demo.testhost.org"),
457-
Arguments.of("jdbc:ydb:ydb-demo.testhost.org:2170?database=/test/pr/testing/ci",
458-
true, "grpc://ydb-demo.testhost.org:2170/test/pr/testing/ci"),
459-
Arguments.of("jdbc:ydb:ydb-demo.testhost.org:2133/test/pr/testing/ci",
460-
true, "grpc://ydb-demo.testhost.org:2133/test/pr/testing/ci"),
461-
Arguments.of("jdbc:ydb:grpcs://ydb-demo.testhost.org?database=test/pr/testing/ci&dc=man",
462-
true, "grpcs://ydb-demo.testhost.org/test/pr/testing/ci"),
463-
Arguments.of("jdbc:ydb:ydb-demo.testhost.org:2135/test/pr/testing/ci?dc=man",
464-
true, "grpc://ydb-demo.testhost.org:2135/test/pr/testing/ci"),
467+
Arguments.of("jdbc:ydb:ydb-demo.testhost.org:2170?database=/test/db",
468+
true, "grpc://ydb-demo.testhost.org:2170/test/db"),
469+
Arguments.of("jdbc:ydb:ydb-demo.testhost.org:2133/test/db",
470+
true, "grpc://ydb-demo.testhost.org:2133/test/db"),
471+
Arguments.of("jdbc:ydb:grpcs://ydb-demo.testhost.org?database=test/db&dc=man",
472+
true, "grpcs://ydb-demo.testhost.org/test/db"),
473+
Arguments.of("jdbc:ydb:ydb-demo.testhost.org:2135/test/db?dc=man",
474+
true, "grpc://ydb-demo.testhost.org:2135/test/db"),
465475
Arguments.of("ydb:",
466476
false, null),
467477
Arguments.of("jdbc:ydb",

jdbc/src/test/java/tech/ydb/jdbc/impl/YdbConnectionImplTest.java

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,7 @@ public void prepareCallNotSupported() {
223223
@Test
224224
public void nativeSQL() throws SQLException {
225225
String nativeSQL = jdbc.connection().nativeSQL("select ? + ?");
226-
Assertions.assertEquals(YdbConst.PREFIX_SYNTAX_V1 +
227-
"\n-- DECLARE 2 PARAMETERS" +
228-
"\nselect $jp1 + $jp2", nativeSQL);
226+
Assertions.assertEquals("-- DECLARE 2 PARAMETERS\nselect $jp1 + $jp2", nativeSQL);
229227
}
230228

231229
@Test
@@ -328,6 +326,42 @@ public void commit() throws SQLException {
328326
}
329327
}
330328

329+
@Test
330+
public void ddlAutoCommit() throws SQLException {
331+
jdbc.connection().setAutoCommit(false);
332+
try (Statement statement = jdbc.connection().createStatement()) {
333+
Assertions.assertTrue(statement.execute(SELECT_2_2));
334+
String txId = currentTxId();
335+
Assertions.assertNotNull(txId);
336+
337+
Assertions.assertTrue(statement.execute(SELECT_2_2));
338+
Assertions.assertEquals(txId, currentTxId());
339+
340+
Assertions.assertTrue(statement.execute(QUERIES.selectAllSQL()));
341+
Assertions.assertEquals(txId, currentTxId());
342+
343+
Assertions.assertFalse(statement.execute(SIMPLE_UPSERT));
344+
Assertions.assertEquals(txId, currentTxId());
345+
346+
Assertions.assertTrue(statement.execute(SELECT_2_2));
347+
Assertions.assertEquals(txId, currentTxId());
348+
349+
// DDL - equals to commit
350+
statement.execute("CREATE TABLE tmp_table (id Int32, primary key (id))");
351+
Assertions.assertNull(currentTxId());
352+
353+
statement.execute("DROP TABLE tmp_table");
354+
Assertions.assertNull(currentTxId());
355+
356+
try (ResultSet result = statement.executeQuery(QUERIES.selectAllSQL())) {
357+
Assertions.assertTrue(result.next());
358+
}
359+
} finally {
360+
cleanTable();
361+
jdbc.connection().setAutoCommit(true);
362+
}
363+
}
364+
331365
@Test
332366
public void rollback() throws SQLException {
333367
jdbc.connection().setAutoCommit(false);

jdbc/src/test/java/tech/ydb/jdbc/query/QueryLexerTest.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
package tech.ydb.jdbc.query;
22

3+
34
import java.sql.SQLException;
45

56
import org.junit.jupiter.api.Assertions;
67
import org.junit.jupiter.api.Test;
78

9+
810
/**
911
*
1012
* @author Aleksandr Gorshenin
1113
*/
1214
public class QueryLexerTest {
1315

1416
private QueryType parseQueryType(YdbQueryOptions opts, String sql) throws SQLException {
15-
YdbQueryBuilder builder = new YdbQueryBuilder(sql);
16-
JdbcQueryLexer.buildQuery(builder, opts);
17-
return builder.getQueryType();
17+
return YdbQuery.from(opts, sql).type();
1818
}
1919

2020
private String parseQuery(YdbQueryOptions opts, String sql) throws SQLException {
21-
YdbQueryBuilder builder = new YdbQueryBuilder(sql);
22-
JdbcQueryLexer.buildQuery(builder, opts);
23-
return builder.buildYQL();
21+
return YdbQuery.from(opts, sql).getYqlQuery(null);
2422
}
2523

2624
private void assertMixType(YdbQueryOptions opts, String types, String sql) {
@@ -31,9 +29,21 @@ private void assertMixType(YdbQueryOptions opts, String types, String sql) {
3129
Assertions.assertEquals("Query cannot contain expressions with different types: " + types, ex.getMessage());
3230
}
3331

32+
@Test
33+
public void enforceV1Test() throws SQLException {
34+
YdbQueryOptions disabled = new YdbQueryOptions(false, true, true, true, true, true);
35+
YdbQueryOptions enabled = new YdbQueryOptions(true, true, true, true, true, true);
36+
37+
Assertions.assertEquals("CREATE TABLE test_table (id int, value text)",
38+
parseQuery(disabled, "CREATE TABLE test_table (id int, value text)"));
39+
40+
Assertions.assertEquals("--!syntax_v1\nCREATE TABLE test_table (id int, value text)",
41+
parseQuery(enabled, "CREATE TABLE test_table (id int, value text)"));
42+
}
43+
3444
@Test
3545
public void queryTypesTest() throws SQLException {
36-
YdbQueryOptions opts = new YdbQueryOptions(true, false, false, false);
46+
YdbQueryOptions opts = new YdbQueryOptions(false, true, false, false, false, false);
3747

3848
Assertions.assertEquals(QueryType.SCHEME_QUERY, parseQueryType(opts,
3949
"CREATE TABLE test_table (id int, value text)"
@@ -62,6 +72,12 @@ public void queryTypesTest() throws SQLException {
6272
"DELETE FROM test_table"
6373
));
6474

75+
Assertions.assertEquals(QueryType.DATA_QUERY, parseQueryType(opts,
76+
"SELECT id, value FROM test_table;\n" +
77+
"UPDATE test_table SET value = ? WHERE id = ?;" +
78+
"SELECT id, value FROM test_table WHERE id=CREATE"
79+
));
80+
6581
Assertions.assertEquals(QueryType.SCAN_QUERY, parseQueryType(opts,
6682
"SCAN SELECT id, value FROM test_table"
6783
));
@@ -73,7 +89,7 @@ public void queryTypesTest() throws SQLException {
7389

7490
@Test
7591
public void mixQueryExceptionTest() throws SQLException {
76-
YdbQueryOptions opts = new YdbQueryOptions(true, false, false, false);
92+
YdbQueryOptions opts = new YdbQueryOptions(false, true, false, false, false, false);
7793

7894
assertMixType(opts, "SCHEME_QUERY, DATA_QUERY",
7995
"CREATE TABLE test_table (id int, value text);" +

0 commit comments

Comments
 (0)