Skip to content

Commit 6021d5a

Browse files
committed
[fea] tornado tests
1 parent 95f74b9 commit 6021d5a

File tree

8 files changed

+93
-51
lines changed

8 files changed

+93
-51
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ __pycache__/
77

88
# Distribution / packaging
99
.Python
10-
env/
10+
env*/
1111
bin/
1212
build/
1313
develop-eggs/
@@ -26,7 +26,7 @@ var/
2626
pip-log.txt
2727
pip-delete-this-directory.txt
2828

29-
# Unit test / coverage reports
29+
# Unit tests / coverage reports
3030
htmlcov/
3131
.tox/
3232
.coverage

test/test.py

Lines changed: 0 additions & 42 deletions
This file was deleted.

tests/__init__.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
import os
4+
from tormysql import ConnectionPool
5+
from tornado.testing import AsyncTestCase
6+
7+
8+
class BaseTestCase(AsyncTestCase):
9+
def setUp(self):
10+
super(BaseTestCase, self).setUp()
11+
self.pool = ConnectionPool(
12+
max_connections=int(os.getenv("MYSQL_POOL", "5")),
13+
idle_seconds=7200,
14+
host=os.getenv("MYSQL_HOST", "127.0.0.1"),
15+
user=os.getenv("MYSQL_USER", "root"),
16+
passwd=os.getenv("MYSQL_PASSWD", ""),
17+
db=os.getenv("MYSQL_DB", "mysql"),
18+
charset=os.getenv("MYSQL_CHARSET", "utf8"),
19+
)
20+
21+
def tearDown(self):
22+
super(BaseTestCase, self).tearDown()
23+
self.pool.close()

tests/test_basic.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# encoding: utf-8
2+
from tornado.testing import gen_test
3+
from . import BaseTestCase
4+
5+
6+
class TestBasic(BaseTestCase):
7+
@gen_test
8+
def test0(self):
9+
sql = "select * from user limit 1"
10+
connection = yield self.pool.Connection()
11+
cursor = connection.cursor()
12+
yield cursor.execute(sql)
13+
14+
datas = cursor.fetchall()
15+
yield cursor.close()
16+
connection.close()
17+
self.assertIsNotNone(datas)

tests/test_with_with.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
from tornado.testing import gen_test
4+
from . import BaseTestCase
5+
6+
7+
class TestWithWith(BaseTestCase):
8+
@gen_test
9+
def test1(self):
10+
sql = "select * from user limit 1"
11+
with (yield self.pool.Connection()) as connection:
12+
with connection.cursor() as cursor:
13+
yield cursor.execute(sql)
14+
datas = cursor.fetchall()
15+
print (datas)

tormysql/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def select_db(self, db):
6565
return async_call_method(self._connection.select_db, db)
6666

6767
def cursor(self, cursor_cls=None):
68-
cursor = self._connection.cursor(cursor_cls.__delegate_class__ if cursor_cls and issubclass(cursor_cls,Cursor) else cursor_cls)
68+
cursor = self._connection.cursor(cursor_cls.__delegate_class__ if cursor_cls and issubclass(cursor_cls, Cursor) else cursor_cls)
6969
if cursor_cls:
7070
return cursor_cls(cursor)
7171
else:

tormysql/cursor.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# -*- coding: utf-8 -*-
22
# 14-8-8
33
# create by: snower
4-
4+
from tornado.ioloop import IOLoop
55
from tornado.concurrent import TracebackFuture
66
from pymysql.cursors import Cursor as OriginCursor, DictCursor as OriginDictCursor, SSCursor as OriginSSCursor, SSDictCursor as OriginSSDictCursor
77
from .util import async_call_method
88

9+
910
class Cursor(object):
1011
__delegate_class__ = OriginCursor
1112

@@ -57,13 +58,23 @@ def __iter__(self):
5758
def __getattr__(self, name):
5859
return getattr(self._cursor, name)
5960

61+
def __enter__(self):
62+
return self
63+
64+
def __exit__(self, *args):
65+
IOLoop.current().add_callback(self.close)
66+
67+
6068
setattr(OriginCursor, "__tormysql_class__", Cursor)
6169

70+
6271
class DictCursor(Cursor):
6372
__delegate_class__ = OriginDictCursor
6473

74+
6575
setattr(OriginDictCursor, "__tormysql_class__", DictCursor)
6676

77+
6778
class SSCursor(Cursor):
6879
__delegate_class__ = OriginSSCursor
6980

tormysql/pool.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,22 @@
99
from tornado.ioloop import IOLoop
1010
from .client import Client
1111

12-
class ConnectionPoolClosedError(Exception):pass
13-
class ConnectionNotFoundError(Exception):pass
14-
class ConnectionNotUsedError(Exception):pass
15-
class ConnectionUsedError(Exception):pass
12+
13+
class ConnectionPoolClosedError(Exception):
14+
pass
15+
16+
17+
class ConnectionNotFoundError(Exception):
18+
pass
19+
20+
21+
class ConnectionNotUsedError(Exception):
22+
pass
23+
24+
25+
class ConnectionUsedError(Exception):
26+
pass
27+
1628

1729
class Connection(Client):
1830
def __init__(self, pool, *args, **kwargs):
@@ -21,11 +33,17 @@ def __init__(self, pool, *args, **kwargs):
2133
self.used_time = time.time()
2234
super(Connection, self).__init__(*args, **kwargs)
2335

24-
def close(self, remote_close = False):
36+
def close(self, remote_close=False):
2537
if remote_close:
2638
return self.do_close()
2739
return self._pool.release_connection(self)
2840

41+
def __enter__(self):
42+
return self
43+
44+
def __exit__(self, *args):
45+
IOLoop.current().add_callback(self.close)
46+
2947
def do_close(self):
3048
return super(Connection, self).close()
3149

0 commit comments

Comments
 (0)