Changeset View
Changeset View
Standalone View
Standalone View
swh/core/db/tests/test_db.py
# Copyright (C) 2019 The Software Heritage developers | # Copyright (C) 2019 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import inspect | |||||
import os.path | import os.path | ||||
import tempfile | import tempfile | ||||
import unittest | import unittest | ||||
from unittest.mock import Mock, MagicMock | from unittest.mock import Mock, MagicMock | ||||
from hypothesis import strategies, given | from hypothesis import strategies, given | ||||
import psycopg2 | import psycopg2 | ||||
import pytest | import pytest | ||||
▲ Show 20 Lines • Show All 132 Lines • ▼ Show 20 Lines | |||||
def test_db_transaction__with_generator(): | def test_db_transaction__with_generator(): | ||||
with pytest.raises(ValueError, match='generator'): | with pytest.raises(ValueError, match='generator'): | ||||
class Storage: | class Storage: | ||||
@db_transaction() | @db_transaction() | ||||
def endpoint(self, cur=None, db=None): | def endpoint(self, cur=None, db=None): | ||||
yield None | yield None | ||||
def test_db_transaction_signature(): | |||||
"""Checks db_transaction removes the 'cur' and 'db' arguments.""" | |||||
def f(self, foo, *, bar=None): | |||||
pass | |||||
expected_sig = inspect.signature(f) | |||||
@db_transaction() | |||||
def g(self, foo, *, bar=None, db=None, cur=None): | |||||
pass | |||||
actual_sig = inspect.signature(g) | |||||
assert actual_sig == expected_sig | |||||
def test_db_transaction_generator(mocker): | def test_db_transaction_generator(mocker): | ||||
expected_cur = object() | expected_cur = object() | ||||
called = False | called = False | ||||
class Storage: | class Storage: | ||||
@db_transaction_generator() | @db_transaction_generator() | ||||
def endpoint(self, cur=None, db=None): | def endpoint(self, cur=None, db=None): | ||||
Show All 22 Lines | |||||
def test_db_transaction_generator__with_nongenerator(): | def test_db_transaction_generator__with_nongenerator(): | ||||
with pytest.raises(ValueError, match='generator'): | with pytest.raises(ValueError, match='generator'): | ||||
class Storage: | class Storage: | ||||
@db_transaction_generator() | @db_transaction_generator() | ||||
def endpoint(self, cur=None, db=None): | def endpoint(self, cur=None, db=None): | ||||
pass | pass | ||||
def test_db_transaction_generator_signature(): | |||||
"""Checks db_transaction removes the 'cur' and 'db' arguments.""" | |||||
def f(self, foo, *, bar=None): | |||||
pass | |||||
expected_sig = inspect.signature(f) | |||||
@db_transaction_generator() | |||||
def g(self, foo, *, bar=None, db=None, cur=None): | |||||
yield None | |||||
actual_sig = inspect.signature(g) | |||||
assert actual_sig == expected_sig |