Skip to content

Commit

Permalink
Much better tests, I guess
Browse files Browse the repository at this point in the history
  • Loading branch information
DemonicSavage committed Nov 29, 2023
1 parent 2add342 commit 548ba2d
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 194 deletions.
3 changes: 0 additions & 3 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@

import pytest

from mikan.main import discover_plugins


@pytest.fixture(autouse=True)
def _cleanup():
discover_plugins()
yield
shutil.rmtree("test/temp", ignore_errors=True)
93 changes: 0 additions & 93 deletions test/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,100 +82,7 @@ async def mock_get_items(*_):
200,
)

mock_list_response = MockResponse(
"""
<div class='top-item'>
<a href='/123/'></a>
</div>
""",
200,
)
mock_sif_list_response = MockResponse(
"""
[123]
""",
200,
)


async def mock_sif_list_json():
return json.loads(await mock_sif_list_response.text())


mock_sif_card_response = MockResponse(
"""{
"idol": {"name": "Name", "main_unit": "Unit", "sub_unit": "Subunit", "year": "Year"},
"rarity": "Rarity",
"attribute": "Attribute",
"card_image": "Normal",
"card_idolized_image": "Idolized",
"id": 98
}""",
200,
)


async def mock_sif_card_json():
return json.loads(await mock_sif_card_response.text())


mock_sif_list_response.json = mock_sif_list_json
mock_sif_card_response.json = mock_sif_card_json

mock_num_pages_response = MockResponse(
"""
<div class='pagination'>
<a href='=1'></a>
<a href='=42'></a>
<a href='last'></a>
</div>
""",
200,
)
mock_card_response = MockResponse(
"""
<div class="top-item">
<a href="98Normal"></a>
<a href="98Idolized"></a>
</div>
<div>
<div data-field="idol">
null\nName
</div>
<div data-field="rarity">
null\nRarity
</div>
<div data-field="attribute">
null\nAttribute
</div>
<div data-field="idol__i_unit">
null\nUnit
</div>
<div data-field="idol__i_subunit">
null\nSubunit
</div>
<div data-field="idol__i_year">
null\nYear
</div>
</div>
""",
200,
)
mock_still_response = MockResponse(
"""
<div class='top-item'>
<a href='98Still'></a>
</div>
""",
200,
)
mock_empty_response = MockResponse("", 200)
mock_still_response_error = MockResponse(mock_still_response._text.replace("top-item", "top-tem"), 200)
mock_card_response_error = MockResponse(mock_card_response._text.replace("top-item", "top-tem"), 200)
mock_list_response_error = MockResponse(mock_list_response._text.replace("top-item", "top-tem"), 200)
mock_num_pages_response_error = MockResponse(mock_list_response._text.replace("pagination", "paginaion"), 200)
mock_card_response_data_error = MockResponse(mock_card_response._text.replace("idol", "idl"), 200)

