Skip to content

Commit b6a3da4

Browse files
committed
Fixed tests
1 parent 8bd84ab commit b6a3da4

File tree

10 files changed

+689
-614
lines changed

10 files changed

+689
-614
lines changed

tests/resources/tibiacom_about.txt

Lines changed: 647 additions & 575 deletions
Large diffs are not rendered by default.

tests/tests_creature.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def test_creatures_section_from_boosted_creature_header_content(self):
1616
creature = CreaturesSection.boosted_creature_from_header(content)
1717

1818
self.assertIsInstance(creature, CreatureEntry)
19-
self.assertEqual("Skeleton Warrior", creature.name)
19+
self.assertEqual("Menacing Carnivor", creature.name)
2020

2121
def test_creatures_section_from_boosted_creature_header_content_not_tibiacom(self):
2222
"""Testing parsing the boosted creature from a page that is not Tibia.com"""

tibiapy/bazaar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ def from_content(cls, content):
384384

385385
bazaar.entries.append(auction)
386386
return bazaar
387-
except ValueError as e:
387+
except (ValueError, IndexError) as e:
388388
raise InvalidContent("content does not belong to the bazaar at Tibia.com", original=e)
389389
# endregion
390390

tibiapy/creature.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ def from_content(cls, content):
295295
boosted_creature_table = parsed_content.find("div", {"class": "TableContainer"})
296296
boosted_creature_text = boosted_creature_table.find("div", {"class": "Text"})
297297
if not boosted_creature_text or "Boosted" not in boosted_creature_text.text:
298-
return None
298+
raise InvalidContent("content is not from the creatures section.")
299299
boosted_creature_link = boosted_creature_table.find("a")
300300
url = urllib.parse.urlparse(boosted_creature_link["href"])
301301
query = urllib.parse.parse_qs(url.query)

tibiapy/guild.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def from_content(cls, content):
122122
selected_world = data["world"] if data["world"] else None
123123
available_worlds = [w for w in data["__options__"]["world"].values() if w]
124124
guilds = cls(selected_world, available_worlds=available_worlds)
125-
except AttributeError as e:
125+
except (AttributeError, KeyError) as e:
126126
raise InvalidContent("Content does not belong to world guild list.", e)
127127
# First TableContainer contains world selector.
128128
_, *containers = parsed_content.find_all('div', class_="TableContainer")

tibiapy/highscores.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def from_content(cls, content):
165165
parsed_content = parse_tibiacom_content(content)
166166
form = parsed_content.find("form")
167167
tables = cls._parse_tables(parsed_content)
168-
if form is None:
168+
if form is None or "Highscores" not in tables:
169169
if "Error" in tables and "The world doesn't exist!" in tables["Error"].text:
170170
return None
171171
raise InvalidContent("content does is not from the highscores section of Tibia.com")

tibiapy/house.py

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -346,39 +346,42 @@ def from_content(cls, content):
346346
InvalidContent
347347
If the content is not the house section on Tibia.com
348348
"""
349-
parsed_content = parse_tibiacom_content(content)
350-
image_column, desc_column, *_ = parsed_content.find_all('td')
351-
if "Error" in image_column.text:
352-
return None
353-
image = image_column.find('img')
354-
for br in desc_column.find_all("br"):
355-
br.replace_with("\n")
356-
description = desc_column.text.replace("\u00a0", " ").replace("\n\n", "\n")
357-
lines = description.splitlines()
358349
try:
359-
name, beds, info, state, *_ = lines
360-
except ValueError:
361-
raise InvalidContent("content does is not from the house section of Tibia.com")
362-
363-
house = cls(name.strip())
364-
house.image_url = image["src"]
365-
house.id = int(id_regex.search(house.image_url).group(1))
366-
m = bed_regex.search(beds)
367-
if m:
368-
if m.group("type").lower() in ["guildhall", "clanhall"]:
369-
house.type = HouseType.GUILDHALL
370-
else:
371-
house.type = HouseType.HOUSE
372-
house.beds = int(m.group("beds"))
373-
374-
m = info_regex.search(info)
375-
if m:
376-
house.world = m.group("world")
377-
house.rent = parse_tibia_money(m.group("rent"))
378-
house.size = int(m.group("size"))
350+
parsed_content = parse_tibiacom_content(content)
351+
image_column, desc_column, *_ = parsed_content.find_all('td')
352+
if "Error" in image_column.text:
353+
return None
354+
image = image_column.find('img')
355+
for br in desc_column.find_all("br"):
356+
br.replace_with("\n")
357+
description = desc_column.text.replace("\u00a0", " ").replace("\n\n", "\n")
358+
lines = description.splitlines()
359+
try:
360+
name, beds, info, state, *_ = lines
361+
except ValueError:
362+
raise InvalidContent("content does is not from the house section of Tibia.com")
363+
364+
house = cls(name.strip())
365+
house.image_url = image["src"]
366+
house.id = int(id_regex.search(house.image_url).group(1))
367+
m = bed_regex.search(beds)
368+
if m:
369+
if m.group("type").lower() in ["guildhall", "clanhall"]:
370+
house.type = HouseType.GUILDHALL
371+
else:
372+
house.type = HouseType.HOUSE
373+
house.beds = int(m.group("beds"))
379374

380-
house._parse_status(state)
381-
return house
375+
m = info_regex.search(info)
376+
if m:
377+
house.world = m.group("world")
378+
house.rent = parse_tibia_money(m.group("rent"))
379+
house.size = int(m.group("size"))
380+
381+
house._parse_status(state)
382+
return house
383+
except (ValueError, TypeError) as e:
384+
raise InvalidContent("content does not belong to a house page", e)
382385
# endregion
383386

384387
def _parse_status(self, status):

tibiapy/kill_statistics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def from_content(cls, content):
112112
else:
113113
entries[columns[0]] = entry
114114
return cls(world, entries, total, available_worlds=available_worlds)
115-
except AttributeError as e:
115+
except (AttributeError, KeyError) as e:
116116
raise InvalidContent("content does not belong to a Tibia.com kill statistics page.", e)
117117

118118

tibiapy/leaderboard.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def from_content(cls, content):
178178
leaderboard.total_pages = total
179179
leaderboard.results_count = count
180180
return leaderboard
181-
except (AttributeError, ValueError) as e:
181+
except (AttributeError, ValueError, KeyError) as e:
182182
raise errors.InvalidContent("content does not belong to the leaderboards", e)
183183

184184
def _parse_entries(self, entries_table):

tibiapy/spell.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def from_content(cls, content):
132132
spells_section.sort_by = try_enum(SpellSorting, data["sort"])
133133
spells_section.premium = "yes" in data["premium"] if data["premium"] else None
134134
return spells_section
135-
except (AttributeError, TypeError) as e:
135+
except (AttributeError, TypeError, KeyError) as e:
136136
raise errors.InvalidContent("content does not belong to the Spells section", e)
137137

138138
@classmethod

0 commit comments

Comments
 (0)