Skip to content

Commit

Permalink
Fix timestamp parsing (#42)
Browse files Browse the repository at this point in the history
* rename wrong test function name
* update timestamp parser in issue tag
  currently, we use timestamp tag in issue tag to get the timestamp and
  convert it to datetime. but we found in test, that it is possible that
  timestamp tag is missing, then raise error. in order to fix it, we use
  another tag which are `year`, `month`, `day`, `hour`, `minute`, and
  `second`.  these tags contain exactly the same value in timestamp tag.
  even though it makes the parsing more complex, it allow the flexible API
  wrapper.
  • Loading branch information
kiraware authored Sep 18, 2024
1 parent 7fd40f2 commit 52ec000
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 16 deletions.
54 changes: 48 additions & 6 deletions src/bmkg/parsers/parse_issue_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,54 @@ def parse_issue_element(element: Element) -> datetime:
>>> issue
datetime.datetime(2024, 1, 16, 3, 23, 47)
"""
timestamp_element = element.find("timestamp")
if timestamp_element is None:
raise WeatherForecastParseError("timestamp tag in issue tag not found")
year_element = element.find("year")
if year_element is None:
raise WeatherForecastParseError("year tag in issue tag not found")

timestamp = timestamp_element.text
if timestamp is None:
raise WeatherForecastParseError("timestamp tag in issue tag has no text")
year = year_element.text
if year is None:
raise WeatherForecastParseError("year tag in issue tag has no text")

month_element = element.find("month")
if month_element is None:
raise WeatherForecastParseError("month tag in issue tag not found")

month = month_element.text
if month is None:
raise WeatherForecastParseError("month tag in issue tag has no text")

day_element = element.find("day")
if day_element is None:
raise WeatherForecastParseError("day tag in issue tag not found")

day = day_element.text
if day is None:
raise WeatherForecastParseError("day tag in issue tag has no text")

hour_element = element.find("hour")
if hour_element is None:
raise WeatherForecastParseError("hour tag in issue tag not found")

hour = hour_element.text
if hour is None:
raise WeatherForecastParseError("hour tag in issue tag has no text")

minute_element = element.find("minute")
if minute_element is None:
raise WeatherForecastParseError("minute tag in issue tag not found")

minute = minute_element.text
if minute is None:
raise WeatherForecastParseError("minute tag in issue tag has no text")

second_element = element.find("second")
if second_element is None:
raise WeatherForecastParseError("second tag in issue tag not found")

second = second_element.text
if second is None:
raise WeatherForecastParseError("second tag in issue tag has no text")

timestamp = year + month + day + hour + minute + second

return datetime.strptime(timestamp, "%Y%m%d%H%M%S")
39 changes: 29 additions & 10 deletions tests/test_parsers/test_parse_issue_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,42 @@
from bmkg.parsers import parse_issue_element


def test_parse_element_with_invalid_attribute():
@pytest.mark.parametrize(
"tag, err_msg",
(
("year", "year tag in issue tag not found"),
("month", "month tag in issue tag not found"),
("day", "day tag in issue tag not found"),
("hour", "hour tag in issue tag not found"),
("minute", "minute tag in issue tag not found"),
("second", "second tag in issue tag not found"),
),
)
def test_parse_element_with_invalid_attribute(tag, err_msg):
element = MagicMock()
element.find.return_value = None
element.find.side_effect = lambda x: None if x == tag else MagicMock()

with pytest.raises(
WeatherForecastParseError, match="timestamp tag in issue tag not found"
):
with pytest.raises(WeatherForecastParseError, match=err_msg):
parse_issue_element(element)


def test_parse_element_with_invalid_humidity_text():
@pytest.mark.parametrize(
"tag, err_msg",
(
("year", "year tag in issue tag has no text"),
("month", "month tag in issue tag has no text"),
("day", "day tag in issue tag has no text"),
("hour", "hour tag in issue tag has no text"),
("minute", "minute tag in issue tag has no text"),
("second", "second tag in issue tag has no text"),
),
)
def test_parse_element_with_invalid_timestamp_text(tag, err_msg):
timestamp = MagicMock()
timestamp.text = None

element = MagicMock()
element.find.return_value = timestamp
with pytest.raises(
WeatherForecastParseError, match="timestamp tag in issue tag has no text"
):
element.find.side_effect = lambda x: timestamp if x == tag else MagicMock()

with pytest.raises(WeatherForecastParseError, match=err_msg):
parse_issue_element(element)

0 comments on commit 52ec000

Please sign in to comment.