summaryrefslogtreecommitdiffstatshomepage
path: root/tests/scrapers/test_types.py
blob: 33f9f895ad6b005d7de53b9bba05431e8ac5fb25 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from datetime import date

import pytest

import hircine.enums as enums
from hircine.api.types import ScrapedComic
from hircine.scraper import ScrapeWarning
from hircine.scraper.types import (
    Artist,
    Category,
    Character,
    Circle,
    Date,
    Language,
    OriginalTitle,
    Rating,
    Tag,
    Title,
    World,
)


@pytest.mark.parametrize(
    "input,options,want",
    [
        ("foo", {}, Tag(namespace="none", tag="foo")),
        ("foo:bar", {}, Tag(namespace="foo", tag="bar")),
        ("foo:bar:baz", {}, Tag(namespace="foo", tag="bar:baz")),
        ("foo/bar", {"delimiter": "/"}, Tag(namespace="foo", tag="bar")),
    ],
    ids=[
        "tag only",
        "tag and namespace",
        "tag with delimiter",
        "custom delimiter",
    ],
)
def test_tag_from_string(input, options, want):
    assert Tag.from_string(input, **options) == want


@pytest.mark.parametrize(
    "input,want",
    [
        ("1998-02-07", Date(value=date(1998, 2, 7))),
        ("2018-07-18T19:15", Date(value=date(2018, 7, 18))),
        (
            "2003-12-30T10:37Z",
            Date(value=date(2003, 12, 30)),
        ),
    ],
)
def test_date_from_iso(input, want):
    assert Date.from_iso(input) == want


@pytest.mark.parametrize(
    "input",
    [
        ("text"),
        ("1997 02 07"),
        ("1997/02/07"),
    ],
)
def test_date_from_iso_fails(input):
    with pytest.raises(ScrapeWarning, match="Could not parse date:"):
        Date.from_iso(input)


@pytest.mark.parametrize(
    "input,want",
    [
        ("886806000", Date(value=date(1998, 2, 7))),
        (886806000, Date(value=date(1998, 2, 7))),
    ],
)
def test_date_from_timestamp(input, want):
    assert Date.from_timestamp(input) == want


@pytest.mark.parametrize(
    "input",
    [
        ("text"),
    ],
)
def test_date_from_timestamp_fails(input):
    with pytest.raises(ScrapeWarning, match="Could not parse date:"):
        Date.from_timestamp(input)


@pytest.mark.parametrize(
    "item,attr,empty",
    [
        (Title(""), "title", None),
        (OriginalTitle(""), "original_title", None),
        (Language(None), "language", None),
        (Date(None), "date", None),
        (Rating(None), "rating", None),
        (Category(None), "category", None),
        (Tag("", ""), "tags", []),
        (Tag(namespace="", tag=""), "tags", []),
        (Tag(namespace=None, tag=""), "tags", []),
        (Tag(namespace="foo", tag=""), "tags", []),
        (Artist(""), "artists", []),
        (Character(""), "characters", []),
        (Circle(""), "circles", []),
        (World(""), "worlds", []),
    ],
    ids=[
        "title",
        "original title",
        "language",
        "date",
        "rating",
        "category",
        "tag (both empty, positional)",
        "tag (both empty)",
        "tag (namespace None, tag empty)",
        "tag (tag empty)",
        "artist",
        "character",
        "circle",
        "world",
    ],
)
def test_scraped_comic_silently_ignores_empty(item, attr, empty):
    def gen():
        yield item

    comic = ScrapedComic.from_generator(gen())

    assert getattr(comic, attr) == empty


@pytest.mark.parametrize(
    "input,want",
    [
        ("EN", Language(value=enums.Language.EN)),
        ("de", Language(value=enums.Language.DE)),
    ],
)
def test_language_from_iso_639_3(input, want):
    assert Language.from_iso_639_3(input) == want


def test_language_from_iso_639_3_fails():
    with pytest.raises(ScrapeWarning, match="Could not parse language code:"):
        Language.from_iso_639_3("ENG")


@pytest.mark.parametrize(
    "input,want",
    [
        ("English", Language(value=enums.Language.EN)),
        ("german", Language(value=enums.Language.DE)),
    ],
)
def test_language_from_name(input, want):
    assert Language.from_name(input) == want


def test_language_from_name_fails():
    with pytest.raises(ScrapeWarning, match="Could not parse language name:"):
        Language.from_name("nonexistent")