aurweb/test
Kevin Morris 27fbda5e7b
feat(FastAPI): add get_(errors|successes) testing HTML helpers
These functions will allow us to more easily check errors or success
messages when testing routes.

Signed-off-by: Kevin Morris <kevr@0cost.org>
2021-10-09 22:04:48 -07:00
..
scripts Makefile: run pytest units 2021-06-05 19:49:19 -07:00
__init__.py aurweb: Globalize a Translator instance, add more utility 2021-06-05 19:49:42 -07:00
Makefile Improve Docker ecosystem 2021-06-24 19:02:35 -07:00
README.md fix(FastAPI): Improve sqlite testing speed 2021-10-03 15:59:52 -07:00
setup.sh add logging.config.fileConfig 2021-06-24 18:56:38 -07:00
sharness.sh sharness: do not use spaces in trash directory 2021-07-27 16:24:34 -07:00
t1100-git-auth.t Makefile: run pytest units 2021-06-05 19:49:19 -07:00
t1200-git-serve.t Makefile: run pytest units 2021-06-05 19:49:19 -07:00
t1300-git-update.t Makefile: run pytest units 2021-06-05 19:49:19 -07:00
t2100-mkpkglists.t Makefile: run pytest units 2021-06-05 19:49:19 -07:00
t2200-tuvotereminder.t Makefile: run pytest units 2021-06-05 19:49:19 -07:00
t2300-pkgmaint.t Makefile: run pytest units 2021-06-05 19:49:19 -07:00
t2400-aurblup.t Makefile: run pytest units 2021-06-05 19:49:19 -07:00
t2500-notify.t Makefile: run pytest units 2021-06-05 19:49:19 -07:00
t2600-rendercomment.t Makefile: run pytest units 2021-06-05 19:49:19 -07:00
t2700-usermaint.t Makefile: run pytest units 2021-06-05 19:49:19 -07:00
test_accepted_term.py eradicate Term records after testing them 2021-06-24 19:52:17 -07:00
test_account_type.py [FastAPI] Refactor db modifications 2021-09-03 15:03:34 -07:00
test_accounts_routes.py [FastAPI] Refactor db modifications 2021-09-03 15:03:34 -07:00
test_api_rate_limit.py [FastAPI] Refactor db modifications 2021-09-03 15:03:34 -07:00
test_asgi.py FastAPI: add redis integration 2021-08-17 19:58:28 -07:00
test_auth.py fix(FastAPI): AnonymousUser.has_credential also takes kwargs 2021-10-02 16:59:21 -07:00
test_auth_routes.py fix(FastAPI): prefill login fields with entered data 2021-10-05 14:13:48 -04:00
test_ban.py [FastAPI] Refactor db modifications 2021-09-03 15:03:34 -07:00
test_cache.py add aurweb.cache, a redis caching utility module 2021-08-17 19:58:31 -07:00
test_captcha.py add aurweb.captcha, a CAPTCHA utility module 2021-06-05 20:11:17 -07:00
test_config.py add aurweb.db.session 2021-06-05 19:52:40 -07:00
test_db.py [FastAPI] Refactor db modifications 2021-09-03 15:03:34 -07:00
test_defaults.py feat(FastAPI): added /requests (get) route 2021-10-02 22:43:48 -07:00
test_dependency_type.py [FastAPI] Refactor db modifications 2021-09-03 15:03:34 -07:00
test_exceptions.py Sanitize and modernize pytests 2021-06-05 20:11:17 -07:00
test_group.py [FastAPI] Refactor db modifications 2021-09-03 15:03:34 -07:00
test_homepage.py fix(FastAPI): use configured letter case for SSH fingerprints 2021-10-04 18:00:50 +00:00
test_html.py feat(FastAPI): add get_(errors|successes) testing HTML helpers 2021-10-09 22:04:48 -07:00
test_initdb.py use mysql backend in config.dev 2021-06-05 20:17:48 -07:00
test_l10n.py jinja2: add 'tn' filter, a numerical translation 2021-08-30 18:49:01 -07:00
test_license.py [FastAPI] Refactor db modifications 2021-09-03 15:03:34 -07:00
test_official_provider.py [FastAPI] Refactor db modifications 2021-09-03 15:03:34 -07:00
test_package.py [FastAPI] Refactor db modifications 2021-09-03 15:03:34 -07:00
test_package_base.py [FastAPI] Refactor db modifications 2021-09-03 15:03:34 -07:00
test_package_blacklist.py [FastAPI] Refactor db modifications 2021-09-03 15:03:34 -07:00
test_package_comaintainer.py test_package_comaintainer: sanitize newlines 2021-06-11 17:15:18 -07:00
test_package_comment.py [FastAPI] Refactor db modifications 2021-09-03 15:03:34 -07:00
test_package_dependency.py [FastAPI] Refactor db modifications 2021-09-03 15:03:34 -07:00
test_package_group.py use declarative_base for all ORM models 2021-06-10 13:54:27 -07:00
test_package_keyword.py Sanitize and modernize pytests 2021-06-05 20:11:17 -07:00
test_package_license.py use declarative_base for all ORM models 2021-06-10 13:54:27 -07:00
test_package_notification.py add PackageNotification SQLAlchemy ORM model 2021-06-11 17:14:28 -07:00
test_package_relation.py [FastAPI] Refactor db modifications 2021-09-03 15:03:34 -07:00
test_package_request.py [FastAPI] Refactor db modifications 2021-09-03 15:03:34 -07:00
test_package_source.py [FastAPI] Refactor db modifications 2021-09-03 15:03:34 -07:00
test_package_vote.py test_package_vote: remove useless stuff 2021-06-11 16:56:15 -07:00
test_packages_routes.py feat(FastAPI): add /pkgbase/{name}/adopt (post) 2021-10-08 15:58:00 -07:00
test_packages_util.py fix(FastAPI): voted/notified query efficiency 2021-09-19 00:34:08 -07:00
test_popupdate.py add test_popupdate.py 2021-06-05 19:49:19 -07:00
test_redis.py FastAPI: add redis integration 2021-08-17 19:58:28 -07:00
test_relation_type.py [FastAPI] Refactor db modifications 2021-09-03 15:03:34 -07:00
test_request_type.py [FastAPI] Refactor db modifications 2021-09-03 15:03:34 -07:00
test_routes.py [FastAPI] Refactor db modifications 2021-09-03 15:03:34 -07:00
test_rpc.py fix(FastAPI): test_rpc.setup() should be a pytest.fixture 2021-09-17 12:42:32 -07:00
test_rss.py [FastAPI] Refactor db modifications 2021-09-03 15:03:34 -07:00
test_session.py [FastAPI] Refactor db modifications 2021-09-03 15:03:34 -07:00
test_ssh_pub_key.py [FastAPI] Refactor db modifications 2021-09-03 15:03:34 -07:00
test_templates.py feat(FastAPI): added /requests (get) route 2021-10-02 22:43:48 -07:00
test_term.py [FastAPI] Refactor db modifications 2021-09-03 15:03:34 -07:00
test_time.py add aurweb.time module 2021-06-05 20:11:17 -07:00
test_trusted_user_routes.py change(FastAPI): @auth_required login kwarg defaulted to True 2021-10-07 12:38:34 -07:00
test_tu_vote.py add TUVote SQLAlchemy ORM model 2021-06-11 22:14:38 -07:00
test_tu_voteinfo.py [FastAPI] Refactor db modifications 2021-09-03 15:03:34 -07:00
test_user.py feat(FastAPI): add /pkgbase/{name}/[un]flag (post) 2021-10-07 16:04:54 -07:00
test_util.py feat(FastAPI): added /requests (get) route 2021-10-02 22:43:48 -07:00

