PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/web2py/gluon/contrib/pymysql/tests/test_issues.py

https://gitlab.com/sunkistm/gitlab-web2py
Python | 278 lines | 270 code | 6 blank | 2 comment | 7 complexity | e08714d9d619cf3cdc9e6ebbe9449595 MD5 | raw file
  1. import pymysql
  2. from pymysql.tests import base
  3. import unittest
  4. import sys
  5. try:
  6. import imp
  7. reload = imp.reload
  8. except AttributeError:
  9. pass
  10. import datetime
  11. # backwards compatibility:
  12. if not hasattr(unittest, "skip"):
  13. unittest.skip = lambda message: lambda f: f
  14. class TestOldIssues(base.PyMySQLTestCase):
  15. def test_issue_3(self):
  16. """ undefined methods datetime_or_None, date_or_None """
  17. conn = self.connections[0]
  18. c = conn.cursor()
  19. c.execute("create table issue3 (d date, t time, dt datetime, ts timestamp)")
  20. try:
  21. c.execute("insert into issue3 (d, t, dt, ts) values (%s,%s,%s,%s)", (None, None, None, None))
  22. c.execute("select d from issue3")
  23. self.assertEqual(None, c.fetchone()[0])
  24. c.execute("select t from issue3")
  25. self.assertEqual(None, c.fetchone()[0])
  26. c.execute("select dt from issue3")
  27. self.assertEqual(None, c.fetchone()[0])
  28. c.execute("select ts from issue3")
  29. self.assertTrue(isinstance(c.fetchone()[0], datetime.datetime))
  30. finally:
  31. c.execute("drop table issue3")
  32. def test_issue_4(self):
  33. """ can't retrieve TIMESTAMP fields """
  34. conn = self.connections[0]
  35. c = conn.cursor()
  36. c.execute("create table issue4 (ts timestamp)")
  37. try:
  38. c.execute("insert into issue4 (ts) values (now())")
  39. c.execute("select ts from issue4")
  40. self.assertTrue(isinstance(c.fetchone()[0], datetime.datetime))
  41. finally:
  42. c.execute("drop table issue4")
  43. def test_issue_5(self):
  44. """ query on information_schema.tables fails """
  45. con = self.connections[0]
  46. cur = con.cursor()
  47. cur.execute("select * from information_schema.tables")
  48. def test_issue_6(self):
  49. """ exception: TypeError: ord() expected a character, but string of length 0 found """
  50. conn = pymysql.connect(host="localhost",user="root",passwd="",db="mysql")
  51. c = conn.cursor()
  52. c.execute("select * from user")
  53. conn.close()
  54. def test_issue_8(self):
  55. """ Primary Key and Index error when selecting data """
  56. conn = self.connections[0]
  57. c = conn.cursor()
  58. c.execute("""CREATE TABLE `test` (`station` int(10) NOT NULL DEFAULT '0', `dh`
  59. datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `echeance` int(1) NOT NULL
  60. DEFAULT '0', `me` double DEFAULT NULL, `mo` double DEFAULT NULL, PRIMARY
  61. KEY (`station`,`dh`,`echeance`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;""")
  62. try:
  63. self.assertEqual(0, c.execute("SELECT * FROM test"))
  64. c.execute("ALTER TABLE `test` ADD INDEX `idx_station` (`station`)")
  65. self.assertEqual(0, c.execute("SELECT * FROM test"))
  66. finally:
  67. c.execute("drop table test")
  68. def test_issue_9(self):
  69. """ sets DeprecationWarning in Python 2.6 """
  70. try:
  71. reload(pymysql)
  72. except DeprecationWarning:
  73. self.fail()
  74. def test_issue_10(self):
  75. """ Allocate a variable to return when the exception handler is permissive """
  76. conn = self.connections[0]
  77. conn.errorhandler = lambda cursor, errorclass, errorvalue: None
  78. cur = conn.cursor()
  79. cur.execute( "create table t( n int )" )
  80. cur.execute( "create table t( n int )" )
  81. def test_issue_13(self):
  82. """ can't handle large result fields """
  83. conn = self.connections[0]
  84. cur = conn.cursor()
  85. try:
  86. cur.execute("create table issue13 (t text)")
  87. # ticket says 18k
  88. size = 18*1024
  89. cur.execute("insert into issue13 (t) values (%s)", ("x" * size,))
  90. cur.execute("select t from issue13")
  91. # use assertTrue so that obscenely huge error messages don't print
  92. r = cur.fetchone()[0]
  93. self.assertTrue("x" * size == r)
  94. finally:
  95. cur.execute("drop table issue13")
  96. def test_issue_14(self):
  97. """ typo in converters.py """
  98. self.assertEqual('1', pymysql.converters.escape_item(1, "utf8"))
  99. self.assertEqual('1', pymysql.converters.escape_item(1L, "utf8"))
  100. self.assertEqual('1', pymysql.converters.escape_object(1))
  101. self.assertEqual('1', pymysql.converters.escape_object(1L))
  102. def test_issue_15(self):
  103. """ query should be expanded before perform character encoding """
  104. conn = self.connections[0]
  105. c = conn.cursor()
  106. c.execute("create table issue15 (t varchar(32))")
  107. try:
  108. c.execute("insert into issue15 (t) values (%s)", (u'\xe4\xf6\xfc',))
  109. c.execute("select t from issue15")
  110. self.assertEqual(u'\xe4\xf6\xfc', c.fetchone()[0])
  111. finally:
  112. c.execute("drop table issue15")
  113. def test_issue_16(self):
  114. """ Patch for string and tuple escaping """
  115. conn = self.connections[0]
  116. c = conn.cursor()
  117. c.execute("create table issue16 (name varchar(32) primary key, email varchar(32))")
  118. try:
  119. c.execute("insert into issue16 (name, email) values ('pete', 'floydophone')")
  120. c.execute("select email from issue16 where name=%s", ("pete",))
  121. self.assertEqual("floydophone", c.fetchone()[0])
  122. finally:
  123. c.execute("drop table issue16")
  124. @unittest.skip("test_issue_17() requires a custom, legacy MySQL configuration and will not be run.")
  125. def test_issue_17(self):
  126. """ could not connect mysql use passwod """
  127. conn = self.connections[0]
  128. host = self.databases[0]["host"]
  129. db = self.databases[0]["db"]
  130. c = conn.cursor()
  131. # grant access to a table to a user with a password
  132. try:
  133. c.execute("create table issue17 (x varchar(32) primary key)")
  134. c.execute("insert into issue17 (x) values ('hello, world!')")
  135. c.execute("grant all privileges on %s.issue17 to 'issue17user'@'%%' identified by '1234'" % db)
  136. conn.commit()
  137. conn2 = pymysql.connect(host=host, user="issue17user", passwd="1234", db=db)
  138. c2 = conn2.cursor()
  139. c2.execute("select x from issue17")
  140. self.assertEqual("hello, world!", c2.fetchone()[0])
  141. finally:
  142. c.execute("drop table issue17")
  143. def _uni(s, e):
  144. # hack for py3
  145. if sys.version_info[0] > 2:
  146. return unicode(bytes(s, sys.getdefaultencoding()), e)
  147. else:
  148. return unicode(s, e)
  149. class TestNewIssues(base.PyMySQLTestCase):
  150. def test_issue_34(self):
  151. try:
  152. pymysql.connect(host="localhost", port=1237, user="root")
  153. self.fail()
  154. except pymysql.OperationalError, e:
  155. self.assertEqual(2003, e.args[0])
  156. except:
  157. self.fail()
  158. def test_issue_33(self):
  159. conn = pymysql.connect(host="localhost", user="root", db=self.databases[0]["db"], charset="utf8")
  160. c = conn.cursor()
  161. try:
  162. c.execute(_uni("create table hei\xc3\x9fe (name varchar(32))", "utf8"))
  163. c.execute(_uni("insert into hei\xc3\x9fe (name) values ('Pi\xc3\xb1ata')", "utf8"))
  164. c.execute(_uni("select name from hei\xc3\x9fe", "utf8"))
  165. self.assertEqual(_uni("Pi\xc3\xb1ata","utf8"), c.fetchone()[0])
  166. finally:
  167. c.execute(_uni("drop table hei\xc3\x9fe", "utf8"))
  168. @unittest.skip("This test requires manual intervention")
  169. def test_issue_35(self):
  170. conn = self.connections[0]
  171. c = conn.cursor()
  172. print "sudo killall -9 mysqld within the next 10 seconds"
  173. try:
  174. c.execute("select sleep(10)")
  175. self.fail()
  176. except pymysql.OperationalError, e:
  177. self.assertEqual(2013, e.args[0])
  178. def test_issue_36(self):
  179. conn = self.connections[0]
  180. c = conn.cursor()
  181. # kill connections[0]
  182. c.execute("show processlist")
  183. kill_id = None
  184. for id,user,host,db,command,time,state,info in c.fetchall():
  185. if info == "show processlist":
  186. kill_id = id
  187. break
  188. # now nuke the connection
  189. conn.kill(kill_id)
  190. # make sure this connection has broken
  191. try:
  192. c.execute("show tables")
  193. self.fail()
  194. except:
  195. pass
  196. # check the process list from the other connection
  197. try:
  198. c = self.connections[1].cursor()
  199. c.execute("show processlist")
  200. ids = [row[0] for row in c.fetchall()]
  201. self.assertFalse(kill_id in ids)
  202. finally:
  203. del self.connections[0]
  204. def test_issue_37(self):
  205. conn = self.connections[0]
  206. c = conn.cursor()
  207. self.assertEqual(1, c.execute("SELECT @foo"))
  208. self.assertEqual((None,), c.fetchone())
  209. self.assertEqual(0, c.execute("SET @foo = 'bar'"))
  210. c.execute("set @foo = 'bar'")
  211. def test_issue_38(self):
  212. conn = self.connections[0]
  213. c = conn.cursor()
  214. datum = "a" * 1024 * 1023 # reduced size for most default mysql installs
  215. try:
  216. c.execute("create table issue38 (id integer, data mediumblob)")
  217. c.execute("insert into issue38 values (1, %s)", (datum,))
  218. finally:
  219. c.execute("drop table issue38")
  220. def disabled_test_issue_54(self):
  221. conn = self.connections[0]
  222. c = conn.cursor()
  223. big_sql = "select * from issue54 where "
  224. big_sql += " and ".join("%d=%d" % (i,i) for i in xrange(0, 100000))
  225. try:
  226. c.execute("create table issue54 (id integer primary key)")
  227. c.execute("insert into issue54 (id) values (7)")
  228. c.execute(big_sql)
  229. self.assertEqual(7, c.fetchone()[0])
  230. finally:
  231. c.execute("drop table issue54")
  232. class TestGitHubIssues(base.PyMySQLTestCase):
  233. def test_issue_66(self):
  234. conn = self.connections[0]
  235. c = conn.cursor()
  236. self.assertEqual(0, conn.insert_id())
  237. try:
  238. c.execute("create table issue66 (id integer primary key auto_increment, x integer)")
  239. c.execute("insert into issue66 (x) values (1)")
  240. c.execute("insert into issue66 (x) values (1)")
  241. self.assertEqual(2, conn.insert_id())
  242. finally:
  243. c.execute("drop table issue66")
  244. __all__ = ["TestOldIssues", "TestNewIssues", "TestGitHubIssues"]
  245. if __name__ == "__main__":
  246. import unittest
  247. unittest.main()