Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@ matrix:
- python: pypy3.5
env: TORTOISE_TEST_DB="sqlite:///tmp/test-{}.sqlite"
install: pip install -r requirements-pypy.txt
script: green
script: pytest tortoise/tests
- python: pypy3.5
env: TORTOISE_TEST_DB="mysql://root:@127.0.0.1:3306/test_{}"
install: pip install -r requirements-pypy.txt
script: green
- python: 3.6
env: TEST_RUNNER=py.test
install: pip install -r requirements-dev.txt
script: py.test
script: pytest tortoise/tests
- python: 3.6
env: TEST_RUNNER=nose2
install: pip install -r requirements-dev.txt
Expand Down
14 changes: 4 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,12 @@ endif
python setup.py check -mrs

test: deps
coverage erase
$(py_warn) coverage run -p --concurrency=multiprocessing `which green`
coverage combine
coverage report
$(py_warn) pytest --cov=tortoise tortoise/tests

testall: deps
coverage erase
-$(py_warn) TORTOISE_TEST_DB=sqlite://:memory: coverage run -p --concurrency=multiprocessing `which green`
-$(py_warn) TORTOISE_TEST_DB=postgres://postgres:@127.0.0.1:5432/test_\{\} coverage run -p --concurrency=multiprocessing `which green`
-$(py_warn) TORTOISE_TEST_DB="mysql://root:@127.0.0.1:3306/test_\{\}" coverage run -p --concurrency=multiprocessing `which green`
coverage combine
coverage report
-$(py_warn) TORTOISE_TEST_DB=sqlite://:memory: pytest --cov=tortoise tortoise/tests
-$(py_warn) TORTOISE_TEST_DB=postgres://postgres:@127.0.0.1:5432/test_\{\} pytest --cov=tortoise tortoise/tests
-$(py_warn) TORTOISE_TEST_DB="mysql://root:@127.0.0.1:3306/test_\{\}" pytest --cov=tortoise tortoise/tests

ci: check test

Expand Down
14 changes: 9 additions & 5 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from tortoise.contrib.test import finalizer, initializer
import os

import pytest

from tortoise.contrib.test import finalizer, initializer

def pytest_runtest_setup(item):
initializer(["tortoise.tests.testmodels"], db_url="sqlite://:memory:")

@pytest.fixture(scope="session", autouse=True)
def initialize_tests(request):
db_url = os.environ.get("TORTOISE_TEST_DB", "sqlite://:memory:")
initializer(["tortoise.tests.testmodels"], db_url=db_url)

def pytest_runtest_teardown(item, nextitem):
finalizer()
request.addfinalizer(finalizer)
17 changes: 17 additions & 0 deletions docs/transactions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@
Transactions
============

