From 67f7fab44d0e399932920751952acbf080d78790 Mon Sep 17 00:00:00 2001 From: Jeff Wasty Date: Wed, 10 Apr 2024 10:27:34 -0700 Subject: [PATCH] Changes to MONEY/SMALLMONEY PR (#2379) (#2383) * Changes as per prior code review * setupMoneyTests can be added to beforeAll, it should be ran each time * Passed in wrong values for tests --- .../jdbc/bulkCopy/BulkCopyAllTypesTest.java | 128 ++++++++++++++++-- .../jdbc/bulkCopy/BulkCopyMoneyTest.java | 123 ----------------- .../sqlserver/testframework/Constants.java | 4 + 3 files changed, 117 insertions(+), 138 deletions(-) delete mode 100644 src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyMoneyTest.java diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyAllTypesTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyAllTypesTest.java index 660406ce50..a78e531613 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyAllTypesTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyAllTypesTest.java @@ -4,13 +4,33 @@ */ package com.microsoft.sqlserver.jdbc.bulkCopy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.Timestamp; import java.sql.Types; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import javax.sql.RowSetMetaData; +import javax.sql.rowset.CachedRowSet; +import javax.sql.rowset.RowSetFactory; +import javax.sql.rowset.RowSetMetaDataImpl; +import javax.sql.rowset.RowSetProvider; + +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; @@ -20,8 +40,10 @@ import com.microsoft.sqlserver.jdbc.ComparisonUtil; import com.microsoft.sqlserver.jdbc.RandomData; import com.microsoft.sqlserver.jdbc.RandomUtil; +import com.microsoft.sqlserver.jdbc.SQLServerBulkCSVFileRecord; import com.microsoft.sqlserver.jdbc.SQLServerBulkCopy; import com.microsoft.sqlserver.jdbc.SQLServerBulkCopyOptions; +import com.microsoft.sqlserver.jdbc.TestResource; import com.microsoft.sqlserver.jdbc.TestUtils; import com.microsoft.sqlserver.testframework.AbstractSQLGenerator; import com.microsoft.sqlserver.testframework.AbstractTest; @@ -31,20 +53,6 @@ import com.microsoft.sqlserver.testframework.DBTable; import com.microsoft.sqlserver.testframework.PrepUtil; -import javax.sql.RowSetMetaData; -import javax.sql.rowset.CachedRowSet; -import javax.sql.rowset.RowSetFactory; -import javax.sql.rowset.RowSetMetaDataImpl; -import javax.sql.rowset.RowSetProvider; - -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.IntStream; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - @RunWith(JUnitPlatform.class) public class BulkCopyAllTypesTest extends AbstractTest { @@ -55,12 +63,26 @@ public class BulkCopyAllTypesTest extends AbstractTest { @BeforeAll public static void setupTests() throws Exception { setConnection(); + setupMoneyTests(); + } + + public static void setupMoneyTests() throws SQLException { + try (Connection con = getConnection(); Statement stmt = con.createStatement()) { + TestUtils.dropTableIfExists(destTableName, stmt); + TestUtils.dropTableIfExists(destTableName2, stmt); + + String table = "create table " + destTableName + " (c1 smallmoney, c2 money)"; + stmt.execute(table); + table = "create table " + destTableName2 + " (c1 smallmoney, c2 money)"; + stmt.execute(table); + } } /** * Test TVP with result set - * + * * @throws SQLException + * an exception */ @Test @Tag(Constants.xAzureSQLDW) @@ -127,6 +149,12 @@ private void terminateVariation() throws SQLException { private static final String dateTimeTestTable = AbstractSQLGenerator .escapeIdentifier(RandomUtil.getIdentifier("bulkCopyTimestampTest")); + /** + * Test money/smallmoney with BulkCopy + * + * @throws SQLException + * an exception + */ @Test public void testBulkCopyTimestamp() throws SQLException { List timeStamps = new ArrayList<>(); @@ -185,4 +213,74 @@ public void testBulkCopyTimestamp() throws SQLException { private static long getTime(Timestamp time) { return (3 * time.getTime() + 5) / 10; } + + static String encoding = Constants.UTF8; + static String delimiter = Constants.COMMA; + static String destTableName = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("moneyBulkCopyDest")); + static String destTableName2 = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("moneyBulkCopyDest")); + + @Test + public void testMoneyWithBulkCopy() throws Exception { + try (Connection conn = PrepUtil.getConnection(connectionString)) { + testMoneyLimits(Constants.MIN_VALUE_SMALLMONEY - 1, Constants.MAX_VALUE_MONEY - 1, conn); // 1 less than SMALLMONEY MIN + testMoneyLimits(Constants.MAX_VALUE_SMALLMONEY + 1, Constants.MAX_VALUE_MONEY - 1, conn); // 1 more than SMALLMONEY MAX + testMoneyLimits(Constants.MAX_VALUE_SMALLMONEY - 1, Constants.MIN_VALUE_MONEY - 1, conn); // 1 less than MONEY MIN + testMoneyLimits(Constants.MAX_VALUE_SMALLMONEY - 1, Constants.MAX_VALUE_MONEY + 1, conn); // 1 more than MONEY MAX + } + } + + private void testMoneyLimits(double smallMoneyVal, double moneyVal, Connection conn) throws Exception { + SQLServerBulkCSVFileRecord fileRecord = constructFileRecord(smallMoneyVal, moneyVal); + + try { + testMoneyWithBulkCopy(conn, fileRecord); + fail(TestResource.getResource("R_expectedExceptionNotThrown")); + } catch (SQLException e) { + assertTrue(e.getMessage().matches(TestUtils.formatErrorMsg("R_valueOutOfRange")), e.getMessage()); + } + } + + private SQLServerBulkCSVFileRecord constructFileRecord(double smallMoneyVal, double moneyVal) throws Exception { + Map data = new HashMap(); + data.put(smallMoneyVal, moneyVal); + + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("smallmoneycol, moneycol\n"); + + for (Map.Entry entry : data.entrySet()) { + stringBuilder.append(String.format("%s,%s\n", entry.getKey(), entry.getValue())); + } + + byte[] bytes = stringBuilder.toString().getBytes(StandardCharsets.UTF_8); + SQLServerBulkCSVFileRecord fileRecord; + try (InputStream inputStream = new ByteArrayInputStream(bytes)) { + fileRecord = new SQLServerBulkCSVFileRecord(inputStream, encoding, delimiter, true); + } + return fileRecord; + } + + private void testMoneyWithBulkCopy(Connection conn, SQLServerBulkCSVFileRecord fileRecord) throws SQLException { + try (SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(conn); Statement stmt = conn.createStatement()) { + + fileRecord.addColumnMetadata(1, "c1", java.sql.Types.DECIMAL, 10, 4); // with smallmoney + fileRecord.addColumnMetadata(2, "c2", java.sql.Types.DECIMAL, 19, 4); // with money + + bulkCopy.setDestinationTableName(destTableName); + bulkCopy.writeToServer(fileRecord); + + try (ResultSet rs = stmt.executeQuery("select * FROM " + destTableName + " order by c1"); + SQLServerBulkCopy bcOperation = new SQLServerBulkCopy(conn)) { + bcOperation.setDestinationTableName(destTableName2); + bcOperation.writeToServer(rs); + } + } + } + + @AfterAll + public static void cleanUp() throws Exception { + try (Connection con = getConnection(); Statement stmt = con.createStatement()) { + TestUtils.dropTableIfExists(destTableName, stmt); + TestUtils.dropTableIfExists(destTableName2, stmt); + } + } } diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyMoneyTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyMoneyTest.java deleted file mode 100644 index bb954e95cd..0000000000 --- a/src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyMoneyTest.java +++ /dev/null @@ -1,123 +0,0 @@ -package com.microsoft.sqlserver.jdbc.bulkCopy; - -import static org.junit.Assert.fail; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.io.ByteArrayInputStream; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.HashMap; -import java.util.Map; - -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.platform.runner.JUnitPlatform; -import org.junit.runner.RunWith; - -import com.microsoft.sqlserver.jdbc.RandomUtil; -import com.microsoft.sqlserver.jdbc.SQLServerBulkCSVFileRecord; -import com.microsoft.sqlserver.jdbc.SQLServerBulkCopy; -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; -import com.microsoft.sqlserver.testframework.PrepUtil; - - -/** - * Tests money/smallmoney limits with BulkCopy - */ -@RunWith(JUnitPlatform.class) -public class BulkCopyMoneyTest extends AbstractTest { - static String encoding = Constants.UTF8; - static String delimiter = Constants.COMMA; - static String destTableName = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("moneyBulkCopyDest")); - static String destTableName2 = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("moneyBulkCopyDest")); - - @Test - /** - * Tests money and smallmoney with bulkcopy using minimum and maximum values of each - */ - public void testMoneyWithBulkCopy() throws Exception { - try (Connection conn = PrepUtil.getConnection(connectionString)) { - testMoneyLimits(-214799.3648, 922337203685387.5887, conn); // SMALLMONEY MIN - testMoneyLimits(214799.3698, 922337203685387.5887, conn); // SMALLMONEY MAX - testMoneyLimits(214719.3698, -922337203685497.5808, conn); // MONEY MIN - testMoneyLimits(214719.3698, 922337203685478.5807, conn); // MONEY MAX - } - } - - private void testMoneyLimits(double smallMoneyVal, double moneyVal, Connection conn) throws Exception { - SQLServerBulkCSVFileRecord fileRecord = constructFileRecord(smallMoneyVal, moneyVal); - - try { - testMoneyWithBulkCopy(conn, fileRecord); - fail(TestResource.getResource("R_expectedExceptionNotThrown")); - } catch (SQLException e) { - assertTrue(e.getMessage().matches(TestUtils.formatErrorMsg("R_valueOutOfRange")), e.getMessage()); - } - } - - private SQLServerBulkCSVFileRecord constructFileRecord(double smallMoneyVal, double moneyVal) throws Exception { - Map data = new HashMap(); - data.put(smallMoneyVal, moneyVal); - - StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append("smallmoneycol, moneycol\n"); - - for (Map.Entry entry : data.entrySet()) { - stringBuilder.append(String.format("%s,%s\n", entry.getKey(), entry.getValue())); - } - - byte[] bytes = stringBuilder.toString().getBytes(StandardCharsets.UTF_8); - SQLServerBulkCSVFileRecord fileRecord = null; - try (InputStream inputStream = new ByteArrayInputStream(bytes)) { - fileRecord = new SQLServerBulkCSVFileRecord(inputStream, encoding, delimiter, true); - } - return fileRecord; - } - - private void testMoneyWithBulkCopy(Connection conn, SQLServerBulkCSVFileRecord fileRecord) throws SQLException { - try (SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(conn); Statement stmt = conn.createStatement()) { - - fileRecord.addColumnMetadata(1, "c1", java.sql.Types.DECIMAL, 10, 4); // with smallmoney - fileRecord.addColumnMetadata(2, "c2", java.sql.Types.DECIMAL, 19, 4); // with money - - bulkCopy.setDestinationTableName(destTableName); - bulkCopy.writeToServer(fileRecord); - - try (ResultSet rs = stmt.executeQuery("select * FROM " + destTableName + " order by c1"); - SQLServerBulkCopy bcOperation = new SQLServerBulkCopy(conn);) { - bcOperation.setDestinationTableName(destTableName2); - bcOperation.writeToServer(rs); - } - } - } - - @BeforeAll - public static void setupTests() throws SQLException { - try (Connection con = getConnection(); Statement stmt = con.createStatement()) { - TestUtils.dropTableIfExists(destTableName, stmt); - TestUtils.dropTableIfExists(destTableName2, stmt); - - String table = "create table " + destTableName + " (c1 smallmoney, c2 money)"; - stmt.execute(table); - table = "create table " + destTableName2 + " (c1 smallmoney, c2 money)"; - stmt.execute(table); - } - } - - @AfterAll - public static void cleanUp() throws Exception { - try (Connection con = getConnection(); Statement stmt = con.createStatement()) { - TestUtils.dropTableIfExists(destTableName, stmt); - TestUtils.dropTableIfExists(destTableName2, stmt); - } - } -} diff --git a/src/test/java/com/microsoft/sqlserver/testframework/Constants.java b/src/test/java/com/microsoft/sqlserver/testframework/Constants.java index 0b1399c456..e4338fc92e 100644 --- a/src/test/java/com/microsoft/sqlserver/testframework/Constants.java +++ b/src/test/java/com/microsoft/sqlserver/testframework/Constants.java @@ -180,6 +180,10 @@ private Constants() {} public static final String PREPARE_METHOD = "PREPAREMETHOD"; public static final String CONFIG_PROPERTIES_FILE = "config.properties"; public static final String UTF8 = "UTF-8"; + public static final double MAX_VALUE_MONEY = 922337203685477.5807; + public static final double MIN_VALUE_MONEY = -922337203685477.5808; + public static final double MAX_VALUE_SMALLMONEY = 214748.3647; + public static final double MIN_VALUE_SMALLMONEY = -214748.3648; public enum LOB { CLOB,