Skip to content

Commit

Permalink
Add python tests
Browse files Browse the repository at this point in the history
  • Loading branch information
philnewm committed Aug 22, 2024
1 parent 6b6515d commit 51c7dec
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 12 deletions.
41 changes: 29 additions & 12 deletions fetch_github_data.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
"""
This script is written in a certain - maybe even unconventional way - by intention.
It's supposed to be run from comamndline right away to be used in a github action workflow yaml file.
Additonally it's test suite relies mainly on putest and therefore the functions need to be importable to the pytest script.
"""

import argparse
import json
import subprocess

repo_name = None
parsed_args = None

def parse_arguments():
"""Parse command-line arguments and store them in a global variable."""
global parsed_args
if parsed_args is None:
parser = argparse.ArgumentParser(description="A python script to convert GitHub PR information to a more simple format.")
parser.add_argument("repo", type=str, help="Repository name consisting of 'repo-owner/repo-name'")
parser.add_argument("query_parameters", type=str, help="Keys to query for.")
parser.add_argument("date", type=str, default="2024-07-08T09:48:33Z", help="Latest release date.")
parsed_args = parser.parse_args()


def get_inputs():
"""Get terminal parameters.
"""Get the parsed command-line arguments.
Returns:
tuple(str): Parameters as string tuple.
"""

parser = argparse.ArgumentParser(description="A python script to convert github pr information to a more simple format.")
parser.add_argument("repo", type=str, help="Repository name consisting of 'repo-owner/repo-name'")
parser.add_argument('query_parameters', type=str, help='Keys to query for.')
parser.add_argument('date', type=str, default="2024-07-08T09:48:33Z", help='Latest release date.')
args = parser.parse_args()

repo_name = args.repo
query_tags = args.query_parameters.split(',')
latest_release_date = args.date
if parsed_args is None:
parse_arguments()

repo_name = parsed_args.repo
query_tags = parsed_args.query_parameters.split(',')
latest_release_date = parsed_args.date

return repo_name, query_tags, latest_release_date

Expand Down Expand Up @@ -80,7 +94,7 @@ def prepare_changelog_markdown():
for pr in pr_query:
# get all label names in a list
pr_label_list = [label["name"] for label in pr["labels"]]
fitlered_label = list(set(label_list).intersection(pr_label_list))[0] # any(label in label_list for label in pr_label_list)
fitlered_label = list(set(label_list).intersection(pr_label_list))[0]

if fitlered_label:
change_list = get_changelog(pr_data=pr["body"], changelog_start="## Changes")
Expand Down Expand Up @@ -125,3 +139,6 @@ def get_version_increment():
return "minor"
if label.lower() in patch_bump_label_list:
return "patch"

if __name__ == "__main__":
parse_arguments()
64 changes: 64 additions & 0 deletions tests/test_fetch_github_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

import argparse
import json
from unittest import mock
import os
import sys

from unittest.mock import patch

# Add the parent directory of 'tests' to the system path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
import fetch_github_data

# Mock data to return for your tests
mock_repo_name = 'ynput/ayon-addon-action-testing'
mock_query_parameters = 'title,body,labels'
mock_date = '2024-07-08T09:48:33Z'
mock_labels_output = ['bug', 'enhancement']

# Mocking the subprocess to prevent actual GitHub CLI calls
def mock_get_raw_output(repo_name, query_tags, latest_release_date):
return [
{
"body": "# Bug Fix PR Template\r\n\r\n[ x] Feature/Enhancement<br>\r\n[ ] Bugfix<br>\r\n[ ] Documentation update<br>\r\n\r\n## Summary\r\n\r\n<!--Provide a concise description of your changes and implementation.-->\r\n\r\n## Root Cause Analysis\r\n\r\n[Issue Link](https://github.com/ynput/ci-testing/blob/develop/.github/ISSUE_TEMPLATE/bug_report.yml)<br>\r\n<!--Detail the reason for your change and which benefits result from it.-->\r\n\r\n## Changes\r\n\r\n<!--Outline the changes made in a list.-->\r\n* Add more test\r\n* Was very important\r\n* Needed to add this here\r\n\r\n## Testing Strategy\r\n\r\n<!--Explain how the fix has been tested to ensure the bug is resolved without introducing new issues.-->\r\n\r\n## Checklist\r\n\r\n* [ x] The fix has been locally tested\r\n* [ x] New unit tests have been added to prevent future regressions\r\n* [ x] The documentation has been updated if necessary\r\n\r\n## Additional Notes\r\n\r\n<!--Any further information needed to understand the fix or its impact.-->",
"labels": [
{
"id": "LA_kwDOMje8_88AAAABtOJ1Ig",
"name": "enhancement",
"description": "New feature or request",
"color": "b9f29d"
}
],
"title": "Add more data"
},
{
"body": "# Date file added\r\n\r\n## Summary\r\n\r\nSome awesome summary going on right here.\r\n\r\n## Root Cause Analysis\r\n\r\n[Issue Link](https://github.com/ynput/ci-testing/blob/develop/.github/ISSUE_TEMPLATE/bug_report.yml)<br>\r\nDate file so absolutely needed.\r\n\r\n## Changes\r\n\r\n<!--Outline the changes made in a list.-->\r\n* Run a command\r\n* Pipe its output to a text file\r\n* Commit dem stuff\r\n\r\n## Testing Strategy\r\n\r\nnahhhhh\r\n\r\n## Checklist\r\n\r\n* [x] The fix has been locally tested\r\n* [x] New unit tests have been added to prevent future regressions\r\n* [x] The documentation has been updated if necessary\r\n\r\n## Additional Notes\r\n\r\nNop",
"labels": [
{
"id": "LA_kwDOMje8_88AAAABtOJ1Gw",
"name": "bug",
"description": "Something isn't working",
"color": "ff9195"
}
],
"title": "Add date file"
}
]

@patch('argparse.ArgumentParser.parse_args')
@patch('fetch_github_data.get_raw_output', side_effect=mock_get_raw_output)
def test_get_labels(mock_parse_args, mock_get_raw_output):
# Configure the mock to return desired arguments
mock_parse_args.return_value = argparse.Namespace(
repo=mock_repo_name,
query_parameters=mock_query_parameters,
date=mock_date
)

fetch_github_data.parse_arguments()

labels = fetch_github_data.get_labels()

assert set(json.loads(labels)) == set(mock_labels_output)

0 comments on commit 51c7dec

Please sign in to comment.