pytest-db 0.1.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pytest_db-0.1.2/LICENSE +21 -0
- pytest_db-0.1.2/MANIFEST.in +8 -0
- pytest_db-0.1.2/PKG-INFO +29 -0
- pytest_db-0.1.2/README.md +81 -0
- pytest_db-0.1.2/pytest_db/__init__.py +0 -0
- pytest_db-0.1.2/pytest_db/mysql_client.py +65 -0
- pytest_db-0.1.2/pytest_db/plugin.py +31 -0
- pytest_db-0.1.2/pytest_db.egg-info/PKG-INFO +29 -0
- pytest_db-0.1.2/pytest_db.egg-info/SOURCES.txt +16 -0
- pytest_db-0.1.2/pytest_db.egg-info/dependency_links.txt +1 -0
- pytest_db-0.1.2/pytest_db.egg-info/entry_points.txt +2 -0
- pytest_db-0.1.2/pytest_db.egg-info/requires.txt +3 -0
- pytest_db-0.1.2/pytest_db.egg-info/top_level.txt +1 -0
- pytest_db-0.1.2/pytest_db.egg-info/zip-safe +1 -0
- pytest_db-0.1.2/requirements.txt +2 -0
- pytest_db-0.1.2/setup.cfg +4 -0
- pytest_db-0.1.2/setup.py +42 -0
- pytest_db-0.1.2/tests/test_pytest_db.py +17 -0
pytest_db-0.1.2/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Hanzhichao
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
pytest_db-0.1.2/PKG-INFO
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pytest-db
|
|
3
|
+
Version: 0.1.2
|
|
4
|
+
Summary: Session scope fixture "db" for mysql query or change
|
|
5
|
+
Home-page: https://github.com/hanzhichao/pytest-db
|
|
6
|
+
Author: Han Zhichao
|
|
7
|
+
Author-email: superhin@126.com
|
|
8
|
+
License: MIT license
|
|
9
|
+
Keywords: pytest,py.test,db,mysql
|
|
10
|
+
Classifier: Framework :: Pytest
|
|
11
|
+
Classifier: Programming Language :: Python
|
|
12
|
+
Classifier: Topic :: Software Development :: Testing
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Requires-Dist: pytest
|
|
16
|
+
Requires-Dist: pytest-runner
|
|
17
|
+
Requires-Dist: pymysql
|
|
18
|
+
Dynamic: author
|
|
19
|
+
Dynamic: author-email
|
|
20
|
+
Dynamic: classifier
|
|
21
|
+
Dynamic: description
|
|
22
|
+
Dynamic: home-page
|
|
23
|
+
Dynamic: keywords
|
|
24
|
+
Dynamic: license
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
Dynamic: requires-dist
|
|
27
|
+
Dynamic: summary
|
|
28
|
+
|
|
29
|
+
Session scope fixture "db" for mysql query or change
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# pytest-db
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
6
|
+

