aurweb/test
moson c6a530f24f
chore(deps): bump pre-commit tools/libs
Prep for python 3.12
Reformat files with latest pre-commit tools

Signed-off-by: moson <moson@archlinux.org>
2024-04-25 07:25:39 +02: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
conftest.py chore(deps): bump pre-commit tools/libs 2024-04-25 07:25:39 +02:00
Makefile change: set -v for sh tests 2021-11-22 10:22:32 -08:00
README.md feat: support LOG_CONFIG environment variable 2022-01-18 07:27:36 -08:00
setup.sh feat: Add blacklist check for pkgbase 2024-02-17 15:55:46 +01:00
sharness.sh sharness: do not use spaces in trash directory 2021-07-27 16:43:16 -07:00
t1100-git-auth.t housekeep: TU rename - Test suite 2023-09-30 16:45:05 +02:00
t1200-git-serve.t housekeep: TU rename - Test suite 2023-09-30 16:45:05 +02:00
t1300-git-update.t feat: Add blacklist check for pkgbase 2024-02-17 15:55:46 +01:00
test_accepted_term.py style: Run pre-commit 2022-08-22 22:40:45 +02:00
test_account_type.py style: Run pre-commit 2022-08-22 22:40:45 +02:00
test_accounts_routes.py fix(test): Fix "TestClient" 2024-04-08 08:37:41 +02:00
test_adduser.py housekeep: TU rename - Misc 2023-09-30 16:45:05 +02:00
test_api_rate_limit.py style: Run pre-commit 2022-08-22 22:40:45 +02:00
test_asgi.py fix(deps): fastapi 0.92.0 upgrade 2023-03-04 10:29:54 +01:00
test_aurblup.py fix: update repo information with aurblup script 2023-05-17 18:22:53 +02:00
test_auth.py housekeep: TU rename - code changes 2023-09-30 16:45:04 +02:00
test_auth_routes.py fix(test): FastAPI 0.87.0 - warning fixes 2022-11-24 22:43:31 +01:00
test_ban.py style: Run pre-commit 2022-08-22 22:40:45 +02:00
test_cache.py feat: Implement statistics class & additional metrics 2023-07-23 13:58:50 +02:00
test_captcha.py style: Run pre-commit 2022-08-22 22:40:45 +02:00
test_config.py style: Run pre-commit 2022-08-22 22:40:45 +02:00
test_cookies.py fix: make AURSID a session cookie if "remember me" is not checked 2023-05-26 22:57:47 +02:00
test_db.py fix: retry transactions who fail due to deadlocks 2022-09-13 12:54:08 -07:00
test_defaults.py feat(FastAPI): added /requests (get) route 2021-10-02 22:43:48 -07:00
test_dependency_type.py style: Run pre-commit 2022-08-22 22:40:45 +02:00
test_email.py style: Run pre-commit 2022-08-22 22:40:45 +02:00
test_exceptions.py feat(exceptions): add InvariantError 2021-12-09 15:10:06 -08:00
test_filelock.py style: Run pre-commit 2022-08-22 22:40:45 +02:00
test_filters.py fix: translations not containing string formatting 2023-07-10 18:02:20 +02:00
test_git_archives.py fix(test): FastAPI 0.87.0 - warning fixes 2022-11-24 22:43:31 +01:00
test_git_update.py feat: expand on update.py tests and show on Gitlab UI 2023-01-27 17:16:25 +00:00
test_group.py style: Run pre-commit 2022-08-22 22:40:45 +02:00
test_homepage.py housekeep: TU rename - UI elements 2023-09-30 16:45:04 +02:00
test_html.py chore(deps): bump pre-commit tools/libs 2024-04-25 07:25:39 +02:00
test_initdb.py style: Run pre-commit 2022-08-22 22:40:45 +02:00
test_l10n.py chore(deps): bump pre-commit tools/libs 2024-04-25 07:25:39 +02:00
test_license.py style: Run pre-commit 2022-08-22 22:40:45 +02:00
test_logging.py chore: rename logging.py and redis.py to avoid circular imports 2022-10-22 18:51:38 +01:00
test_metrics.py fix(tests): Fix test case for Prometheus metrics 2023-11-27 13:21:37 +01:00
test_mkpkglists.py chore(lint): reformatting after black update 2023-02-06 09:15:18 +01:00
test_notify.py feat: Set reply-to header for notifications to ML 2023-11-28 09:33:07 +01:00
test_official_provider.py style: Run pre-commit 2022-08-22 22:40:45 +02:00
test_package.py chore(deps): bump pre-commit tools/libs 2024-04-25 07:25:39 +02:00
test_package_base.py style: Run pre-commit 2022-08-22 22:40:45 +02:00
test_package_blacklist.py style: Run pre-commit 2022-08-22 22:40:45 +02:00
test_package_comaintainer.py style: Run pre-commit 2022-08-22 22:40:45 +02:00
test_package_comment.py style: Run pre-commit 2022-08-22 22:40:45 +02:00
test_package_dependency.py feat: Indicate dependency source 2023-09-03 14:17:11 +02:00
test_package_group.py style: Run pre-commit 2022-08-22 22:40:45 +02:00
test_package_keyword.py style: Run pre-commit 2022-08-22 22:40:45 +02:00
test_package_license.py style: Run pre-commit 2022-08-22 22:40:45 +02:00
test_package_maintainer_routes.py housekeep: TU rename - Table/Column names, scripts 2023-09-30 16:45:05 +02:00
test_package_notification.py style: Run pre-commit 2022-08-22 22:40:45 +02:00
test_package_relation.py style: Run pre-commit 2022-08-22 22:40:45 +02:00
test_package_request.py feat: add link to mailing list article on requests page 2023-01-12 12:06:28 +01:00
test_package_source.py style: Run pre-commit 2022-08-22 22:40:45 +02:00
test_package_vote.py style: Run pre-commit 2022-08-22 22:40:45 +02:00
test_packages_routes.py housekeep: TU rename - Misc 2023-09-30 16:45:05 +02:00
test_packages_util.py fix: make dependency source use superscript tag 2023-10-18 16:19:58 +00:00
test_pkgbase_routes.py feat: Limit comment length 2024-02-25 10:46:47 +01:00
test_pkgmaint.py housekeep: stop "pkgmaint" script (cron job) 2023-02-21 11:19:02 +01:00
test_pm_vote.py housekeep: TU rename - Table/Column names, scripts 2023-09-30 16:45:05 +02:00
test_popupdate.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_ratelimit.py chore(lint): reformatting after black update 2023-02-06 09:15:18 +01:00
test_redis.py chore: rename logging.py and redis.py to avoid circular imports 2022-10-22 18:51:38 +01:00
test_relation_type.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_rendercomment.py feat: Parse markdown within html block elements 2023-12-08 14:14:24 +01:00
test_request_type.py change(aurweb): add parallel tests and improve aurweb.db 2021-11-17 01:34:59 -08:00
test_requests.py chore(deps): bump pre-commit tools/libs 2024-04-25 07:25:39 +02:00
test_routes.py fix(test): FastAPI 0.87.0 - warning fixes 2022-11-24 22:43:31 +01:00
test_rpc.py fix(test): Fix "TestClient" 2024-04-08 08:37:41 +02:00
test_rss.py chore: rename logging.py and redis.py to avoid circular imports 2022-10-22 18:51:38 +01:00
test_session.py chore(deps): bump pre-commit tools/libs 2024-04-25 07:25:39 +02:00
test_spawn.py housekeep: remove PHP implementation 2023-04-28 16:10:32 +02:00
test_ssh_pub_key.py style: Run pre-commit 2022-08-22 22:40:45 +02:00
test_statistics.py fix(tests): Fix test case for Prometheus metrics 2023-11-27 13:21:37 +01:00
test_templates.py chore(deps): update dependencies 2023-11-25 20:23:56 +01:00
test_term.py style: Run pre-commit 2022-08-22 22:40:45 +02:00
test_time.py feat: Support timezone and language query params 2023-10-21 10:41:44 +02:00
test_user.py housekeep: TU rename - Misc 2023-09-30 16:45:05 +02:00
test_usermaint.py style: Run pre-commit 2022-08-22 22:40:45 +02:00
test_util.py fix(test): correct test for ssh-key parsing 2023-07-09 16:13:02 +02:00
test_voteinfo.py housekeep: TU rename - Table/Column names, scripts 2023-09-30 16:45:05 +02:00
test_votereminder.py housekeep: TU rename - Table/Column names, scripts 2023-09-30 16:45:05 +02:00

