Skip to content

Commit

Permalink
Chapter n of better tests saga
Browse files Browse the repository at this point in the history
  • Loading branch information
DemonicSavage committed Dec 1, 2023
1 parent 3c8f47c commit 99309c5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 53 deletions.
53 changes: 28 additions & 25 deletions test/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,47 @@

# You should have received a copy of the GNU General Public License

import json
from test.utils import MockResponse

from mikan.plugins.default import DefaultPlugin

cards_json = """{
"SIFAS_Stills": {
"6": ["//url6.png"],
"5": ["//url5.png"],
"4": ["//url4.png"],
"3": ["//url3.png"],
"2": ["//url2.png"],
"1": ["//url1.png"]
"Mock": {
"1": ["//not_a_real_url/item/1/item.png"]
}
}"""

still_files = [
"url1.png",
"url2.png",
"url3.png",
"url4.png",
"url5.png",
"url6.png",
card_files = [
"item.png",
]


mock_num_pages = 3
class MockPlugin:
card_dir = "Mock"
url = "https://not_a_real_url/item/"
list_url = "https://not_a_real_url/items/?page="
cli_arg = "mock"
desc = "mocks"
is_api = False

@staticmethod
def item_renamer_fn(_):
return DefaultPlugin.item_renamer_fn(_)

async def mock_get_items(*_):
pass
class ListParser:
async def get_page(self, data) -> list[int]:
return [1]

async def get_num_pages(self, data) -> int:
return 3

mock_objs = json.loads(cards_json)
class ItemParser:
async def create_item(self, data) -> list[str]:
return ["//not_a_real_url/item/1/item.png"]


async def mock_get_items(*_):
pass


mock_file = MockResponse(
Expand All @@ -52,9 +61,3 @@ async def mock_get_items(*_):
)

mock_empty_response = MockResponse("", 200)

pre_json = """{
"SIFAS_Stills": {
"4": ["//url4.png"]
}
}"""
37 changes: 17 additions & 20 deletions test/test_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import pytest

import mikan.downloader
from mikan.plugins import registry


def check_files(path, answer):
Expand All @@ -42,42 +43,38 @@ async def get(self, _):

@pytest.mark.usefixtures("_cleanup")
@pytest.mark.asyncio()
async def test_downloader_cards(mocker):
async def test_downloader_cards():
registry["MockPlugin"] = test.mocks.MockPlugin()
downloader = mikan.downloader.Downloader(
Path("test/temp"), Path("test/temp"), "SIFASStills", MockSession(test.mocks.mock_file)
Path("test/temp"), Path("test/temp"), "MockPlugin", MockSession(test.mocks.mock_file)
)

mocker.patch(
"mikan.html_parser.Parser.get_items",
test.mocks.mock_get_items,
)
downloader.objs = test.mocks.mock_objs
await downloader.update()
await downloader.get()

with Path("test/temp/items.json").open() as file:
assert json.loads(file.read())["SIFAS_Stills"] == json.loads(test.mocks.cards_json)["SIFAS_Stills"]
assert check_files(f"test/temp/SIFAS_Stills", test.mocks.still_files)
assert json.loads(file.read())["Mock"] == json.loads(test.mocks.cards_json)["Mock"]
assert check_files(f"test/temp/Mock", test.mocks.card_files)


@pytest.mark.usefixtures("_cleanup")
@pytest.mark.asyncio()
async def test_downloader_fail(mocker):
registry["MockPlugin"] = test.mocks.MockPlugin()
downloader = mikan.downloader.Downloader(
Path("test/temp"), Path("test/temp"), "SIFASStills", MockSession(test.mocks.mock_file)
Path("test/temp"), Path("test/temp"), "MockPlugin", MockSession(test.mocks.mock_file)
)

mocker.patch(
"mikan.html_parser.Parser.get_items",
test.mocks.mock_get_items,
)
downloader.objs = json.loads(test.mocks.cards_json)

mocker.patch("test.test_downloader.MockSession.get", side_effect=aiohttp.ClientError("Err"))

downloader.objs = test.mocks.mock_objs
await downloader.update()
with pytest.raises(aiohttp.ClientError):
await downloader.update()

await downloader.get()

assert not Path("test/temp/SIFAS_Stills").exists()
assert not Path("test/temp/Mock").exists()


@pytest.mark.usefixtures("_cleanup")
Expand All @@ -86,6 +83,6 @@ async def test_downloader_card_load():
directory = Path("test/temp")
directory.mkdir(parents=True)
with open(directory / "items.json", "w") as file:
file.write(test.mocks.pre_json)
downloader = mikan.downloader.Downloader(directory, directory, "SIFASStills", MockSession(test.mocks.mock_file))
assert set(downloader.objs.keys()) == set(["SIFAS_Stills"])
file.write(test.mocks.cards_json)
downloader = mikan.downloader.Downloader(directory, directory, "MockPlugin", MockSession(test.mocks.mock_file))
assert set(downloader.objs.keys()) == set(["Mock"])
12 changes: 4 additions & 8 deletions test/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import pytest

import mikan.html_parser
from mikan.plugins import registry


class Parser:
Expand All @@ -35,15 +36,10 @@ async def __aexit__(self, a, b, c):

@pytest.mark.asyncio()
async def test_parser(mocker):
parser = mikan.html_parser.Parser({"SIFAS_Cards": {}}, "SIFAS", aiohttp.ClientSession())
mocker.patch(
"mikan.plugins.sifas.SIFAS.ItemParser.create_item",
return_value=["//normal1.png", "//idolized1.png"],
)
mocker.patch("mikan.plugins.sifas.SIFAS.ListParser.get_num_pages", return_value=2),
mocker.patch("mikan.plugins.sifas.SIFAS.ListParser.get_page", return_value=[1]),
registry["MockPlugin"] = test.mocks.MockPlugin()
parser = mikan.html_parser.Parser({"Mock": {}}, "MockPlugin", aiohttp.ClientSession())
mocker.patch("aiohttp.ClientSession.get", test.mocks.mock_get_items)
parser.objs = {}
await parser.get_items()
await parser.session.close()
assert parser.objs["SIFAS_Cards"] == {"1": ["//normal1.png", "//idolized1.png"]}
assert parser.objs["Mock"] == {"1": ["//not_a_real_url/item/1/item.png"]}

0 comments on commit 99309c5

Please sign in to comment.