Skip to content

Sweep (slow): Replace pytest with flask test and improve testing setup #107

@nlake44

Description

@nlake44

Add tests for each file in src/services directory and add framework so I can run "flask test" to execute the tests.

Prevent this from happening:
(myvenv) nchohan slack-bot % flask test
Usage: flask [OPTIONS] COMMAND [ARGS]...
Try 'flask --help' for help.

Error: No such command 'test'.

Also: Make it so that the OPENAI_API_KEY environment variable does not have to be set to run unit tests.

Also prevent the following error from happening:
(myvenv) nchohan slack-bot % flask test
Traceback (most recent call last):
File "/Users/nchohan/Dev/upmortem/slack-bot/myvenv/bin/flask", line 8, in
sys.exit(main())
^^^^^^
File "/Users/nchohan/Dev/upmortem/slack-bot/myvenv/lib/python3.11/site-packages/flask/cli.py", line 1047, in main
cli.main()
File "/Users/nchohan/Dev/upmortem/slack-bot/myvenv/lib/python3.11/site-packages/click/core.py", line 1078, in main
rv = self.invoke(ctx)
^^^^^^^^^^^^^^^^
File "/Users/nchohan/Dev/upmortem/slack-bot/myvenv/lib/python3.11/site-packages/click/core.py", line 1688, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/nchohan/Dev/upmortem/slack-bot/myvenv/lib/python3.11/site-packages/click/core.py", line 1434, in invoke
return ctx.invoke(self.callback, **ctx.params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/nchohan/Dev/upmortem/slack-bot/myvenv/lib/python3.11/site-packages/click/core.py", line 783, in invoke
return __callback(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/nchohan/Dev/upmortem/slack-bot/myvenv/lib/python3.11/site-packages/click/decorators.py", line 33, in new_func
return f(get_current_context(), *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/nchohan/Dev/upmortem/slack-bot/myvenv/lib/python3.11/site-packages/flask/cli.py", line 357, in decorator
return __ctx.invoke(f, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/nchohan/Dev/upmortem/slack-bot/myvenv/lib/python3.11/site-packages/click/core.py", line 783, in invoke
return __callback(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/nchohan/Dev/upmortem/slack-bot/src/index.py", line 33, in test
tests = unittest.TestLoader().discover("tests")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/unittest/loader.py", line 304, in discover
os.path.dirname((the_module.file)))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "", line 152, in dirname
TypeError: expected str, bytes or os.PathLike object, not NoneType

And also prevent this issue from happening:
ImportError: Failed to import test module: tests.test_openai_service
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/unittest/loader.py", line 407, in _find_test_path
module = self._get_module_from_name(name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/unittest/loader.py", line 350, in _get_module_from_name
import(name)
File "/Users/nchohan/Dev/upmortem/slack-bot/src/tests/test_openai_service.py", line 4, in
from src.services import openai_service
ModuleNotFoundError: No module named 'src'

Also prevent this when pytest is run on the repo:
============================= test session starts ==============================
platform linux -- Python 3.10.13, pytest-7.4.2, pluggy-1.3.0
rootdir: /home/runner/work/slack-bot/slack-bot
collected 1 item / 1 error

==================================== ERRORS ====================================
______________ ERROR collecting src/tests/test_openai_service.py _______________
ImportError while importing test module '/home/runner/work/slack-bot/slack-bot/src/tests/test_openai_service.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/importlib/init.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
src/tests/test_openai_service.py:4: in
from src.services import openai_service
E ModuleNotFoundError: No module named 'src'
=========================== short test summary info ============================
ERROR src/tests/test_openai_service.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.20s ===============================
Error: Process completed with exit code 2.

Modify .github/workflows/build-push.yml with contents:

Change pytest to "flask test"
Prepend FLASK_APP=src/index.py before the flask test command

Prevent this from happening: test build failed with the following errors:

2023-09-25T23:45:01.8827513Z ##[group]Run export FLASK_APP=src/index.py && flask test
2023-09-25T23:45:01.8895349Z ##[endgroup]
2023-09-25T23:45:02.2699042Z Error: While importing 'src.index', an ImportError was raised:
2023-09-25T23:45:02.2699669Z
2023-09-25T23:45:02.2700551Z Traceback (most recent call last):
2023-09-25T23:45:02.2706991Z File "/opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/site-packages/flask/cli.py", line 218, in locate_app
2023-09-25T23:45:02.2707652Z import(module_name)
2023-09-25T23:45:02.2708403Z File "/home/runner/work/slack-bot/slack-bot/src/index.py", line 7, in
2023-09-25T23:45:02.2709183Z from lib.guards import shared_secret_guard
2023-09-25T23:45:02.2709723Z ModuleNotFoundError: No module named 'lib'
2023-09-25T23:45:02.2709912Z
2023-09-25T23:45:02.2709922Z
2023-09-25T23:45:02.2710077Z Usage: flask [OPTIONS] COMMAND [ARGS]...
2023-09-25T23:45:02.2710387Z Try 'flask --help' for help.
2023-09-25T23:45:02.2710539Z
2023-09-25T23:45:02.2713606Z Error: No such command 'test'.

Checklist
  • src/tests/test_api_service.py ❌ Failed
  • src/tests/test_openai_service.py
  • src/tests/test_slack_service.py
  • src/index.py

Metadata

Metadata

Assignees

No one assigned

    Labels

    sweepAssigns Sweep to an issue or pull request.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions