Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test fixtures #55

Merged
merged 11 commits into from
Oct 6, 2023
Merged

test fixtures #55

merged 11 commits into from
Oct 6, 2023

Conversation

dlax
Copy link
Member

@dlax dlax commented Sep 28, 2023

A series of small changes in tests, to prepare for the large one "Use fake HTTP server for the Patroni API in tests" which replaces the previous mock by a real HTTP server faking the Patroni API.

Should fix #51 as the dependency on pytest-mock is dropped.

Most of the times, it's 200, so the default value simplifies usage in
actual tests.
This fixture itself uses the 'use_old_replica_state' fixture, so that
it's no longer needed to use it explicitly in test functions.
Instead of defining the CliRunner value in each test, we use a fixture.
The CliRunner is also configured with stdout and stderr separated
because mixing them will pose problem if we use stderr for other
purposes in tests, e.g. to emit log messages from a forth-coming HTTP
server.
This way, our log messages (and those from our stack) will show up in
case of errors or test failures, which makes debugging easier.
@dlax dlax force-pushed the fixtures branch 2 times, most recently from 597f07e to 198af5e Compare October 3, 2023 12:59
@dlax dlax marked this pull request as draft October 3, 2023 13:01
@dlax dlax marked this pull request as ready for review October 3, 2023 14:05
@dlax dlax requested a review from blogh October 3, 2023 14:05
Copy link
Collaborator

@blogh blogh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR. It's really nice.

Can you add an entry about the HTTP server and the code coverage in the
CHANGELOG please ?

tests/__init__.py Show resolved Hide resolved
tests/conftest.py Show resolved Hide resolved
CONTRIBUTING.md Outdated Show resolved Hide resolved
tests/test_cluster_config_has_changed.py Show resolved Hide resolved
CONTRIBUTING.md Outdated Show resolved Hide resolved
Instead of requiring the user to run the test suite with and without the
--use-old-replica-state flag, we introduce an 'old_replica_state()'
parametrized fixture that is used only when needed (i.e. in
test_cluster_{has_replica,node_count}.py).
To be run with 'pytest --cov --cov-report=html'.
Instead of repeating the dependencies.
We introduce a patroni_api fixture, defined in tests/conftest.py, which
sets up an HTTP server serving files in a temporary directory. The
server is itself defined by the PatroniAPI class; it has a 'routes()'
context manager method to be used in actual tests to setup expected
responses based on specified JSON files.

We set up some logging in order to improve debugging.

The direct advantage of this is that PatroniResource.rest_api() method
is now covered by the test suite.

Coverage before this commit:

  Name                        Stmts   Miss  Cover
  -----------------------------------------------
  check_patroni/__init__.py       3      0   100%
  check_patroni/cli.py          193     18    91%
  check_patroni/cluster.py      113      0   100%
  check_patroni/convert.py       23      5    78%
  check_patroni/node.py         146      1    99%
  check_patroni/types.py         50     23    54%
  -----------------------------------------------
  TOTAL                         528     47    91%

and after this commit:

  Name                        Stmts   Miss  Cover
  -----------------------------------------------
  check_patroni/__init__.py       3      0   100%
  check_patroni/cli.py          193     18    91%
  check_patroni/cluster.py      113      0   100%
  check_patroni/convert.py       23      5    78%
  check_patroni/node.py         146      1    99%
  check_patroni/types.py         50      9    82%
  -----------------------------------------------
  TOTAL                         528     33    94%

In actual test functions, we either invoke patroni_api.routes() to
configure which JSON file(s) should be served for each endpoint, or we
define dedicated fixtures (e.g. cluster_config_has_changed()) to
configure this for several test functions or the whole module.

The 'old_replica_state' parametrized fixture is used when needed to
adjust such fixtures, e.g. in cluster_has_replica_ok(), to modify the
JSON content using cluster_api_set_replica_running() (previously in
tests/tools.py, now in tests/__init__.py).

The dependency on pytest-mock is no longer needed.
From previous commit, the test suite also type-checks.
@dlax dlax merged commit 5932782 into master Oct 6, 2023
6 checks passed
@dlax dlax deleted the fixtures branch October 6, 2023 09:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tests fail on Debian buster/bullseye and Ubuntu focal
2 participants