aurweb Test Collection

To run all tests, you may run make -C test sh and pytest within the project root:

$ make -C test sh    # Run Sharness tests.
$ poetry run pytest  # Run Pytest suites.

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.

Dependencies

For all tests to run dependencies provided via poetry are required:

$ poetry install

Logging

Tests also require the logging.test.conf logging configuration file to be used. You can specify the LOG_CONFIG environment variable to override:

$ export LOG_CONFIG=logging.test.conf

logging.test.conf enables debug logging for the aurweb package, for which we run tests against.

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

Test Databases

Python tests create and drop hashed database names based on PYTEST_CURRENT_TEST. To run tests with a database, the database user must have privileges to create and drop their own databases. Typically, this is the root user, but can be configured for any other user:

GRANT ALL ON *.* TO 'user'@'localhost' WITH GRANT OPTION

The aurweb platform is intended to use the mysql backend, but the sqlite backend is still used for sharness tests. These tests will soon be replaced with pytest suites and sqlite removed.

After ensuring you've configured a test database, users can continue on to Running Tests.

Running tests

Makefile test targets: sh, clean.

Recommended method of running tests: pytest.

Legacy sharness tests: make -C test sh.

aurweb is currently going through a refactor where the majority of sharness tests have been replaced with pytest units. There are still a few sharness tests around, and they are required to gain as much coverage as possible over an entire test run. Users should be writing pytest units for any new features.

Run tests from the project root.

$ cd /path/to/aurweb

Ensure you have the proper AUR_CONFIG and LOG_CONFIG exported:

$ export AUR_CONFIG=conf/config
$ export LOG_CONFIG=logging.test.conf

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

$ make -C test sh

To run pytest Python test suites:

# With poetry-installed aurweb
$ poetry run pytest

# With globally-installed aurweb
$ pytest

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:

  • db_test pytest fixture
    • Prepares test databases for the module and cleans out database tables for each test function requiring this fixture.
  • 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.account_type import USER_ID
from aurweb.models.user import User
from aurweb.testing.requests import Request

# We need to use the `db_test` fixture at some point
# during our test functions.
@pytest.fixture(autouse=True)
def setup(db_test: None) -> None:
    return

# Or... specify it in a dependency fixture.
@pytest.fixture
def user(db_test: None) -> User:
    with db.begin():
        user = db.create(User, Username="test",
                         Email="test@example.org",
                         Passwd="testPassword",
                         AccountTypeID=USER_ID)
    yield user

def test_user_login(user: 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.