Skip to content

Commit

Permalink
test: add tests for column case sensitivity in batch execution
Browse files Browse the repository at this point in the history
- Add test case for matching column case in batch execution
- Add test case for mismatched column case in batch execution, expected SQLServerException
  • Loading branch information
wooln committed Jan 21, 2025
1 parent 2a3d372 commit 2b29dfb
Showing 1 changed file with 53 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

import static org.junit.Assert.assertNotNull;
import static org.junit.Assume.assumeTrue;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.*;

import java.lang.reflect.Field;
import java.sql.BatchUpdateException;
Expand All @@ -23,6 +20,7 @@
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
Expand All @@ -33,6 +31,8 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.microsoft.sqlserver.jdbc.*;
import microsoft.sql.DateTimeOffset;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
Expand All @@ -41,12 +41,6 @@
import org.junit.runner.RunWith;
import org.opentest4j.TestAbortedException;

import com.microsoft.sqlserver.jdbc.RandomUtil;
import com.microsoft.sqlserver.jdbc.SQLServerConnection;
import com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement;
import com.microsoft.sqlserver.jdbc.SQLServerStatement;
import com.microsoft.sqlserver.jdbc.TestResource;
import com.microsoft.sqlserver.jdbc.TestUtils;
import com.microsoft.sqlserver.testframework.AbstractSQLGenerator;
import com.microsoft.sqlserver.testframework.AbstractTest;
import com.microsoft.sqlserver.testframework.Constants;
Expand Down Expand Up @@ -568,6 +562,55 @@ public void testBatchStatementCancellation() throws Exception {
}
}

@Test
public void testExecuteBatchColumnCaseMatching() throws Exception {
String varcharTable1 = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("varchartable1"));
// Insert Timestamp using prepared statement when useBulkCopyForBatchInsert=true
try (Connection con = DriverManager.getConnection(connectionString
+ ";useBulkCopyForBatchInsert=true;sendTemporalDataTypesAsStringForBulkCopy=false;")) {
try (Statement statement = con.createStatement()) {
TestUtils.dropTableIfExists(varcharTable1, statement);
String createSql = "CREATE TABLE" + varcharTable1 + " (c1 varchar(10))";
statement.execute(createSql);
}
try (PreparedStatement preparedStatement = con.prepareStatement("INSERT INTO " + varcharTable1 + "(c1) VALUES(?)")) {
preparedStatement.setObject(1, "value1");
preparedStatement.addBatch();
preparedStatement.setObject(1, "value2");
preparedStatement.addBatch();
preparedStatement.executeBatch();
}
}
}

@Test
public void testExecuteBatchColumnCaseMismatch() throws Exception {
String varcharTable1 = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("varchartable1"));
// Insert Timestamp using prepared statement when useBulkCopyForBatchInsert=true
try (Connection con = DriverManager.getConnection(connectionString
+ ";useBulkCopyForBatchInsert=true;sendTemporalDataTypesAsStringForBulkCopy=false;")) {
try (Statement statement = con.createStatement()) {
TestUtils.dropTableIfExists(varcharTable1, statement);
String createSql = "CREATE TABLE" + varcharTable1 + " (c1 varchar(10))";
statement.execute(createSql);
}
// upper case C1
try (PreparedStatement preparedStatement = con.prepareStatement("INSERT INTO " + varcharTable1 + "(C1) VALUES(?)")) {
preparedStatement.setObject(1, "value1");
preparedStatement.addBatch();
preparedStatement.setObject(1, "value2");
preparedStatement.addBatch();
try {
preparedStatement.executeBatch();
fail("Should have failed");
} catch (Exception ex) {
assertInstanceOf(SQLServerException.class, ex);
assertEquals("Unable to retrieve column metadata.", ex.getMessage());
}
}
}
}

/**
* Get a PreparedStatement object and call the addBatch() method with 3 SQL statements and call the executeBatch()
* method and it should return array of Integer values of length 3
Expand Down

0 comments on commit 2b29dfb

Please sign in to comment.