Skip to content

Commit

Permalink
Merge pull request #23 from ivansaul/feature/implement-tests
Browse files Browse the repository at this point in the history
feat: add basic unit tests
  • Loading branch information
ivansaul authored Oct 10, 2024
2 parents b85a6f5 + da5640e commit 60300ac
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ on:
push:
branches:
- master
paths-ignore:
- '*.md'

jobs:
release:
Expand Down
50 changes: 50 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Test

on:
push:
paths-ignore:
- '*.md'

branches:
- develop

pull_request:
paths-ignore:
- '*.md'

jobs:
test:
strategy:
matrix:
python-version: ["3.10"]
os: [ubuntu-latest, macos-latest, windows-latest]

runs-on: ${{ matrix.os }}

steps:
- name: Setup | Checkout Repository
uses: actions/checkout@v4

- name: Install Poetry
shell: bash
run: pipx install poetry

- name: Install FFmpeg
uses: AnimMouse/setup-ffmpeg@v1
with:
version: master

- name: Setup | Install Python
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: 'poetry'

- name: Install Dependencies
run: poetry install --with=dev

- name: Lint with Ruff
run: poetry run ruff check --output-format=github .

- name: Run Tests with Pytest
run: poetry run pytest
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Custom
tmp/
*.mp4
!src/tests/resources/sample*.mp4
.DS_Store

# Byte-compiled / optimized / DLL files
Expand Down
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"cSpell.words": [
"acodec",
"autouse",
"ffprobe",
"github",
"ivansaul",
Expand Down
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,8 @@ commit_version_number = true
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.ruff.lint]
preview = true
select = ['E', 'F', 'W', 'I']
ignore = ["E501"]
Binary file added src/tests/resources/sample_video_1.mp4
Binary file not shown.
Binary file added src/tests/resources/sample_video_2.mp4
Binary file not shown.
117 changes: 117 additions & 0 deletions src/tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import os
from pathlib import Path

import pytest
from typer.testing import CliRunner

from vidpack.cli import app

runner = CliRunner()

PROGRESS_COMPLETION_OUTPUT = "100% 0:00:00"

TEST_VIDEOS_PATH: Path = Path(__file__).parent / "resources"
INPUT: str = str(TEST_VIDEOS_PATH / "sample_video_1.mp4")
INPUT_2: str = str(TEST_VIDEOS_PATH / "sample_video_2.mp4")
OUTPUT: str = str(TEST_VIDEOS_PATH / "output.mp4")


@pytest.fixture(scope="function", autouse=True)
def setup_teardown():
"""
Fixture to setup and teardown the test environment.
"""
yield
for file in TEST_VIDEOS_PATH.glob("*_compressed.mp4"):
os.remove(file)


def test_single_file_compression_with_default_options():
"""
Test single file compression with default options.
Expected results:
- Exit code: 0
- Progress completion output: "100% 0:00:00"
"""
result = runner.invoke(app, [INPUT])
assert result.exit_code == 0, result.stdout
assert PROGRESS_COMPLETION_OUTPUT in result.stdout


@pytest.mark.parametrize("codec", ["h264", "libx265"])
def test_single_file_compression(codec):
"""
Test single file compression with different video codecs.
Expected results:
- Exit code: 0
- Progress completion output: "100% 0:00:00"
"""
result = runner.invoke(app, [INPUT, "--codec", codec])
assert result.exit_code == 0, result.stdout
assert PROGRESS_COMPLETION_OUTPUT in result.stdout


@pytest.mark.parametrize("codec", ["libx267", "123"])
def test_single_file_compression_with_invalid_codec(codec):
"""
Test single file compression with invalid video codec.
Expected results:
- Exit code: 2
"""
result = runner.invoke(app, [INPUT, "--codec", codec])
assert result.exit_code == 2, result.stdout


def test_single_file_compression_with_quality_option():
"""
Test single file compression with quality option.
Expected results:
- Exit code: 0
- Progress completion output: "100% 0:00:00"
"""
result = runner.invoke(app, [INPUT, "--quality", "50"])
assert result.exit_code == 0, result.stdout
assert PROGRESS_COMPLETION_OUTPUT in result.stdout


def test_single_file_compression_with_overwrite_option():
"""
Test single file compression with overwrite option.
Expected results:
- Exit code: 0
- Progress completion output: "100% 0:00:00"
"""
result = runner.invoke(app, [INPUT, "--overwrite"])
assert result.exit_code == 0, result.stdout
assert PROGRESS_COMPLETION_OUTPUT in result.stdout


def test_single_file_compression_with_delete_original_option():
"""
Test single file compression with delete original option.
Expected results:
- Exit code: 0
- Progress completion output: "100% 0:00:00"
"""
result = runner.invoke(app, [INPUT_2, "--delete-original"])
assert result.exit_code == 0, result.stdout
assert PROGRESS_COMPLETION_OUTPUT in result.stdout


def test_single_file_compression_with_output_option():
"""
Test single file compression with output option.
Expected results:
- Exit code: 0
- Progress completion output: "100% 0:00:00"
"""
result = runner.invoke(app, [INPUT, "--output", OUTPUT])
assert result.exit_code == 0, result.stdout
assert PROGRESS_COMPLETION_OUTPUT in result.stdout
14 changes: 7 additions & 7 deletions src/tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import subprocess
from pathlib import Path
from unittest.mock import patch

import pytest
Expand All @@ -9,19 +10,18 @@
@pytest.mark.parametrize(
"file, prefix, suffix, expected_output",
[
("/path/to/file.txt", "", "", "/path/to/file.txt"),
("file.txt", "prefix_", "", "prefix_file.txt"),
("/path/to/file.txt", "", "_suffix", "/path/to/file_suffix.txt"),
("/file.txt", "prefix_", "_suffix", "/prefix_file_suffix.txt"),
(Path("/path/to/file.txt"), "", "", Path("/path/to/file.txt")),
(Path("file.txt"), "prefix_", "", Path("prefix_file.txt")),
(Path("/path/to/file.txt"), "", "_suffix", Path("/path/to/file_suffix.txt")),
(Path("/file.txt"), "prefix_", "_suffix", Path("/prefix_file_suffix.txt")),
],
)
def test_add_affixes(file, prefix, suffix, expected_output):
"""
Test add_affixes function with different combinations of prefix and suffix.
"""

result = add_affixes(file=file, prefix=prefix, suffix=suffix)
assert result == expected_output
result = add_affixes(file=str(file), prefix=prefix, suffix=suffix)
assert result == str(expected_output)


def test_ffmpeg_installed(monkeypatch):
Expand Down

0 comments on commit 60300ac

Please sign in to comment.