.. note::
Tortoise uses contextvars (and it's polyfill `aiocontextvars
<https://github.com/fantix/aiocontextvars>`_) to manage transactions.
Which means, that automatic propagation of transactions could be achieved only
in same task context. If you are going to spawn more tasks through
``asyncio.ensure_future()`` (or any other method to spawn tasks) transactions won't be
propagated. But in such cases you can explicitly pass transaction instance
to method you are calling, like this:

.. code-block:: python3

async with in_transaction() as tr:
await asyncio.gather(*[Tournament.create(name="Test", using_db=tr) for _ in range(100)])




.. automodule:: tortoise.transactions
:members:
:undoc-members:
2 changes: 1 addition & 1 deletion requirements-dev.in
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ black;python_version>="3.6"
## Test tools
tox
asynctest
green
coveralls
pytest
pytest-cov
nose2

# Pypi
Expand Down
71 changes: 27 additions & 44 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,57 @@
# This file is autogenerated by pip-compile
# To update, run:
#
# make up
# pip-compile --output-file=requirements-dev.txt requirements-dev.in
#
aenum==2.1.2 # via pypika
aiocontextvars==0.2.2 ; python_version < "3.7"
aiofiles==0.4.0 # via quart
aiomysql==0.0.20
aiosqlite==0.10.0
alabaster==0.7.12 # via sphinx
appdirs==1.4.3 # via black
asn1crypto==0.24.0 # via cryptography
astroid==2.2.5 # via pylint
asyncpg==0.18.3
asynctest==0.13.0
atomicwrites==1.3.0 # via pytest
attrs==19.1.0 # via black, pytest
attrs==19.1.0 # via pytest
babel==2.7.0 # via sphinx
bandit==1.6.1
black==19.3b0 ; python_version >= "3.6"
bandit==1.6.2
bleach==3.1.0 # via readme-renderer
blinker==1.4 # via quart
certifi==2019.6.16 # via requests
cffi==1.12.3 # via cryptography
chardet==3.0.4 # via requests
ciso8601==2.1.1
click==7.0 # via black, pip-tools, quart
click==7.0 # via pip-tools
cloud-sptheme==1.9.4
colorama==0.4.1 # via green
contextvars==2.4 # via aiocontextvars
coverage==4.5.3 # via coveralls, green, nose2
coverage==4.5.3 # via coveralls, nose2, pytest-cov
coveralls==1.8.1
cryptography==2.7 # via pymysql
docopt==0.6.2 # via coveralls
docutils==0.14
entrypoints==0.3 # via flake8
filelock==3.0.12 # via tox
flake8-isort==2.7.0
flake8==3.7.7 # via flake8-isort
flake8==3.7.8 # via flake8-isort
gitdb2==2.0.5 # via gitpython
gitpython==2.1.11 # via bandit
green==2.16.1
h11==0.9.0 # via hypercorn, wsproto
h2==3.1.0 # via hypercorn
hpack==3.0.0 # via h2
hypercorn==0.5.4 ; python_version >= "3.6"
hyperframe==5.2.0 # via h2
idna==2.8 # via requests
imagesize==1.1.0 # via sphinx
immutables==0.9 # via contextvars
importlib-metadata==0.18 # via pluggy, pytest
isort==4.3.20 # via flake8-isort, pylint
itsdangerous==1.1.0 # via quart
jinja2==2.10.1 # via quart, sphinx
importlib-metadata==0.18 # via pluggy, pytest, tox
isort==4.3.21 # via flake8-isort, pylint
jinja2==2.10.1 # via sphinx
lazy-object-proxy==1.4.1 # via astroid
lxml==4.3.4 # via green
markupsafe==1.1.1 # via jinja2
mccabe==0.6.1 # via flake8, pylint
more-itertools==7.0.0 # via pytest
multidict==4.5.2 # via quart
mock==2.0.0 # via nose2
more-itertools==7.1.0 # via pytest
mypy-extensions==0.4.1 # via mypy
mypy==0.710
mypy==0.720
nose2==0.9.1
packaging==19.0 # via pytest, sphinx
pbr==5.3.0 # via stevedore
pip-tools==3.8.0
packaging==19.0 # via pytest, sphinx, tox
pathlib2==2.3.4 # via pytest
pbr==5.4.1 # via mock, stevedore
pip-tools==3.9.0
pkginfo==1.5.0.1 # via twine
pluggy==0.12.0 # via pytest, tox
py==1.8.0 # via pytest, tox
Expand All @@ -76,19 +63,17 @@ pygments==2.4.2
pylint==2.3.1
pymysql==0.9.2 # via aiomysql
pyparsing==2.4.0 # via packaging
pypika==0.28.0
pytest==4.6.3
pytoml==0.1.20 # via hypercorn
pypika==0.29.0
pytest-cov==2.7.1
pytest==5.0.1
pytz==2019.1 # via babel
pyyaml==5.1.1
quart==0.6.13 ; python_version >= "3.6"
readme-renderer==24.0 # via twine
requests-toolbelt==0.9.1 # via twine
requests==2.22.0 # via coveralls, requests-toolbelt, sphinx, twine
six==1.12.0 # via astroid, bandit, bleach, cryptography, nose2, packaging, pip-tools, pytest, readme-renderer, stevedore, tox
six==1.12.0 # via astroid, bandit, bleach, cryptography, mock, nose2, packaging, pathlib2, pip-tools, readme-renderer, stevedore, tox
smmap2==2.0.5 # via gitdb2
snowballstemmer==1.2.1 # via sphinx
sortedcontainers==2.1.0 # via quart
snowballstemmer==1.9.0 # via sphinx
sphinx-autodoc-typehints==1.6.0
sphinx==2.1.2
sphinxcontrib-applehelp==1.0.1 # via sphinx
Expand All @@ -99,20 +84,18 @@ sphinxcontrib-qthelp==1.0.2 # via sphinx
sphinxcontrib-serializinghtml==1.1.3 # via sphinx
stevedore==1.30.1 # via bandit
testfixtures==6.10.0 # via flake8-isort
toml==0.10.0 # via black, tox
tox==3.12.1
toml==0.10.0 # via tox
tox==3.13.2
tqdm==4.32.2 # via twine
twine==1.13.0
typed-ast==1.4.0 # via astroid, mypy
typing-extensions==3.7.4 # via hypercorn
unidecode==1.1.0 # via green
typing-extensions==3.7.4 # via mypy
urllib3==1.25.3 # via requests
virtualenv==16.6.1 # via tox
virtualenv==16.6.2 # via tox
wcwidth==0.1.7 # via pytest
webencodings==0.5.1 # via bleach
wrapt==1.11.2 # via astroid
wsproto==0.14.1 # via hypercorn
zipp==0.5.1 # via importlib-metadata
zipp==0.5.2 # via importlib-metadata

# The following packages are considered to be unsafe in a requirements file:
# setuptools==41.0.1 # via sphinx, tox, twine
# setuptools==41.0.1 # via sphinx, twine
2 changes: 1 addition & 1 deletion requirements-pypy.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ aiomysql

## Test tools
asynctest
green
pytest
22 changes: 14 additions & 8 deletions requirements-pypy.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,30 @@
#
# make up
#
aenum==2.1.2 # via pypika
aiocontextvars==0.2.2 ; python_version < "3.7"
aiomysql==0.0.20
aiosqlite==0.10.0
asn1crypto==0.24.0 # via cryptography
asynctest==0.13.0
atomicwrites==1.3.0 # via pytest
attrs==19.1.0 # via pytest
cffi==1.12.3 # via cryptography
ciso8601==2.1.1
colorama==0.4.1 # via green
contextvars==2.4 # via aiocontextvars
coverage==4.5.3 # via green
cryptography==2.7 # via pymysql
green==2.16.1
immutables==0.9 # via contextvars
lxml==4.3.4 # via green
importlib-metadata==0.18 # via pluggy, pytest
more-itertools==7.1.0 # via pytest
packaging==19.0 # via pytest
pathlib2==2.3.4 # via pytest
pluggy==0.12.0 # via pytest
py==1.8.0 # via pytest
pycparser==2.19 # via cffi
pymysql==0.9.2 # via aiomysql
pypika==0.28.0
pyparsing==2.4.0 # via packaging
pypika==0.29.0
pytest==5.0.1
pyyaml==5.1.1
six==1.12.0 # via cryptography
unidecode==1.1.0 # via green
six==1.12.0 # via cryptography, packaging, pathlib2
wcwidth==0.1.7 # via pytest
zipp==0.5.2 # via importlib-metadata
10 changes: 1 addition & 9 deletions tortoise/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os
from copy import deepcopy
from inspect import isclass
from typing import Coroutine, Dict, List, Optional, Tuple, Type, Union, cast # noqa
from typing import Coroutine, Dict, List, Optional, Tuple, Type, cast # noqa

from tortoise import fields
from tortoise.backends.base.client import BaseDBAsyncClient
Expand All @@ -15,14 +15,8 @@
from tortoise.filters import get_m2m_filters
from tortoise.models import Model
from tortoise.queryset import QuerySet # noqa
from tortoise.transactions import current_transaction_map
from tortoise.utils import generate_schema_for_client

try:
from contextvars import ContextVar
except ImportError:
from aiocontextvars import ContextVar # type: ignore

logger = logging.getLogger("tortoise")


Expand Down Expand Up @@ -220,7 +214,6 @@ async def _init_connections(cls, connections_config: dict, create_db: bool) -> N
await connection.db_create()
await connection.create_connection(with_db=True)
cls._connections[name] = connection
current_transaction_map[name] = ContextVar(name, default=connection)

@classmethod
def _init_apps(cls, apps_config: dict) -> None:
Expand Down Expand Up @@ -399,7 +392,6 @@ async def _reset_apps(cls) -> None:
for model in app.values():
model._meta.default_connection = None
cls.apps = {}
current_transaction_map.clear()

@classmethod
async def generate_schemas(cls, safe: bool = True) -> None:
Expand Down
Loading