Skip to content

Commit

Permalink
Updated test
Browse files Browse the repository at this point in the history
  • Loading branch information
muskan124947 committed Feb 25, 2025
1 parent 9abe09f commit 4f4e19a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1914,7 +1914,7 @@ private void validateColumnMappings() throws SQLServerException {

// Generate default column mappings
ColumnMapping cm;
for (int i = 1; i <= srcColumnCount; ++i) {
for (Integer i : destColumnMetadata.keySet()) {
// Only skip identity column mapping if KEEP IDENTITY OPTION is FALSE
if (!(destColumnMetadata.get(i).isIdentity && !copyOptions.isKeepIdentity())) {
cm = new ColumnMapping(i, i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.BufferedReader;
import java.io.FileInputStream;
Expand Down Expand Up @@ -449,7 +450,7 @@ public void testCSV2400() {
}
}

/**
/**
* Test to perform bulk copy with a computed column as the last column in the table.
*/
@Test
Expand All @@ -459,18 +460,19 @@ public void testBulkCopyWithComputedColumnAsLastColumn() {
String fileName = filePath + computeColumnCsvFile;

assertDoesNotThrow(() -> {
try (Connection con = getConnection(); Statement stmt = con.createStatement();
SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(con);
SQLServerBulkCSVFileRecord fileRecord = new SQLServerBulkCSVFileRecord(fileName, encoding, ",", true)) {

String createTableSQL = "IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='Test' AND xtype='U') " +
"CREATE TABLE [dbo].[Test](" +
"[NAME] varchar(50) NOT NULL," +
"[AGE] int NULL," +
"[CAL_COL] numeric(17, 2) NULL," +
"[ORIGINAL] varchar(50) NOT NULL," +
"[COMPUTED_COL] AS (right([NAME], 8)) PERSISTED" +
") ON [PRIMARY]";
try (Connection con = getConnection();
Statement stmt = con.createStatement();
SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(con);
SQLServerBulkCSVFileRecord fileRecord = new SQLServerBulkCSVFileRecord(fileName, encoding, ",",
true)) {

String createTableSQL = "CREATE TABLE " + tableName + " (" +
"[NAME] varchar(50) NOT NULL," +
"[AGE] int NULL," +
"[CAL_COL] numeric(17, 2) NULL," +
"[ORIGINAL] varchar(50) NOT NULL," +
"[COMPUTED_COL] AS (right([NAME], 8)) PERSISTED" +
")";
stmt.executeUpdate(createTableSQL);

fileRecord.addColumnMetadata(1, "NAME", java.sql.Types.VARCHAR, 50, 0);
Expand All @@ -487,15 +489,22 @@ public void testBulkCopyWithComputedColumnAsLastColumn() {

bulkCopy.writeToServer(fileRecord);

TestUtils.dropTableIfExists(tableName, stmt);
try (ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM " + tableName)) {
if (rs.next()) {
int rowCount = rs.getInt(1);
assertTrue(rowCount > 0);
}
} finally {
TestUtils.dropTableIfExists(tableName, stmt);
}
} catch (Exception e) {
e.printStackTrace();
}
});
}

/**
* Test to perform bulk copy with a computed column as not the last column in the table.
* Test to perform bulk copy with a computed column not as the last column in the table.
*/
@Test
@DisplayName("Test bulk copy with computed column not as last column")
Expand All @@ -504,19 +513,20 @@ public void testBulkCopyWithComputedColumnNotAsLastColumn() {
String fileName = filePath + computeColumnCsvFile;

assertDoesNotThrow(() -> {
try (Connection con = getConnection(); Statement stmt = con.createStatement();
SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(con);
SQLServerBulkCSVFileRecord fileRecord = new SQLServerBulkCSVFileRecord(fileName, encoding, ",", true)) {

String createTableSQL = "IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='Test' AND xtype='U') " +
"CREATE TABLE [dbo].[Test](" +
"[NAME] varchar(50) NOT NULL," +
"[AGE] int NULL," +
"[CAL_COL] numeric(17, 2) NULL," +
"[ORIGINAL] varchar(50) NOT NULL," +
"[COMPUTED_COL] AS (right([NAME], 8)) PERSISTED," +
"[LAST_COL] varchar(50) NULL" +
") ON [PRIMARY]";
try (Connection con = getConnection();
Statement stmt = con.createStatement();
SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(con);
SQLServerBulkCSVFileRecord fileRecord = new SQLServerBulkCSVFileRecord(fileName, encoding, ",",
true)) {

String createTableSQL = "CREATE TABLE " + tableName + " (" +
"[NAME] varchar(50) NOT NULL," +
"[AGE] int NULL," +
"[CAL_COL] numeric(17, 2) NULL," +
"[ORIGINAL] varchar(50) NOT NULL," +
"[COMPUTED_COL] AS (right([NAME], 8)) PERSISTED," +
"[LAST_COL] varchar(50) NULL" +
")";
stmt.executeUpdate(createTableSQL);

fileRecord.addColumnMetadata(1, "NAME", java.sql.Types.VARCHAR, 50, 0);
Expand All @@ -534,7 +544,14 @@ public void testBulkCopyWithComputedColumnNotAsLastColumn() {

bulkCopy.writeToServer(fileRecord);

TestUtils.dropTableIfExists(tableName, stmt);
try (ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM " + tableName)) {
if (rs.next()) {
int rowCount = rs.getInt(1);
assertTrue(rowCount > 0);
}
} finally {
TestUtils.dropTableIfExists(tableName, stmt);
}
} catch (Exception e) {
e.printStackTrace();
}
Expand Down

0 comments on commit 4f4e19a

Please sign in to comment.