|
|
7
|
+
|
|
8
|
+
Pytest操作数据库
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
### 如何使用
|
|
13
|
+
|
|
14
|
+
1. 安装 `pytest-db`
|
|
15
|
+
|
|
16
|
+
使用pip从github安装
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
pip install pytest-db
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
2. 使用方法
|
|
23
|
+
在环境变量中添加
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
export DB_URI=mysql://root:password@localhost:3306/test
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
或在pytest.ini中配置
|
|
30
|
+
|
|
31
|
+
```ini
|
|
32
|
+
[pytest]
|
|
33
|
+
db_uri = mysql://root:password@localhost:3306/test
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
或命令行传入
|
|
37
|
+
|
|
38
|
+
```sh
|
|
39
|
+
$ pytest --db-uri=mysql://root:password@localhost:3306/test
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
或使用Fixture配置
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
import pytest
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
@pytest.fixture
|
|
49
|
+
def db_uri():
|
|
50
|
+
return 'mysql://root:password@localhost:3306/test'
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
3. 使用fixture函数: db
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
def test_a(db):
|
|
57
|
+
print(db.query('select id from user limit 1;')
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
使用pytest -s 运行,查看结果
|
|
61
|
+
|
|
62
|
+
```
|
|
63
|
+
...
|
|
64
|
+
[{'id': 123321336}]
|
|
65
|
+
...
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
> 游标使用pymysql.cursors.DictCursor,结果使用fetchone,返回字典格式的结果
|
|
69
|
+
|
|
70
|
+
4. db对象支持的方法
|
|
71
|
+
|
|
72
|
+
- db.query(sql): 执行查询sql,返回一条结果
|
|
73
|
+
- db.query_all(sql): 执行查询sql,返回所有结果
|
|
74
|
+
- db.execute(sql): 执行修改sql
|
|
75
|
+
|
|
76
|
+
---
|
|
77
|
+
|
|
78
|
+
- Email: <a href="mailto:superhin@126.com?Subject=Pytest%20Email" target="_blank">`superhin@126.com`</a>
|
|
79
|
+
- Blog: <a href="https://www.cnblogs.com/superhin/" target="_blank">`博客园 韩志超`</a>
|
|
80
|
+
- 简书: <a href="https://www.jianshu.com/u/0115903ded22" target="_blank">`简书 韩志超`</a>
|
|
81
|
+
|
|
File without changes
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
from typing import List
|
|
3
|
+
from urllib.parse import urlparse, parse_qs
|
|
4
|
+
|
|
5
|
+
import pymysql
|
|
6
|
+
|
|
7
|
+
DEFAULT_MYSQL_PORT = 3306
|
|
8
|
+
DEFAULT_MYSQL_CHARSET = 'utf8'
|
|
9
|
+
|
|
10
|
+
LOGGER = logging.getLogger('pytest-db')
|
|
11
|
+
|
|
12
|
+
def parse_db_uri(db_uri):
|
|
13
|
+
parsed_uri = urlparse(db_uri)
|
|
14
|
+
assert parsed_uri.scheme == 'mysql', 'Only support mysql'
|
|
15
|
+
|
|
16
|
+
user = parsed_uri.username
|
|
17
|
+
password = parsed_uri.password
|
|
18
|
+
host = parsed_uri.hostname
|
|
19
|
+
port = parsed_uri.port or DEFAULT_MYSQL_PORT
|
|
20
|
+
db = parsed_uri.path.lstrip('/')
|
|
21
|
+
# db = parsed_uri.fragment
|
|
22
|
+
|
|
23
|
+
charset = DEFAULT_MYSQL_CHARSET
|
|
24
|
+
if parsed_uri.query is not None:
|
|
25
|
+
charset = parse_qs(parsed_uri.query).get('charset', [DEFAULT_MYSQL_CHARSET])[0]
|
|
26
|
+
|
|
27
|
+
db_conf = dict(host=host, port=int(port), user=user, password=password, db=db, charset=charset)
|
|
28
|
+
return db_conf
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class MySQLClient(object):
|
|
32
|
+
def __init__(self, db_uri: str):
|
|
33
|
+
db_conf = parse_db_uri(db_uri)
|
|
34
|
+
db_conf.setdefault('charset', 'utf8')
|
|
35
|
+
LOGGER.debug(f'Connect db: {db_conf["host"]}:{db_conf["port"]}')
|
|
36
|
+
self.conn = pymysql.connect(**db_conf, autocommit=True)
|
|
37
|
+
self.cursor = self.conn.cursor(pymysql.cursors.DictCursor)
|
|
38
|
+
|
|
39
|
+
def query(self, sql: str) -> dict:
|
|
40
|
+
LOGGER.debug(f'Query sql: {sql}')
|
|
41
|
+
self.cursor.execute(sql)
|
|
42
|
+
result = self.cursor.fetchone()
|
|
43
|
+
LOGGER.debug(f"Query result: {result}")
|
|
44
|
+
return result
|
|
45
|
+
|
|
46
|
+
def query_all(self, sql: str) -> List[dict]:
|
|
47
|
+
LOGGER.debug(f'Query sql: {sql}')
|
|
48
|
+
self.cursor.execute(sql)
|
|
49
|
+
result = self.cursor.fetchall()
|
|
50
|
+
LOGGER.debug(f"Query result num: {len(result)}")
|
|
51
|
+
return result
|
|
52
|
+
|
|
53
|
+
def execute(self, sql):
|
|
54
|
+
LOGGER.debug(f'Execute sql: {sql}')
|
|
55
|
+
try:
|
|
56
|
+
self.cursor.execute(sql)
|
|
57
|
+
self.conn.commit()
|
|
58
|
+
except Exception as e:
|
|
59
|
+
LOGGER.exception(e)
|
|
60
|
+
self.conn.rollback()
|
|
61
|
+
|
|
62
|
+
def close(self):
|
|
63
|
+
LOGGER.debug(f'Close db cursor and connection')
|
|
64
|
+
self.cursor.close()
|
|
65
|
+
self.conn.close()
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import re
|
|
3
|
+
import pytest
|
|
4
|
+
|
|
5
|
+
from pytest_db.mysql_client import MySQLClient, LOGGER
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def pytest_addoption(parser):
|
|
9
|
+
parser.addoption('--db-uri', help='DB URI such like mysql://root:password@localhost:3306/test')
|
|
10
|
+
parser.addini('db_uri', help='DB URI such like mysql://root:password@localhost:3306/test')
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@pytest.fixture(scope='session')
|
|
14
|
+
def db_uri(request):
|
|
15
|
+
"""Get or overwrite the db uri config"""
|
|
16
|
+
return request.config.getoption('--db-uri') or request.config.getini('db_uri') or os.getenv('DB_URI')
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@pytest.fixture(scope='session')
|
|
20
|
+
def db(db_uri):
|
|
21
|
+
"""MySQL db client"""
|
|
22
|
+
if not db_uri:
|
|
23
|
+
pytest.skip('Not set db_uri')
|
|
24
|
+
try:
|
|
25
|
+
db = MySQLClient(db_uri)
|
|
26
|
+
except Exception as ex:
|
|
27
|
+
LOGGER.exception(ex)
|
|
28
|
+
pytest.skip(str(ex))
|
|
29
|
+
else:
|
|
30
|
+
yield db
|
|
31
|
+
db.close()
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pytest-db
|
|
3
|
+
Version: 0.1.2
|
|
4
|
+
Summary: Session scope fixture "db" for mysql query or change
|
|
5
|
+
Home-page: https://github.com/hanzhichao/pytest-db
|
|
6
|
+
Author: Han Zhichao
|
|
7
|
+
Author-email: superhin@126.com
|
|
8
|
+
License: MIT license
|
|
9
|
+
Keywords: pytest,py.test,db,mysql
|
|
10
|
+
Classifier: Framework :: Pytest
|
|
11
|
+
Classifier: Programming Language :: Python
|
|
12
|
+
Classifier: Topic :: Software Development :: Testing
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Requires-Dist: pytest
|
|
16
|
+
Requires-Dist: pytest-runner
|
|
17
|
+
Requires-Dist: pymysql
|
|
18
|
+
Dynamic: author
|
|
19
|
+
Dynamic: author-email
|
|
20
|
+
Dynamic: classifier
|
|
21
|
+
Dynamic: description
|
|
22
|
+
Dynamic: home-page
|
|
23
|
+
Dynamic: keywords
|
|
24
|
+
Dynamic: license
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
Dynamic: requires-dist
|
|
27
|
+
Dynamic: summary
|
|
28
|
+
|
|
29
|
+
Session scope fixture "db" for mysql query or change
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
MANIFEST.in
|
|
3
|
+
README.md
|
|
4
|
+
requirements.txt
|
|
5
|
+
setup.py
|
|
6
|
+
pytest_db/__init__.py
|
|
7
|
+
pytest_db/mysql_client.py
|
|
8
|
+
pytest_db/plugin.py
|
|
9
|
+
pytest_db.egg-info/PKG-INFO
|
|
10
|
+
pytest_db.egg-info/SOURCES.txt
|
|
11
|
+
pytest_db.egg-info/dependency_links.txt
|
|
12
|
+
pytest_db.egg-info/entry_points.txt
|
|
13
|
+
pytest_db.egg-info/requires.txt
|
|
14
|
+
pytest_db.egg-info/top_level.txt
|
|
15
|
+
pytest_db.egg-info/zip-safe
|
|
16
|
+
tests/test_pytest_db.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pytest_db
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
pytest_db-0.1.2/setup.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
from setuptools import setup, find_packages
|
|
5
|
+
|
|
6
|
+
version = '0.1.2'
|
|
7
|
+
|
|
8
|
+
setup_requirements = ['pytest-runner',]
|
|
9
|
+
|
|
10
|
+
setup(
|
|
11
|
+
author="Han Zhichao",
|
|
12
|
+
author_email='superhin@126.com',
|
|
13
|
+
description='Session scope fixture "db" for mysql query or change',
|
|
14
|
+
long_description='Session scope fixture "db" for mysql query or change',
|
|
15
|
+
classifiers=[
|
|
16
|
+
'Framework :: Pytest',
|
|
17
|
+
'Programming Language :: Python',
|
|
18
|
+
'Topic :: Software Development :: Testing',
|
|
19
|
+
'Programming Language :: Python :: 3.7',
|
|
20
|
+
],
|
|
21
|
+
license="MIT license",
|
|
22
|
+
include_package_data=True,
|
|
23
|
+
keywords=[
|
|
24
|
+
'pytest', 'py.test', 'db', 'mysql'
|
|
25
|
+
],
|
|
26
|
+
name='pytest-db',
|
|
27
|
+
packages=find_packages(include=['pytest_db']),
|
|
28
|
+
setup_requires=setup_requirements,
|
|
29
|
+
url='https://github.com/hanzhichao/pytest-db',
|
|
30
|
+
version=version,
|
|
31
|
+
zip_safe=True,
|
|
32
|
+
install_requires=[
|
|
33
|
+
'pytest',
|
|
34
|
+
'pytest-runner',
|
|
35
|
+
'pymysql'
|
|
36
|
+
],
|
|
37
|
+
entry_points={
|
|
38
|
+
'pytest11': [
|
|
39
|
+
'pytest-db = pytest_db.plugin',
|
|
40
|
+
]
|
|
41
|
+
}
|
|
42
|
+
)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
|
|
3
|
+
from pytest_db.mysql_client import parse_db_uri
|
|
4
|
+
|
|
5
|
+
@pytest.fixture(scope='session')
|
|
6
|
+
def db_uri():
|
|
7
|
+
return 'mysql://root:passw0rd@localhost:3306/test?charset=utf8'
|
|
8
|
+
|
|
9
|
+
def test_parse_db_uri(db_uri):
|
|
10
|
+
db_conf = parse_db_uri(db_uri)
|
|
11
|
+
assert db_conf == {'host': 'localhost',
|
|
12
|
+
'port': 3306,
|
|
13
|
+
'user': 'root',
|
|
14
|
+
'password': 'passw0rd',
|
|
15
|
+
'db': 'test',
|
|
16
|
+
'charset': 'utf8'}
|
|
17
|
+
|