aurweb Test Collection

To run all tests, you may run make check under test/ (alternative targets: make pytest, make sh).

For more control, you may use the prove or pytest command, which receives a directory or a list of files to run, and produces a report.

Each test script is standalone, so you may run them individually. Some tests may receive command-line options to help debugging. See for example sharness's documentation for shell test scripts: https://github.com/chriscool/sharness/blob/master/README.git

Dependencies

For all the test to run, the following Arch packages should be installed:

  • pyalpm
  • python-alembic
  • python-bleach
  • python-markdown
  • python-pygit2
  • python-sqlalchemy
  • python-srcinfo
  • python-coverage
  • python-pytest
  • python-pytest-cov
  • python-pytest-asyncio
  • postfix
  • openssh

Optional (faster testing)

  • libeatmydata

Test Configuration

To perform any tests, we need to supply aurweb with a valid configuration. For development (and testing) purposes, an example conf/config.dev can be slightly modified.

Start off by copying config.dev to a new configuration.

$ cp -v conf/config.dev conf/config

First, we must tell aurweb where the root of our project lives by replacing YOUR_AUR_ROOT with the path to the aurweb repository.

$ sed -i "s;YOUR_AUR_ROOT;/path/to/aurweb;g" conf/config

Now, one must decide a database backend to use; see Test Database for details on configuring the different supported backends.

