My favorites | Sign in
Project Home Downloads Wiki
READ-ONLY: This project has been archived. For more information see this post.
Search
for
  Advanced search   Search tips   Subscriptions

Issue 91 attachment: dectest.py (2.6 KB)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

"""This unit test demonstrates an apparent bug with pyodbc 2.1.7 when
trying to insert Decimal values which occupy the maximum precision of 38.

The first test case passes because the precision is less than 38.

The second fails when the precision is right at 38 (But it ought to pass.)

You might need to modify the CONNSTR line to get this to work on your site.

"""

Driver = '{SQL Server}'
host = 'localhost'
database = 'journyx'
CONNSTR = "DRIVER=%(Driver)s;SERVER=%(host)s;DATABASE=%(database)s;Trusted_Connection=Yes" % locals()

import unittest, sys
from decimal import Decimal

class pyodbcDecimalTestBase(unittest.TestCase):

"""Test less than the maximum allowed precision."""

NEGATIVE = False
PRECISION = 37
SCALE = 10

def setUp(self):

import pyodbc
self.db = pyodbc.connect(CONNSTR, autocommit=True)

self.cursor = self.db.cursor()

createTableSQL = "create table dectest (decval decimal(%s,%s) null)" % (self.PRECISION, self.SCALE)
self.cursor.execute(createTableSQL)
return

def tearDown(self):
createTableSQL = "drop table dectest"
self.cursor.execute(createTableSQL)
return


def testWithinBounds(self):

# Construct a decimal that uses the maximum precision and scale.
decStr = '9' * (self.PRECISION - self.SCALE)
if self.SCALE:
decStr = decStr + "." + '9' * self.SCALE
if self.NEGATIVE:
decStr = "-" + decStr
testDec = Decimal(decStr)
print testDec

# Assert that it matches our declared precision
extraChars = 0
if self.SCALE:
extraChars +=1
if self.NEGATIVE:
extraChars +=1
self.assert_(len(str(testDec)) == (self.PRECISION + extraChars))

testSQL = "insert into dectest values (?)"
params = (testDec,)
#r=raw_input("-->")

self.cursor.execute(testSQL, params)

self.cursor.execute("select * from dectest")
result = self.cursor.fetchall()
outDec = result[0][0]

self.assertEqual(outDec, testDec)
return

class pyodbcDecimalTestMaxPrec(pyodbcDecimalTestBase):
"""Test the maximum allowed precision. This should also pass but
isn't under pyodbc 2.1.7."""
PRECISION = 38
SCALE = 10

class pyodbcDecimalTestMaxPrecNegative(pyodbcDecimalTestMaxPrec):
NEGATIVE = True

class pyodbcDecimalTestMaxPrecNonDec(pyodbcDecimalTestBase):
"""Test the maximum allowed precision. This should also pass but
isn't under pyodbc 2.1.7."""
PRECISION = 38
SCALE = 0


if __name__ == "__main__":
unittest.main()

Powered by Google Project Hosting