Skip to content

Commit

Permalink
Rearrange project structure, add tests for upload_image
Browse files Browse the repository at this point in the history
  • Loading branch information
wwakabobik committed Aug 19, 2024
1 parent cee07b6 commit a223c00
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 11 deletions.
11 changes: 7 additions & 4 deletions .github/workflows/linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ jobs:
pip install black
pip install types-xmltodict
pip install types-requests
pip install pytest
pip install faker
- name: Analysing the code with pylint
id: pylint
continue-on-error: true
run: |
changed_files=$(git diff --diff-filter=d --name-only $(git merge-base HEAD origin/master) HEAD | grep '\.py$') || true
echo $changed_files
if [ -n "$changed_files" ]; then
PYTHONPATH=. pylint $changed_files
PYTHONPATH=$PYTHONPATH:. pylint $changed_files
else
echo "No files changed, passing by"
exit 0
Expand All @@ -52,7 +54,8 @@ jobs:
changed_files=$(git diff --diff-filter=d --name-only $(git merge-base HEAD origin/master) HEAD | grep '\.py$') || true
echo $changed_files
if [ -n "$changed_files" ]; then
PYTHONPATH=. mypy $changed_files --install-types --non-interactive --ignore-missing-imports
mkdir -p .mypy_cache
PYTHONPATH=$PYTHONPATH:. mypy $changed_files --install-types --non-interactive --ignore-missing-imports
else
echo "No files changed, passing by"
exit 0
Expand All @@ -64,7 +67,7 @@ jobs:
changed_files=$(git diff --diff-filter=d --name-only $(git merge-base HEAD origin/master) HEAD | grep '\.py$') || true
echo $changed_files
if [ -n "$changed_files" ]; then
PYTHONPATH=. flake8 $changed_files
PYTHONPATH=$PYTHONPATH:. flake8 $changed_files
else
echo "No files changed, passing by"
exit 0
Expand All @@ -76,7 +79,7 @@ jobs:
changed_files=$(git diff --diff-filter=d --name-only $(git merge-base HEAD origin/master) HEAD | grep '\.py$') || true
echo $changed_files
if [ -n "$changed_files" ]; then
PYTHONPATH=. black --diff --check --color $changed_files
PYTHONPATH=$PYTHONPATH:. black --diff --check --color $changed_files
else
echo "No files changed, passing by"
exit 0
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/master_linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ jobs:
changed_files=$(git diff --diff-filter=d --name-only $(git merge-base HEAD origin/master) HEAD | grep '\.py$') || true
echo $changed_files
if [ -n "$changed_files" ]; then
mkdir -p .mypy_cache
PYTHONPATH=. mypy $changed_files --install-types --non-interactive --ignore-missing-imports
else
echo "No files changed, passing by"
Expand Down
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""testrail_api_reporter tests package"""
Binary file added tests/assets/test_image.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/assets/test_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""testrail_api_reporter tests for utils"""
19 changes: 12 additions & 7 deletions tests/utils/test_reporter_utils_upload_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,69 @@

from os import getcwd, getenv
from random import choice
from unittest.mock import patch, Mock

from faker import Faker
from pytest import raises as pytest_raises
from unittest.mock import patch, Mock

from testrail_api_reporter.utils.reporter_utils import upload_image


test_filename = choice((f"{getcwd()}/tests/assets/test_image.png", f"{getcwd()}/tests/assets/test_image.jpeg"))

def test_upload_image_mock_success():
"""Test success image upload (mock)"""
with patch('requests.post') as mock_post:
with patch("requests.post") as mock_post:
faker = Faker()
url = faker.image_url()
thumb_url = faker.image_url()
mock_response = Mock()
mock_response.json.return_value = {
"image": {"url": url, "thumb": {"url": thumb_url}}
}
mock_response.json.return_value = {"image": {"url": url, "thumb": {"url": thumb_url}}}
mock_post.return_value = mock_response
result = upload_image(test_filename, "test_api_token")
assert result == {"image": url, "thumb": thumb_url}


def test_upload_image_mock_nonexistent_file():
"""Test response for not existent file (mock)"""
with pytest_raises(FileNotFoundError):
faker = Faker()
upload_image(faker.file_path(extension=choice(("png", "jpg", "jpeg"))), faker.password())


def test_upload_image_mock_invalid_token():
"""Test against invalid token (mock)"""
with patch('requests.post') as mock_post:
with patch("requests.post") as mock_post:
mock_response = Mock()
mock_response.json.return_value = {"error": "Invalid API token"}
mock_post.return_value = mock_response
with pytest_raises(KeyError):
upload_image(test_filename, Faker().password())


def test_upload_image_mock_api_error():
"""Test against API error (invalid response/request) (mock)"""
with patch('requests.post') as mock_post:
with patch("requests.post") as mock_post:
mock_response = Mock()
mock_response.json.return_value = {"error": "Upload error"}
mock_post.return_value = mock_response
with pytest_raises(KeyError):
upload_image(test_filename, Faker().password())


def test_upload_image_live_success():
"""Test success image upload"""
result = upload_image(test_filename, getenv("FREEIMAGEHOST_API_KEY"))
assert "image" in result
assert "thumb" in result


def test_upload_image_live_nonexistent_file():
"""Test response for not existent file"""
with pytest_raises(FileNotFoundError):
upload_image(Faker().file_path(extension=choice(("png", "jpg", "jpeg"))), getenv("FREEIMAGEHOST_API_KEY"))


def test_upload_image_live_invalid_token():
"""Test against invalid token"""
with pytest_raises(KeyError):
Expand Down

0 comments on commit a223c00

Please sign in to comment.