fix timezone in test (#145)

* added timezone fixture

* Make output dependent on local timezone.

* Fix non deterministic behavior in html to text conversion.

Co-authored-by: Giacomo Leidi <goodoldpaul@autistici.org>
This commit is contained in:
Simone Robutti 2022-02-22 11:28:16 +01:00 committed by GitHub
parent e003cf9a90
commit bda4b8ee0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 41 additions and 43 deletions

View File

@ -28,7 +28,9 @@ def show_events(events: Iterable[MobilizonEvent]):
def pretty(event: MobilizonEvent): def pretty(event: MobilizonEvent):
return ( return (
f"{event.name : ^40}{click.style(event.status.name, fg=status_to_color[event.status]) : ^22}" f"{event.name : ^40}{click.style(event.status.name, fg=status_to_color[event.status]) : ^22}"
f"{str(event.mobilizon_id) : <40}{event.begin_datetime.isoformat() : <29}{event.end_datetime.isoformat()}" f"{str(event.mobilizon_id) : <40}"
f"{event.begin_datetime.to('local').isoformat() : <29}"
f"{event.end_datetime.to('local').isoformat()}"
) )

View File

@ -1,28 +1,28 @@
from typing import List
from bs4 import BeautifulSoup, Tag from bs4 import BeautifulSoup, Tag
import markdownify import markdownify
def get_bottom_paragraphs(soup: BeautifulSoup) -> List[Tag]: def get_bottom_paragraphs(soup: BeautifulSoup) -> list[Tag]:
return [d for d in soup.findAll("p") if not d.find("p")] return [d for d in soup.findAll("p") if not d.find("p")]
def html_to_plaintext(content): def html_to_plaintext(content) -> str:
""" """
Transform a HTML in a plaintext sting that can be more easily processed by the publishers. Transform a HTML in a plaintext string that can be more easily processed by the publishers.
:param content: :param content:
:return: :return:
""" """
# TODO: support links and quotes # TODO: support links and quotes
soup = BeautifulSoup(content) soup = BeautifulSoup(content)
return "\n".join( p_list = get_bottom_paragraphs(soup)
" ".join(tag.stripped_strings) for tag in get_bottom_paragraphs(soup) if p_list:
) return "\n".join(" ".join(tag.stripped_strings) for tag in p_list)
else:
return soup.text
def html_to_markdown(content): def html_to_markdown(content) -> str:
markdown = markdownify.markdownify(content) markdown = markdownify.markdownify(content)
escaped_markdown = markdown.replace(">", "\\>") escaped_markdown = markdown.replace(">", "\\>")
return escaped_markdown.strip() return escaped_markdown.strip()

View File

@ -1,6 +1,6 @@
# {{ name }} # {{ name }}
🕒 {{ begin_datetime.format('DD MMMM, HH:mm') }} - {{ end_datetime.format('DD MMMM, HH:mm') }} 🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm') }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm') }}
{% if location %} {% if location %}
📍 {{ location }} 📍 {{ location }}

View File

@ -1,6 +1,6 @@
# {{ name }} # {{ name }}
🕒 {{ begin_datetime.format('DD MMMM, HH:mm') }} - {{ end_datetime.format('DD MMMM, HH:mm') }} 🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm') }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm') }}
{% if location %} {% if location %}
📍 {{ location }} 📍 {{ location }}

View File

@ -1,6 +1,6 @@
{{ name }} {{ name }}
🕒 {{ begin_datetime.format('DD MMMM, HH:mm') }} - {{ end_datetime.format('DD MMMM, HH:mm') }} 🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm') }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm') }}
{% if location %} {% if location %}
📍 {{ location }} 📍 {{ location }}

View File

@ -1,5 +1,5 @@
*{{ name }}* *{{ name }}*
🕒 {{ begin_datetime.format('DD MMMM, HH:mm') }} - {{ end_datetime.format('DD MMMM, HH:mm') }} 🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm') }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm') }}
{% if location %}📍 {{ location }}{% endif %} {% if location %}📍 {{ location }}{% endif %}
🔗 {{mobilizon_link}} 🔗 {{mobilizon_link}}

View File

@ -1,6 +1,6 @@
<strong>{{ name }}</strong> <strong>{{ name }}</strong>
🕒 {{ begin_datetime.format('DD MMMM, HH:mm') }} - {{ end_datetime.format('DD MMMM, HH:mm') }} 🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm') }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm') }}
{% if location %}📍 {{ location }}{% endif %} {% if location %}📍 {{ location }}{% endif %}
{{ description }} {{ description }}

View File

@ -1,5 +1,5 @@
*{{ name }}* *{{ name }}*
🕒 {{ begin_datetime.format('DD MMMM, HH:mm') }} - {{ end_datetime.format('DD MMMM, HH:mm') }} 🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm') }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm') }}
{% if location %}📍 {{ location }}{% endif %} {% if location %}📍 {{ location }}{% endif %}
🔗 [Link]({{mobilizon_link}}) 🔗 [Link]({{mobilizon_link}})

