Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_odbc.py 8.3KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. # odbc test suite kindly contributed by Frank Millman.
  2. import os
  3. import sys
  4. import tempfile
  5. import unittest
  6. import odbc
  7. import pythoncom
  8. from pywin32_testutil import TestSkipped, str2bytes, str2memory
  9. from win32com.client import constants
  10. # We use the DAO ODBC driver
  11. from win32com.client.gencache import EnsureDispatch
  12. class TestStuff(unittest.TestCase):
  13. def setUp(self):
  14. self.tablename = "pywin32test_users"
  15. self.db_filename = None
  16. self.conn = self.cur = None
  17. try:
  18. # Test any database if a connection string is supplied...
  19. conn_str = os.environ["TEST_ODBC_CONNECTION_STRING"]
  20. except KeyError:
  21. # Create a local MSAccess DB for testing.
  22. self.db_filename = tempfile.NamedTemporaryFile().name + ".mdb"
  23. # Create a brand-new database - what is the story with these?
  24. for suffix in (".36", ".35", ".30"):
  25. try:
  26. dbe = EnsureDispatch("DAO.DBEngine" + suffix)
  27. break
  28. except pythoncom.com_error:
  29. pass
  30. else:
  31. raise TestSkipped("Can't find a DB engine")
  32. workspace = dbe.Workspaces(0)
  33. newdb = workspace.CreateDatabase(
  34. self.db_filename, constants.dbLangGeneral, constants.dbEncrypt
  35. )
  36. newdb.Close()
  37. conn_str = "Driver={Microsoft Access Driver (*.mdb)};dbq=%s;Uid=;Pwd=;" % (
  38. self.db_filename,
  39. )
  40. ## print 'Connection string:', conn_str
  41. self.conn = odbc.odbc(conn_str)
  42. # And we expect a 'users' table for these tests.
  43. self.cur = self.conn.cursor()
  44. ## self.cur.setoutputsize(1000)
  45. try:
  46. self.cur.execute("""drop table %s""" % self.tablename)
  47. except (odbc.error, odbc.progError):
  48. pass
  49. ## This needs to be adjusted for sql server syntax for unicode fields
  50. ## - memo -> TEXT
  51. ## - varchar -> nvarchar
  52. self.assertEqual(
  53. self.cur.execute(
  54. """create table %s (
  55. userid varchar(25),
  56. username varchar(25),
  57. bitfield bit,
  58. intfield integer,
  59. floatfield float,
  60. datefield datetime,
  61. rawfield varbinary(100),
  62. longtextfield memo,
  63. longbinaryfield image
  64. )"""
  65. % self.tablename
  66. ),
  67. -1,
  68. )
  69. def tearDown(self):
  70. if self.cur is not None:
  71. try:
  72. self.cur.execute("""drop table %s""" % self.tablename)
  73. except (odbc.error, odbc.progError) as why:
  74. print("Failed to delete test table %s" % self.tablename, why)
  75. self.cur.close()
  76. self.cur = None
  77. if self.conn is not None:
  78. self.conn.close()
  79. self.conn = None
  80. if self.db_filename is not None:
  81. try:
  82. os.unlink(self.db_filename)
  83. except OSError:
  84. pass
  85. def test_insert_select(self, userid="Frank", username="Frank Millman"):
  86. self.assertEqual(
  87. self.cur.execute(
  88. "insert into %s (userid, username) \
  89. values (?,?)"
  90. % self.tablename,
  91. [userid, username],
  92. ),
  93. 1,
  94. )
  95. self.assertEqual(
  96. self.cur.execute(
  97. "select * from %s \
  98. where userid = ?"
  99. % self.tablename,
  100. [userid.lower()],
  101. ),
  102. 0,
  103. )
  104. self.assertEqual(
  105. self.cur.execute(
  106. "select * from %s \
  107. where username = ?"
  108. % self.tablename,
  109. [username.lower()],
  110. ),
  111. 0,
  112. )
  113. def test_insert_select_unicode(self, userid="Frank", username="Frank Millman"):
  114. self.assertEqual(
  115. self.cur.execute(
  116. "insert into %s (userid, username)\
  117. values (?,?)"
  118. % self.tablename,
  119. [userid, username],
  120. ),
  121. 1,
  122. )
  123. self.assertEqual(
  124. self.cur.execute(
  125. "select * from %s \
  126. where userid = ?"
  127. % self.tablename,
  128. [userid.lower()],
  129. ),
  130. 0,
  131. )
  132. self.assertEqual(
  133. self.cur.execute(
  134. "select * from %s \
  135. where username = ?"
  136. % self.tablename,
  137. [username.lower()],
  138. ),
  139. 0,
  140. )
  141. def test_insert_select_unicode_ext(self):
  142. userid = "t-\xe0\xf2"
  143. username = "test-\xe0\xf2 name"
  144. self.test_insert_select_unicode(userid, username)
  145. def _test_val(self, fieldName, value):
  146. for x in range(100):
  147. self.cur.execute("delete from %s where userid='Frank'" % self.tablename)
  148. self.assertEqual(
  149. self.cur.execute(
  150. "insert into %s (userid, %s) values (?,?)"
  151. % (self.tablename, fieldName),
  152. ["Frank", value],
  153. ),
  154. 1,
  155. )
  156. self.cur.execute(
  157. "select %s from %s where userid = ?" % (fieldName, self.tablename),
  158. ["Frank"],
  159. )
  160. rows = self.cur.fetchmany()
  161. self.assertEqual(1, len(rows))
  162. row = rows[0]
  163. self.assertEqual(row[0], value)
  164. def testBit(self):
  165. self._test_val("bitfield", 1)
  166. self._test_val("bitfield", 0)
  167. def testInt(self):
  168. self._test_val("intfield", 1)
  169. self._test_val("intfield", 0)
  170. try:
  171. big = sys.maxsize
  172. except AttributeError:
  173. big = sys.maxint
  174. self._test_val("intfield", big)
  175. def testFloat(self):
  176. self._test_val("floatfield", 1.01)
  177. self._test_val("floatfield", 0)
  178. def testVarchar(
  179. self,
  180. ):
  181. self._test_val("username", "foo")
  182. def testLongVarchar(self):
  183. """Test a long text field in excess of internal cursor data size (65536)"""
  184. self._test_val("longtextfield", "abc" * 70000)
  185. def testLongBinary(self):
  186. """Test a long raw field in excess of internal cursor data size (65536)"""
  187. self._test_val("longbinaryfield", str2memory("\0\1\2" * 70000))
  188. def testRaw(self):
  189. ## Test binary data
  190. self._test_val("rawfield", str2memory("\1\2\3\4\0\5\6\7\8"))
  191. def test_widechar(self):
  192. """Test a unicode character that would be mangled if bound as plain character.
  193. For example, previously the below was returned as ascii 'a'
  194. """
  195. self._test_val("username", "\u0101")
  196. def testDates(self):
  197. import datetime
  198. for v in ((1900, 12, 25, 23, 39, 59),):
  199. d = datetime.datetime(*v)
  200. self._test_val("datefield", d)
  201. def test_set_nonzero_length(self):
  202. self.assertEqual(
  203. self.cur.execute(
  204. "insert into %s (userid,username) " "values (?,?)" % self.tablename,
  205. ["Frank", "Frank Millman"],
  206. ),
  207. 1,
  208. )
  209. self.assertEqual(
  210. self.cur.execute("update %s set username = ?" % self.tablename, ["Frank"]),
  211. 1,
  212. )
  213. self.assertEqual(self.cur.execute("select * from %s" % self.tablename), 0)
  214. self.assertEqual(len(self.cur.fetchone()[1]), 5)
  215. def test_set_zero_length(self):
  216. self.assertEqual(
  217. self.cur.execute(
  218. "insert into %s (userid,username) " "values (?,?)" % self.tablename,
  219. [str2bytes("Frank"), ""],
  220. ),
  221. 1,
  222. )
  223. self.assertEqual(self.cur.execute("select * from %s" % self.tablename), 0)
  224. self.assertEqual(len(self.cur.fetchone()[1]), 0)
  225. def test_set_zero_length_unicode(self):
  226. self.assertEqual(
  227. self.cur.execute(
  228. "insert into %s (userid,username) " "values (?,?)" % self.tablename,
  229. ["Frank", ""],
  230. ),
  231. 1,
  232. )
  233. self.assertEqual(self.cur.execute("select * from %s" % self.tablename), 0)
  234. self.assertEqual(len(self.cur.fetchone()[1]), 0)
  235. if __name__ == "__main__":
  236. unittest.main()