-
Notifications
You must be signed in to change notification settings - Fork 3
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
Sweep (slow): Replace pytest with flask test and improve testing setup #107
Comments
💎 Sweep Pro: I'm creating this ticket using GPT-4. You have unlimited GPT-4 tickets.
* Install Sweep Configs: [Pull Request](https://github.com//pull/104)
Actions (click)
Step 1: 🔎 SearchingI found the following snippets in your repository. I will now analyze these snippets and come up with a plan. Some code snippets I looked at (click to expand). If some file is missing from here, you can mention the path in the ticket description.Lines 1 to 24 in 6ba7139
slack-bot/src/services/slack_service.py Lines 1 to 361 in 6ba7139
slack-bot/src/services/openai_service.py Lines 10 to 126 in 6ba7139
Lines 1 to 4 in 6ba7139
I also found the following external resources that might be helpful:Summaries of links found in the content: https://github.com/UpMortem/slack-bot/actions/runs/6227988039/job/16903806905?pr=83#step:5:13)6: The page is a GitHub repository for a Slack bot called "slack-bot". The user is requesting to add a unit test framework and a "test" command to the bot so that they can run tests using the command "flask test". They want to prevent errors such as "No such command 'test'" and ensure that the OPENAI_API_KEY environment variable does not need to be set to run the tests. The user also wants to prevent errors related to importing modules and running pytest on the repository. They provide code snippets and instructions on how to modify the build-push.yml file to include the "flask test" command and set the FLASK_APP environment variable. They also mention an error related to importing 'src.index' and a ModuleNotFoundError for 'lib'. https://github.com/UpMortem/slack-bot/actions/runs/6227988039/job/16903806905?pr=83#step:5:21)s: The page is a GitHub repository for a Slack bot called "slack-bot". The user is requesting to add a unit test framework and a "test" command to the bot so that they can run tests using the command "flask test". They want to prevent errors such as "No such command 'test'" and ensure that the OPENAI_API_KEY environment variable does not need to be set to run the tests. The user also wants to prevent errors related to importing modules and running pytest on the repository. They provide code snippets and instructions on how to modify the build-push.yml file to include the "flask test" command and set the FLASK_APP environment variable. They also mention an error related to importing 'src.index' and a ModuleNotFoundError for 'lib'. I also found some related docs:
Step 2: ⌨️ Coding
• Import the necessary modules at the top of the file, including unittest and the api_service module from the src/services directory. • Define a new class called TestApiService that inherits from unittest.TestCase. • Inside this class, define a new method for each function in the api_service module. Each method should test a specific function in the api_service module. • Use the unittest.mock.patch decorator to mock the OpenAI API in the tests.
• Import the necessary modules at the top of the file, including unittest and the openai_service module from the src/services directory. • Define a new class called TestOpenaiService that inherits from unittest.TestCase. • Inside this class, define a new method for each function in the openai_service module. Each method should test a specific function in the openai_service module. • Use the unittest.mock.patch decorator to mock the OpenAI API in the tests.
• Import the necessary modules at the top of the file, including unittest and the slack_service module from the src/services directory. • Define a new class called TestSlackService that inherits from unittest.TestCase. • Inside this class, define a new method for each function in the slack_service module. Each method should test a specific function in the slack_service module. • Use the unittest.mock.patch decorator to mock the Slack API in the tests.
• Import the unittest module at the top of the file. • Define a new command for the Flask application called "test". This command should discover and run all tests in the src/tests directory. Step 3: 🔁 Code ReviewWorking on it... 🎉 Latest improvements to Sweep:
💡 To recreate the pull request edit the issue title or description. To tweak the pull request, leave a comment on the pull request. |
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
❌ FailedThe text was updated successfully, but these errors were encountered: