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

Increase unit test coverage to 100% #8

Merged
merged 13 commits into from
Sep 13, 2020
Merged

Conversation

br3ndonland
Copy link
Owner

Description

PR #7 added test coverage measurement with pytest-cov and Codecov. The initial coverage measurement showed 86% unit test coverage.

This PR will increase the unit test coverage to 100%.

Changes

  • Increase test coverage of inboard/__init__.py (2a56650): 71% -> 100% coverage
  • Add unit tests for base ASGI app (62d2e32, ed03219, 5e6af54): 0% -> 100% coverage
    • Add unit tests for base ASGI app endpoints using Starlette TestClient
    • Update base ASGI app with PROCESS_MANAGER environment variable
    • Verify that PROCESS_MANAGER environment variable is set to either "gunicorn" or "uvicorn"
    • Add unit test for exception when using inappropriate PROCESS_MANAGER
    • Type-annotate base ASGI app instance as Callable
  • Increase test coverage of Starlette utilities (c40a020): 93% -> 100% coverage
    • Generalize Starlette exception handling in inboard/app/utilities.py: Exceptions are passed to inboard/app/starlettebase/main.py and handled by on_auth_error().
    • Add test method to verify Starlette exception message returned by inboard/app/starlettebase/main.py
  • Increase test coverage of start.py (ca4eacd): 89% -> 100% coverage
    • Remove ImportError conditional from start.configure_logging(): Python will raise an ImportError automatically in this case.
    • Add test method for incorrect logging module path to test_start.py
    • Remove exception handling from start.run_pre_start_script: Don't yet have a test case in which an exception would be raised.
    • Condense __main__ block: Helps avoid replicate method calls when running start.py. Previously, some methods were running multiple times (such as start.configure_logging).
    • Add pragma: no cover comments to start.py __main__ block: Methods are tested separately.
  • Increase test coverage of Gunicorn conf (1275ac4, c6e04dc, cd7604c): 84% -> 100% coverage
    • Update test_start.py for default Gunicorn config: Test default configuration and custom configuration with separate test methods.
    • Streamline Gunicorn config logging: As in tiangolo/uvicorn-gunicorn-docker, the gunicorn_conf.py had been configured to print a JSON dump of the custom Gunicorn configuration. This is unnecessary, because Gunicorn already outputs its configuration to the logger when LOG_LEVEL=debug is used, so the JSON dump will be removed.
    • Remove exception handling when loading logconfig_dict in gunicorn_conf.py: The start.configure_logging method has its own exception handling.
    • Use same syntax when calculating max_workers and workers_per_core in gunicorn_conf.py
    • Add test method to test_start.py to check worker configuration
  • Update docs (801dfdc, 2a74775, 090e15a, 24c6738)

Related

#4
#7
5b9706f
397638e
b0a36de
735618a
3906b3d
92e6b68
735618a

92e6b68
d2220ba
735618a
62d2e32

- Add unit tests for base ASGI app endpoints using Starlette TestClient
- Update base ASGI app with `PROCESS_MANAGER` environment variable
- Type-annotate base ASGI app instance as `Callable`

Test coverage of inboard/app/base/main.py: 0% -> 100%
92e6b68
d2220ba
735618a
62d2e32
ed03219

- Verify that `PROCESS_MANAGER` environment variable is set to either
  "gunicorn" or "uvicorn"
- Add unit test for exception when using inappropriate `PROCESS_MANAGER`
- Generalize Starlette exception handling in inboard/app/utilities.py:
  Exceptions are passed to inboard/app/starlettebase/main.py and handled
  by `on_auth_error()`.
- Add test method to verify Starlette exception message returned by
  inboard/app/starlettebase/main.py
- Improve test coverage of inboard/app/utilities.py (93% -> 100%)
89% -> 100%

- Remove `ImportError` conditional from `start.configure_logging()`:
  Python will raise an `ImportError` automatically in this case.
- Add test method for incorrect logging module path to test_start.py
- Remove exception handling from `start.run_pre_start_script`: Don't yet
  have a test case in which an exception would be raised.