Test Database

Users may choose to configure one of several backends, including: mysql and sqlite. By default, conf/config.dev is configured for a the mysql backend using a test database named aurweb_test.

Users can initialize an empty MySQL database by performing the following:

$ cat conf/config
[database]
backend = mysql
name = aurweb_test
user = aur
password = aur
socket = /var/run/mysqld/mysqld.sock
...

# mysql -u root -e "CREATE USER 'aur'@'localhost' IDENTIFIED BY 'aur'"
# mysql -u root -e "CREATE DATABASE aurweb_test"
# mysql -u root -e "GRANT ALL ON aurweb_test.* TO 'aur'@'localhost'"
# mysql -u root -e "FLUSH ALL PRIVILEGES"

$ export AUR_CONFIG=conf/config
$ python3 -m aurweb.initdb

Or more lightweight with sqlite:

$ cat $AUR_CONFIG
[database]
backend = sqlite
name = aurweb.sqlite3
...

$ export AUR_CONFIG=conf/config
$ python3 -m aurweb.initdb

After initializing a fresh test database, users can continue on to Running Tests.

Running tests

Recommended method of running tests: make check.

Makefile test targets: sh, pytest.

Run tests from the project root.

$ cd /path/to/aurweb

Ensure you have the proper AUR_CONFIG exported:

$ export AUR_CONFIG=conf/config

To run sharness shell test suites (requires Arch Linux):

$ make -C test sh

To run pytest Python test suites:

$ make -C test pytest

Note: For SQLite tests, users may want to use eatmydata to improve speed:

$ eatmydata -- make -C test sh
$ eatmydata -- make -C test pytest

To produce coverage reports related to Python when running tests manually, use the following method:

$ coverage run --append /path/to/python/file.py

Note: Sharness test suites (shell) internally run coverage run.

After tests are run, one can produce coverage reports.

# Print out a CLI coverage report.
$ coverage report

# Produce an HTML-based coverage report.
$ coverage html

Writing Python tests (current)

Almost all of our pytest suites use the database in some way. There are a few particular testing utilities in aurweb that one should keep aware of to aid testing code:

  • aurweb.testing.setup_init_db(*tables)
    • Prepares test database tables to be cleared before a test is run. Be careful not to specify any tables we depend on for constant records, like AccountTypes, DependencyTypes, RelationTypes and RequestTypes.
  • aurweb.testing.requests.Request
    • A fake stripped down version of fastapi.Request that can be passed to any functions in our codebase which use fastapi.Request parameters.

Example code:

import pytest

from aurweb import db
from aurweb.models.user import User
from aurweb.testing import setup_test_db
from aurweb.testing.requests import Request


@pytest.fixture(autouse=True)
def setup():
    setup_test_db(User.__tablename__)

@pytest.fixture
def user():
    yield db.create(User, Passwd="testPassword", ...)

def test_user_login(user):
    assert isinstance(user, User) is True

    fake_request = Request()
    sid = user.login(fake_request, "testPassword")
    assert sid is not None

Writing Sharness tests (legacy)

Shell test scripts must follow the Test Anything Protocol specification: http://testanything.org/tap-specification.html

Python tests must be compatible with pytest and included in pytest test/ execution after setting up a configuration.

Tests must support being run from any directory. They may use $0 to determine their location. Python scripts should expect aurweb to be installed and importable without toying with os.path or PYTHONPATH.

Tests written in shell should use sharness. In general, new tests should be consistent with existing tests unless they have a good reason not to.

Debugging Sharness tests

By default, make -C test is quiet and does not print out verbose information about tests being run. If a test is failing, one can look into verbose details of sharness tests by executing them with the --verbose flag. Example: ./t1100_git_auth.t --verbose. This is particularly useful when tests happen to fail in a remote continuous integration environment, where the reader does not have complete access to the runner.