-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathtest_asyncio_alone.py
67 lines (57 loc) · 1.96 KB
/
test_asyncio_alone.py
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
# -*- coding: utf-8 -*-
# 17/12/11
# create by: snower
import os
try:
import asyncio
except:
asyncio = None
import unittest
from tormysql.cursor import SSCursor
from tormysql.helpers import ConnectionPool
from tormysql.util import py3
@unittest.skipIf(asyncio is None, "asyncio module not present")
class TestAsyncioCase(unittest.TestCase):
PARAMS = dict(
host=os.getenv("MYSQL_HOST", "127.0.0.1"),
port=int(os.getenv("MYSQL_PORT", "3306")),
user=os.getenv("MYSQL_USER", "root"),
passwd=os.getenv("MYSQL_PASSWD", ""),
db=os.getenv("MYSQL_DB", "test"),
charset=os.getenv("MYSQL_CHARSET", "utf8"),
sql_mode="REAL_AS_FLOAT",
init_command="SET max_join_size=DEFAULT"
)
def setUp(self):
super(TestAsyncioCase, self).setUp()
self.pool = ConnectionPool(
max_connections=int(os.getenv("MYSQL_POOL", 5)),
idle_seconds=7200,
**self.PARAMS
)
def tearDown(self):
super(TestAsyncioCase, self).tearDown()
self.pool.close()
if py3:
exec("""
async def run_execute(self):
cursor = await self.pool.execute("select * from test limit 1")
datas = cursor.fetchall()
assert datas
async with await self.pool.begin() as transaction:
cursor = await transaction.execute("select * from test limit 1")
datas = cursor.fetchall()
assert datas
async with await self.pool.Connection() as conn:
async with conn.cursor() as cursor:
await cursor.execute("SELECT * FROM test limit 10")
datas = cursor.fetchall()
assert datas
async with await self.pool.Connection() as conn:
async with conn.cursor(SSCursor) as cursor:
await cursor.execute("SELECT * FROM test limit 10000")
async for data in cursor:
assert data
""")
def test_execute(self):
asyncio.get_event_loop().run_until_complete(self.run_execute())