pre_json = """{
"SIFAS_Cards": {
Expand Down
89 changes: 0 additions & 89 deletions test/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
import pytest

import mikan.html_parser
from mikan.main import discover_plugins

discover_plugins()


class Parser:
Expand All @@ -36,40 +33,6 @@ async def __aexit__(self, a, b, c):
await self.parser.session.close()


list_types = [
(mikan.plugins.sif.SIF.ListParser, test.mocks.mock_sif_list_response, 1),
(mikan.plugins.sifas.SIFAS.ListParser, test.mocks.mock_list_response, 42),
]

list_num_pages_types = [
(mikan.plugins.sif.SIF.ListParser, test.mocks.mock_sif_list_response, 1),
(mikan.plugins.sifas.SIFAS.ListParser, test.mocks.mock_num_pages_response, 42),
]

card_types = [
(
mikan.plugins.sifas.SIFAS.ItemParser,
test.mocks.mock_card_response,
["98Normal", "98Idolized"],
),
(
mikan.plugins.sif.SIF.ItemParser,
test.mocks.mock_sif_card_response,
["Normal", "Idolized"],
),
(
mikan.plugins.sifasstills.SIFASStills.ItemParser,
test.mocks.mock_still_response,
["98Still"],
),
]

card_error_types = [
(mikan.plugins.sifas.SIFAS.ItemParser, test.mocks.mock_card_response_error),
(mikan.plugins.sifasstills.SIFASStills.ItemParser, test.mocks.mock_still_response_error),
]


@pytest.mark.asyncio()
async def test_parser(mocker):
parser = mikan.html_parser.Parser({"SIFAS_Cards": {}}, "SIFAS", aiohttp.ClientSession())
Expand All @@ -84,55 +47,3 @@ async def test_parser(mocker):
await parser.get_items()
await parser.session.close()
assert parser.objs["SIFAS_Cards"] == {"1": ["//normal1.png", "//idolized1.png"]}


@pytest.mark.asyncio()
async def test_list_parser():
async with Parser(mikan.plugins.sifas.SIFAS.ListParser()) as parser:
assert await parser.parser.get_page(test.mocks.mock_list_response) == [123]


@pytest.mark.asyncio()
async def test_sif_list_parser():
async with Parser(mikan.plugins.sif.SIF.ListParser()) as parser:
await parser.parser.get_num_pages(test.mocks.mock_sif_list_response)
assert await parser.parser.get_page(1) == [123]


@pytest.mark.parametrize(("list_parser", "list_mock", "list_res"), list_num_pages_types)
@pytest.mark.asyncio()
async def test_num_pages_parser(list_parser, list_mock, list_res):
async with Parser(list_parser()) as parser:
assert await parser.parser.get_num_pages(list_mock) == list_res


@pytest.mark.parametrize(("card_parser", "card_mock", "card_res"), card_types)
@pytest.mark.asyncio()
async def test_sif_card_parser(card_parser, card_mock, card_res):
async with Parser(card_parser()) as parser:
assert await parser.parser.create_item(card_mock) == card_res


@pytest.mark.asyncio()
async def test_list_parser_fail():
async with Parser(mikan.plugins.sifas.SIFAS.ListParser()) as parser:
with pytest.raises(mikan.html_parser.ParsingError) as ex:
await parser.parser.get_page(test.mocks.mock_list_response_error)
assert ex.type == mikan.html_parser.ParsingError


@pytest.mark.asyncio()
async def test_num_pages_parser_fail():
async with Parser(mikan.plugins.sifas.SIFAS.ListParser()) as parser:
with pytest.raises(mikan.html_parser.ParsingError) as ex:
await parser.parser.get_num_pages(test.mocks.mock_num_pages_response_error)
assert ex.type == mikan.html_parser.ParsingError


@pytest.mark.parametrize(("card_parser", "card_mock"), card_error_types)
@pytest.mark.asyncio()
async def test_card_parser_fail(card_parser, card_mock):
async with Parser(card_parser()) as parser:
with pytest.raises(mikan.html_parser.ParsingError) as ex:
await parser.parser.create_item(card_mock)
assert ex.type == mikan.html_parser.ParsingError
9 changes: 6 additions & 3 deletions test/test_plugins.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import base64
import json
from sys import api_version
from test.mocks import mock_empty_response
from vcr import VCR

Expand All @@ -12,7 +13,8 @@

discover_plugins()

plugins = [plugin for _, plugin in registry.items()]
api_plugins = [plugin for _, plugin in registry.items() if plugin.is_api]
scraper_plugins = [plugin for _, plugin in registry.items() if not plugin.is_api]


class B64Serializer:
Expand All @@ -25,7 +27,7 @@ def serialize(cassette_dict):
return encoded.decode("utf8")


@pytest.mark.parametrize("plugin", plugins)
@pytest.mark.parametrize("plugin", api_plugins + scraper_plugins)
@pytest.fixture()
def vcr_cassette_name(plugin):
return f"cassette_{plugin.cli_arg}"
Expand All @@ -44,10 +46,10 @@ async def get_test_data(url):
return resp


@pytest.mark.parametrize("plugin", plugins)
@pytest.mark.vcr(serializer="b64")
@pytest.mark.asyncio()
class TestPlugins:
@pytest.mark.parametrize("plugin", scraper_plugins + api_plugins)
async def test_plugin(self, vcr, plugin, vcr_cassette_name):
first_page = await get_test_data(f"{plugin.list_url}{'' if plugin.is_api else '2'}")
list_parser = plugin.ListParser()
Expand All @@ -59,6 +61,7 @@ async def test_plugin(self, vcr, plugin, vcr_cassette_name):
data = await get_test_data(f"{plugin.url}{items[0]}")
await plugin.ItemParser().create_item(data)

@pytest.mark.parametrize("plugin", scraper_plugins)
async def test_plugin_fail(self, mocker, plugin, vcr_cassette_name):
if not plugin.is_api:
with pytest.raises(ParsingError):
Expand Down
6 changes: 0 additions & 6 deletions test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,3 @@ async def __aexit__(self, exc_type, exc, tb):

async def __aenter__(self):
return self


def awaitable_res(res):
f = asyncio.Future()
f.set_result(res)
return f

0 comments on commit 548ba2d

Please sign in to comment.