2021-08-14 18:23:55 +02:00
|
|
|
from datetime import timedelta
|
2021-05-31 01:11:50 +02:00
|
|
|
|
|
|
|
import arrow
|
|
|
|
import pytest
|
|
|
|
|
2021-08-16 10:49:52 +02:00
|
|
|
from mobilizon_reshare.event.event import MobilizonEvent, EventPublicationStatus
|
|
|
|
from mobilizon_reshare.models.event import Event
|
|
|
|
from mobilizon_reshare.models.publication import PublicationStatus
|
|
|
|
from mobilizon_reshare.storage.query import events_with_status
|
|
|
|
from mobilizon_reshare.storage.query import (
|
2021-07-05 23:07:12 +02:00
|
|
|
get_published_events,
|
|
|
|
get_unpublished_events,
|
|
|
|
create_unpublished_events,
|
2021-08-05 00:29:50 +02:00
|
|
|
get_mobilizon_event_publications,
|
|
|
|
prefetch_event_relations,
|
|
|
|
get_publishers,
|
|
|
|
publications_with_status,
|
2021-07-05 23:07:12 +02:00
|
|
|
)
|
2021-08-14 18:23:55 +02:00
|
|
|
from tests.storage import complete_specification
|
2021-08-27 23:45:24 +02:00
|
|
|
from tests.storage import result_publication
|
2021-08-14 18:23:55 +02:00
|
|
|
from tests.storage import today
|
|
|
|
|
|
|
|
event_0 = MobilizonEvent(
|
|
|
|
name="event_0",
|
|
|
|
description="desc_0",
|
|
|
|
mobilizon_id="mobid_0",
|
|
|
|
mobilizon_link="moblink_0",
|
|
|
|
thumbnail_link="thumblink_0",
|
|
|
|
location="loc_0",
|
|
|
|
publication_time={
|
2021-08-27 23:45:24 +02:00
|
|
|
"telegram": arrow.get(today + timedelta(hours=0)),
|
|
|
|
"twitter": arrow.get(today + timedelta(hours=1)),
|
|
|
|
"mastodon": arrow.get(today + timedelta(hours=2)),
|
2021-08-14 18:23:55 +02:00
|
|
|
},
|
|
|
|
status=EventPublicationStatus.COMPLETED,
|
|
|
|
begin_datetime=arrow.get(today + timedelta(days=0)),
|
|
|
|
end_datetime=arrow.get(today + timedelta(days=0) + timedelta(hours=2)),
|
2021-08-05 00:29:50 +02:00
|
|
|
)
|
2021-05-31 01:11:50 +02:00
|
|
|
|
2021-08-05 00:29:50 +02:00
|
|
|
|
2021-08-14 18:23:55 +02:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_get_published_events(generate_models):
|
|
|
|
await generate_models(complete_specification)
|
|
|
|
published_events = list(await get_published_events())
|
|
|
|
|
2021-08-27 23:45:24 +02:00
|
|
|
assert len(published_events) == 3
|
2021-07-05 23:07:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2021-08-05 00:29:50 +02:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"specification,expected_result",
|
|
|
|
[
|
|
|
|
[
|
|
|
|
complete_specification,
|
|
|
|
[
|
|
|
|
MobilizonEvent(
|
|
|
|
name="event_3",
|
|
|
|
description="desc_3",
|
|
|
|
mobilizon_id="mobid_3",
|
|
|
|
mobilizon_link="moblink_3",
|
|
|
|
thumbnail_link="thumblink_3",
|
|
|
|
location="loc_3",
|
2021-08-14 18:23:55 +02:00
|
|
|
status=EventPublicationStatus.WAITING,
|
2021-08-05 00:29:50 +02:00
|
|
|
begin_datetime=arrow.get(today + timedelta(days=3)),
|
|
|
|
end_datetime=arrow.get(
|
|
|
|
today + timedelta(days=3) + timedelta(hours=2)
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
]
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_get_unpublished_events(specification, expected_result, generate_models):
|
|
|
|
await generate_models(specification)
|
2021-07-15 18:13:11 +02:00
|
|
|
unpublished_events = list(await get_unpublished_events())
|
2021-07-05 23:07:12 +02:00
|
|
|
|
2021-08-05 00:29:50 +02:00
|
|
|
assert len(unpublished_events) == len(expected_result)
|
|
|
|
assert unpublished_events == expected_result
|
2021-07-05 23:07:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2021-08-05 00:29:50 +02:00
|
|
|
@pytest.mark.parametrize(
|
2021-08-14 18:23:55 +02:00
|
|
|
"expected_result",
|
2021-08-05 00:29:50 +02:00
|
|
|
[
|
|
|
|
[
|
|
|
|
[
|
|
|
|
Event(
|
|
|
|
name="event_1",
|
|
|
|
description="desc_1",
|
|
|
|
mobilizon_id="mobid_1",
|
|
|
|
mobilizon_link="moblink_1",
|
|
|
|
thumbnail_link="thumblink_1",
|
|
|
|
location="loc_1",
|
|
|
|
begin_datetime=today + timedelta(days=1),
|
|
|
|
end_datetime=today + timedelta(days=1) + timedelta(hours=2),
|
|
|
|
),
|
|
|
|
Event(
|
|
|
|
name="test event",
|
|
|
|
description="description of the event",
|
|
|
|
mobilizon_id="12345",
|
|
|
|
mobilizon_link="http://some_link.com/123",
|
|
|
|
thumbnail_link="http://some_link.com/123.jpg",
|
|
|
|
location="location",
|
|
|
|
begin_datetime=today + timedelta(days=6),
|
|
|
|
end_datetime=today + timedelta(days=6) + timedelta(hours=2),
|
|
|
|
),
|
|
|
|
Event(
|
|
|
|
name="test event",
|
|
|
|
description="description of the event",
|
|
|
|
mobilizon_id="67890",
|
|
|
|
mobilizon_link="http://some_link.com/123",
|
|
|
|
thumbnail_link="http://some_link.com/123.jpg",
|
|
|
|
location="location",
|
|
|
|
begin_datetime=today + timedelta(days=12),
|
|
|
|
end_datetime=today + timedelta(days=12) + timedelta(hours=2),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
]
|
|
|
|
],
|
|
|
|
)
|
2021-07-05 23:07:12 +02:00
|
|
|
async def test_create_unpublished_events(
|
2021-08-28 13:17:39 +02:00
|
|
|
expected_result,
|
|
|
|
generate_models,
|
|
|
|
event_generator,
|
2021-07-05 23:07:12 +02:00
|
|
|
):
|
2021-08-14 18:23:55 +02:00
|
|
|
await generate_models(complete_specification)
|
2021-08-05 00:29:50 +02:00
|
|
|
event_3 = event_generator(begin_date=arrow.get(today + timedelta(days=6)))
|
|
|
|
event_4 = event_generator(
|
2021-07-05 23:07:12 +02:00
|
|
|
begin_date=arrow.get(today + timedelta(days=12)), mobilizon_id="67890"
|
2021-05-31 01:11:50 +02:00
|
|
|
)
|
2021-08-05 00:29:50 +02:00
|
|
|
models = await prefetch_event_relations(Event.filter(name="event_1"))
|
2021-05-31 01:11:50 +02:00
|
|
|
|
2021-08-05 00:29:50 +02:00
|
|
|
events_from_internet = [MobilizonEvent.from_model(models[0]), event_3, event_4]
|
2021-05-31 01:11:50 +02:00
|
|
|
|
2021-08-28 13:17:39 +02:00
|
|
|
await create_unpublished_events(
|
|
|
|
unpublished_mobilizon_events=events_from_internet,
|
|
|
|
)
|
2021-07-05 23:07:12 +02:00
|
|
|
unpublished_events = list(await get_unpublished_events())
|
2021-05-31 01:11:50 +02:00
|
|
|
|
2021-08-14 18:23:55 +02:00
|
|
|
assert len(unpublished_events) == 4
|
2021-08-05 00:29:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2021-08-14 18:23:55 +02:00
|
|
|
async def test_get_mobilizon_event_publications(generate_models):
|
|
|
|
await generate_models(complete_specification)
|
2021-08-05 00:29:50 +02:00
|
|
|
|
|
|
|
models = await prefetch_event_relations(Event.filter(name="event_0"))
|
|
|
|
mobilizon_event = MobilizonEvent.from_model(models[0])
|
|
|
|
|
|
|
|
publications = list(await get_mobilizon_event_publications(mobilizon_event))
|
|
|
|
for pub in publications:
|
|
|
|
await pub.fetch_related("event")
|
|
|
|
await pub.fetch_related("publisher")
|
|
|
|
|
2021-08-14 18:23:55 +02:00
|
|
|
assert len(publications) == 3
|
2021-08-05 00:29:50 +02:00
|
|
|
|
|
|
|
assert publications[0].event.name == "event_0"
|
2021-08-27 23:45:24 +02:00
|
|
|
assert publications[0].publisher.name == "telegram"
|
2021-08-14 18:23:55 +02:00
|
|
|
assert publications[0].status == PublicationStatus.COMPLETED
|
2021-08-05 00:29:50 +02:00
|
|
|
|
|
|
|
assert publications[1].event.name == "event_0"
|
2021-08-27 23:45:24 +02:00
|
|
|
assert publications[1].publisher.name == "twitter"
|
2021-08-05 00:29:50 +02:00
|
|
|
assert publications[1].status == PublicationStatus.COMPLETED
|
|
|
|
|
2021-08-14 18:23:55 +02:00
|
|
|
assert publications[2].event.name == "event_0"
|
2021-08-27 23:45:24 +02:00
|
|
|
assert publications[2].publisher.name == "mastodon"
|
2021-08-14 18:23:55 +02:00
|
|
|
assert publications[2].status == PublicationStatus.COMPLETED
|
|
|
|
|
2021-08-05 00:29:50 +02:00
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
@pytest.mark.parametrize(
|
2021-08-14 18:23:55 +02:00
|
|
|
"name,expected_result",
|
2021-08-27 23:45:24 +02:00
|
|
|
[[None, {"telegram", "twitter", "mastodon"}], ["telegram", {"telegram"}]],
|
2021-08-05 00:29:50 +02:00
|
|
|
)
|
|
|
|
async def test_get_publishers(
|
2021-08-28 13:17:39 +02:00
|
|
|
name,
|
|
|
|
expected_result,
|
|
|
|
generate_models,
|
2021-08-05 00:29:50 +02:00
|
|
|
):
|
2021-08-14 18:23:55 +02:00
|
|
|
await generate_models(complete_specification)
|
2021-08-05 00:29:50 +02:00
|
|
|
result = await get_publishers(name)
|
|
|
|
|
|
|
|
if type(result) == list:
|
|
|
|
publishers = set(p.name for p in result)
|
|
|
|
else:
|
|
|
|
publishers = {result.name}
|
|
|
|
|
|
|
|
assert len(publishers) == len(expected_result)
|
|
|
|
assert publishers == expected_result
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
@pytest.mark.parametrize(
|
2021-08-14 18:23:55 +02:00
|
|
|
"status,mobilizon_id,from_date,to_date,expected_result",
|
2021-08-05 00:29:50 +02:00
|
|
|
[
|
|
|
|
[
|
|
|
|
PublicationStatus.WAITING,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
[
|
2021-08-14 18:23:55 +02:00
|
|
|
result_publication[3],
|
|
|
|
result_publication[4],
|
|
|
|
result_publication[8],
|
|
|
|
result_publication[9],
|
|
|
|
result_publication[10],
|
|
|
|
result_publication[11],
|
2021-08-05 00:29:50 +02:00
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
PublicationStatus.WAITING,
|
|
|
|
"mobid_1",
|
|
|
|
None,
|
|
|
|
None,
|
2021-08-14 18:23:55 +02:00
|
|
|
[result_publication[3], result_publication[4]],
|
2021-08-05 00:29:50 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
PublicationStatus.WAITING,
|
|
|
|
None,
|
2021-08-14 18:23:55 +02:00
|
|
|
arrow.get(today),
|
|
|
|
arrow.get(today + timedelta(hours=6)),
|
|
|
|
[result_publication[3], result_publication[4]],
|
2021-08-05 00:29:50 +02:00
|
|
|
],
|
|
|
|
[
|
2021-08-14 18:23:55 +02:00
|
|
|
PublicationStatus.COMPLETED,
|
2021-08-05 00:29:50 +02:00
|
|
|
None,
|
|
|
|
arrow.get(today + timedelta(hours=1)),
|
|
|
|
None,
|
2021-08-14 18:23:55 +02:00
|
|
|
[result_publication[2], result_publication[5], result_publication[7]],
|
2021-08-05 00:29:50 +02:00
|
|
|
],
|
|
|
|
[
|
2021-08-14 18:23:55 +02:00
|
|
|
PublicationStatus.COMPLETED,
|
2021-08-05 00:29:50 +02:00
|
|
|
None,
|
|
|
|
None,
|
2021-08-14 18:23:55 +02:00
|
|
|
arrow.get(today + timedelta(hours=2)),
|
|
|
|
[result_publication[0], result_publication[1]],
|
2021-08-05 00:29:50 +02:00
|
|
|
],
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_publications_with_status(
|
2021-08-28 13:17:39 +02:00
|
|
|
status,
|
|
|
|
mobilizon_id,
|
|
|
|
from_date,
|
|
|
|
to_date,
|
|
|
|
expected_result,
|
|
|
|
generate_models,
|
2021-08-05 00:29:50 +02:00
|
|
|
):
|
2021-08-14 18:23:55 +02:00
|
|
|
await generate_models(complete_specification)
|
2021-08-05 00:29:50 +02:00
|
|
|
publications = await publications_with_status(
|
|
|
|
status=status,
|
|
|
|
event_mobilizon_id=mobilizon_id,
|
|
|
|
from_date=from_date,
|
|
|
|
to_date=to_date,
|
|
|
|
)
|
|
|
|
|
2021-08-27 23:45:24 +02:00
|
|
|
assert publications == {pub.id: pub for pub in expected_result}
|
2021-08-05 00:29:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
@pytest.mark.parametrize(
|
2021-08-14 18:23:55 +02:00
|
|
|
"status, expected_events_count",
|
2021-08-05 00:29:50 +02:00
|
|
|
[
|
2021-08-14 18:23:55 +02:00
|
|
|
(EventPublicationStatus.COMPLETED, 1),
|
|
|
|
(EventPublicationStatus.FAILED, 1),
|
|
|
|
(EventPublicationStatus.PARTIAL, 1),
|
|
|
|
(EventPublicationStatus.WAITING, 1),
|
2021-08-05 00:29:50 +02:00
|
|
|
],
|
|
|
|
)
|
2021-08-14 18:23:55 +02:00
|
|
|
async def test_event_with_status(generate_models, status, expected_events_count):
|
|
|
|
await generate_models(complete_specification)
|
|
|
|
result = list(await events_with_status([status]))
|
2021-08-05 00:29:50 +02:00
|
|
|
|
2021-08-14 18:23:55 +02:00
|
|
|
assert len(result) == expected_events_count
|
2021-08-05 00:29:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
@pytest.mark.parametrize(
|
2021-08-14 18:23:55 +02:00
|
|
|
"status, expected_events_count, begin_window, end_window",
|
2021-08-05 00:29:50 +02:00
|
|
|
[
|
2021-08-14 18:23:55 +02:00
|
|
|
(
|
|
|
|
EventPublicationStatus.COMPLETED,
|
|
|
|
1,
|
|
|
|
arrow.get(today + timedelta(hours=-1)),
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
EventPublicationStatus.COMPLETED,
|
|
|
|
0,
|
|
|
|
arrow.get(today + timedelta(hours=1)),
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
EventPublicationStatus.COMPLETED,
|
|
|
|
1,
|
|
|
|
arrow.get(today + timedelta(hours=-2)),
|
|
|
|
arrow.get(today + timedelta(hours=1)),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
EventPublicationStatus.COMPLETED,
|
|
|
|
0,
|
|
|
|
arrow.get(today + timedelta(hours=-2)),
|
|
|
|
arrow.get(today + timedelta(hours=0)),
|
|
|
|
),
|
2021-08-05 00:29:50 +02:00
|
|
|
],
|
|
|
|
)
|
2021-08-14 18:23:55 +02:00
|
|
|
async def test_event_with_status_window(
|
|
|
|
generate_models, status, expected_events_count, begin_window, end_window
|
2021-08-05 00:29:50 +02:00
|
|
|
):
|
2021-08-14 18:23:55 +02:00
|
|
|
await generate_models(complete_specification)
|
|
|
|
result = list(
|
|
|
|
await events_with_status([status], from_date=begin_window, to_date=end_window)
|
|
|
|
)
|
|
|
|
|
|
|
|
assert len(result) == expected_events_count
|