diff --git a/swh/core/api/tests/__init__.py b/swh/core/api/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/swh/core/tests/server_testing.py b/swh/core/api/tests/server_testing.py similarity index 100% rename from swh/core/tests/server_testing.py rename to swh/core/api/tests/server_testing.py diff --git a/swh/core/tests/test_api.py b/swh/core/api/tests/test_api.py similarity index 100% rename from swh/core/tests/test_api.py rename to swh/core/api/tests/test_api.py diff --git a/swh/core/tests/test_serializers.py b/swh/core/api/tests/test_serializers.py similarity index 100% rename from swh/core/tests/test_serializers.py rename to swh/core/api/tests/test_serializers.py diff --git a/swh/core/db/tests/__init__.py b/swh/core/db/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/swh/core/tests/conftest.py b/swh/core/db/tests/conftest.py similarity index 100% rename from swh/core/tests/conftest.py rename to swh/core/db/tests/conftest.py diff --git a/swh/core/tests/db_testing.py b/swh/core/db/tests/db_testing.py similarity index 100% rename from swh/core/tests/db_testing.py rename to swh/core/db/tests/db_testing.py diff --git a/swh/core/tests/test_db.py b/swh/core/db/tests/test_db.py similarity index 98% rename from swh/core/tests/test_db.py rename to swh/core/db/tests/test_db.py index a9c70e8..355a384 100644 --- a/swh/core/tests/test_db.py +++ b/swh/core/db/tests/test_db.py @@ -1,102 +1,102 @@ # Copyright (C) 2019 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import os.path import tempfile import unittest from hypothesis import strategies, given import pytest from swh.core.db import BaseDb -from swh.core.tests.db_testing import ( +from .db_testing import ( SingleDbTestFixture, db_create, db_destroy, db_close, ) INIT_SQL = ''' create table test_table ( i int, txt text, bytes bytea ); ''' db_rows = strategies.lists(strategies.tuples( strategies.integers(-2147483648, +2147483647), strategies.text( alphabet=strategies.characters( blacklist_categories=['Cs'], # surrogates blacklist_characters=[ '\x00', # pgsql does not support the null codepoint '\r', # pgsql normalizes those ] ), ), strategies.binary(), )) @pytest.mark.db def test_connect(): db_name = db_create('test-db2', dumps=[]) try: db = BaseDb.connect('dbname=%s' % db_name) with db.cursor() as cur: cur.execute(INIT_SQL) cur.execute("insert into test_table values (1, %s, %s);", ('foo', b'bar')) cur.execute("select * from test_table;") assert list(cur) == [(1, 'foo', b'bar')] finally: db_close(db.conn) db_destroy(db_name) @pytest.mark.db class TestDb(SingleDbTestFixture, unittest.TestCase): TEST_DB_NAME = 'test-db' @classmethod def setUpClass(cls): with tempfile.TemporaryDirectory() as td: with open(os.path.join(td, 'init.sql'), 'a') as fd: fd.write(INIT_SQL) cls.TEST_DB_DUMP = os.path.join(td, '*.sql') super().setUpClass() def setUp(self): super().setUp() self.db = BaseDb(self.conn) def test_initialized(self): cur = self.db.cursor() cur.execute("insert into test_table values (1, %s, %s);", ('foo', b'bar')) cur.execute("select * from test_table;") self.assertEqual(list(cur), [(1, 'foo', b'bar')]) def test_reset_tables(self): cur = self.db.cursor() cur.execute("insert into test_table values (1, %s, %s);", ('foo', b'bar')) self.reset_db_tables('test-db') cur.execute("select * from test_table;") self.assertEqual(list(cur), []) @given(db_rows) def test_copy_to(self, data): # the table is not reset between runs by hypothesis self.reset_db_tables('test-db') items = [dict(zip(['i', 'txt', 'bytes'], item)) for item in data] self.db.copy_to(items, 'test_table', ['i', 'txt', 'bytes']) cur = self.db.cursor() cur.execute('select * from test_table;') self.assertCountEqual(list(cur), data) diff --git a/tox.ini b/tox.ini index 71a8504..586c09b 100644 --- a/tox.ini +++ b/tox.ini @@ -1,26 +1,44 @@ [tox] -envlist=flake8,py3 +envlist=flake8,py3-{core,db,server} -[testenv:py3] +[testenv:py3-core] deps = - .[testing] - pytest-cov + -rrequirements-test.txt + . +commands = + pytest --doctest-modules swh/core/tests {posargs} + +[testenv:py3-db] +deps = + -rrequirements-test.txt + .[db] pifpaf commands = - pifpaf run postgresql -- pytest --doctest-modules --hypothesis-profile=fast --cov=swh --cov-branch {posargs} + pifpaf run postgresql -- \ + pytest swh/core/db/tests {posargs} -[testenv:py3-slow] +[testenv:py3-server] +deps = + -rrequirements-test.txt + .[http] +commands = + pytest swh/core/api/tests {posargs} + +[testenv:py3] deps = .[testing] pytest-cov pifpaf commands = - pifpaf run postgresql -- pytest --doctest-modules --hypothesis-profile=slow --cov=swh --cov-branch {posargs} - + pifpaf run postgresql -- \ + pytest --doctest-modules \ + --hypothesis-profile=slow \ + --cov=swh --cov-branch \ + {posargs} [testenv:flake8] skip_install = true deps = flake8 commands = {envpython} -m flake8