removed datetime
This commit is contained in:
parent
8de9874023
commit
5a6b00494f
|
@ -1,6 +1,6 @@
|
|||
from abc import ABC, abstractmethod
|
||||
from datetime import timedelta, datetime
|
||||
from typing import List, Optional
|
||||
import arrow
|
||||
|
||||
from mobilizon_bots.event.event import MobilizonEvent
|
||||
|
||||
|
@ -16,8 +16,10 @@ class EventSelectionStrategy(ABC):
|
|||
|
||||
|
||||
class SelectNextEventStrategy(EventSelectionStrategy):
|
||||
def __init__(self, minimum_break_between_events: timedelta = timedelta(days=0)):
|
||||
self.minimum_break_between_events = minimum_break_between_events
|
||||
def __init__(self, minimum_break_between_events_in_minutes: int):
|
||||
self.minimum_break_between_events_in_minutes = (
|
||||
minimum_break_between_events_in_minutes
|
||||
)
|
||||
|
||||
def select(
|
||||
self,
|
||||
|
@ -27,15 +29,17 @@ class SelectNextEventStrategy(EventSelectionStrategy):
|
|||
|
||||
last_published_event = published_events[-1]
|
||||
first_unpublished_event = unpublished_events[0]
|
||||
assert last_published_event.publication_time < datetime.now(), (
|
||||
now = arrow.now()
|
||||
assert last_published_event.publication_time < now, (
|
||||
f"Last published event has been published in the future\n"
|
||||
f"{last_published_event.publication_time}\n"
|
||||
f"{datetime.now()}"
|
||||
f"{now}"
|
||||
)
|
||||
|
||||
if (
|
||||
last_published_event.publication_time + self.minimum_break_between_events
|
||||
> datetime.now()
|
||||
last_published_event.publication_time.shift(
|
||||
minutes=self.minimum_break_between_events_in_minutes
|
||||
)
|
||||
> now
|
||||
):
|
||||
return None
|
||||
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
from datetime import datetime, timedelta
|
||||
|
||||
import arrow
|
||||
import pytest
|
||||
|
||||
from mobilizon_bots.event.event import MobilizonEvent, PublicationStatus
|
||||
|
@ -12,7 +11,7 @@ def generate_publication_status(published):
|
|||
@pytest.fixture
|
||||
def event_generator():
|
||||
def _event_generator(
|
||||
begin_date=datetime(year=2021, month=1, day=1, hour=11, minute=30),
|
||||
begin_date=arrow.Arrow(year=2021, month=1, day=1, hour=11, minute=30),
|
||||
published=False,
|
||||
publication_time=None,
|
||||
):
|
||||
|
@ -21,15 +20,15 @@ def event_generator():
|
|||
name="test event",
|
||||
description="description of the event",
|
||||
begin_datetime=begin_date,
|
||||
end_datetime=begin_date + timedelta(hours=2),
|
||||
last_accessed=datetime.now(),
|
||||
end_datetime=begin_date.shift(hours=2),
|
||||
last_accessed=arrow.now(),
|
||||
mobilizon_link="http://some_link.com/123",
|
||||
mobilizon_id="12345",
|
||||
thumbnail_link="http://some_link.com/123.jpg",
|
||||
location="location",
|
||||
publication_status=generate_publication_status(published),
|
||||
publication_time=publication_time
|
||||
or (begin_date - timedelta(days=1) if published else None),
|
||||
or (begin_date.shift(days=-1) if published else None),
|
||||
)
|
||||
|
||||
return _event_generator
|
||||
|
@ -40,9 +39,9 @@ def event() -> MobilizonEvent:
|
|||
return MobilizonEvent(
|
||||
name="test event",
|
||||
description="description of the event",
|
||||
begin_datetime=datetime(year=2021, month=1, day=1, hour=11, minute=30),
|
||||
end_datetime=datetime(year=2021, month=1, day=1, hour=12, minute=30),
|
||||
last_accessed=datetime.now(),
|
||||
begin_datetime=arrow.Arrow(year=2021, month=1, day=1, hour=11, minute=30),
|
||||
end_datetime=arrow.Arrow(year=2021, month=1, day=1, hour=12, minute=30),
|
||||
last_accessed=arrow.now(),
|
||||
mobilizon_link="http://some_link.com/123",
|
||||
mobilizon_id="12345",
|
||||
thumbnail_link="http://some_link.com/123.jpg",
|
||||
|
|
|
@ -1,104 +1,98 @@
|
|||
from datetime import datetime, timedelta
|
||||
|
||||
import arrow
|
||||
import pytest
|
||||
|
||||
from mobilizon_bots.event.event_selector import SelectNextEventStrategy
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"desired_break_window,days_passed_from_publication", [[2, 1], [3, 2]]
|
||||
"desired_break_window_days,days_passed_from_publication", [[2, 1], [3, 2]]
|
||||
)
|
||||
def test_window_simple_no_event(
|
||||
event_generator, desired_break_window, days_passed_from_publication
|
||||
event_generator, desired_break_window_days, days_passed_from_publication
|
||||
):
|
||||
"Testing that the break between events is respected"
|
||||
unpublished_events = [
|
||||
event_generator(
|
||||
published=False,
|
||||
begin_date=datetime(year=2021, month=1, day=5, hour=11, minute=30),
|
||||
begin_date=arrow.Arrow(year=2021, month=1, day=5, hour=11, minute=30),
|
||||
)
|
||||
]
|
||||
published_events = [
|
||||
event_generator(
|
||||
published=True,
|
||||
publication_time=datetime.now()
|
||||
- timedelta(days=days_passed_from_publication),
|
||||
publication_time=arrow.now().shift(days=-days_passed_from_publication),
|
||||
)
|
||||
]
|
||||
|
||||
selected_event = SelectNextEventStrategy(
|
||||
minimum_break_between_events=timedelta(days=desired_break_window)
|
||||
minimum_break_between_events_in_minutes=desired_break_window_days * 24 * 60
|
||||
).select(published_events, unpublished_events)
|
||||
assert selected_event is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"desired_break_window,days_passed_from_publication", [[1, 2], [2, 10], [4, 4]]
|
||||
"desired_break_window_days,days_passed_from_publication", [[1, 2], [2, 10], [4, 4]]
|
||||
)
|
||||
def test_window_simple_event_found(
|
||||
event_generator, desired_break_window, days_passed_from_publication,
|
||||
event_generator, desired_break_window_days, days_passed_from_publication,
|
||||
):
|
||||
"Testing that the break between events is respected and an event is found"
|
||||
unpublished_events = [
|
||||
event_generator(
|
||||
published=False,
|
||||
begin_date=datetime(year=2021, month=1, day=5, hour=11, minute=30),
|
||||
begin_date=arrow.Arrow(year=2021, month=1, day=5, hour=11, minute=30),
|
||||
)
|
||||
]
|
||||
published_events = [
|
||||
event_generator(
|
||||
published=True,
|
||||
publication_time=datetime.now()
|
||||
- timedelta(days=days_passed_from_publication),
|
||||
publication_time=arrow.now().shift(days=-days_passed_from_publication),
|
||||
)
|
||||
]
|
||||
|
||||
selected_event = SelectNextEventStrategy(
|
||||
minimum_break_between_events=timedelta(days=desired_break_window)
|
||||
minimum_break_between_events_in_minutes=desired_break_window_days * 24 * 60
|
||||
).select(published_events, unpublished_events)
|
||||
assert selected_event is unpublished_events[0]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"desired_break_window,days_passed_from_publication", [[1, 2], [2, 10], [4, 4]]
|
||||
"desired_break_window_days,days_passed_from_publication", [[1, 2], [2, 10], [4, 4]]
|
||||
)
|
||||
def test_window_multi_event_found(
|
||||
event_generator, desired_break_window, days_passed_from_publication,
|
||||
event_generator, desired_break_window_days, days_passed_from_publication,
|
||||
):
|
||||
"Testing that the break between events is respected when there are multiple events"
|
||||
unpublished_events = [
|
||||
event_generator(
|
||||
published=False,
|
||||
begin_date=datetime(year=2022, month=1, day=5, hour=11, minute=30),
|
||||
begin_date=arrow.Arrow(year=2022, month=1, day=5, hour=11, minute=30),
|
||||
),
|
||||
event_generator(
|
||||
published=False,
|
||||
begin_date=datetime(year=2022, month=3, day=5, hour=11, minute=30),
|
||||
begin_date=arrow.Arrow(year=2022, month=3, day=5, hour=11, minute=30),
|
||||
),
|
||||
event_generator(
|
||||
published=False,
|
||||
begin_date=datetime(year=2021, month=1, day=5, hour=11, minute=30),
|
||||
begin_date=arrow.Arrow(year=2021, month=1, day=5, hour=11, minute=30),
|
||||
),
|
||||
]
|
||||
published_events = [
|
||||
event_generator(
|
||||
published=True,
|
||||
publication_time=datetime.now()
|
||||
- timedelta(days=days_passed_from_publication),
|
||||
publication_time=arrow.now().shift(days=-days_passed_from_publication),
|
||||
),
|
||||
event_generator(
|
||||
published=True,
|
||||
publication_time=datetime.now()
|
||||
- timedelta(days=days_passed_from_publication + 2),
|
||||
publication_time=arrow.now().shift(days=-days_passed_from_publication - 2),
|
||||
),
|
||||
event_generator(
|
||||
published=True,
|
||||
publication_time=datetime.now()
|
||||
- timedelta(days=days_passed_from_publication + 4),
|
||||
publication_time=arrow.now().shift(days=-days_passed_from_publication - 4),
|
||||
),
|
||||
]
|
||||
|
||||
selected_event = SelectNextEventStrategy(
|
||||
minimum_break_between_events=timedelta(days=desired_break_window)
|
||||
minimum_break_between_events_in_minutes=desired_break_window_days * 24 * 60
|
||||
).select(published_events, unpublished_events)
|
||||
assert selected_event is unpublished_events[0]
|
||||
|
|
Loading…
Reference in New Issue