- Condense start.py `__main__` block: Helps avoid replicate method calls
  when running start.py. Previously some methods were running multiple
  times (such as `start.configure_logging`).
- Add `pragma: no cover` comments to start.py `__main__` block: Methods
  are tested separately.
00a3319

As in tiangolo/uvicorn-gunicorn-docker, the gunicorn_conf.py had been
configured to print a JSON dump of the custom Gunicorn configuration.
This is unnecessary, because Gunicorn already outputs its configuration
to the logger when `LOG_LEVEL=debug` is used.

This commit will remove the exception handling for `logconfig_dict`. The
`start.configure_logging` method has its own exception handling. If
additional exception handling is ever needed here, the following code
could be used:

```py
import logging

try:
    logconfig_dict = configure_logging(
        logging_conf=os.getenv("LOGGING_CONF", "inboard.logging_conf")
    )
except Exception as e:
    logger: logging.Logger = logging.getLogger()
    logger.debug(f"Error loading logging config with Gunicorn: {e}")

```
84% -> 100%

- Use same syntax when calculating `max_workers` and `workers_per_core`
- Add test method to check worker configuration to test_start.py
- pytest features
- pytest plugins
- pytest configuration
- FastAPI and Starlette testing
- test coverage tooling
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Sep 13, 2020

Sourcery Code Quality Report (beta)

✅  Merging this PR will increase code quality in the affected files by 0.03 out of 10.

Quality metrics Before After Change
Complexity 0.79 0.65 -0.14 🔵
Method Length 46.57 45.25 -1.32 🔵
Quality 8.83 8.86 0.03 🔵
Other metrics Before After Change
Lines 921 999 78
Changed files Quality Before Quality After Quality Change
inboard/init.py 9.78 9.76 -0.02 🔴
inboard/gunicorn_conf.py 4.38 5.07 0.69 🔵
inboard/start.py 7.95 8.12 0.17 🔵
inboard/app/utilities.py 8.32 8.38 0.06 🔵
inboard/app/base/main.py 9.34 9.24 -0.10 🔴
tests/conftest.py 9.24 9.26 0.02 🔵
tests/test_start.py 8.93 8.88 -0.05 🔴
tests/test_version.py 9.86 9.76 -0.10 🔴
tests/app/test_main.py 8.46 8.53 0.07 🔵

Here are some functions in these files that still need a tune-up:

File Function Complexity Length Overall Recommendation

Please see our documentation here for details on how these metrics are calculated.

We are actively working on this report - lots more documentation and extra metrics to come!

Let us know what you think of it by mentioning @sourcery-ai in a comment.

@codecov
Copy link

codecov bot commented Sep 13, 2020

Codecov Report

Merging #8 into develop will increase coverage by 14.40%.
The diff coverage is 100.00%.

Impacted file tree graph

@@              Coverage Diff              @@
##           develop        #8       +/-   ##
=============================================
+ Coverage    85.59%   100.00%   +14.40%     
=============================================
  Files            8         8               
  Lines          243       223       -20     
=============================================
+ Hits           208       223       +15     
+ Misses          35         0       -35     
Flag Coverage Δ
#unittests 100.00% <100.00%> (+14.40%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
inboard/app/base/main.py 100.00% <100.00%> (+100.00%) ⬆️
inboard/app/utilities.py 100.00% <100.00%> (+6.45%) ⬆️
inboard/gunicorn_conf.py 100.00% <100.00%> (+16.00%) ⬆️
inboard/start.py 100.00% <100.00%> (+10.97%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e239fe5...24c6738. Read the comment docs.

@br3ndonland br3ndonland merged commit 5828e05 into develop Sep 13, 2020
@br3ndonland br3ndonland deleted the test-coverage-increase branch September 13, 2020 23:41
br3ndonland added a commit to br3ndonland/template-python that referenced this pull request Oct 3, 2020
br3ndonland/inboard#8

- Add version calculation using `importlib.metadata`
  https://docs.python.org/3/library/importlib.metadata.html
- Avoid throwing error if project is not installed (such as in Docker)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant