br-g / openf1

OpenF1 API - Real-time and historical Formula 1 data
Other
398 stars 22 forks source link

Adding Python Tests #121

Open PrestonHager opened 3 weeks ago

PrestonHager commented 3 weeks ago

Added tests using the pytest module. They can be run with pytest tests in the root directory.

Only unit tests for the utility files have been added. A fixture for mongodb is included.

This branch does include the join_url from the url-fix branch which means this should only get merged if #120 is merged. Other than that, the files in this branch have no operational impacts and are only used for development purposes. There are currently 13 total tests for three different utility files.

Plans for the future:

  1. Write more tests for every other source file (as applicable)
  2. Include a requests fixture for any web related data
  3. Add regression tests so further pull requests can be easily verified
PrestonHager commented 2 weeks ago

120 has been merged with a few conflicts, so conflicts were resolved.

Current there are only 13 unit tests.

3 tests fail because they expect utility functions to raise an error when bad data is put in (this shouldn't happen in the modules code, I think?) Another test fails because I'm not sure what the actual return value of session_key_to_path is supposed to be since the _path attribute wasn't in any of the session data when I ran the module's ingestor.

Suggestions for branch management with tests:

  1. Create a test, dev, or similar named branch
  2. Merge this PR into that instead of main
  3. Setup CI/CD to run whenever a PR is made from the test branch to the main branch
  4. Maybe setup CI/CD to be manually activated for PRs to test branch from any other branch

Additionally, I didn't notice a linting file; should we also integrate pylint or a similar library into the test suite?

br-g commented 1 week ago

Hello @PrestonHager, Thank you for this contribution!

When I run the tests locally, 4 of them (out of 9) are failing. Is it the same for you?

FAILED tests/unit/util/test_db.py::test_session_key_to_path - AssertionError: assert None == 'sessions/1'
FAILED tests/unit/util/test_misc.py::test_to_datetime - Failed: DID NOT RAISE <class 'ValueError'>
FAILED tests/unit/util/test_misc.py::test_to_timedelta - Failed: DID NOT RAISE <class 'ValueError'>
FAILED tests/unit/util/test_misc.py::test_add_timezone_info - AttributeError: 'int' object has no attribute 'replace'