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

Adding unit tests for utilities.py #415

Merged
merged 26 commits into from
Apr 4, 2023
Merged

Adding unit tests for utilities.py #415

merged 26 commits into from
Apr 4, 2023

Conversation

PhillypHenning
Copy link
Contributor

@PhillypHenning PhillypHenning commented Mar 15, 2023

Adds to #286

Description

Adding unit tests for the utilities.py file

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

It is the testing 👍
python3 -m unittest scripts/tests/unit/test_utilities.py

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation

@runforesight
Copy link

runforesight bot commented Mar 15, 2023

Foresight Summary

    
Major Impacts

CI duration(37 seconds) has decreased 2 minutes 30 seconds compared to main branch avg(3 minutes 7 seconds).
View More Details

✅  CI workflow has finished in 37 seconds (2 minutes 30 seconds less than main branch avg.) and finished at 4th Apr, 2023.


Job Failed Steps Tests
tests-unit -     🔗  N/A See Details
lint-pylint -     🔗  N/A See Details
lint-black -     🔗  N/A See Details

🔎 See details on Foresight

*You can configure Foresight comments in your organization settings page.

Copy link
Member

@arm4b arm4b left a comment

Choose a reason for hiding this comment

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

Nice!

Yes, these are really unit tests as you're testing a specific function.

Move them under the /unit/ dir.
Also, rename utilities_test.py -> test_utilities.py.

See https://github.com/bitovi/bitops/tree/main/scripts/tests/unit existing structure.

@PhillypHenning
Copy link
Contributor Author

So the BITOPS_RUN_MODE = 'testing' doesn't work but it does need to.
I'm not exactly sure how to accomplish this yet, but I also spent 2 minutes thinking about it.

Other then that, these were created using OpenAI. The prompt goes as followed:

Let's define a persona called PythonTestWriter. The PythonTestWriter is an expert in python. The PythonTestWriter seeks to create comprehensive unit tests for functions. The PythonTestWriter will prioritize using python default libraries such as unittest.

Using the PythonTestWriter, please write unit tests for the following function:
"""
FUNCTION
"""

@PhillypHenning
Copy link
Contributor Author

These still need to be cleaned up. The first one that should be cleaned up is the run_cmd as I believe it will close a few of the handle_hook test failures

Ran 9 tests in 0.019s

FAILED (failures=4)

@PhillypHenning PhillypHenning changed the title Adding OpenAI generated functional level tests Adding OpenAI generated unit tests for utilities.py Mar 16, 2023
@PhillypHenning PhillypHenning changed the title Adding OpenAI generated unit tests for utilities.py Adding unit tests for utilities.py Mar 17, 2023
scripts/tests/unit/test_utilities.py Outdated Show resolved Hide resolved
scripts/plugins/utilities.py Outdated Show resolved Hide resolved
@arm4b arm4b added tests enhancement ✨ New feature or request labels Mar 18, 2023
@arm4b
Copy link
Member

arm4b commented Mar 18, 2023

How Has This Been Tested?

It is the testing 👍
python3 -m unittest scripts/tests/unit/test_utilities.py

See https://github.com/bitovi/bitops/tree/main/scripts/tests#running-the-tests which gives an example command with all the configuration/environment.

tox.ini Outdated
Comment on lines 42 to 43
setenv =
BITOPS_LOGGING_FILENAME=
Copy link
Member

Choose a reason for hiding this comment

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

Unit Tests ideally shouldn't write to a file for logging.

I saw you tried to disable the logging in another PR, but this should do it in a more native way.

This fixes the Unit Tests run in this PR without relying on a chain of PRs.

@arm4b arm4b added this to the v2.5.0 milestone Mar 28, 2023
Copy link
Member

@arm4b arm4b left a comment

Choose a reason for hiding this comment

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

Allowing to test our stuff triggered a lot of the changes.

As we're moving the exception handling outside of the function, - it's better to also log errors outside of the function.

Example pattern:

# Function definition should raise an exception
def example():
  if ok:
    do thing
    logger.debug("Some debugging information in the function is OK")
  else:
    raise ExampleException("Custom error here goes outside as an Exception")

# Usage of the function in the main code
# should catch the exception
# and handle the error logging and return codes, eg. full error handling. 
try:
  example()
except ExampleException as e:
  # error handling is moved outside of the function
  logger.error("An error occurred while trying to run an example: " + e)
  sys.exit(1)

scripts/plugins/utilities.py Outdated Show resolved Hide resolved
scripts/plugins/utilities.py Outdated Show resolved Hide resolved
scripts/plugins/install_plugins.py Outdated Show resolved Hide resolved
scripts/plugins/deploy_plugins.py Outdated Show resolved Hide resolved
scripts/plugins/config/parser.py Outdated Show resolved Hide resolved
scripts/plugins/utilities.py Outdated Show resolved Hide resolved
scripts/plugins/utilities.py Outdated Show resolved Hide resolved
Copy link
Member

@arm4b arm4b left a comment

Choose a reason for hiding this comment

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

Updated it to be ready for merge.
Many things needed more work after the AI touch.

@PhillypHenning Let me know in case of any questions and let's merge it.

@arm4b arm4b mentioned this pull request Mar 30, 2023
9 tasks
result = run_cmd([plugin_install_language, plugin_install_script_path])
except Exception as e:
logger.error(
f"Failed to run plugin install script: [{plugin_install_script_path}]. Error: [{e}]"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Error message for deploy and install should be similar.

Copy link
Member

@arm4b arm4b Mar 31, 2023

Choose a reason for hiding this comment

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

This one comes from the Exception, eg. when something wrong happens on the Popen system side like a timeout, permissions issue, etc.

What would be the more expected error message to you?
Feel free to push directly to the branch or you can also propose a code suggestion (https://egghead.io/lessons/github-add-suggestions-in-a-github-pr-review) for better clarity.

f"Required config file was not found. \
To fix this please add the following file: [{e.filename}]"
)
sys.exit(2)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

run_cmd try catch blocks exit with 1xx, should we consider making this a 1xx exit as well? What denotes a 2 vs 101

Copy link
Member

@arm4b arm4b Mar 31, 2023

Choose a reason for hiding this comment

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

That's how you added it here initially: eaeba19#diff-ec9057b7cfb0579064835fe003bf275eb479b2910d976987366051d5d7593c9dR24, so that's more a question to you.

There is no difference to me, unless we're returning the non-zero error code.

Copy link
Member

@arm4b arm4b Apr 4, 2023

Choose a reason for hiding this comment

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

Updated in 92ffbd1

Suggested change
sys.exit(2)
sys.exit(101)

@arm4b arm4b merged commit 9c19229 into main Apr 4, 2023
@arm4b arm4b deleted the bitops_utest branch April 4, 2023 14:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement ✨ New feature or request tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants