Skip to content

Commit

Permalink
chore: mock tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravencentric committed Jun 26, 2024
1 parent 9710eb4 commit 8498406
Show file tree
Hide file tree
Showing 29 changed files with 249 additions and 28 deletions.
16 changes: 15 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pytest = "^8.1.1"
pytest-asyncio = "^0.23.5.post1"
pre-commit = "^3.7.0"
coverage = "^7.5.4"
respx = "^0.21.1"
types-beautifulsoup4 = "^4.12.0.20240229"
types-xmltodict = "^0.13.0.3"

Expand All @@ -63,6 +64,7 @@ fixable = ["ALL"]
[tool.mypy]
strict = true
pretty = true
exclude = "tests/"

[[tool.mypy.overrides]]
module = ["torf"]
Expand Down
Binary file not shown.
Binary file added tests/__responses__/1422797
Binary file not shown.
Binary file added tests/__responses__/1422797.torrent
Binary file not shown.
Binary file added tests/__responses__/1544043
Binary file not shown.
Binary file added tests/__responses__/1544043.torrent
Binary file not shown.
Binary file added tests/__responses__/1586776
Binary file not shown.
Binary file added tests/__responses__/1586776.torrent
Binary file not shown.
Binary file added tests/__responses__/1694824
Binary file not shown.
Binary file added tests/__responses__/1694824.torrent
Binary file not shown.
Binary file added tests/__responses__/1755409
Binary file not shown.
Binary file added tests/__responses__/1755409.torrent
Binary file not shown.
Binary file added tests/__responses__/1765655
Binary file not shown.
Binary file added tests/__responses__/1765655.torrent
Binary file not shown.
Binary file added tests/__responses__/1837420
Binary file not shown.
Binary file added tests/__responses__/1837420.torrent
Binary file not shown.
Binary file added tests/__responses__/1837736
Binary file not shown.
Binary file added tests/__responses__/1837736.torrent
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added tests/__responses__/884488
Binary file not shown.
Binary file added tests/__responses__/884488.torrent
Binary file not shown.
Binary file not shown.
74 changes: 74 additions & 0 deletions tests/__responses__/_generate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"""
This file saves the httpx.Response object returned by the GET method.
These saved responses are then used as mock responses in the tests.
"""

import pickle
from hashlib import sha256
from pathlib import Path

import httpx

links = [
"https://nyaa.si/view/1422797",
"https://nyaa.si/view/1544043",
"https://nyaa.si/view/1586776",
"https://nyaa.si/view/1694824",
"https://nyaa.si/view/1755409",
"https://nyaa.si/view/1765655",
"https://nyaa.si/view/884488",
# Called within .searc()
"https://nyaa.si/view/1755409",
"https://nyaa.si/view/1755409",
"https://nyaa.si/view/1837736",
"https://nyaa.si/view/1837420",
]

searches = {
"akldlaskdjsaljdksd": "",
"smol shelter": "https://nyaa.si?page=rss&f=0&c=0_0&q=smol%20shelter",
'"[smol] Shelter (2016) (BD 1080p HEVC FLAC)"': "https://nyaa.si?page=rss&f=0&c=0_0&q=%22%5Bsmol%5D%20Shelter%20%282016%29%20%28BD%201080p%20HEVC%20FLAC%29%22",
"vodes": "https://nyaa.si?page=rss&f=2&c=3_1&q=vodes",
"mtbb": "https://nyaa.si?page=rss&f=0&c=1_2&q=mtbb",
}


def save_pages() -> None:
for link in links:
response = httpx.get(link)
filename = Path(__file__).with_name(link.split("/")[-1])

print(f"Saving {link} ({response}) to {filename}")

with open(filename, "wb") as file:
pickle.dump(response, file=file)

def save_torrents() -> None:
for link in links:
id = link.split("/")[-1]
link = link.replace("view", "download") + ".torrent"
response = httpx.get(link)
filename = Path(__file__).with_name(f"{id}.torrent")

print(f"Saving {link} ({response}) to {filename}")

with open(filename, "wb") as file:
pickle.dump(response, file=file)

def save_searches() -> None:
for query, url in searches.items():

response = httpx.get(url)
name = sha256(query.encode()).hexdigest()
filename = Path(__file__).with_name(f"{name}.search")

print(f"Saving {url} ({response}) to {filename}")

with open(filename, "wb") as file:
pickle.dump(response, file=file)


if __name__ == "__main__":
save_pages()
save_torrents()
save_searches()
43 changes: 43 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import pickle
from hashlib import sha256
from pathlib import Path

from httpx import Response

BASE_DIR = Path(__file__).parent / "__responses__"

# This workaround of loading a Response object and then re-casting is necessary because: https://github.com/lundberg/respx/issues/272


def get_response(nyaa_id: int) -> Response:
"""
Grab the response object corresponding to nyaa_id,
unpickle it, and cast it in a new Response object.
"""
filename = BASE_DIR / str(nyaa_id)
with open(filename, "rb") as file:
response: Response = pickle.load(file)
return Response(status_code=response.status_code, content=response.content)

def get_torrent(nyaa_id: int) -> Response:
"""
Grab the response object of the torrent corresponding to nyaa_id,
unpickle it, and cast it in a new Response object.
"""
filename = BASE_DIR / f"{nyaa_id}.torrent"
with open(filename, "rb") as file:
response: Response = pickle.load(file)
return Response(status_code=response.status_code, content=response.content)

def get_search(query: str) -> Response:
"""
Grab the response object of a search string,
unpickle it, and cast it in a new Response object.
This search string is saved with it's SHA265 digest being the filename.
"""
name = sha256(query.encode()).hexdigest()
filename = BASE_DIR / f"{name}.search"
with open(filename, "rb") as file:
response: Response = pickle.load(file)
return Response(status_code=response.status_code, content=response.content)
75 changes: 61 additions & 14 deletions tests/test_async.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
# type: ignore
from pynyaa import AsyncNyaa, NyaaCategory, NyaaFilter

from .helpers import get_response, get_search, get_torrent

headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36"
}
client = AsyncNyaa(headers=headers)

async def test_nyaa_default(respx_mock) -> None:
respx_mock.get("https://nyaa.si/view/1755409").mock(return_value=get_response(1755409))
respx_mock.get("https://nyaa.si/download/1755409.torrent").mock(return_value=get_torrent(1755409))

async def test_nyaa_default() -> None:
nyaa = await client.get("https://nyaa.si/view/1755409")
assert nyaa.title == "[smol] Shelter (2016) (BD 1080p HEVC FLAC) | Porter Robinson & Madeon - Shelter"
assert nyaa.submitter.name == "smol"
assert nyaa.submitter.is_trusted is False
assert nyaa.submitter.is_banned is False


async def test_nyaa_trusted() -> None:
async def test_nyaa_trusted(respx_mock) -> None:
description = """[I Want to Eat Your Pancreas](https://myanimelist.net/anime/36098/Kimi_no_Suizou_wo_Tabetai)
PAS subs, additional TS by [nedragrevev](https://github.com/nedragrevev/custom-subs).
Expand All @@ -28,6 +33,9 @@ async def test_nyaa_trusted() -> None:
Please read this short [playback guide](https://gist.github.com/motbob/754c24d5cd381334bb64b93581781a81) if you want to know how to make the video and subtitles of this release look better.
**Anyone wanting to do their own release is free to use any part of this torrent without permission or credit.**"""

respx_mock.get("https://nyaa.si/view/1544043").mock(return_value=get_response(1544043))
respx_mock.get("https://nyaa.si/download/1544043.torrent").mock(return_value=get_torrent(1544043))

nyaa = await client.get("https://nyaa.si/view/1544043")
assert nyaa.title == "[MTBB] I Want to Eat Your Pancreas (BD 1080p) | Kimi no Suizou wo Tabetai"
assert nyaa.submitter.is_trusted is True
Expand All @@ -37,7 +45,10 @@ async def test_nyaa_trusted() -> None:
assert nyaa.category == NyaaCategory.ANIME_ENGLISH_TRANSLATED


async def test_nyaa_trusted_and_remake() -> None:
async def test_nyaa_trusted_and_remake(respx_mock) -> None:
respx_mock.get("https://nyaa.si/view/1694824").mock(return_value=get_response(1694824))
respx_mock.get("https://nyaa.si/download/1694824.torrent").mock(return_value=get_torrent(1694824))

