@@ -9984,119 +9984,127 @@ def test_setinputsizes_sql_decimal_with_executemany(db_connection):
99849984 cursor = db_connection.cursor()
99859985
99869986 cursor.execute("DROP TABLE IF EXISTS #test_sis_decimal")
9987- cursor.execute("""
9988- CREATE TABLE #test_sis_decimal (
9989- Name NVARCHAR(100),
9990- CategoryID INT,
9991- Price DECIMAL(18,2)
9992- )
9993- """)
9994-
9995- cursor.setinputsizes([
9996- (mssql_python.SQL_WVARCHAR, 100, 0),
9997- (mssql_python.SQL_INTEGER, 0, 0),
9998- (mssql_python.SQL_DECIMAL, 18, 2),
9999- ])
9987+ try:
9988+ cursor.execute("""
9989+ CREATE TABLE #test_sis_decimal (
9990+ Name NVARCHAR(100),
9991+ CategoryID INT,
9992+ Price DECIMAL(18,2)
9993+ )
9994+ """)
100009995
10001- cursor.executemany(
10002- "INSERT INTO #test_sis_decimal (Name, CategoryID, Price) VALUES (?, ?, ?)",
10003- [
10004- ("Widget", 1, decimal.Decimal("19.99")),
10005- ("Gadget", 2, decimal.Decimal("29.99")),
10006- ("Gizmo", 3, decimal.Decimal("0.01")),
10007- ],
10008- )
9996+ cursor.setinputsizes(
9997+ [
9998+ (mssql_python.SQL_WVARCHAR, 100, 0),
9999+ (mssql_python.SQL_INTEGER, 0, 0),
10000+ (mssql_python.SQL_DECIMAL, 18, 2),
10001+ ]
10002+ )
1000910003
10010- cursor.execute("SELECT Name, CategoryID, Price FROM #test_sis_decimal ORDER BY CategoryID")
10011- rows = cursor.fetchall()
10004+ cursor.executemany(
10005+ "INSERT INTO #test_sis_decimal (Name, CategoryID, Price) VALUES (?, ?, ?)",
10006+ [
10007+ ("Widget", 1, decimal.Decimal("19.99")),
10008+ ("Gadget", 2, decimal.Decimal("29.99")),
10009+ ("Gizmo", 3, decimal.Decimal("0.01")),
10010+ ],
10011+ )
1001210012
10013- assert len(rows) == 3
10014- assert rows[0][0] == "Widget"
10015- assert rows[0][1] == 1
10016- assert rows[0][2] == decimal.Decimal("19.99")
10017- assert rows[1][0] == "Gadget"
10018- assert rows[1][1] == 2
10019- assert rows[1][2] == decimal.Decimal("29.99")
10020- assert rows[2][0] == "Gizmo"
10021- assert rows[2][1] == 3
10022- assert rows[2][2] == decimal.Decimal("0.01")
10013+ cursor.execute("SELECT Name, CategoryID, Price FROM #test_sis_decimal ORDER BY CategoryID")
10014+ rows = cursor.fetchall()
1002310015
10024- cursor.execute("DROP TABLE IF EXISTS #test_sis_decimal")
10016+ assert len(rows) == 3
10017+ assert rows[0][0] == "Widget"
10018+ assert rows[0][1] == 1
10019+ assert rows[0][2] == decimal.Decimal("19.99")
10020+ assert rows[1][0] == "Gadget"
10021+ assert rows[1][1] == 2
10022+ assert rows[1][2] == decimal.Decimal("29.99")
10023+ assert rows[2][0] == "Gizmo"
10024+ assert rows[2][1] == 3
10025+ assert rows[2][2] == decimal.Decimal("0.01")
10026+ finally:
10027+ cursor.execute("DROP TABLE IF EXISTS #test_sis_decimal")
1002510028
1002610029
1002710030def test_setinputsizes_sql_numeric_with_executemany(db_connection):
1002810031 """Test setinputsizes with SQL_NUMERIC accepts Python Decimal values (GH-503)."""
1002910032 cursor = db_connection.cursor()
1003010033
1003110034 cursor.execute("DROP TABLE IF EXISTS #test_sis_numeric")
10032- cursor.execute("""
10033- CREATE TABLE #test_sis_numeric (
10034- Value NUMERIC(10,4)
10035- )
10036- """)
10037-
10038- cursor.setinputsizes([
10039- (mssql_python.SQL_NUMERIC, 10, 4),
10040- ])
10035+ try:
10036+ cursor.execute("""
10037+ CREATE TABLE #test_sis_numeric (
10038+ Value NUMERIC(10,4)
10039+ )
10040+ """)
1004110041
10042- cursor.executemany(
10043- "INSERT INTO #test_sis_numeric (Value) VALUES (?)",
10044- [
10045- (decimal.Decimal("123.4567"),),
10046- (decimal.Decimal("-99.0001"),),
10047- (decimal.Decimal("0.0000"),),
10048- ],
10049- )
10042+ cursor.setinputsizes(
10043+ [
10044+ (mssql_python.SQL_NUMERIC, 10, 4),
10045+ ]
10046+ )
1005010047
10051- cursor.execute("SELECT Value FROM #test_sis_numeric ORDER BY Value")
10052- rows = cursor.fetchall()
10048+ cursor.executemany(
10049+ "INSERT INTO #test_sis_numeric (Value) VALUES (?)",
10050+ [
10051+ (decimal.Decimal("123.4567"),),
10052+ (decimal.Decimal("-99.0001"),),
10053+ (decimal.Decimal("0.0000"),),
10054+ ],
10055+ )
1005310056
10054- assert len(rows) == 3
10055- assert rows[0][0] == decimal.Decimal("-99.0001")
10056- assert rows[1][0] == decimal.Decimal("0.0000")
10057- assert rows[2][0] == decimal.Decimal("123.4567")
10057+ cursor.execute("SELECT Value FROM #test_sis_numeric ORDER BY Value")
10058+ rows = cursor.fetchall()
1005810059
10059- cursor.execute("DROP TABLE IF EXISTS #test_sis_numeric")
10060+ assert len(rows) == 3
10061+ assert rows[0][0] == decimal.Decimal("-99.0001")
10062+ assert rows[1][0] == decimal.Decimal("0.0000")
10063+ assert rows[2][0] == decimal.Decimal("123.4567")
10064+ finally:
10065+ cursor.execute("DROP TABLE IF EXISTS #test_sis_numeric")
1006010066
1006110067
1006210068def test_setinputsizes_sql_decimal_with_execute(db_connection):
1006310069 """Test setinputsizes with SQL_DECIMAL works with single execute() too (GH-503)."""
1006410070 cursor = db_connection.cursor()
1006510071
1006610072 cursor.execute("DROP TABLE IF EXISTS #test_sis_dec_exec")
10067- cursor.execute("CREATE TABLE #test_sis_dec_exec (Price DECIMAL(18,2))")
10068-
10069- cursor.setinputsizes([(mssql_python.SQL_DECIMAL, 18, 2)])
10070- cursor.execute(
10071- "INSERT INTO #test_sis_dec_exec (Price) VALUES (?)",
10072- decimal.Decimal("99.95"),
10073- )
10073+ try:
10074+ cursor.execute("CREATE TABLE #test_sis_dec_exec (Price DECIMAL(18,2))")
1007410075
10075- cursor.execute("SELECT Price FROM #test_sis_dec_exec")
10076- row = cursor.fetchone()
10077- assert row[0] == decimal.Decimal("99.95")
10076+ cursor.setinputsizes([(mssql_python.SQL_DECIMAL, 18, 2)])
10077+ cursor.execute(
10078+ "INSERT INTO #test_sis_dec_exec (Price) VALUES (?)",
10079+ decimal.Decimal("99.95"),
10080+ )
1007810081
10079- cursor.execute("DROP TABLE IF EXISTS #test_sis_dec_exec")
10082+ cursor.execute("SELECT Price FROM #test_sis_dec_exec")
10083+ row = cursor.fetchone()
10084+ assert row[0] == decimal.Decimal("99.95")
10085+ finally:
10086+ cursor.execute("DROP TABLE IF EXISTS #test_sis_dec_exec")
1008010087
1008110088
1008210089def test_setinputsizes_sql_decimal_null(db_connection):
1008310090 """Test setinputsizes with SQL_DECIMAL handles NULL values correctly (GH-503)."""
1008410091 cursor = db_connection.cursor()
1008510092
1008610093 cursor.execute("DROP TABLE IF EXISTS #test_sis_dec_null")
10087- cursor.execute("CREATE TABLE #test_sis_dec_null (Price DECIMAL(18,2))")
10088-
10089- cursor.setinputsizes([(mssql_python.SQL_DECIMAL, 18, 2)])
10090- cursor.execute(
10091- "INSERT INTO #test_sis_dec_null (Price) VALUES (?)",
10092- None,
10093- )
10094+ try:
10095+ cursor.execute("CREATE TABLE #test_sis_dec_null (Price DECIMAL(18,2))")
1009410096
10095- cursor.execute("SELECT Price FROM #test_sis_dec_null")
10096- row = cursor.fetchone()
10097- assert row[0] is None
10097+ cursor.setinputsizes([(mssql_python.SQL_DECIMAL, 18, 2)])
10098+ cursor.execute(
10099+ "INSERT INTO #test_sis_dec_null (Price) VALUES (?)",
10100+ None,
10101+ )
1009810102
10099- cursor.execute("DROP TABLE IF EXISTS #test_sis_dec_null")
10103+ cursor.execute("SELECT Price FROM #test_sis_dec_null")
10104+ row = cursor.fetchone()
10105+ assert row[0] is None
10106+ finally:
10107+ cursor.execute("DROP TABLE IF EXISTS #test_sis_dec_null")
1010010108
1010110109
1010210110def test_cursor_setinputsizes_reset(db_connection):
0 commit comments