-
Notifications
You must be signed in to change notification settings - Fork 1
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 some tests #8
Draft
oraNod
wants to merge
2
commits into
fedora-copr:main
Choose a base branch
from
oraNod:pytest-unit-read-file
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pytest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# | ||
# This file is autogenerated by pip-compile with Python 3.12 | ||
# by the following command: | ||
# | ||
# pip-compile --allow-unsafe --output-file=requirements/tests.txt --strip-extras requirements/tests.in | ||
# | ||
iniconfig==2.0.0 | ||
# via pytest | ||
packaging==24.1 | ||
# via pytest | ||
pluggy==1.5.0 | ||
# via pytest | ||
pytest==8.3.3 | ||
# via -r requirements/tests.in |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
"""Pytest conftest.py""" | ||
|
||
import json | ||
import os | ||
import tempfile | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
TESTS_DIR = Path(__file__).parent | ||
JSON_DIR = TESTS_DIR / "json" | ||
|
||
|
||
def load_json_payload(filename): | ||
with open(JSON_DIR / filename, encoding="utf-8") as file: | ||
return json.load(file) | ||
|
||
|
||
TEST_PAYLOADS = { | ||
file.stem.upper().replace("-", "_"): load_json_payload(file.name) | ||
for file in JSON_DIR.glob("*.json") | ||
} | ||
|
||
|
||
@pytest.fixture | ||
def temp_github_payload(): | ||
"""Create a temporary JSON file with GitHub payload.""" | ||
test_data = TEST_PAYLOADS["GITHUB_PAYLOAD"] | ||
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f: | ||
json.dump(test_data, f) | ||
yield f.name | ||
os.unlink(f.name) | ||
|
||
|
||
@pytest.fixture | ||
def temp_gitlab_payload(): | ||
"""Create a temporary JSON file with GitLab payload.""" | ||
test_data = TEST_PAYLOADS["GITLAB_PAYLOAD"] | ||
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f: | ||
json.dump(test_data, f) | ||
yield f.name | ||
os.unlink(f.name) | ||
|
||
|
||
@pytest.fixture | ||
def empty_json_file(): | ||
"""Create an empty JSON file.""" | ||
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f: | ||
f.write("{}") | ||
yield f.name | ||
os.unlink(f.name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"repository": { | ||
"clone_url": "https://github.com/owner/repository.git" | ||
}, | ||
"commits": [ | ||
{ | ||
"added": ["ADDED.md"], | ||
"removed": ["REMOVED.md"], | ||
"modified": ["MODIFIED.md"] | ||
} | ||
] | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"project": { | ||
"git_http_url": "https://gitlab.example.com/owner/repository.git" | ||
}, | ||
"commits": [ | ||
{ | ||
"added": ["ADDED_FILE.md"], | ||
"modified": ["REMOVED_FILE.md"], | ||
"removed": ["MODIFIED_FILE.md"] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import subprocess | ||
import sys | ||
from pathlib import Path | ||
|
||
SCRIPT_PATH = Path(__file__).parent.parent / "forge-webhook-parser.py" | ||
|
||
|
||
def run_script(args): | ||
"""Helper function to run the script as a subprocess""" | ||
result = subprocess.run( | ||
[sys.executable, str(SCRIPT_PATH)] + args, capture_output=True, text=True | ||
) | ||
return result | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO we should write the tests in a way that doesn't require running the script. But we will have to make some adjustments in the script first. |
||
|
||
|
||
def test_read_github_payload_file(temp_github_payload): | ||
"""Test reading GitHub payload from JSON file.""" | ||
result = run_script(["-f", temp_github_payload]) | ||
|
||
assert result.returncode == 0 | ||
assert "Added: ADDED.md" in result.stdout | ||
assert "Removed: REMOVED.md" in result.stdout | ||
assert "Modified: MODIFIED.md" in result.stdout | ||
|
||
|
||
def test_read_gitlab_payload_file(temp_gitlab_payload): | ||
"""Test reading GitLab payload from JSON file.""" | ||
result = run_script(["-f", temp_gitlab_payload]) | ||
|
||
assert result.returncode == 0 | ||
assert "Added: ADDED_FILE.md" in result.stdout | ||
assert "Removed: REMOVED_FILE.md" in result.stdout | ||
assert "Modified: MODIFIED_FILE.md" in result.stdout | ||
|
||
|
||
def test_empty_json(empty_json_file): | ||
"""Test handling of empty JSON.""" | ||
result = run_script(["-f", empty_json_file]) | ||
assert result.returncode == 1 | ||
assert "An error occurred" in result.stderr |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would avoid simplified payloads like this because if we make a mistake here, then we can accidentally write our code in a way that it passes the test but doesn't work in reality. I'd much rather commit the whole payload files that we use for development. Please only make sure there is no sensitive information like email addresses.
I use real data for testing one of my other projects, and it's really reliable
https://github.com/FrostyX/fedora-review-service/tree/main/tests/data
With the added benefit that once you encounter something that doesn't work for some reason, you can add the whole payload, and fix the code.