nyaa = await client.get("https://nyaa.si/view/1694824")
assert (
nyaa.title
Expand All @@ -48,7 +59,10 @@ async def test_nyaa_trusted_and_remake() -> None:
assert nyaa.submitter.is_trusted is True


async def test_nyaa_anon() -> None:
async def test_nyaa_anon(respx_mock) -> None:
respx_mock.get("https://nyaa.si/view/1765655").mock(return_value=get_response(1765655))
respx_mock.get("https://nyaa.si/download/1765655.torrent").mock(return_value=get_torrent(1765655))

nyaa = await client.get(1765655)
assert (
nyaa.title
Expand All @@ -60,44 +74,77 @@ async def test_nyaa_anon() -> None:
assert nyaa.category == NyaaCategory.LITERATURE_ENGLISH_TRANSLATED


async def test_nyaa_banned() -> None:
async def test_nyaa_banned(respx_mock) -> None:
# Thoughts and Prayers for our good friend succ_
respx_mock.get("https://nyaa.si/view/1422797").mock(return_value=get_response(1422797))
respx_mock.get("https://nyaa.si/download/1422797.torrent").mock(return_value=get_torrent(1422797))

nyaa = await client.get("https://nyaa.si/view/1422797")
assert nyaa.title == "[succ_] Tsugumomo [BDRip 1920x1080 x264 FLAC]"
assert nyaa.submitter.is_trusted is False
assert nyaa.submitter.is_banned is True
assert len(nyaa.torrent.files) == 20


async def test_nyaa_banned_and_trusted() -> None:
async def test_nyaa_banned_and_trusted(respx_mock) -> None:
respx_mock.get("https://nyaa.si/view/884488").mock(return_value=get_response(884488))
respx_mock.get("https://nyaa.si/download/884488.torrent").mock(return_value=get_torrent(884488))

nyaa = await client.get("https://nyaa.si/view/884488")
assert nyaa.title == "[FMA1394] Fullmetal Alchemist (2003) [Dual Audio] [US BD] (batch)"
assert nyaa.submitter.is_trusted is True
assert nyaa.submitter.is_banned is True
assert len(nyaa.torrent.files) == 51


async def test_nyaa_empty_desc_info() -> None:
async def test_nyaa_empty_desc_info(respx_mock) -> None:
respx_mock.get("https://nyaa.si/view/1586776").mock(return_value=get_response(1586776))
respx_mock.get("https://nyaa.si/download/1586776.torrent").mock(return_value=get_torrent(1586776))

nyaa = await client.get("https://nyaa.si/view/1586776")
assert nyaa.information is None
assert nyaa.description is None


async def test_nyaa_search() -> None:
async def test_nyaa_search_empty(respx_mock) -> None:
respx_mock.get("https://nyaa.si?page=rss&f=0&c=0_0&q=akldlaskdjsaljdksd").mock(
return_value=get_search("akldlaskdjsaljdksd")
)

zero = await client.search("akldlaskdjsaljdksd")
assert zero == tuple()


async def test_search_single(respx_mock) -> None:
respx_mock.get("https://nyaa.si?page=rss&f=0&c=0_0&q=smol%20shelter").mock(return_value=get_search("smol shelter"))
respx_mock.get("https://nyaa.si/view/1755409").mock(return_value=get_response(1755409))
respx_mock.get("https://nyaa.si/download/1755409.torrent").mock(return_value=get_torrent(1755409))

single = await client.search("smol shelter")
assert single[0].title == "[smol] Shelter (2016) (BD 1080p HEVC FLAC) | Porter Robinson & Madeon - Shelter"

single = await client.search('"[smol] Shelter (2016) (BD 1080p HEVC FLAC)"', limit=74)
assert single[0].title == "[smol] Shelter (2016) (BD 1080p HEVC FLAC) | Porter Robinson & Madeon - Shelter"
single_with_limit = await client.search('"[smol] Shelter (2016) (BD 1080p HEVC FLAC)"', limit=74)
assert single_with_limit[0].title == "[smol] Shelter (2016) (BD 1080p HEVC FLAC) | Porter Robinson & Madeon - Shelter"

limited = await client.search("smol", limit=2)
assert len(limited) == 2

limited_not_trusted_literature = await client.search("smol", category=NyaaCategory.LITERATURE_ENGLISH_TRANSLATED, filter=NyaaFilter.TRUSTED_ONLY, limit=2)
async def test_search_0_results(respx_mock) -> None:
respx_mock.get("https://nyaa.si?page=rss&f=2&c=3_1&q=vodes").mock(return_value=get_search("vodes"))
limited_not_trusted_literature = await client.search(
"vodes", category=NyaaCategory.LITERATURE_ENGLISH_TRANSLATED, filter=NyaaFilter.TRUSTED_ONLY, limit=2
)
assert len(limited_not_trusted_literature) == 0

limited_trusted_english = await client.search("mtbb", category=NyaaCategory.ANIME_ENGLISH_TRANSLATED, filter=NyaaFilter.NO_FILTER, limit=2)

async def test_search_filtered(respx_mock) -> None:
respx_mock.get("https://nyaa.si?page=rss&f=0&c=1_2&q=mtbb").mock(return_value=get_search("mtbb"))

respx_mock.get("https://nyaa.si/view/1837736").mock(return_value=get_response(1837736))
respx_mock.get("https://nyaa.si/download/1837736.torrent").mock(return_value=get_torrent(1837736))

respx_mock.get("https://nyaa.si/view/1837420").mock(return_value=get_response(1837420))
respx_mock.get("https://nyaa.si/download/1837420.torrent").mock(return_value=get_torrent(1837420))

limited_trusted_english = await client.search(
"mtbb", category=NyaaCategory.ANIME_ENGLISH_TRANSLATED, filter=NyaaFilter.NO_FILTER, limit=2
)
assert len(limited_trusted_english) == 2
Loading

0 comments on commit 8498406

Please sign in to comment.