1
0

Moved tests

This commit is contained in:
Jon Michael Aanes 2024-05-09 16:58:09 +02:00
parent f5731ffa35
commit 8fef2e9ef5
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA
5 changed files with 38 additions and 30 deletions

View File

@ -37,7 +37,7 @@ class LodestoneAchievementScraper(Scraper):
response = self.session.get(url)
response.raise_for_status()
NOW = personal_data.parse_util.response_datetime(response)
NOW = personal_data.parse_util.parse_response_datetime(response)
# Parse data
soup = bs4.BeautifulSoup(response.content, 'lxml')

View File

@ -16,34 +16,12 @@ logger = logging.getLogger(__name__)
URL_PROFILE = 'https://psnprofiles.com/{psn_id}'
URL_USER_GAME_TROPHIES = 'https://psnprofiles.com/trophies/{game_id}/{psn_id}'
FORMAT_DAY_MONTH_YEAR = '%d %B %Y'
def game_psnprofiles_id_from_url(relative_url: str) -> int:
m = re.match(r'/(?:trophy|trophies)/(\d+)\-(?:[\w-]+)(/[\w-]*)?', relative_url)
result = m.group(1)
return int(result)
assert game_psnprofiles_id_from_url('/trophies/21045-theatrhythm-final-bar-line')
assert game_psnprofiles_id_from_url('/trophies/21045-theatrhythm-final-bar-line/')
assert game_psnprofiles_id_from_url(
'/trophies/21045-theatrhythm-final-bar-line/Jmaanes',
)
assert game_psnprofiles_id_from_url(
'/trophy/21045-theatrhythm-final-bar-line/19-seasoned-hunter',
)
def parse_time(text: str) -> datetime.datetime:
text = text.replace('\n', ' ')
text = text.strip()
return datetime.datetime.strptime(text, '%d %b %Y %I:%M:%S %p')
assert parse_time('06 Apr 2024 06:11:42 PM')
assert parse_time('26 Mar 2024 7:07:01 PM')
MAX_GAME_ITERATIONS = 10
@ -76,7 +54,7 @@ class PsnProfilesScraper(Scraper):
response = self.session.get(url)
response.raise_for_status()
NOW = personal_data.parse_util.response_datetime(response)
NOW = personal_data.parse_util.parse_response_datetime(response)
# Parse data
soup = bs4.BeautifulSoup(response.content, 'lxml')
@ -139,10 +117,7 @@ class PsnProfilesScraper(Scraper):
if len(small_infos) > 2:
time_played_div = small_infos[2]
time_played_div.sup.extract()
time_played = datetime.datetime.strptime(
time_played_div.get_text().strip(),
FORMAT_DAY_MONTH_YEAR,
).date()
time_played = personal_data.parse_util.parse_date(time_played_div.get_text())
else:
time_played = None

View File

@ -19,6 +19,8 @@ DATETIME_UNITS = {
FORMAT_DATE_HEADER = '%a, %d %b %Y %H:%M:%S GMT'
FORMAT_DAY_MONTH_YEAR = '%d %B %Y'
def parse_duration(text: str) -> datetime.timedelta:
(num, unit) = text.split(' ')
@ -26,6 +28,16 @@ def parse_duration(text: str) -> datetime.timedelta:
unit = DATETIME_UNITS[unit]
return unit * num
def response_datetime(response) -> datetime.datetime:
def parse_response_datetime(response) -> datetime.datetime:
return datetime.datetime.strptime(response.headers['Date'], FORMAT_DATE_HEADER)
def parse_time(text: str) -> datetime.datetime:
text = text.replace('\n', ' ')
text = text.strip()
return datetime.datetime.strptime(text, '%d %b %Y %I:%M:%S %p')
def parse_date(text: str) -> datetime.date:
return datetime.datetime.strptime(
text.strip(),
FORMAT_DAY_MONTH_YEAR,
).date()

6
test/test_parse_util.py Normal file
View File

@ -0,0 +1,6 @@
from personal_data.parse_util import parse_time
def test_parse_tme():
assert parse_time('06 Apr 2024 06:11:42 PM')
assert parse_time('26 Mar 2024 7:07:01 PM')

15
test/test_psnprofiles.py Normal file
View File

@ -0,0 +1,15 @@
import pytest
from personal_data.fetchers import psnprofiles
URLS_AND_IDS = [
(21045, '/trophies/21045-theatrhythm-final-bar-line'),
(21045, '/trophies/21045-theatrhythm-final-bar-line/'),
(21045, '/trophies/21045-theatrhythm-final-bar-line/HelloWorld'),
(21045, '/trophy/21045-theatrhythm-final-bar-line/19-seasoned-hunter'),
]
@pytest.mark.parametrize('id, url', URLS_AND_IDS)
def test_game_psnprofiles_id_from_url(id, url):
assert psnprofiles.game_psnprofiles_id_from_url(url) == id