From 8fef2e9ef545e1399602bc92707d4a1ea601aa1b Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Thu, 9 May 2024 16:58:09 +0200 Subject: [PATCH] Moved tests --- personal_data/fetchers/ffxiv_lodestone.py | 2 +- personal_data/fetchers/psnprofiles.py | 29 ++--------------------- personal_data/parse_util.py | 16 +++++++++++-- test/test_parse_util.py | 6 +++++ test/test_psnprofiles.py | 15 ++++++++++++ 5 files changed, 38 insertions(+), 30 deletions(-) create mode 100644 test/test_parse_util.py create mode 100644 test/test_psnprofiles.py diff --git a/personal_data/fetchers/ffxiv_lodestone.py b/personal_data/fetchers/ffxiv_lodestone.py index 4f22adc..a86d750 100644 --- a/personal_data/fetchers/ffxiv_lodestone.py +++ b/personal_data/fetchers/ffxiv_lodestone.py @@ -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') diff --git a/personal_data/fetchers/psnprofiles.py b/personal_data/fetchers/psnprofiles.py index 2d4ed66..3a08b95 100644 --- a/personal_data/fetchers/psnprofiles.py +++ b/personal_data/fetchers/psnprofiles.py @@ -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 diff --git a/personal_data/parse_util.py b/personal_data/parse_util.py index e20a841..c619f2a 100644 --- a/personal_data/parse_util.py +++ b/personal_data/parse_util.py @@ -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() diff --git a/test/test_parse_util.py b/test/test_parse_util.py new file mode 100644 index 0000000..5c42240 --- /dev/null +++ b/test/test_parse_util.py @@ -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') diff --git a/test/test_psnprofiles.py b/test/test_psnprofiles.py new file mode 100644 index 0000000..bc90da3 --- /dev/null +++ b/test/test_psnprofiles.py @@ -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