View File

@ -1,6 +1,6 @@
# {{ name }} # {{ name }}
🕒 {{ begin_datetime.format('DD MMMM, HH:mm') }} - {{ end_datetime.format('DD MMMM, HH:mm') }} 🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm') }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm') }}
{% if location %} {% if location %}
📍 {{ location }} 📍 {{ location }}

View File

@ -1,5 +1,5 @@
# {{ name }} # {{ name }}
🕒 {{ begin_datetime.format('DD MMMM, HH:mm') }} - {{ end_datetime.format('DD MMMM, HH:mm') }} 🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm') }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm') }}
{% if location %} {% if location %}
📍 {{ location }} 📍 {{ location }}
{% endif %} {% endif %}

View File

@ -1,6 +1,6 @@
# {{ name }} # {{ name }}
🕒 {{ begin_datetime.format('DD MMMM, HH:mm') }} - {{ end_datetime.format('DD MMMM, HH:mm') }} 🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm') }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm') }}
{% if location %} {% if location %}
📍 {{ location }} 📍 {{ location }}

View File

@ -1,6 +1,6 @@
# {{ name }} # {{ name }}
🕒 {{ begin_datetime.format('DD MMMM, HH:mm') }} - {{ end_datetime.format('DD MMMM, HH:mm') }} 🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm') }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm') }}
{% if location %} {% if location %}
📍 {{ location }} 📍 {{ location }}

View File

@ -39,8 +39,3 @@ build-backend = "poetry.core.masonry.api"
[tool.poetry.scripts] [tool.poetry.scripts]
mobilizon-reshare="mobilizon_reshare.cli.cli:mobilizon_reshare" mobilizon-reshare="mobilizon_reshare.cli.cli:mobilizon_reshare"
[tool.pytest.ini_options]
markers=[
"timezone_sensitive"
]

View File

@ -2,4 +2,4 @@
set -e set -e
poetry run pytest -m "not timezone_sensitive" poetry run pytest

View File

@ -10,7 +10,6 @@ from mobilizon_reshare.publishers.platforms.platform_mapping import (
from mobilizon_reshare.storage.query.event_converter import to_model from mobilizon_reshare.storage.query.event_converter import to_model
@pytest.mark.timezone_sensitive
@pytest.mark.parametrize("publisher_name", name_to_formatter_class.keys()) @pytest.mark.parametrize("publisher_name", name_to_formatter_class.keys())
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_format_event(runner, event, capsys, publisher_name): async def test_format_event(runner, event, capsys, publisher_name):
@ -26,7 +25,6 @@ async def test_format_event(runner, event, capsys, publisher_name):
) )
@pytest.mark.timezone_sensitive
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_format_event_missing(runner, capsys): async def test_format_event_missing(runner, capsys):
event_id = uuid.uuid4() event_id = uuid.uuid4()

View File

@ -7,24 +7,27 @@ import pytest
from mobilizon_reshare.event.event import MobilizonEvent from mobilizon_reshare.event.event import MobilizonEvent
from mobilizon_reshare.publishers.platforms.platform_mapping import get_formatter_class from mobilizon_reshare.publishers.platforms.platform_mapping import get_formatter_class
begin_date = arrow.get(
datetime(
year=2021,
month=1,
day=1,
hour=11,
minute=30,
tzinfo=timezone(timedelta(hours=1)),
)
).to("local")
end_date = begin_date.shift(hours=1)
@pytest.fixture() @pytest.fixture()
def event() -> MobilizonEvent: def event() -> MobilizonEvent:
begin_date = arrow.get(
datetime(
year=2021,
month=1,
day=1,
hour=11,
minute=30,
tzinfo=timezone(timedelta(hours=1)),
)
)
return MobilizonEvent( return MobilizonEvent(
name="test event", name="test event",
description="<p>description of the event</p>", description="<p>description of the event</p>",
begin_datetime=begin_date, begin_datetime=begin_date,
end_datetime=begin_date.shift(hours=1), end_datetime=end_date,
mobilizon_link="http://some_link.com/123", mobilizon_link="http://some_link.com/123",
mobilizon_id=UUID(int=12345), mobilizon_id=UUID(int=12345),
thumbnail_link="http://some_link.com/123.jpg", thumbnail_link="http://some_link.com/123.jpg",
@ -38,9 +41,9 @@ def event() -> MobilizonEvent:
[ [
[ [
"facebook", "facebook",
"""# f"""# test event
🕒 01 January, 11:30 - 01 January, 12:30 🕒 01 January, {begin_date.format('HH:mm')} - 01 January, {end_date.format('HH:mm')}
📍 location 📍 location
@ -53,9 +56,9 @@ Link: http://some_link.com/123
], ],
[ [
"telegram", "telegram",
"""<strong>test event</strong> f"""<strong>test event</strong>
🕒 01 January, 11:30 - 01 January, 12:30 🕒 01 January, {begin_date.format('HH:mm')} - 01 January, {end_date.format('HH:mm')}
📍 location 📍 location
description of the event description of the event