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 some tests #8

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements/tests.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest
14 changes: 14 additions & 0 deletions requirements/tests.txt
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
51 changes: 51 additions & 0 deletions tests/conftest.py
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)
12 changes: 12 additions & 0 deletions tests/json/github-payload.json
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"]
}
]
}
Copy link
Member

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.

12 changes: 12 additions & 0 deletions tests/json/gitlab-payload.json
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"]
}
]
}
40 changes: 40 additions & 0 deletions tests/test_webhook_parser.py
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
Copy link
Member

Choose a reason for hiding this comment

The 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