Skip to content

Commit

Permalink
Tests | Added more datatype tests to increase code coverage (#878)
Browse files Browse the repository at this point in the history
* ported tests from VSO and added tests to increase code coverage

* moved resultset tests to resultset

* removed unused import

* added tests to increase code coverage

* fixed issue with timezone

* fixed issue with timezone

* modified testWithThaiLocale to use current time

* fixed issue with timezone

* fixed issue with timezone

* fixed issue with timezone

* added SparseTest and BigIntegerTest and cleaned up with try-with-resources

* review changes

* review comments

* more review updates

* review updates

* removed dummy file

* review udpates

* more cleanup

* updated isSqlAzure

* review updates

* more review updates

* fixed imports

* updated failed error strings with more detail

* added locale to time format

* format date for comparision

* 1 more timezone fix

* cleanup leaded stream resources

* manually merged with #903 changes for now

* more cleanup on assertEqual checks

* more cleanup on assertEqual checks

* cosmetic changes from review

* updated all assertEquals params to proper expected, actual order

* fixed comments and leaked resultsets
  • Loading branch information
lilgreenbird authored Dec 25, 2018
2 parents 3f9635a + db38827 commit ec052ca
Show file tree
Hide file tree
Showing 10 changed files with 2,413 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ protected Object[][] getContents() {
{"R_expectedValue", "Expected value: "}, {"R_expectedValueAtIndex", "Expected value at index: "},
{"R_switchFailed", "Switch case is not matched with data"},
{"R_resultsetNotInstance", "Result set is not instance of SQLServerResultSet"},
{"R_noJRESupport", "No JRE support for {0}"},
{"R_incorrectColumnNum", "Column name or number of supplied values does not match table definition."},
{"R_incorrectColumnNumInsert",
"There are fewer columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement."},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.MessageFormat;
import java.util.concurrent.ThreadLocalRandom;

Expand All @@ -22,6 +23,7 @@

import com.microsoft.sqlserver.jdbc.ComparisonUtil;
import com.microsoft.sqlserver.jdbc.TestResource;
import com.microsoft.sqlserver.jdbc.TestUtils;
import com.microsoft.sqlserver.testframework.DBConnection;
import com.microsoft.sqlserver.testframework.DBResultSet;
import com.microsoft.sqlserver.testframework.DBStatement;
Expand Down Expand Up @@ -56,7 +58,7 @@ public static void closeConnection() throws SQLException {

@Test
@DisplayName("BulkCopy:test no explicit column mapping")
public void testNoExplicitCM() {
public void testNoExplicitCM() throws SQLException {
DBTable destTable = null;
try {
// create dest table
Expand All @@ -68,16 +70,13 @@ public void testNoExplicitCM() {
bulkWrapper.setUsingConnection((0 == ThreadLocalRandom.current().nextInt(2)) ? true : false);
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTable, destTable);
} finally {
if (null != destTable) {
// drop dest table
stmt.dropTable(destTable);
}
TestUtils.dropTableIfExists(destTable.getEscapedTableName(), (Statement) stmt.product());
}
}

@Test
@DisplayName("BulkCopy:test explicit column mapping")
public void testExplicitCM() {
public void testExplicitCM() throws SQLException {
DBTable destTable = null;
try {
// create dest table
Expand Down Expand Up @@ -110,16 +109,13 @@ public void testExplicitCM() {
}
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTable, destTable);
} finally {
// drop dest table
if (null != destTable) {
stmt.dropTable(destTable);
}
TestUtils.dropTableIfExists(destTable.getEscapedTableName(), (Statement) stmt.product());
}
}

@Test
@DisplayName("BulkCopy:test unicode column mapping")
public void testUnicodeCM() {
public void testUnicodeCM() throws SQLException {
DBTable sourceTableUnicode = null;
DBTable destTableUnicode = null;
try {
Expand Down Expand Up @@ -158,18 +154,14 @@ public void testUnicodeCM() {
}
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTableUnicode, destTableUnicode);
} finally {
if (null != sourceTableUnicode) {
dropTable(sourceTableUnicode.getEscapedTableName());
}
if (null != destTableUnicode) {
dropTable(destTableUnicode.getEscapedTableName());
}
TestUtils.dropTableIfExists(sourceTableUnicode.getEscapedTableName(), (Statement) stmt.product());
TestUtils.dropTableIfExists(destTableUnicode.getEscapedTableName(), (Statement) stmt.product());
}
}

@Test
@DisplayName("BulkCopy:test repetitive column mapping")
public void testRepetitiveCM() {
public void testRepetitiveCM() throws SQLException {
DBTable sourceTable1 = null;
DBTable destTable = null;
try {
Expand Down Expand Up @@ -225,18 +217,14 @@ public void testRepetitiveCM() {
fail(form.format(msgArgs) + "\n" + destTable.getTableName() + "\n" + e.getMessage());
}
} finally {
if (null != sourceTable1) {
dropTable(sourceTable1.getEscapedTableName());
}
if (null != destTable) {
dropTable(destTable.getEscapedTableName());
}
TestUtils.dropTableIfExists(sourceTable1.getEscapedTableName(), (Statement) stmt.product());
TestUtils.dropTableIfExists(destTable.getEscapedTableName(), (Statement) stmt.product());
}
}

@Test
@DisplayName("BulkCopy:test implicit mismatched column mapping")
public void testImplicitMismatchCM() {
public void testImplicitMismatchCM() throws SQLException {
DBTable destTable = null;
try {
// create non unicode dest table with different schema from source table
Expand Down Expand Up @@ -269,15 +257,13 @@ public void testImplicitMismatchCM() {
}
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTable, destTable, true, true);
} finally {
if (null != destTable) {
stmt.dropTable(destTable);
}
TestUtils.dropTableIfExists(destTable.getEscapedTableName(), (Statement) stmt.product());
}
}

@Test
@DisplayName("BulkCopy:test invalid column mapping")
public void testInvalidCM() {
public void testInvalidCM() throws SQLException {
DBTable destTable = null;
try {
// create dest table
Expand Down Expand Up @@ -360,9 +346,7 @@ public void testInvalidCM() {
bulkWrapper.setColumnMapping(Integer.MIN_VALUE, Integer.MAX_VALUE);
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTable, destTable, true, true);
} finally {
if (null != destTable) {
stmt.dropTable(destTable);
}
TestUtils.dropTableIfExists(destTable.getEscapedTableName(), (Statement) stmt.product());
}
}

Expand Down Expand Up @@ -411,15 +395,4 @@ private void validateValuesRepetitiveCM(DBConnection con, DBTable sourceTable,
assertTrue(destinationTable.getTotalRows() == numRows);
}
}

private void dropTable(String tableName) {

String dropSQL = "DROP TABLE [dbo]." + tableName;
try {
stmt.execute(dropSQL);
} catch (SQLException e) {
fail(tableName + " " + TestResource.getResource("R_tableNotDropped") + "\n" + e.getMessage());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.microsoft.sqlserver.jdbc.callablestatement;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

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.TestUtils;
import com.microsoft.sqlserver.testframework.AbstractSQLGenerator;
import com.microsoft.sqlserver.testframework.AbstractTest;


@RunWith(JUnitPlatform.class)
public class CallableMixedTest extends AbstractTest {

@Test
public void datatypestest() throws Exception {
String tableName = RandomUtil.getIdentifier("TFOO3");
String escapedTableName = AbstractSQLGenerator.escapeIdentifier(tableName);
String procName = RandomUtil.getIdentifier("SPFOO3");
String escapedProcName = AbstractSQLGenerator.escapeIdentifier(procName);

try (Connection conn = DriverManager.getConnection(connectionString)) {
try (Statement stmt = conn.createStatement()) {
TestUtils.dropTableIfExists(escapedTableName, stmt);

String createSQL = "create table " + escapedTableName + "(c1_int int primary key, col2 int)";
stmt.executeUpdate(createSQL);

stmt.executeUpdate("Insert into " + escapedTableName + " values(0, 1)");

stmt.executeUpdate("CREATE PROCEDURE " + escapedProcName
+ " (@p2_int int, @p2_int_out int OUTPUT, @p4_smallint smallint, @p4_smallint_out smallint OUTPUT) AS begin transaction SELECT * FROM "
+ escapedTableName
+ " ; SELECT @p2_int_out=@p2_int, @p4_smallint_out=@p4_smallint commit transaction RETURN -2147483648");
}

try (CallableStatement cstmt = conn.prepareCall("{ ? = CALL " + escapedProcName + " (?, ?, ?, ?) }")) {
cstmt.registerOutParameter((int) 1, (int) 4);
cstmt.setObject((int) 2, Integer.valueOf("31"), (int) 4);
cstmt.registerOutParameter((int) 3, (int) 4);

// Test OUT param re-registration
cstmt.registerOutParameter((int) 5, java.sql.Types.BINARY);
cstmt.registerOutParameter((int) 5, (int) 5);
cstmt.setObject((int) 4, Short.valueOf("-5372"), (int) 5);

// get results and a value
try (ResultSet rs = cstmt.executeQuery()) {
rs.next();
assertEquals(0, rs.getInt(1));
assertEquals(-5372, cstmt.getInt((int) 5));
}

// get the param without getting the resultset
try (ResultSet rs = cstmt.executeQuery()) {
assertEquals(-2147483648, cstmt.getInt((int) 1));
}

try (ResultSet rs = cstmt.executeQuery()) {
rs.next();
assertEquals(0, rs.getInt(1));
assertEquals(-2147483648, cstmt.getInt((int) 1));
assertEquals(-5372, cstmt.getInt((int) 5));
}
}
} finally {
try (Connection conn = DriverManager.getConnection(connectionString);
Statement stmt = conn.createStatement()) {
TestUtils.dropTableIfExists(escapedTableName, stmt);
} catch (SQLException e) {
fail(e.toString());
}
}
}
}
Loading

0 comments on commit ec052ca

Please sign in to comment.