Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 3952fdd

Browse files
coriverasaurabh500
authored andcommitted
Fix issues with test tables in SqlClient tests (#26809)
* Check if full-text index exists before running WarningsBeforeRowsTest. * Remove identity columns from the temp tables created in AdapterTest.
1 parent 47c4317 commit 3952fdd

File tree

2 files changed

+77
-62
lines changed

2 files changed

+77
-62
lines changed

src/System.Data.SqlClient/tests/ManualTests/SQL/AdapterTest/AdapterTest.cs

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public class AdapterTest
1818

1919
// data value and server consts
2020
private const string MagicName = "Magic";
21+
// Use a union statement so that Identity columns don't carry over
22+
private const string _createTableQuery = "select * into {0} from Employees where EmployeeID < 3 union all (select * from Employees where 1 = 0)";
2123
private string _tempTable;
2224
private string _tempKey;
2325

@@ -567,20 +569,20 @@ public void ParameterTest_InOut()
567569
[CheckConnStrSetupFact]
568570
public void UpdateTest()
569571
{
570-
try
572+
using (SqlConnection conn = new SqlConnection(DataTestUtility.TcpConnStr))
573+
using (SqlCommand cmd = conn.CreateCommand())
574+
using (SqlDataAdapter adapter = new SqlDataAdapter())
575+
using (SqlDataAdapter adapterVerify = new SqlDataAdapter())
571576
{
572-
using (SqlConnection conn = new SqlConnection(DataTestUtility.TcpConnStr))
573-
using (SqlCommand cmd = conn.CreateCommand())
574-
using (SqlDataAdapter adapter = new SqlDataAdapter())
575-
using (SqlDataAdapter adapterVerify = new SqlDataAdapter())
576-
{
577-
conn.Open();
577+
conn.Open();
578578

579-
cmd.CommandText = string.Format("SELECT EmployeeID, LastName, FirstName, Title, Address, City, Region, PostalCode, Country into {0} from Employees where EmployeeID < 3", _tempTable);
580-
cmd.ExecuteNonQuery();
581-
cmd.CommandText = "alter table " + _tempTable + " add constraint " + _tempKey + " primary key (EmployeeID)";
582-
cmd.ExecuteNonQuery();
579+
cmd.CommandText = string.Format(_createTableQuery, _tempTable);
580+
cmd.ExecuteNonQuery();
581+
cmd.CommandText = "alter table " + _tempTable + " add constraint " + _tempKey + " primary key (EmployeeID)";
582+
cmd.ExecuteNonQuery();
583583

584+
try
585+
{
584586
PrepareUpdateCommands(adapter, conn, _tempTable);
585587

586588
adapter.SelectCommand = new SqlCommand(string.Format("SELECT EmployeeID, LastName, FirstName, Title, Address, City, Region, PostalCode, Country from {0} where EmployeeID < 3", _tempTable), conn);
@@ -639,10 +641,10 @@ public void UpdateTest()
639641

640642
dataSet.AcceptChanges();
641643
}
642-
}
643-
finally
644-
{
645-
ExecuteNonQueryCommand("DROP TABLE " + _tempTable);
644+
finally
645+
{
646+
ExecuteNonQueryCommand("DROP TABLE " + _tempTable);
647+
}
646648
}
647649
}
648650

@@ -656,15 +658,15 @@ public void BulkUpdateTest()
656658
using (SqlDataAdapter adapter = new SqlDataAdapter())
657659
using (SqlDataAdapter adapterVerify = new SqlDataAdapter())
658660
{
659-
try
660-
{
661-
conn.Open();
661+
conn.Open();
662662

663-
cmd.CommandText = "SELECT EmployeeID, LastName, FirstName, Title, Address, City, Region, PostalCode, Country into " + _tempTable + " from Employees where EmployeeID < 3";
664-
cmd.ExecuteNonQuery();
665-
cmd.CommandText = "alter table " + _tempTable + " add constraint " + _tempKey + " primary key (EmployeeID)";
666-
cmd.ExecuteNonQuery();
663+
cmd.CommandText = string.Format(_createTableQuery, _tempTable);
664+
cmd.ExecuteNonQuery();
665+
cmd.CommandText = "alter table " + _tempTable + " add constraint " + _tempKey + " primary key (EmployeeID)";
666+
cmd.ExecuteNonQuery();
667667

668+
try
669+
{
668670
PrepareUpdateCommands(adapter, conn, _tempTable);
669671

670672
adapter.SelectCommand = new SqlCommand("SELECT EmployeeID, LastName, FirstName, Title, Address, City, Region, PostalCode, Country FROM " + _tempTable + " WHERE EmployeeID < 3", conn);
@@ -762,16 +764,16 @@ public void UpdateRefreshTest()
762764

763765
string spDropInsert = "DROP PROCEDURE sp_insert" + _tempTable;
764766
bool dropSP = false;
765-
try
767+
768+
using (SqlDataAdapter adapter = new SqlDataAdapter())
769+
using (SqlConnection conn = new SqlConnection(DataTestUtility.TcpConnStr))
770+
using (SqlCommand cmd = new SqlCommand(null, conn))
771+
using (SqlCommand temp = new SqlCommand("SELECT id, LastName, FirstName into " + _tempTable + " from ident", conn))
772+
using (SqlCommand tableClean = new SqlCommand("", conn))
766773
{
767-
using (SqlDataAdapter adapter = new SqlDataAdapter())
768-
using (SqlConnection conn = new SqlConnection(DataTestUtility.TcpConnStr))
769-
using (SqlCommand cmd = new SqlCommand(null, conn))
770-
using (SqlCommand temp = new SqlCommand("SELECT id, LastName, FirstName into " + _tempTable + " from ident", conn))
771-
using (SqlCommand tableClean = new SqlCommand("", conn))
774+
ExecuteNonQueryCommand(createIdentTable);
775+
try
772776
{
773-
ExecuteNonQueryCommand(createIdentTable);
774-
775777
adapter.InsertCommand = new SqlCommand()
776778
{
777779
CommandText = "sp_insert" + _tempTable,
@@ -827,15 +829,15 @@ public void UpdateRefreshTest()
827829
(i1 != 0) && (i2 != 0) && (i2 == (i1 + 1)),
828830
string.Format("FAILED: UpdateRefresh, i2 should equal (i1 + 1). i1: {0}. i2: {1}.", i1, i2));
829831
}
830-
}
831-
finally
832-
{
833-
if (dropSP)
832+
finally
834833
{
835-
ExecuteNonQueryCommand(spDropInsert);
836-
ExecuteNonQueryCommand("DROP TABLE " + _tempTable);
834+
if (dropSP)
835+
{
836+
ExecuteNonQueryCommand(spDropInsert);
837+
ExecuteNonQueryCommand("DROP TABLE " + _tempTable);
838+
}
839+
ExecuteNonQueryCommand("DROP TABLE ident");
837840
}
838-
ExecuteNonQueryCommand("DROP TABLE ident");
839841
}
840842
}
841843

@@ -979,21 +981,20 @@ public void SelectAllTest()
979981
[CheckConnStrSetupFact]
980982
public void AutoGenUpdateTest()
981983
{
982-
try
984+
using (SqlConnection conn = new SqlConnection(DataTestUtility.TcpConnStr))
985+
using (SqlCommand cmd = conn.CreateCommand())
986+
using (SqlDataAdapter adapter = new SqlDataAdapter())
987+
using (SqlDataAdapter adapterVerify = new SqlDataAdapter())
983988
{
984-
using (SqlConnection conn = new SqlConnection(DataTestUtility.TcpConnStr))
985-
using (SqlCommand cmd = conn.CreateCommand())
986-
using (SqlDataAdapter adapter = new SqlDataAdapter())
987-
using (SqlDataAdapter adapterVerify = new SqlDataAdapter())
988-
{
989-
conn.Open();
990-
991-
cmd.CommandText = string.Format("SELECT EmployeeID, LastName, FirstName, Title, Address, City, Region, PostalCode, Country into {0} from Employees where EmployeeID < 3", _tempTable);
992-
cmd.ExecuteNonQuery();
989+
conn.Open();
993990

994-
cmd.CommandText = "alter table " + _tempTable + " add constraint " + _tempKey + " primary key (EmployeeID)";
995-
cmd.ExecuteNonQuery();
991+
cmd.CommandText = string.Format(_createTableQuery, _tempTable);
992+
cmd.ExecuteNonQuery();
993+
cmd.CommandText = "alter table " + _tempTable + " add constraint " + _tempKey + " primary key (EmployeeID)";
994+
cmd.ExecuteNonQuery();
996995

996+
try
997+
{
997998
adapter.SelectCommand = new SqlCommand(string.Format("SELECT EmployeeID, LastName, FirstName, Title, Address, City, Region, PostalCode, Country from {0} where EmployeeID < 3", _tempTable), conn);
998999
adapterVerify.SelectCommand = new SqlCommand("SELECT LastName, FirstName FROM " + _tempTable + " where FirstName='" + MagicName + "'", conn);
9991000

@@ -1041,10 +1042,10 @@ public void AutoGenUpdateTest()
10411042
// Verify that set is empty
10421043
VerifyUpdateRow(adapterVerify, dataSetVerify, 0, _tempTable);
10431044
}
1044-
}
1045-
finally
1046-
{
1047-
ExecuteNonQueryCommand("DROP TABLE " + _tempTable);
1045+
finally
1046+
{
1047+
ExecuteNonQueryCommand("DROP TABLE " + _tempTable);
1048+
}
10481049
}
10491050
}
10501051

@@ -1100,16 +1101,15 @@ public void AutoGenBulkUpdateTest()
11001101
using (SqlDataAdapter adapter = new SqlDataAdapter())
11011102
using (SqlDataAdapter adapterVerify = new SqlDataAdapter())
11021103
{
1103-
try
1104-
{
1105-
conn.Open();
1106-
1107-
cmd.CommandText = "SELECT EmployeeID, LastName, FirstName, Title, Address, City, Region, PostalCode, Country into " + _tempTable + " from Employees where EmployeeID < 3";
1108-
cmd.ExecuteNonQuery();
1104+
conn.Open();
11091105

1110-
cmd.CommandText = "alter table " + _tempTable + " add constraint " + _tempKey + " primary key (EmployeeID)";
1111-
cmd.ExecuteNonQuery();
1106+
cmd.CommandText = string.Format(_createTableQuery, _tempTable);
1107+
cmd.ExecuteNonQuery();
1108+
cmd.CommandText = "alter table " + _tempTable + " add constraint " + _tempKey + " primary key (EmployeeID)";
1109+
cmd.ExecuteNonQuery();
11121110

1111+
try
1112+
{
11131113
adapter.SelectCommand = new SqlCommand("SELECT EmployeeID, LastName, FirstName, Title, Address, City, Region, PostalCode, Country FROM " + _tempTable + " WHERE EmployeeID < 3", conn);
11141114
adapterVerify.SelectCommand = new SqlCommand("SELECT LastName, FirstName FROM " + _tempTable + " where FirstName='" + MagicName + "'", conn);
11151115

src/System.Data.SqlClient/tests/ManualTests/SQL/ExceptionTest/ExceptionTest.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,22 @@ public static void WarningTest()
4747
}
4848
}
4949

50-
[CheckConnStrSetupFact]
50+
private static bool EmployeesTableHasFullTextIndex()
51+
{
52+
if (DataTestUtility.TcpConnStr == null)
53+
return false;
54+
55+
using (SqlConnection conn = new SqlConnection(DataTestUtility.TcpConnStr))
56+
using (SqlCommand cmd = conn.CreateCommand())
57+
{
58+
conn.Open();
59+
cmd.CommandText = "SELECT object_id FROM sys.fulltext_indexes WHERE object_id = object_id('Northwind.dbo.Employees')";
60+
61+
return (cmd.ExecuteScalar() != null);
62+
}
63+
}
64+
65+
[ConditionalFact(nameof(EmployeesTableHasFullTextIndex))]
5166
public static void WarningsBeforeRowsTest()
5267
{
5368
bool hitWarnings = false;

0 commit comments

Comments
 (0)