Skip to content

Commit

Permalink
- add pre-commit, pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
xNykram committed Mar 16, 2024
1 parent b79d827 commit 32dce24
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ jobs:
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
password: ${{ secrets.PYPI_API_TOKEN }}
23 changes: 23 additions & 0 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Run Tests

on: [push, pull_request]

jobs:
tests:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.12

- name: Install dependencies
run: |
pip install .
- name: Run pytest
run: pytest
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ youtube_search2.egg-info
.vscode
.pytest_cache
.coverage
.coverage-report
.coverage-report
25 changes: 25 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
repos:

# STANDARD HOOKS #
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-added-large-files
- id: debug-statements
- id: detect-private-key
- id: end-of-file-fixer
- id: forbid-new-submodules
- id: mixed-line-ending
- id: trailing-whitespace

# LINTER #
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.2.2
hooks:
- id: ruff

# CODE FORMATTER #
- repo: https://github.com/psf/black
rev: 24.2.0
hooks:
- id: black
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org/>
For more information, please refer to <http://unlicense.org/>
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ output: [{'id': 'jNQXAC9IVRw', 'thumbnails': ['https://i.ytimg.com/vi/jNQXAC9IVR
- add types,
- fix & write more tests.

Please report any suggestions and issues [here](https://github.com/xNykram/youtube_search2/issues).
Please report any suggestions and issues [here](https://github.com/xNykram/youtube_search2/issues).
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ omit = ["tests/*", "__init__.py"]

[tool.coverage.report]
show_missing = true
fail_under = 75
fail_under = 75
2 changes: 1 addition & 1 deletion src/ytsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,5 @@ def _prepare_data(self, response):
searched_obj = data["contents"]["twoColumnSearchResultsRenderer"][
"primaryContents"
]["sectionListRenderer"]["contents"]

return searched_obj
14 changes: 10 additions & 4 deletions tests/test_search.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
import pytest
from src.ytsearch import YTSearch
import src
from requests import RequestException


@pytest.fixture
def yt_search():
return YTSearch()


def test_search_by_url_valid(yt_search):
url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
result = yt_search.search_by_url(url)
assert result["id"] == "dQw4w9WgXcQ"


def test_search_by_url_invalid(yt_search):
url = "invalid_url"
with pytest.raises(ValueError):
yt_search.search_by_url(url)


def test_search_by_term(yt_search):
term = "openai"
result = yt_search.search_by_term(term)
assert len(result) > 0


def test_search_by_term_not_finished_loop(yt_search, mocker):
mocker.patch('src.ytsearch.YTSearch._prepare_data', return_value=[])
mocker.patch("src.ytsearch.YTSearch._prepare_data", return_value=[])
term = "openai"
result = yt_search.search_by_term(term)

assert result == []


def test_search_by_term_no_results(yt_search):
term = "#########################"
result = yt_search.search_by_term(term)
Expand All @@ -46,4 +52,4 @@ def test_search_by_url_exception(yt_search):
with pytest.raises(RequestException) as exc_info:
url = "https://wrong_url"
yt_search.search_by_url(url)
assert str(exc_info.value) == "Failed to fetch data from YouTube."
assert str(exc_info.value) == "Failed to fetch data from YouTube."

0 comments on commit 32dce24

Please sign in to comment.