2021-08-14 18:23:55 +02:00
|
|
|
from datetime import timedelta
|
2021-08-29 14:51:52 +02:00
|
|
|
from uuid import UUID
|
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.publication import PublicationStatus
|
2021-11-24 23:58:06 +01:00
|
|
|
from mobilizon_reshare.storage.query.read import (
|
2021-11-11 15:18:04 +01:00
|
|
|
get_published_events,
|
|
|
|
events_with_status,
|
2021-08-05 00:29:50 +02:00
|
|
|
publications_with_status,
|
2021-11-11 15:18:04 +01:00
|
|
|
events_without_publications,
|
2021-11-24 23:58:06 +01:00
|
|
|
build_publications,
|
2021-07-05 23:07:12 +02:00
|
|
|
)
|
2022-02-09 00:54:56 +01:00
|
|
|
from tests.storage import complete_specification, event_0, event_1, event_3
|
2021-08-27 23:45:24 +02:00
|
|
|
from tests.storage import result_publication
|
2021-11-24 23:58:06 +01:00
|
|
|
from tests import today
|
2021-08-14 18:23:55 +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
|
|
|
|
|
|
|
|
2021-08-05 00:29:50 +02:00
|
|
|
@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
|
|
|
[
|
|
|
|
[
|
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-11-11 15:18:04 +01:00
|
|
|
[result_publication[2], result_publication[4], result_publication[5]],
|
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
|
|
|
],
|
2021-11-11 15:18:04 +01:00
|
|
|
[
|
|
|
|
PublicationStatus.FAILED,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
arrow.get(today + timedelta(hours=5)),
|
|
|
|
[result_publication[3]],
|
|
|
|
],
|
2021-08-05 00:29:50 +02:00
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_publications_with_status(
|
2021-11-24 23:58:06 +01: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-11-30 00:05:11 +01:00
|
|
|
assert publications == 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-11-11 15:18:04 +01:00
|
|
|
[(EventPublicationStatus.COMPLETED, 2), (EventPublicationStatus.PARTIAL, 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,
|
2021-11-11 15:18:04 +01:00
|
|
|
2,
|
2021-08-14 18:23:55 +02:00
|
|
|
arrow.get(today + timedelta(hours=-1)),
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
EventPublicationStatus.COMPLETED,
|
2021-11-11 15:18:04 +01:00
|
|
|
1,
|
2021-08-14 18:23:55 +02:00
|
|
|
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
|
2021-11-11 15:18:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"spec, expected_events",
|
|
|
|
[
|
|
|
|
(
|
|
|
|
{"event": 2, "publications": [], "publisher": ["zulip"]},
|
|
|
|
[
|
|
|
|
event_0,
|
2022-02-09 00:54:56 +01:00
|
|
|
event_1,
|
2021-11-11 15:18:04 +01:00
|
|
|
],
|
|
|
|
),
|
2021-11-24 23:58:06 +01:00
|
|
|
(
|
|
|
|
{
|
|
|
|
"event": 3,
|
|
|
|
"publications": [
|
|
|
|
{
|
|
|
|
"event_idx": 1,
|
|
|
|
"publisher_idx": 0,
|
|
|
|
"status": PublicationStatus.FAILED,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"event_idx": 2,
|
|
|
|
"publisher_idx": 0,
|
|
|
|
"status": PublicationStatus.COMPLETED,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
"publisher": ["zulip"],
|
|
|
|
},
|
|
|
|
[event_0],
|
|
|
|
),
|
2021-11-11 15:18:04 +01:00
|
|
|
(
|
|
|
|
complete_specification,
|
|
|
|
[
|
2022-02-09 00:54:56 +01:00
|
|
|
event_3,
|
2021-11-11 15:18:04 +01:00
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_events_without_publications(spec, expected_events, generate_models):
|
|
|
|
await generate_models(spec)
|
|
|
|
unpublished_events = list(await events_without_publications())
|
|
|
|
assert len(unpublished_events) == len(expected_events)
|
|
|
|
assert unpublished_events == expected_events
|
2021-11-24 23:58:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"mock_active_publishers, spec, event, n_publications",
|
|
|
|
[
|
|
|
|
(
|
|
|
|
[],
|
|
|
|
{"event": 2, "publications": [], "publisher": ["zulip"]},
|
|
|
|
event_0,
|
|
|
|
0,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
["zulip"],
|
|
|
|
{"event": 2, "publications": [], "publisher": ["zulip"]},
|
|
|
|
event_0,
|
|
|
|
1,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
["telegram", "zulip", "mastodon", "facebook"],
|
|
|
|
{
|
|
|
|
"event": 2,
|
|
|
|
"publications": [],
|
|
|
|
"publisher": ["telegram", "zulip", "mastodon", "facebook"],
|
|
|
|
},
|
|
|
|
event_0,
|
|
|
|
4,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
indirect=["mock_active_publishers"],
|
|
|
|
)
|
|
|
|
async def test_build_publications(
|
|
|
|
mock_active_publishers, spec, event, n_publications, generate_models
|
|
|
|
):
|
|
|
|
await generate_models(spec)
|
|
|
|
|
|
|
|
publications = list(await build_publications(event))
|
|
|
|
|
|
|
|
assert len(publications) == n_publications
|
|
|
|
|
|
|
|
for p in publications:
|
|
|
|
assert p.event == event
|
|
|
|
assert p.publisher.name in mock_active_publishers
|